{"id":16548224,"url":"https://github.com/dgraham/libds","last_synced_at":"2025-10-28T16:31:58.089Z","repository":{"id":17653041,"uuid":"20457590","full_name":"dgraham/libds","owner":"dgraham","description":"Simple data structures in C.","archived":false,"fork":false,"pushed_at":"2018-02-10T19:06:07.000Z","size":67,"stargazers_count":13,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T16:47:32.185Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/dgraham.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}},"created_at":"2014-06-03T20:04:40.000Z","updated_at":"2024-03-22T17:43:19.000Z","dependencies_parsed_at":"2022-09-11T15:10:40.672Z","dependency_job_id":null,"html_url":"https://github.com/dgraham/libds","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgraham%2Flibds","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgraham%2Flibds/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgraham%2Flibds/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgraham%2Flibds/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgraham","download_url":"https://codeload.github.com/dgraham/libds/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238681533,"owners_count":19512799,"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-11T19:25:21.954Z","updated_at":"2025-10-28T16:31:57.779Z","avatar_url":"https://github.com/dgraham.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple Data Structures in C\n\nBecause, after forty short years, a hash table isn't in the standard library yet.\n\n## Hash table\n\nStores key/value pairs in insertion order.\n\n```c\n// allocate memory\nstruct hashmap *map = hashmap_create();\n\n// store first key/value pair\nint id = 42;\nstruct hkey key = {\u0026id, sizeof(id)};\nchar *a = \"item 1\";\n\nhashmap_get(map, \u0026key);      // =\u003e NULL\nhashmap_set(map, \u0026key, a);   // =\u003e NULL\nhashmap_get(map, \u0026key);      // =\u003e \"item 1\"\nhashmap_contains(map, \u0026key); // =\u003e true\n\n// store another key\nint id2 = 12;\nstruct hkey key2 = {\u0026id2, sizeof(id2)};\nchar *b = \"item 2\";\n\nhashmap_set(map, \u0026key2, b);  // =\u003e NULL\n\n// iterate through keys in insertion order\nstruct iterator *entries = hashmap_iterator(map);\nwhile (entries-\u003enext(entries)) {\n    struct hentry *entry = entries-\u003ecurrent;\n    printf(\"%d =\u003e %s\\n\", entry-\u003ekey, entry-\u003evalue);\n}\n\n// free memory\nentries-\u003edestroy(entries);\nhashmap_destroy(map);\n```\n\n## Heap\n\nStore nodes in sorted order. Useful as a priority queue.\n\n```c\n// define comparator function\nint compare_nodes(const void *a, const void *b) {\n    return strcmp(a, b);\n}\n\n// allocate memory\nstruct heap *queue = heap_create(compare_nodes);\n\n// add nodes\nchar *a = \"item 1\";\nchar *b = \"item 2\";\nheap_push(queue, b);\nheap_push(queue, a);\n\n// remove nodes in sorted order\nheap_pop(queue); // =\u003e \"item 1\"\nheap_pop(queue); // =\u003e \"item 2\"\nheap_pop(queue); // =\u003e NULL\n\n// free memory\nheap_destroy(queue);\n```\n\n## Linked List\n\nDynamically sized list. Useful as a queue.\n\n```c\n// allocate memory\nstruct list *queue = list_create();\n\n// append items to queue\nchar *a = \"item 1\";\nchar *b = \"item 2\";\nlist_push(queue, a);\nlist_push(queue, b);\n\n// iterate through items\nstruct iterator *items = list_iterator(queue);\nwhile (items-\u003enext(items)) {\n    char *item = items-\u003ecurrent;\n    printf(\"[%d] =\u003e %s\\n\", items-\u003eindex, item);\n}\n\n// remove from front of queue\nlist_shift(queue); // =\u003e \"item 1\"\nlist_shift(queue); // =\u003e \"item 2\"\nlist_shift(queue); // =\u003e NULL\n\n// free memory\nitems-\u003edestroy(items);\nlist_destroy(queue);\n```\n\n## Vector\n\nDynamically sized array. Useful as a stack.\n\n```c\n// allocate memory\nstruct vector *stack = vector_create();\n\n// push items onto the stack\nchar *a = \"item 1\";\nchar *b = \"item 2\";\nvector_push(stack, a);\nvector_push(stack, b);\n\n// iterate through items\nstruct iterator *items = vector_iterator(stack);\nwhile (items-\u003enext(items)) {\n    char *item = items-\u003ecurrent;\n    printf(\"[%d] =\u003e %s\\n\", items-\u003eindex, item);\n}\n\n// pop items off\nvector_pop(vector); // =\u003e \"item 2\"\nvector_pop(vector); // =\u003e \"item 1\"\n\n// free memory\nitems-\u003edestroy(items);\nvector_destroy(stack);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgraham%2Flibds","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgraham%2Flibds","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgraham%2Flibds/lists"}