Guest User

Untitled

a guest
May 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. #ifndef HASHMAP_H
  2. #define HASHMAP_H
  3.  
  4. typedef struct HashmapNode{
  5. unsigned int hash_index;
  6. int value;
  7. }HashmapNode;
  8.  
  9. typedef struct HashMap{
  10. int element_count;
  11. int map_size;
  12. HashmapNode ** node_list;
  13. }HashMap;
  14.  
  15. HashmapNode* hashmap_new_node(unsigned int hash_index, int * values);
  16. HashMap* hashmap_new(int size);
  17. unsigned int hashmap_hash_func(HashMap * hashmap, unsigned int key);
  18. void hashmap_expand(HashMap *hashmap);
  19. void hashmap_delete(HashMap *hashmap, int key);
  20. void hashmap_insert(HashMap *hashmap, unsigned int key, int * values, int values_size);
  21. HashmapNode* hashmap_lookup(HashMap *hashmap, unsigned int key);
  22. void hashmap_destroy(HashMap *hashmap);
  23.  
  24.  
  25. #endif
  26.  
  27. #include <stdlib.h>
  28. #include "hashmap.h"
  29.  
  30. HashmapNode* hashmap_new_node(unsigned int hash_index,int values_size)
  31. {
  32. HashmapNode *hashmap_node = malloc(sizeof(HashmapNode));
  33. hashmap_node->hash_index = hash_index;
  34. hashmap_node->values_size = values_size;
  35. hashmap_node->values = malloc(sizeof(values_size * sizeof(int)));
  36.  
  37. return hashmap_node;
  38. }
  39.  
  40. HashMap* hashmap_new(int size)
  41. {
  42. int i;
  43. HashMap *hashmap = (HashMap*)malloc(sizeof(HashMap));
  44. hashmap->node_list = malloc(size * sizeof(HashmapNode*));
  45. hashmap->element_count = 0;
  46. hashmap->map_size = size;
  47.  
  48. for(i = 0; i < size; i++)
  49. {
  50. hashmap->node_list[i] = malloc(sizeof(HashmapNode));
  51. hashmap->node_list[i] = NULL;
  52. }
  53. return hashmap;
  54. }
  55.  
  56. unsigned int hashmap_hash_func(HashMap *hashmap, unsigned int key)
  57. {
  58. int hash = key;
  59.  
  60. hash = (hash >> 3) * 2654435761;
  61. hash = hash % hashmap->map_size;
  62. return hash;
  63. }
  64.  
  65. void hashmap_expand(HashMap *hashmap)
  66. {
  67. int i;
  68. int hash_index;
  69. int old_size = hashmap->map_size;
  70. HashmapNode *hashmap_node;
  71.  
  72. hashmap->map_size = old_size * 2;
  73. HashmapNode **new_node_list = malloc(hashmap->map_size * sizeof(HashmapNode*));
  74.  
  75. for(i = 0; i < hashmap->map_size; i++)
  76. {
  77. new_node_list[i] = malloc(sizeof(HashmapNode));
  78. new_node_list[i] = NULL;
  79. }
  80.  
  81. for(i = 0; i < old_size; i++)
  82. {
  83. hashmap_node = hashmap->node_list[i];
  84. if(hashmap_node != NULL)
  85. {
  86. hash_index = hashmap_hash_func(hashmap, hashmap_node->hash_index);
  87. hashmap_node->hash_index = hash_index;
  88. new_node_list[hash_index] = hashmap_node;
  89. }
  90. free(hashmap_node);
  91. }
  92. hashmap->node_list = new_node_list;
  93. }
  94.  
  95. void hashmap_delete(HashMap *hashmap, int key)
  96. {
  97. int hash = hashmap_hash_func(hashmap, key);
  98. HashmapNode *hashmap_node = hashmap->node_list[hash];
  99. hashmap_node->values = NULL;
  100. hashmap_node->hash_index = 0;
  101. hashmap_node->values_size = 0;
  102. }
  103.  
  104. HashmapNode* hashmap_lookup(HashMap *hashmap, unsigned int key)
  105. {
  106. unsigned int hash = hashmap_hash_func(hashmap, key);
  107. HashmapNode *hashmap_node = hashmap->node_list[hash] ;
  108.  
  109. if(hashmap_node == NULL)
  110. {
  111. return NULL;
  112. }
  113. else
  114. {
  115. return hashmap->node_list[hash];
  116. }
  117. }
  118.  
  119. void hashmap_insert(HashMap *hashmap, unsigned int key, int value)
  120. {
  121. unsigned int hash = hashmap_hash_func(hashmap, key);
  122. double map_percentage;
  123. HashmapNode *hashmap_node = hashmap->node_list[hash];
  124.  
  125. map_percentage = (double)hashmap->element_count / (double)hashmap->map_size;
  126.  
  127. if(map_percentage >= 0.75)
  128. {
  129. hashmap_expand(hashmap);
  130. }
  131.  
  132. if(hashmap_node != NULL && hashmap->node_list[hash]->hash_index == key)
  133. {
  134. return;
  135. }
  136. else
  137. {
  138. hashmap_node = hashmap_new_node(hash, value);
  139. hashmap->element_count++;
  140. }
  141.  
  142. }
  143.  
  144. void hashmap_update_value(HashMap *hashmap, unsigned int key, int value)
  145. {
  146. HashmapNode *hashmap_node = hashmap_lookup(hashmap, key);
  147.  
  148. if(hashmap_node != NULL && hashmap->node_list[hash]->hash_index == key)
  149. {
  150. return;
  151. }
  152. else
  153. {
  154. hashmap_node->value = value;
  155. }
  156. }
  157.  
  158. void hashmap_destroy(HashMap *hashmap)
  159. {
  160. int i;
  161.  
  162. for(i = 0; i < hashmap->map_size; i++)
  163. {
  164. free(hashmap->node_list[i]);
  165. }
  166. free(hashmap);
  167. }
  168.  
  169. HashmapNode *hashmap_node = NULL;
  170.  
  171. hashmap->map_size = old_size * 2;
  172. HashmapNode **new_node_list = calloc(hashmap->map_size, sizeof(HashmapNode*));
  173.  
  174. if (new_node_list != NULL)
  175. {
  176. for(i = 0; i < hashmap->map_size; i++)
  177. {
  178. HashmapNode new_nodep = NULL;
  179. new_nodep = malloc(sizeof(HashmapNode));
  180. if (new_nodep == NULL)
  181. {
  182. // **PUT ERROR HANDLING CODE HERE**
  183. }
  184. else
  185. {
  186. memset(*new_nodep, 0, sizeof(HashmapNode); // Initialize memory to null values
  187. new_node_list[i] = new_nodep;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. // **PUT ERROR HANDLING CODE HERE**
  194. }
  195.  
  196. int i = 0;
  197. int hash_index = 0;
  198. int old_size = hashmap->map_size;
  199. HashmapNode *hashmap_node = NULL;
  200.  
  201. #include "hashmap.h"
  202.  
  203. if(hashmap_node != NULL && hashmap->node_list[hash]->hash_index == key)
  204. {
  205. return;
  206. }
  207. else
  208. {
  209. hashmap_node = hashmap_new_node(hash, value);
  210. hashmap->element_count++;
  211. }
  212.  
  213. HashmapNode ** node_list;
  214.  
  215. HashmapNode* hashmap_new_node(unsigned int hash_index, int * values);
  216. HashmapNode* hashmap_new_node(unsigned int hash_index,int values_size){}
  217.  
  218. hashmap->node_list[i] = malloc(sizeof(HashmapNode));
  219. hashmap->node_list[i] = NULL;
  220.  
  221. unsigned int hashmap_hash_func(HashMap *hashmap, unsigned int key)
  222. {
  223. int hash = key;
  224.  
  225. hash = (hash >> 3) * 2654435761;
  226. hash = hash % hashmap->map_size;
  227. return hash;
  228. }
Add Comment
Please, Sign In to add comment