Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

270 строки
9.2 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(const 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(char* str) {
  16. u32 value = str[0] << 7;
  17. s32 i = 0;
  18. while (str[i]) {
  19. value = (10000003 * value) ^ str[i++];
  20. }
  21. return value ^ i;
  22. }
  23. u32 hm_hash(void* ptr) {
  24. return ((u64)ptr * 2654435761) % 4294967296;
  25. }
  26. inline bool hm_objects_match(const char* a, const char* b) {
  27. return strcmp(a, b) == 0;
  28. }
  29. inline bool hm_objects_match(char* a, char* b) {
  30. return strcmp(a, b) == 0;
  31. }
  32. inline bool hm_objects_match(void* a, void* b) {
  33. return a == b;
  34. }
  35. template <typename key_type, typename value_type>
  36. struct Hash_Map {
  37. u32 current_capacity;
  38. u32 cell_count;
  39. struct HM_Cell {
  40. key_type original;
  41. u64 hash;
  42. enum struct Occupancy : u8 {
  43. Avaliable = 0,
  44. Occupied,
  45. Deleted
  46. } occupancy;
  47. value_type object;
  48. }* data;
  49. template <typename lambda>
  50. void for_each(lambda p) {
  51. for(u32 index = 0; index < current_capacity; ++index)
  52. if (data[index].occupancy == HM_Cell::Occupancy::Occupied)
  53. p(data[index].original, data[index].object, index);
  54. }
  55. void alloc(u32 initial_capacity = 8) {
  56. // round up to next pow of 2
  57. --initial_capacity;
  58. initial_capacity |= initial_capacity >> 1;
  59. initial_capacity |= initial_capacity >> 2;
  60. initial_capacity |= initial_capacity >> 4;
  61. initial_capacity |= initial_capacity >> 8;
  62. initial_capacity |= initial_capacity >> 16;
  63. ++initial_capacity;
  64. // until here
  65. current_capacity = initial_capacity;
  66. cell_count = 0;
  67. data = (HM_Cell*)calloc(initial_capacity, sizeof(HM_Cell));
  68. }
  69. void dealloc() {
  70. free(data);
  71. data = nullptr;
  72. }
  73. s32 get_index_of_living_cell_if_it_exists(key_type key, u64 hash_val) {
  74. s32 index = hash_val & (current_capacity - 1);
  75. HM_Cell cell = data[index];
  76. /* test if there is or was something there */
  77. if (cell.occupancy != HM_Cell::Occupancy::Avaliable) {
  78. /* check if objects match */
  79. if (hm_objects_match(key, cell.original)) {
  80. /* we found it, now check it it is deleted: */
  81. if (cell.occupancy == HM_Cell::Occupancy::Deleted) {
  82. /* we found it but it was deleted, we */
  83. /* dont have to check for collisions then */
  84. return -1;
  85. } else {
  86. /* we found it and it is not deleted */
  87. return index;
  88. }
  89. } else {
  90. /* objects dont match, this means we have */
  91. /* a collision. We just search forward */
  92. for (u32 i = 0; i < current_capacity; ++i) {
  93. u32 new_idx = (i + index) & (current_capacity - 1);
  94. cell = data[new_idx];
  95. /* If we find a avaliable cell while looking */
  96. /* forward, the object is not in the hm */
  97. if (cell.occupancy == HM_Cell::Occupancy::Avaliable)
  98. return -1;
  99. /* If the objects don't match, keep looking */
  100. if (!hm_objects_match(key, cell.original))
  101. continue;
  102. /* TODO(Felix): If the objects do match, */
  103. /* and it is deleted, we should return -1? */
  104. if (cell.occupancy == HM_Cell::Occupancy::Deleted)
  105. continue;
  106. return new_idx;
  107. }
  108. /* not or only deleted cells found */
  109. return -1;
  110. }
  111. } else {
  112. /* no cell exists at this index so the item was never in the */
  113. /* hashmap. Either it would be there or be ther and 'deleted' */
  114. /* or another item would be there and therefore a collistion */
  115. /* would exist */
  116. return -1;
  117. }
  118. }
  119. bool key_exists(key_type key) {
  120. return get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key)) != -1;
  121. }
  122. key_type search_key_to_object(value_type v) {
  123. for (u32 i = 0; i < current_capacity; ++i) {
  124. if (data[i].object == v &&
  125. data[i].occupancy == HM_Cell::Occupancy::Occupied)
  126. {
  127. return data[i].original;
  128. }
  129. }
  130. return nullptr;
  131. }
  132. Array_List<key_type> get_all_keys() {
  133. Array_List<key_type> ret;
  134. ret.alloc();
  135. // QUESTION(Felix): Does it make sense to
  136. // ret.reserve(this->cell_count)?
  137. for (u32 i = 0; i < current_capacity; ++i) {
  138. if (data[i].occupancy == HM_Cell::Occupancy::Occupied)
  139. ret.append(data[i].original);
  140. }
  141. return ret;
  142. }
  143. value_type get_object(key_type key, u64 hash_val) {
  144. s32 index = get_index_of_living_cell_if_it_exists(key, hash_val);
  145. if (index != -1) {
  146. return data[index].object;
  147. }
  148. return 0;
  149. }
  150. value_type get_object(key_type key) {
  151. return get_object(key, hm_hash((key_type)key));
  152. }
  153. value_type* get_object_ptr(key_type key, u64 hash_val) {
  154. s32 index = get_index_of_living_cell_if_it_exists(key, hash_val);
  155. if (index != -1) {
  156. return &(data[index].object);
  157. }
  158. return 0;
  159. }
  160. value_type* get_object_ptr(key_type key) {
  161. return get_object_ptr(key, hm_hash((key_type)key));
  162. }
  163. void delete_object(key_type key) {
  164. s32 index = get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key));
  165. if (index != -1) {
  166. data[index].occupancy = HM_Cell::Occupancy::Deleted;
  167. }
  168. }
  169. void set_object(key_type key, value_type obj, u64 hash_val) {
  170. u32 index = hash_val & (current_capacity - 1);
  171. /* if we the desired cell is avaliable, write to it and done :) */
  172. if (data[index].occupancy == HM_Cell::Occupancy::Avaliable) {
  173. /* insert new cell into desired slot */
  174. ++cell_count;
  175. } else {
  176. if (hm_objects_match(key, data[index].original)) {
  177. /* overwrite object with same key, dont increment cell */
  178. /* count */
  179. } else {
  180. /* collision, check resize */
  181. if ((cell_count*1.0f / current_capacity) > 0.666f) {
  182. auto old_data = data;
  183. data = (HM_Cell*)calloc(current_capacity*4, sizeof(HM_Cell));
  184. cell_count = 0;
  185. current_capacity *= 4;
  186. /* insert all old items again */
  187. for (u32 i = 0; i < current_capacity/4; ++i) {
  188. auto cell = old_data[i];
  189. if (cell.occupancy == HM_Cell::Occupancy::Occupied) {
  190. set_object(cell.original, cell.object, cell.hash);
  191. }
  192. }
  193. free(old_data);
  194. index = hash_val & (current_capacity - 1);
  195. }
  196. ++cell_count;
  197. /* search for empty slot for new cell starting at desired index; */
  198. /* preventing gotos using lambdas! */
  199. [&]{
  200. for (u32 i = index; i < current_capacity; ++i) {
  201. if (data[i].occupancy == HM_Cell::Occupancy::Avaliable ||
  202. hm_objects_match(data[i].original, key))
  203. {
  204. index = i;
  205. return;
  206. }
  207. }
  208. for (u32 i = 0; i < index; ++i) {
  209. if (data[i].occupancy == HM_Cell::Occupancy::Avaliable ||
  210. hm_objects_match(data[i].original, key))
  211. {
  212. index = i;
  213. return;
  214. }
  215. }
  216. }();
  217. }
  218. }
  219. data[index].occupancy = HM_Cell::Occupancy::Occupied;
  220. data[index].original = key;
  221. data[index].hash = hash_val;
  222. data[index].object = obj;
  223. }
  224. void set_object(key_type key, value_type obj) {
  225. u64 hash_val = hm_hash((key_type)key);
  226. set_object(key, obj, hash_val);
  227. }
  228. void dump_occupancy(const char* path) {
  229. FILE* out = fopen(path, "w");
  230. defer { fclose(out); };
  231. for (u32 i = 0; i < current_capacity; ++i) {
  232. if (data[i].occupancy == HM_Cell::Occupancy::Avaliable) {
  233. fprintf(out, "%04u [FREE]\n", i);
  234. } else if (data[i].occupancy == HM_Cell::Occupancy::Deleted) {
  235. fprintf(out, "%04u [DELETED] hash: %llu (wants to be %llu)\n", i, data[i].hash, data[i].hash & (current_capacity - 1));
  236. } else {
  237. fprintf(out, "%04u [OCCUPIED] hash: %llu (wants to be %llu)\n", i, data[i].hash, data[i].hash & (current_capacity - 1));
  238. }
  239. }
  240. }
  241. };