Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

213 рядки
7.0 KiB

  1. #pragma once
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "types.hpp"
  6. #include "arraylist.hpp"
  7. u32 hm_hash(char* str) {
  8. u32 value = str[0] << 7;
  9. s32 i = 0;
  10. while (str[i]) {
  11. value = (10000003 * value) ^ str[i++];
  12. }
  13. return value ^ i;
  14. }
  15. u32 hm_hash(void* ptr) {
  16. return ((u64)ptr * 2654435761) % 4294967296;
  17. }
  18. inline bool hm_objects_match(char* a, char* b) {
  19. return strcmp(a, b) == 0;
  20. }
  21. inline bool hm_objects_match(void* a, void* b) {
  22. return a == b;
  23. }
  24. #define for_hash_map(hm) \
  25. if (decltype((hm).data[0].original) key = 0); else \
  26. if (decltype((hm).data[0].object) value = 0); else \
  27. for(u32 index = 0; index < (hm).current_capacity; ++index) \
  28. if (!((!(hm).data[index].deleted) && \
  29. (key = (hm).data[index].original) && \
  30. (value = (hm).data[index].object))); else
  31. template <typename key_type, typename value_type>
  32. struct Hash_Map {
  33. u32 current_capacity;
  34. u32 cell_count;
  35. struct HM_Cell {
  36. key_type original;
  37. u64 hash;
  38. bool deleted;
  39. value_type object;
  40. }* data;
  41. void alloc(u32 initial_capacity = 8) {
  42. current_capacity = initial_capacity;
  43. cell_count = 0;
  44. data = (HM_Cell*)calloc(initial_capacity, sizeof(HM_Cell));
  45. }
  46. void dealloc() {
  47. free(data);
  48. data = nullptr;
  49. }
  50. s32 get_index_of_living_cell_if_it_exists(key_type key, u64 hash_val) {
  51. // s32 index = hash_val & (current_capacity - 1);
  52. s32 index = hash_val % current_capacity;
  53. HM_Cell cell = data[index];
  54. /* test if cell exists at that index */
  55. if (cell.original) {
  56. /* check if strings match */
  57. if (hm_objects_match(key, cell.original)) {
  58. /* we found it, now check it it is deleted: */
  59. if (cell.deleted) {
  60. /* we found it but it was deleted, we */
  61. /* dont have to check for collisions then */
  62. return -1;
  63. } else {
  64. /* we found it and it is not deleted */
  65. return index;
  66. }
  67. } else {
  68. /* strings dont match, this means we have */
  69. /* a collision. We just search forward */
  70. for (u32 i = 0; i < current_capacity; ++i) {
  71. u32 new_idx = (i + index) % current_capacity;
  72. cell = data[new_idx];
  73. if (!cell.original)
  74. return -1;
  75. if (!hm_objects_match(key, cell.original))
  76. continue;
  77. if (cell.deleted)
  78. continue;
  79. return new_idx;
  80. }
  81. /* not or only deleted cells found */
  82. return -1;
  83. }
  84. } else {
  85. /* no cell exists at this index so the item was never in the */
  86. /* hashmap. Either it would be there or be ther and 'deleted' */
  87. /* or another item would be there and therefore a collistion */
  88. /* would exist */
  89. return -1;
  90. }
  91. }
  92. bool key_exists(key_type key) {
  93. return get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key)) != -1;
  94. }
  95. key_type search_key_to_object(value_type v) {
  96. for (u32 i = 0; i < current_capacity; ++i) {
  97. if (data[i].object == v && !data[i].deleted)
  98. return data[i].original;
  99. }
  100. return nullptr;
  101. }
  102. Array_List<key_type> get_all_keys() {
  103. Array_List<key_type> ret;
  104. ret.alloc();
  105. // QUESTION(Felix): Does it make sense to
  106. // ret.reserve(this->cell_count)?
  107. for (u32 i = 0; i < current_capacity; ++i) {
  108. if (data[i].original && !data[i].deleted)
  109. ret.append(data[i].original);
  110. }
  111. return ret;
  112. }
  113. value_type get_object(key_type key, u64 hash_val) {
  114. s32 index = get_index_of_living_cell_if_it_exists(key, hash_val);
  115. if (index != -1) {
  116. return data[index].object;
  117. }
  118. return 0;
  119. }
  120. value_type get_object(key_type key) {
  121. return get_object(key, hm_hash((key_type)key));
  122. }
  123. void delete_object(key_type key) {
  124. s32 index = get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key));
  125. if (index != -1) {
  126. data[index].deleted = true;
  127. }
  128. }
  129. void set_object(key_type key, value_type obj, u64 hash_val) {
  130. u32 index = hash_val % current_capacity;
  131. /* if we the desired cell is just empty, write to it and done :) */
  132. if (!data[index].original) {
  133. /* insert new cell into desired slot */
  134. ++cell_count;
  135. } else {
  136. if (hm_objects_match(key, data[index].original)) {
  137. /* overwrite object with same key, dont increment cell */
  138. /* count */
  139. } else {
  140. /* collision, check resize */
  141. if ((cell_count*1.0f / current_capacity) > 0.666f) {
  142. auto old_data = data;
  143. data = (HM_Cell*)calloc(current_capacity*4, sizeof(HM_Cell));
  144. cell_count = 0;
  145. current_capacity *= 4;
  146. /* insert all old items again */
  147. for (u32 i = 0; i < current_capacity/4; ++i) {
  148. auto cell = old_data[i];
  149. if (cell.original) {
  150. set_object(cell.original, cell.object, cell.hash);
  151. }
  152. }
  153. free(old_data);
  154. index = hash_val % current_capacity;
  155. }
  156. ++cell_count;
  157. /* search for empty slot for new cell starting at desired index; */
  158. /* preventing gotos using lambdas! */
  159. [&]{
  160. for (u32 i = index; i < current_capacity; ++i) {
  161. if (!data[i].original ||
  162. hm_objects_match(data[i].original, key))
  163. {
  164. index = i;
  165. return;
  166. }
  167. }
  168. for (u32 i = 0; i < index; ++i) {
  169. if (!data[i].original ||
  170. hm_objects_match(data[i].original, key))
  171. {
  172. index = i;
  173. return;
  174. }
  175. }
  176. }();
  177. }
  178. }
  179. data[index].deleted = false;
  180. data[index].original = key;
  181. data[index].hash = hash_val;
  182. data[index].object = obj;
  183. }
  184. void set_object(key_type key, value_type obj) {
  185. u64 hash_val = hm_hash((key_type)key);
  186. set_object(key, obj, hash_val);
  187. }
  188. };