Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. #include <stdio.h> /* printf */
  2. #include <stdlib.h> /* malloc, free */
  3. #include <string.h> /* strcmp */
  4.  
  5. /* type abbreviations for callbacks */
  6. typedef unsigned (hashfunc_t) (const void*);
  7. typedef int (comparefunc_t) (const void*, const void*);
  8.  
  9. /* entries in our hash table are key-value pairs,
  10. * just like in any other map
  11. */
  12. typedef struct entry_t entry_t;
  13.  
  14. struct entry_t {
  15. const void *key;
  16. const void *value;
  17. entry_t *next;
  18. };
  19.  
  20. typedef struct {
  21. hashfunc_t *hash; /* callback to get the hash value of a key */
  22. comparefunc_t *compare; /* callback to compare two keys */
  23. unsigned count; /* number of entries */
  24. unsigned numbuckets; /* size of our table */
  25. entry_t **buckets; /* the actual table */
  26. } hashtable_t;
  27.  
  28. hashtable_t* ht_create(hashfunc_t *hash, comparefunc_t *compare) {
  29.  
  30. /* allocate container */
  31. hashtable_t *this = malloc(sizeof(hashtable_t));
  32.  
  33. /* init all members */
  34. this->hash = hash;
  35. this->compare = compare;
  36. this->count = 0;
  37. this->numbuckets = 2;
  38.  
  39. /* allocate table of buckets -- all set to NULL pointer */
  40. this->buckets = malloc(sizeof(entry_t*) * this->numbuckets);
  41. memset(this->buckets, 0, sizeof(entry_t*) * this->numbuckets);
  42.  
  43. return this;
  44. }
  45.  
  46. void ht_destroy(hashtable_t *this) {
  47.  
  48. /* first delete all entries */
  49. for (unsigned bid = 0; bid < this->numbuckets; bid++) {
  50. /* skip empty buckets */
  51. if (!this->buckets[bid]) continue;
  52.  
  53. /* walk though bucket and delete entries */
  54. entry_t *entry = this->buckets[bid];
  55. while (entry) {
  56. entry_t *tmp = entry->next;
  57. free(entry);
  58. entry = tmp;
  59. }
  60. }
  61.  
  62. /* then delete the table and the surrounding container */
  63. free(this->buckets);
  64. free(this);
  65. }
  66.  
  67. const void* ht_get(hashtable_t *this, const void *key) {
  68.  
  69. /* get the bucket id */
  70. unsigned khash = this->hash(key);
  71. unsigned bid = khash % this->numbuckets;
  72.  
  73. /* then search in bucket */
  74. entry_t *entry = this->buckets[bid];
  75. while (entry) {
  76. int cmpvalue = this->compare(entry->key, key);
  77. if (cmpvalue == 0) return entry->value; /* found it */
  78. entry = entry->next;
  79. }
  80.  
  81. /* nothing found */
  82. return NULL;
  83. }
  84.  
  85. void ht_put(hashtable_t *this, const void *key, const void *value) {
  86.  
  87. /* get the bucket id */
  88. unsigned khash = this->hash(key);
  89. unsigned bid = khash % this->numbuckets;
  90.  
  91. /* then search in bucket */
  92. entry_t *entry = this->buckets[bid];
  93. while (entry) {
  94. int cmpvalue = this->compare(entry->key, key);
  95. if (cmpvalue == 0) {
  96. /* key is already there, just override value */
  97. entry->value = value;
  98. return;
  99. }
  100. entry = entry->next;
  101. }
  102.  
  103. /* nothing found, so make new entry */
  104. entry_t *newentry = malloc(sizeof(entry_t));
  105. newentry->key = key;
  106. newentry->value = value;
  107. newentry->next = this->buckets[bid];
  108.  
  109. this->buckets[bid] = newentry;
  110. this->count++;
  111.  
  112. /* if our buckets are getting two full, make more buckets and redistribute */
  113. float fill = (float) this->count / (float) this->numbuckets;
  114. if (fill > 2.0f) {
  115. /* save old values */
  116. unsigned numbuckets = this->numbuckets;
  117. entry_t **buckets = this->buckets;
  118.  
  119. /* override with new ones */
  120. this->numbuckets *= 2;
  121. this->buckets = malloc(sizeof(entry_t*) * this->numbuckets);
  122. memset(this->buckets, 0, sizeof(entry_t*) * this->numbuckets);
  123.  
  124. /* redistribute the old entries into the new table */
  125. for (unsigned obid = 0; obid < numbuckets; obid++) {
  126. /* skip empty buckets */
  127. if (!buckets[obid]) continue;
  128.  
  129. /* walk through entries and redistribute each */
  130. entry_t *entry = buckets[obid];
  131. while (entry) {
  132. entry_t *tmp = entry->next;
  133.  
  134. /* rehash */
  135. unsigned khash = this->hash(entry->key);
  136. unsigned bid = khash % this->numbuckets;
  137.  
  138. /* and insert into new bucket */
  139. entry->next = this->buckets[bid];
  140. this->buckets[bid] = entry;
  141.  
  142. entry = tmp;
  143. }
  144. }
  145. free(buckets);
  146. }
  147. }
  148.  
  149. /* a simple hash function that maps strings to a number
  150. * multiple strings can map to the same number, they will go into the same bucket
  151. */
  152. unsigned strhash(const char *str) {
  153. unsigned h = 0;
  154. while (*str) h+= *(str++);
  155. return h;
  156. }
  157.  
  158. int main() {
  159. hashtable_t *mytable = ht_create((hashfunc_t*)strhash, (comparefunc_t*)strcmp);
  160.  
  161. printf("===========================\n");
  162. printf(" test put and get: \n");
  163. printf("===========================\n");
  164.  
  165. ht_put(mytable, "one", "1");
  166. ht_put(mytable, "two", "2");
  167. ht_put(mytable, "three", "3");
  168. ht_put(mytable, "four", "4");
  169. ht_put(mytable, "five", "5");
  170.  
  171. const char *keys[] = { "zero", "one", "two", "three", "four", "five", "six", NULL};
  172.  
  173. for (int i = 0; keys[i]; i++) {
  174. const char *key = keys[i];
  175. const char *value = ht_get(mytable, key);
  176. printf("[%5s] -> %s\n", key, value ? value : "[not found]");
  177. }
  178.  
  179. printf("===========================\n");
  180. printf(" print buckets: \n");
  181. printf("===========================\n");
  182.  
  183. for (unsigned bid = 0; bid < mytable->numbuckets; bid++) {
  184. printf("%2i: ", bid);
  185. if (mytable->buckets[bid]) {
  186. entry_t *entry = mytable->buckets[bid];
  187. while (entry) {
  188. printf("[%s, %s]; ", (const char*)entry->key, (const char*)entry->value);
  189. entry = entry->next;
  190. }
  191. }
  192. else {
  193. printf("EMPTY");
  194. }
  195. printf("\n");
  196. }
  197.  
  198. ht_destroy(mytable);
  199. return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement