HASH(3)
π¦ HASH(3) β Library Functions Manual π¦
π NAME π
hashβ hash table support (obsolete: usecdtinstead) π
β οΈ obsolete alert: this page ate in its day, but it's giving legacy API energy. Use cdt instead. no cap, periodt.
β¨ SYNOPSIS β¨
#include <hash.h>
π DESCRIPTION π
okay so the hash routines manipulate collections of dynamic, scoped hash tables. Think of it as the original group chat for keys and values, bestie. π«Ά
A hash table provides an association between a key and its value. A key is a sequence of char elements, and a value is a user supplied pointer to the value. Each hash table has a dynamic number of slots, each pointing to the head of a forward linked collision chain. π
Hashing occurs as follows: a hash function takes a key as an argument and returns a non-negative index. The index modulo the table size produces a slot that points to a collision chain. The collision chain is sequentially searched until a match is found for key. The hash tables are automatically resized as new entries are added. fr fr. β¨
Each hash table has one key type. The default key type is a pointer to a null-terminated string. The alternate key type is a pointer to a fixed length byte buffer, declared for a given table by the hashalloc() function described below. πΈ
Hash table information is partitioned into two parts for efficient scoping. Vibe check: the root part contains fixed information that is shared among a set of related hash tables. The remaining information is maintained on a per-table basis. iykyk. π
These basic types are defined in the header file hash.h (alternate names are listed in parenthesis) β ngl, the parentheses are important:
Hash_table_t(HASHTABLE) β The per-table information. The readonly public elements are lowkey iconic:int bucketsβ The number of table entries.char* nameβ The hash table name.rootβ The root information. The public elements are:int root->accessesβ The number of lookups.int root->collisionsβ The number of lookup collisions.
Hash_table_t* scopeβ The table that this scope covers,NULLif the table is not a scope.int sizeβ The current hash table size.
Hash_bucket_t(HASHBUCKET) β A collision chain hash bucket. The public structure elements are:char* hashname(Hash_bucket_t*)β Returns a pointer to the hash bucket key given the bucket pointer.char* valueβ The value associated with the key.
Hash_header_t(HASHHEADER) β The hash bucket header that must be the first element in all user defined buckets. Heads up:HASH_VALUEmay not be used with user defined buckets. π«Hash_position_t(HASHPOSITION) β Stores the hash table position forhashscan. The public elements are:Hash_bucket_t* bucketβ The current hash bucket pointer.
The routines are giving main character energy π:
π§ hashalloc
Hash_table_t* hashalloc(Hash_table_t* ref, int op, ...)
Creates a new hash table and returns a pointer to the table β not me still relying on malloc(3) for space here, but okay π. NULL is returned if the table cannot be created. ref is a pointer to a reference hash table that provides default values for unspecified information. The new hash table and ref share the same root information. If ref is NULL then new root information is created for the new table. The remaining arguments appear in op-arg pairs, followed by a final 0 argument. The op-arg pairs are:
HASH_alloc,(char(*)()) allocβallocis a function that is called to processHash_bucket_tvalue assignments. The single argument ischar* valueand the processedchar* valueis returned. πHASH_clear,int flagsβflagsare therefflags to be cleared in the new hash table. SeeHASH_setbelow.HASH_compare,(int(*)()) compareβ Specifies an alternate key comparison function. The arguments and return value forcompareare the same as forstrncmp(3)ifHASH_namesizeis specified andstrcmp(3)otherwise. The first argument is the key from the current hash bucket on the collision chain and the second argument is the user supplied key.HASH_free,(int(*)()) freeβfreeis a function that is called when a hash bucket is freed. IfHASH_BUCKETwas set inhashallocthen the hash bucket pointer is passed, otherwise the bucket value pointer is passed.HASH_hash,(int(*)()) hashβ Specifies an alternate key hash function. Achar* keyargument (and, ifHASH_namesizeis specified, anint key sizeargument) is passed tohash. The return value must be a non-negativeint.HASH_meanchain,int meanchainβ Specifies the mean collision chain length. The hash table is automatically resized when this value is exceeded. The default mean chain length is2.HASH_name,char* nameβ Associatesnamewith the hash table. Used byhashdump).HASH_namesize,int namesizeβ The fixed size in bytes for keys in the table. Ifnamesizeis0(the default) then the keys are interpreted as null-terminated strings.HASH_set,int flagsβ Changes the hash table flags by oring inflags. Theflags, which may be ored together, are:HASH_ALLOCATEβ Keys for new hash table entries are to be copied to data areas obtained frommalloc(3).HASH_FIXEDβ Fixes the hash table size, disabling any automatic table resizing.HASH_SCOPEβ The new hash table is a scope that is to be pushed on top ofref.refmust be non-NULL.
HASH_va_list,va_list apβapis ava_listvariable argument list pointer (see<stdarg.h>).
π§ hashfree
Hash_table_t* hashfree(Hash_table_t* tab)
The hash table tab is freed. The scope covered table pointer is returned, NULL if tab is not a scope. easy peasy.
π₯ hashlook
char* hashlook(Hash_table_t* tab, char* name, int flags, char* value)
okay so this operates on the key name in the hash table tab according to flags and value. A Hash_bucket_t pointer is returned unless otherwise noted. There are three basic lookup operations:
HASH_CREATEβnameis entered into the top level scope if it does not already exist. Ifnamealso appears in a lower scope andHASH_ALLOCis set for the table then the new bucket will share thenamefield value with the lower scope.HASH_DELETEβnameis deleted from the top level scope if it exists.NULLis returned.HASH_LOOKUPβ The scopes are searched in order from top to bottom forname. The bucket pointer for the first occurrence is returned.NULLis returned ifnameis not found.
The basic operations may be qualified by the following (the qualifiers are restricted to the basic operations in the parenthesized list):
HASH_BUCKET(HASH_CREATE,HASH_DELETE,HASH_LOOKUP) βnameis a pointer to a bucket that has already been entered into the table.HASH_FIXED(HASH_CREATE) βvalueis taken to be the size of the hash bucket to be created fornamein the top level scope. The minimum bucket size is silently restricted tosizeof(Hash_header_t).HASH_INSTALL(HASH_CREATE) βnameis a pointer to a bucket that has not been entered into the table.HASH_NOSCOPE(HASH_LOOKUP) β The lookup is restricted to the top level scope.HASH_OPAQUE(HASH_CREATE,HASH_DELETE) β Sets (HASH_CREATE) or clears (HASH_DELETE) the opaque property for the bucket. An opaque bucket is not visible in lower scopes.HASH_SCOPE(HASH_CREATE,HASH_DELETE) β All scopes are searched for the bucket. If the bucket is not found forHASH_CREATEthen a new bucket is created in the lowest scope.HASH_VALUE(HASH_CREATE,HASH_LOOKUP) β ForHASH_CREATEthe bucket value field is set tovalueand the bucket name value is returned. ForHASH_LOOKUPthe bucket value field is returned,NULLif the bucket is not found.
If name is NULL then the name from the most recent hashlook() is used, avoiding recomputation of some internal parameters. β¨
π¦ hashget
char* hashget(Hash_table_t* tab, char* name)
Returns the value associated with the key name in the hash table tab. If name is NULL then the name from the most recent hashget() is used, avoiding recomputation of some internal parameters. NULL is returned if name is not in the table. All scope covered tables are searched.
π hashlast
Hash_bucket_t* hashlast(Hash_table_t* tab)
Returns a pointer to the most recent hash bucket for tab. The value is set by hashlook(), hashscan() and hashwalk().
π hashput
char* hashput(Hash_table_t* tab, char* name, char* value)
Set the value of the key name to value in the top level scope of the hash table tab. name is entered into the top level scope if necessary. The (possibly re-allocated) key name pointer is returned (see HASH_ALLOCATE). If name is 0 then the most recent lookup name to hashlook() or hashget() is used. This eliminates a re-hash and re-lookup of name.
π§ hashwalk
int hashwalk(Hash_table_t* tab, int flags, (int(*)()) walker, char* handle)
The function walker is applied to each entry (not covered by a scope starting at tab) in the hash table tab. If flags is HASH_NOSCOPE then only the top level hash table is used, otherwise the walk includes all scope covered tables. walker is called with char* key as the first argument, char* value as the second argument, and char* handle as the third argument. handle may be 0. The walk terminates after the last entry or when walker returns a negative value. The return value of the last call to walker is returned. Only one walk may be active within a collection of scoped tables.
Okay so, continuing the function tea β β same section energy, no heading repeat, just the rest of the lineup:
Hash_position_t* hashscan(Hash_table_t* tab, int flags)β hands you aHash_position_tpointer for a sequential scan on the hash table tab. If flags isHASH_NOSCOPE, then only the top level hash table is used; otherwise the scan includes all scope covered tables. Only one scan may be active within a collection of scoped tables.hashdone()must be called to terminate the scan.0is returned on error. π«ΆHash_bucket_t* hashnext(Hash_position_t* pos)β returns a pointer to the next bucket in the sequential scan set up byhashscan()on pos. If no elements remain then0is returned. π₯void hashdone(Hash_position_t* pos)β completes a scan initiated byhashscan()on pos. Bye scan, it was iconic πint hashset(Hash_table_t* tab, int flags)β sets the flags for the hash table tab by oring in flags. OnlyHASH_ALLOCATEandHASH_FIXEDmay be set. Periodt πint hashclear(Hash_table_t* tab, int flags)β clears the flags for the hash table tab by masking out flags. OnlyHASH_ALLOCATEandHASH_FIXEDmay be cleared. Cleanup era β¨void hashdump(Hash_table_t* tab, int flags)β dumps hash table accounting info to standard error. If tab isNULLthen all allocated hash tables are dumped; otherwise only information on tab is dumped. If flags isHASH_BUCKETthen the hash bucket key-value pairs for each collision chain are also dumped. Spill the tea, hash tables βπvoid hashsize(Hash_table_t* tab, int size)β changes the size of the hash table tab to size, where size must be a power of 2. Explicit calls to this routine are not necessary as hash tables are automatically resized.
Bestie, power of 2 or itβs not happening β thatβs the rule πΈ
int strhash(char* name)β hashes the null terminated character string name using a linear congruent pseudo-random number generator algorithm and returns a non-negativeinthash value. Math but make it cute π§int memhash(char* buf, int siz)β hashes the buffer buf of siz bytes using a linear congruent pseudo-random number generator algorithm and returns a non-negativeinthash value. Buffer gets the hash treatment too π«Άlong strsum(char* name, long sum)β returns a running 31-bit checksum of the string name, where sum is0on the first call and the return value from a previousmemsumorstrsumcall otherwise. The checksum value is consistent across all implementations. Reliable queen behavior πlong memsum(char* buf, int siz, long sum)β returns a running 31-bit checksum of buffer buf of siz bytes, where sum is0on the first call and the return value from a previousmemsumorstrsumcall otherwise. The checksum value is consistent across all implementations. Same checksum energy, memory edition π
π SEE ALSO π
sum(1)β the one and only cross-reference, periodt.
Cute sign-off: the footer says HASH(3), and thereβs no extra source/version/date line in what we got β so thatβs the whole footer vibe. Stay hashed and hydrated, bestie π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms