{"id":16317680,"url":"https://github.com/zfletch/zhash-c","last_synced_at":"2025-03-22T21:31:36.344Z","repository":{"id":72191208,"uuid":"44448338","full_name":"zfletch/zhash-c","owner":"zfletch","description":"Hash Table Library for C","archived":false,"fork":false,"pushed_at":"2023-05-13T00:43:25.000Z","size":19,"stargazers_count":16,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T14:53:45.114Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zfletch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-10-17T18:01:21.000Z","updated_at":"2025-01-11T09:16:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"e0d26307-23b8-4580-93fb-fbb61d5788fa","html_url":"https://github.com/zfletch/zhash-c","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfletch%2Fzhash-c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfletch%2Fzhash-c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfletch%2Fzhash-c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfletch%2Fzhash-c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zfletch","download_url":"https://codeload.github.com/zfletch/zhash-c/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245022545,"owners_count":20548555,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T22:09:00.519Z","updated_at":"2025-03-22T21:31:36.057Z","avatar_url":"https://github.com/zfletch.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hash Table\n\nThis is a hash table library implemented in C. Both a basic hash table (ZHash)\nand a hash table with entries sorted by insertion order (ZSortedHash) are\nprovided. The keys are strings and the values are void pointers.\n\nThe following hashing algorithm is used:\n```c\nhash = 0;\nwhile ((ch = *key++)) hash = (17 * hash + ch) % size;\n```\n(See\n[http://algs4.cs.princeton.edu/34hash/](http://algs4.cs.princeton.edu/34hash/)\nfor more information about hash functions.)\n\nCollisions are resolved with separate chaining and a singly linked list.\nIf the hash table is more than 50% full, it will increase the number of slots\nand rehash. Likewise, if it's less than 12.5% full, it will decrease the number\nof slots and rehash.\n\nThe possible numbers of slots are all prime numbers; each size is roughly two\ntimes the previous size. The maximum number of slots is `1000000007` so\nperformance may degrade with more than  `1000000007 / 2` entries.\n\n## ZHash\n\nStandard hash table. Basic hash table operations are supported: `set`, `get`,\n`exists`, `delete`.\n\n### Example\n\n```c\n#include \u003cstdio.h\u003e\n#include \"../src/zhash.h\"\n\n// gcc -Wall -Wextra hello.c ../src/zhash.c\n// prints \"hello world\" to stdout\nint main ()\n{\n  struct ZHashTable *hash_table;\n\n  hash_table = zcreate_hash_table();\n\n  zhash_set(hash_table, \"hello\", (void *) \"world\");\n\n  if (zhash_exists(hash_table, \"hello\")) {\n    printf(\"hello %s\\n\", (char *) zhash_get(hash_table, \"hello\"));\n  }\n\n  if (zhash_exists(hash_table, \"goodbye\")) {\n    printf(\"goodbye %s\\n\", (char *) zhash_get(hash_table, \"goodbye\"));\n  }\n\n  zfree_hash_table(hash_table);\n\n  return 0;\n}\n```\n\n### Public Interface\n\n```c\n// create hash table\nstruct ZHashTable *zcreate_hash_table(void);\n\n// free hash table (note that this only frees the table and the entry structs)\nvoid zfree_hash_table(struct ZHashTable *hash_table);\n\n// set key to val (if there is already a value, overwrite it)\nvoid zhash_set(struct ZHashTable *hash_table, char *key, void *val);\n\n// get the value stored at key (if no value, return NULL)\nvoid *zhash_get(struct ZHashTable *hash_table, char *key);\n\n// delete entry stored at key and return the value (if no value, return NULL)\nvoid *zhash_delete(struct ZHashTable *hash_table, char *key);\n\n// return true if there is a value stored at the key and false otherwise\nbool zhash_exists(struct ZHashTable *hash_table, char *key);\n```\n\n## ZSortedHash\n\nHash table with entries sorted by insertion order. The same hash table\noperations as ZHash are supported. In addition, an iterator is provided, which\ncan be used to iterate through entries.\n\nZSortedHash is built on top of ZHash. Essentially, what it's doing is wrapping\nthe the value in a linked list node when it is inserted. Then it does the\nnecessary bookkeeping when an entry is deleted.\n\n### Example\n\n```c\n#include \u003cstdio.h\u003e\n#include \"../src/zsorted_hash.h\"\n\n// gcc -Wall -Wextra sorted_hello.c ../src/zhash.c ../src/zsorted_hash.c\n// prints \"hello world\" in English and French to stdout\nint main ()\n{\n  struct ZSortedHashTable *hash_table;\n  struct ZIterator *iterator;\n\n  hash_table = zcreate_sorted_hash_table();\n\n  zsorted_hash_set(hash_table, \"hello\", (void *) \"world\");\n  zsorted_hash_set(hash_table, \"bonjour\", (void *) \"le monde\");\n\n  for (iterator = zcreate_iterator(hash_table);\n      ziterator_exists(iterator); ziterator_next(iterator)) {\n\n    printf(\"%s %s\\n\", ziterator_get_key(iterator),\n        (char *) ziterator_get_val(iterator));\n  }\n\n  zfree_iterator(iterator);\n  zfree_sorted_hash_table(hash_table);\n\n  return 0;\n}\n```\n\n### Public Interface\n\n```c\n// these functions behave the same as their counterparts in zhash.h\nstruct ZSortedHashTable *zcreate_sorted_hash_table(void);\nvoid zfree_sorted_hash_table(struct ZSortedHashTable *hash_table);\nvoid zsorted_hash_set(struct ZSortedHashTable *hash_table, char *key, void *val);\nvoid *zsorted_hash_get(struct ZSortedHashTable *hash_table, char *key);\nvoid *zsorted_hash_delete(struct ZSortedHashTable *hash_table, char *key);\nbool zsorted_hash_exists(struct ZSortedHashTable *hash_table, char *key);\n\n// create an iterator to be used in iteration functions below\nstruct ZIterator *zcreate_iterator(struct ZSortedHashTable *hash_table);\n\n// free iterator\nvoid zfree_iterator(struct ZIterator *iterator);\n\n// return number of entries stored in the hash table\nsize_t zsorted_hash_count(struct ZSortedHashTable *hash_table);\n\n// return true if the iterator is within bounds and false otherwise\nbool ziterator_exists(struct ZIterator *iterator);\n\n// get the key at the current position (if out of bounds, then return NULL)\nchar *ziterator_get_key(struct ZIterator *iterator);\n\n// get the value at the current position (if out of bounds, then return NULL)\nvoid *ziterator_get_val(struct ZIterator *iterator);\n\n// move the position forward by one step\nvoid ziterator_next(struct ZIterator *iterator);\n\n// move the position back by one step\nvoid ziterator_prev(struct ZIterator *iterator);\n```\n\n## Running Tests\n\n```bash\ncd ./test\n./zhash_test.sh\n```\nThe test uses either the `valgrind` or `leaks` command to detect memory leaks.\nThe tests will fail if neither is present.\n\n## Notes\n\nTested on `Ubuntu 22.04` with `GCC 11.3.0`\nand on `macOS 13.3.1` with `Apple clang version 14.0.3`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfletch%2Fzhash-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzfletch%2Fzhash-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfletch%2Fzhash-c/lists"}