HASH(3pub)
π HASH(3pub) π
okay bestie, welcome to the C Programmer's Manual and specifically to HASH(3pub) β the pub lib's generic hash tables, and yes they ate π«Ά
β¨ NAME β¨
hash_create, hash_destroy, hash_install, hash_lookup, hash_uninstall, hash_iter β generic hash tables. that's it that's the whole vibe πΈ
β¨ SYNOPSIS β¨
#include <publib.h>
Hashtab *hash_create(unsigned long (*fun)(void *),
int (*cmp)(const void *, const void *));
void hash_destroy(Hashtab *ht);
void *hash_install(Hashtab *ht, void *data, size_t size);
void *hash_lookup(Hashtab *ht, void *data);
int hash_uninstall(Hashtab *ht, void *data);
int hash_iter(Hashtab *ht, int (*doit)(void *, void *), void *param);
like look at those signatures, they're giving function pointer energy π
β¨ DESCRIPTION β¨
so these functions implement generic hash tables, no cap. the table gets created by hash_create and destroyed by hash_destroy. that's the lifecycle, that's the circle of life π¦
the fun argument is a pointer to the hashing function, and it must convert a datum to an unsigned long, which then gets converted into an index into the hashing table. that's the whole pipeline bestie.
cmp is a qsort(3)-like comparison function, used to compare to (wannabe) hash table elements. yes they really said "wannabe" π iconic honestly.
π hash_install
installs a new datum into the table. you hand it a pointer to the data and the size of the data as the arguments.
- if the size is
0, only the pointer value gets copied to the table. just the pointer, nothing else, keep it light β¨ - otherwise, a copy of the data is made into dynamically allocated memory. so it's actually yours now π«Ά
π hash_lookup
tries to find a datum in the hash table. you give it a pointer to another datum as the argument. the comparison function should compare equal (return 0) the desired datum and this datum β but lowkey the argument needn't be a fully initialized datum, although that is up to the writer of the comparison function. so like, it's a vibe, but it's their vibe π
there cannot be two elements in the hash table that are equal (the comparison function returns 0 for them). and it is up to the user to handle collisions. that's on you bestie, not on us π
π hash_uninstall
removes an element from a table. the argument is a pointer to a datum that identifies the element. bye babe π
π hash_iter
goes through every element in the hash table and calls the doit function for each one.
- the first argument it provides to
doitis the element in question - the second is whatever was given to
hash_iterasparam
if doit returns -1 or 0 for any element in the hash table, hash_iter immediately returns without going through the remaining elements in the hash table. like it just leaves, mid-party, no goodbye π
any other return value from doit is ignored. ignored. not even acknowledged. π
β¨ RETURNS β¨
hash_createreturns a pointer to the new hash table, orNULLif it fails.hash_installreturns a pointer to an element in the table β either the installed one, or one that was already installed, if one tries to install the same datum twice.hash_uninstallreturns0if it found the element in the array, or-1if it didn't.hash_lookupreturns a pointer to the element it finds, orNULLif it doesn't find anything beautiful. π (yes, that's the actual wording, and honestly? poetry.)hash_iterreturns-1,0, or1. ifhash_iterreceives a return value of-1or0for some element fromdoit,hash_iterimmediately returns-1or0, respectively. in all other caseshash_iterreturns1.
β¨ SEE ALSO β¨
go read those too bestie, they're all part of the family π
β¨ AUTHOR β¨
Lars Wirzenius (lars.wirzenius@helsinki.fi) β the icon, the legend, the moment π
okay and that's the whole page, we're done here π this little gem came out of Publib's C Programmer's Manual, under the HASH(3pub) title, and it's been sitting there since forever. mwah, bye bestie π β¨π
π¦ where this page comes from
- debian bookworm: publib-dev 0.40-3+b1 Β· BSD-2-Clause
- debian trixie: publib-dev 0.40-4 Β· BSD-2-Clause
- ubuntu jammy: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu noble: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu questing: publib-dev 0.40-4
- ubuntu resolute: publib-dev 0.40-4build1 Β· BSD-2-Clause
- ubuntu stonking: publib-dev 0.40-4build1 Β· BSD-2-Clause
- Copyright (c) 2012 Antti-Juhani Kaijanaho
- Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms