Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

169 rindas
12 KiB

  1. #pragma once
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "types.hpp"
  6. // inline void print_hm(StringHashMap* hm) {
  7. // for_str_hash_map(hm) {
  8. // printf("index: %d desired-index: %llu key: %s hash: %llu value: %llu\n", i, (hm->data[i].hash % hm->current_capacity), (char*)key, hm->data[i].hash, (unsigned long long)value);
  9. // }
  10. // }
  11. #define __for_hm_generator(key_type, hm) \
  12. if (key_type key = nullptr); else \
  13. if (void* value = nullptr); else \
  14. for(int index = 0; index < hm->current_capacity; ++index) \
  15. if (!((key = hm->data[index].original) && \
  16. (value = hm->data[index].object))); else
  17. #define define_hash_map(type, name) \
  18. bool hm_objects_match(type,type); \
  19. u32 hm_hash(type); \
  20. \
  21. struct name##_Hash_Map_Cell { \
  22. type original; \
  23. u64 hash; \
  24. bool deleted; \
  25. void* object; \
  26. }; \
  27. \
  28. struct name##_Hash_Map { \
  29. int current_capacity; \
  30. int cell_count; \
  31. name##_Hash_Map_Cell* data; \
  32. }; \
  33. \
  34. name##_Hash_Map* create_##name##_hashmap(int initial_capacity=8) { \
  35. name##_Hash_Map* hm = new name##_Hash_Map; \
  36. hm->current_capacity = initial_capacity; \
  37. hm->cell_count = 0; \
  38. /* set all data to nullptr */ \
  39. hm->data = (name##_Hash_Map_Cell*)calloc(initial_capacity, sizeof(name##_Hash_Map_Cell)); \
  40. /* printf("check: %s\n", hm->data[6].original_string); */ \
  41. return hm; \
  42. } \
  43. \
  44. int hm_get_index_of_living_cell_if_it_exists(name##_Hash_Map* hm, type key, u64 hash_val) { \
  45. int index = hash_val % hm->current_capacity; \
  46. name##_Hash_Map_Cell cell = hm->data[index]; \
  47. /* test if cell exists at that index */ \
  48. if (cell.original) { \
  49. /* check if strings match */ \
  50. if (hm_objects_match(key, cell.original)) { \
  51. /* we found it, now check it it is deleted: */ \
  52. if (cell.deleted) { \
  53. /* we found it but it was deleted, we */ \
  54. /* dont have to check for collisions then */ \
  55. return -1; \
  56. } else { \
  57. /* we found it and it is not deleted */ \
  58. return index; \
  59. } \
  60. } else { \
  61. /* strings dont match, this means we have */ \
  62. /* a collision. We just search forward */ \
  63. for (int i = 0; i < hm->current_capacity; ++i) { \
  64. int new_idx = (i + index) % hm->current_capacity; \
  65. cell = hm->data[new_idx]; \
  66. if (!cell.original) \
  67. return -1; \
  68. if (!hm_objects_match(key, cell.original)) \
  69. continue; \
  70. if (cell.deleted) \
  71. continue; \
  72. return new_idx; \
  73. } \
  74. /* not or only deleted cells found */ \
  75. return -1; \
  76. } \
  77. } else { \
  78. /* no cell exists at this index so the item was never in the */ \
  79. /* hashmap. Either it would be there or be ther and 'deleted' */ \
  80. /* or another item would be there and therefore a collistion */ \
  81. /* would exist */ \
  82. return -1; \
  83. } \
  84. } \
  85. \
  86. inline bool hm_key_exists(name##_Hash_Map* hm, type key) { \
  87. return hm_get_index_of_living_cell_if_it_exists(hm, key, hm_hash(key)) != -1; \
  88. } \
  89. \
  90. inline void* hm_get_object(name##_Hash_Map* hm, type key) { \
  91. int index = hm_get_index_of_living_cell_if_it_exists(hm, key, hm_hash(key)); \
  92. if (index != -1) { \
  93. return hm->data[index].object; \
  94. } \
  95. return nullptr; \
  96. } \
  97. \
  98. inline void hm_delete_object(name##_Hash_Map* hm, type key) { \
  99. int index = hm_get_index_of_living_cell_if_it_exists(hm, key, hm_hash(key)); \
  100. if (index != -1) { \
  101. hm->data[index].deleted = true; \
  102. } \
  103. } \
  104. \
  105. void hm_set(name##_Hash_Map* hm, type key, void* obj) { \
  106. u64 hash_val = hm_hash(key); \
  107. int index = hash_val % hm->current_capacity; \
  108. \
  109. /* if we the desired cell is just empty, write to it and done :) */ \
  110. if (!hm->data[index].original) { \
  111. /* insert new cell into desired slot */ \
  112. ++hm->cell_count; \
  113. } else { \
  114. if (hm_objects_match(key, hm->data[index].original)) { \
  115. /* overwrite object with same key, dont increment cell */ \
  116. /* count */ \
  117. } else { \
  118. /* collision, check resize */ \
  119. ++hm->cell_count; \
  120. if ((hm->cell_count*1.0f / hm->current_capacity) > 0.666f) { \
  121. auto old_data = hm->data; \
  122. hm->data = (name##_Hash_Map_Cell*)calloc(hm->current_capacity*4, sizeof(name##_Hash_Map_Cell)); \
  123. hm->cell_count = 0; \
  124. hm->current_capacity *= 4; \
  125. \
  126. /* insert all old items again */ \
  127. for (int i = 0; i < hm->current_capacity/4; ++i) { \
  128. auto cell = old_data[i]; \
  129. if (cell.original) { \
  130. hm_set(hm, cell.original, cell.object); \
  131. } \
  132. } \
  133. free(old_data); \
  134. index = hash_val % hm->current_capacity; \
  135. } \
  136. /* search for empty slot for new cell starting at desired index; */ \
  137. /* preventing gotos using lambdas! */ \
  138. [&](){ \
  139. for (int i = index; i < hm->current_capacity; ++i) { \
  140. if (!hm->data[i].original || \
  141. hm_objects_match(hm->data[i].original, key)) \
  142. { \
  143. index = i; \
  144. return; \
  145. } \
  146. } \
  147. for (int i = 0; i < index; ++i) { \
  148. if (!hm->data[i].original || \
  149. hm_objects_match(hm->data[i].original, key)) \
  150. { \
  151. index = i; \
  152. return; \
  153. } \
  154. } \
  155. }(); \
  156. } \
  157. } \
  158. \
  159. hm->data[index].deleted = false; \
  160. hm->data[index].original = key; \
  161. hm->data[index].hash = hash_val; \
  162. hm->data[index].object = obj; \
  163. } \