Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

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