Last active
October 24, 2020 11:24
-
-
Save Xophmeister/feaeddccdcf2e71d1669 to your computer and use it in GitHub Desktop.
Learning how to use khash (https://github.com/attractivechaos/klib/blob/master/khash.h)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* MIT License | |
| * Copyright (c) 2015 Genome Research Limited */ | |
| #include <stdio.h> | |
| #include "khash.h" | |
| /* | |
| Learning how to use khash, focusing on string keys with string values | |
| Not tried: | |
| * Hash sets | |
| * kh_clear reset a hash table without deallocating memory | |
| * kh_resize resize a hash table | |
| * kh_begin the start iterator (although used in kh_foreach) | |
| * kh_n_buckets the number of buckets in the hash table | |
| * kh_foreach_value iterate over the values in the hash table | |
| */ | |
| /* Initialise hash table type */ | |
| KHASH_MAP_INIT_STR(str_hash_t, const char*) | |
| /* Print hash table */ | |
| void print_str_hash(khash_t(str_hash_t)* h) { | |
| const char* k; | |
| const char* v; | |
| kh_foreach(h, k, v, { | |
| (void)fprintf(stdout, "%s: %s\n", k, v); | |
| }) | |
| } | |
| int main(int argc, char** argv) { | |
| /* Initialise hash table */ | |
| khash_t(str_hash_t) *myHash = kh_init(str_hash_t); | |
| /* Print size of hash table (should be 0) */ | |
| (void)fprintf(stdout, "size = %d\n", kh_size(myHash)); | |
| /* Populate hash from argv */ | |
| char** keys = malloc(argc * sizeof(char*)); | |
| for (int i = 0; i < argc; ++i) { | |
| int ret; | |
| /* Create keys */ | |
| *(keys + i) = malloc(10 * sizeof(char)); | |
| (void)snprintf(*(keys + i), 10, "arg %d", i); | |
| /* Insert key */ | |
| khiter_t newKey = kh_put(str_hash_t, myHash, *(keys + i), &ret); | |
| /* Set key value */ | |
| kh_value(myHash, newKey) = *(argv + i); | |
| } | |
| /* Print hash table */ | |
| print_str_hash(myHash); | |
| /* Print size of hash table (should be argc) */ | |
| (void)fprintf(stdout, "size = %d\n", kh_size(myHash)); | |
| /* Check "arg 0" exists (it should) and change it */ | |
| { | |
| khiter_t arg0 = kh_get(str_hash_t, myHash, "arg 0"); | |
| if (arg0 == kh_end(myHash)) { | |
| /* This shouldn't happen */ | |
| (void)fprintf(stderr, "No such key\n"); | |
| } else { | |
| /* We've got our key bucket, now check it contains data */ | |
| if (kh_exist(myHash, arg0)) { | |
| int ret; | |
| /* Print the value */ | |
| (void)fprintf(stdout, "Fetched \"%s\" value = %s\n", | |
| kh_key(myHash, arg0), | |
| kh_value(myHash, arg0)); | |
| /* Change the value */ | |
| const char* newValue = "Changed!"; | |
| kh_value(myHash, arg0) = newValue; | |
| /* Print hash table */ | |
| (void)fprintf(stdout, "\nCHANGED\n"); | |
| print_str_hash(myHash); | |
| /* Delete "arg 0" */ | |
| (void)kh_del(str_hash_t, myHash, arg0); | |
| /* Print hash table and size (should be argc - 1)*/ | |
| (void)fprintf(stdout, "\nDELETED\n"); | |
| print_str_hash(myHash); | |
| (void)fprintf(stdout, "size = %d\n", kh_size(myHash)); | |
| } else { | |
| /* This shouldn't happen */ | |
| (void)fprintf(stderr, "Key doesn't contain data\n"); | |
| } | |
| } | |
| } | |
| /* Free */ | |
| for (int i = 0; i < argc; ++i) { free(*(keys + i)); } | |
| free(keys); | |
| kh_destroy(str_hash_t, myHash); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment