Advertisement
DarkoreXOR

Untitled

Jul 4th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct linked_list_node
  6. {
  7.     int key;
  8.     int value;
  9.     struct linked_list_node *next;
  10. } linked_list_node_t;
  11.  
  12. typedef struct hash_table
  13. {
  14.     linked_list_node_t **heads;
  15.     size_t capacity;
  16.     unsigned int (*hash_func)(int);
  17. } hash_table_t;
  18.  
  19. //
  20. // hashtable
  21. //
  22.  
  23. void
  24. ht_init (
  25.     hash_table_t * const ht,
  26.     unsigned int (*hash_func)(int),
  27.     size_t capacity
  28. )
  29. {
  30.     ht->heads = malloc(capacity * sizeof(linked_list_node_t *));
  31.     ht->capacity = capacity;
  32.     ht->hash_func = hash_func;
  33.  
  34.     for (size_t i = 0; i < capacity; ++i)
  35.     {
  36.         ht->heads[i] = NULL;
  37.     }
  38. }
  39.  
  40. int ht_contains_key(
  41.     hash_table_t *ht,
  42.     int key,
  43.     int *out_value,
  44.     int *out_index
  45. )
  46. {
  47.     const int hash = ht->hash_func(key);
  48.     const int index = hash % ht->capacity;
  49.  
  50.     if (ht->heads[index] == NULL)
  51.     {
  52.         return 0;
  53.     }
  54.  
  55.     linked_list_node_t *head = ht->heads[index];
  56.  
  57.     while (head != NULL)
  58.     {
  59.         if (head->key == key)
  60.         {
  61.             if (out_value != NULL)
  62.             {
  63.                 *out_value = head->value;
  64.             }
  65.  
  66.             if (out_index != NULL)
  67.             {
  68.                 *out_index = index;
  69.             }
  70.  
  71.             return 1;
  72.         }
  73.  
  74.         head = head->next;
  75.     }
  76.  
  77.     return 0;
  78. }
  79.  
  80. void
  81. ht_add (
  82.     hash_table_t *ht,
  83.     int key,
  84.     int value
  85. )
  86. {
  87.     int index;
  88.  
  89.     if (ht_contains_key(ht, key, NULL, &index))
  90.     {
  91.         ht->heads[index]->value = value;
  92.         return;
  93.     }
  94.  
  95.     const int hash = ht->hash_func(key);
  96.     index = hash % ht->capacity;
  97.  
  98.     linked_list_node_t *new_node = malloc(sizeof(linked_list_node_t));
  99.     new_node->key = key;
  100.     new_node->value = value;
  101.  
  102.     if (ht->heads[index] == NULL)
  103.     {
  104.         new_node->next = NULL;
  105.     }
  106.     else
  107.     {
  108.         linked_list_node_t *head = ht->heads[index];
  109.         new_node->next = ht->heads[index];
  110.     }
  111.  
  112.     ht->heads[index] = new_node;
  113. }
  114.  
  115. unsigned int
  116. hash_function (
  117.     int key
  118. )
  119. {
  120.     key = ((key >> 16) ^ key) * 0x45d9f3b;
  121.     key = ((key >> 16) ^ key) * 0x45d9f3b;
  122.     key = (key >> 16) ^ key;
  123.     return key;
  124. }
  125.  
  126. int main()
  127. {
  128.     hash_table_t ht;
  129.  
  130.     ht_init(&ht, &hash_function, 128);
  131.  
  132.     ht_add(&ht, 10, 1);
  133.     ht_add(&ht, 10, 2);
  134.     ht_add(&ht, 10, 3);
  135.     ht_add(&ht, 20, 2);
  136.     ht_add(&ht, 30, 3);
  137.     ht_add(&ht, 40, 4);
  138.     ht_add(&ht, 50, 5);
  139.     ht_add(&ht, 60, 6);
  140.     ht_add(&ht, 70, 7);
  141.     ht_add(&ht, 80, 8);
  142.     ht_add(&ht, 90, 9);
  143.     ht_add(&ht, 100, 10);
  144.  
  145.     for (int key = 0; key < 200; ++key)
  146.     {
  147.         int value;
  148.         int index;
  149.  
  150.         if (ht_contains_key(&ht, key, &value, &index))
  151.         {
  152.             printf("[contains] key: %d, value: %d, at index: %d\n", key, value, index);
  153.         }
  154.     }
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement