Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

276 linhas
9.4 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*)ftb_calloc(initial_capacity, sizeof(HM_Cell));
  68. }
  69. void dealloc() {
  70. ftb_free(data);
  71. data = nullptr;
  72. }
  73. void clear() {
  74. cell_count = 0;
  75. memset(data, 0, current_capacity * sizeof(HM_Cell));
  76. }
  77. s32 get_index_of_living_cell_if_it_exists(key_type key, u64 hash_val) {
  78. s32 index = hash_val & (current_capacity - 1);
  79. HM_Cell cell = data[index];
  80. /* test if there is or was something there */
  81. if (cell.occupancy != HM_Cell::Occupancy::Avaliable) {
  82. /* check if objects match */
  83. if (hm_objects_match(key, cell.original)) {
  84. /* we found it, now check it it is deleted: */
  85. if (cell.occupancy == HM_Cell::Occupancy::Deleted) {
  86. /* we found it but it was deleted, we */
  87. /* dont have to check for collisions then */
  88. return -1;
  89. } else {
  90. /* we found it and it is not deleted */
  91. return index;
  92. }
  93. } else {
  94. /* objects dont match, this means we have */
  95. /* a collision. We just search forward */
  96. for (u32 i = 0; i < current_capacity; ++i) {
  97. u32 new_idx = (i + index) & (current_capacity - 1);
  98. cell = data[new_idx];
  99. /* If we find a avaliable cell while looking */
  100. /* forward, the object is not in the hm */
  101. if (cell.occupancy == HM_Cell::Occupancy::Avaliable)
  102. return -1;
  103. /* If the objects don't match, keep looking */
  104. if (!hm_objects_match(key, cell.original))
  105. continue;
  106. /* TODO(Felix): If the objects do match, */
  107. /* and it is deleted, we should return -1? */
  108. if (cell.occupancy == HM_Cell::Occupancy::Deleted)
  109. continue;
  110. return new_idx;
  111. }
  112. /* not or only deleted cells found */
  113. return -1;
  114. }
  115. } else {
  116. /* no cell exists at this index so the item was never in the */
  117. /* hashmap. Either it would be there or be ther and 'deleted' */
  118. /* or another item would be there and therefore a collistion */
  119. /* would exist */
  120. return -1;
  121. }
  122. }
  123. bool key_exists(key_type key) {
  124. return get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key)) != -1;
  125. }
  126. key_type search_key_to_object(value_type v) {
  127. for (u32 i = 0; i < current_capacity; ++i) {
  128. if (data[i].object == v &&
  129. data[i].occupancy == HM_Cell::Occupancy::Occupied)
  130. {
  131. return data[i].original;
  132. }
  133. }
  134. return nullptr;
  135. }
  136. Array_List<key_type> get_all_keys() {
  137. Array_List<key_type> ret;
  138. ret.alloc();
  139. // QUESTION(Felix): Does it make sense to
  140. // ret.reserve(this->cell_count)?
  141. for (u32 i = 0; i < current_capacity; ++i) {
  142. if (data[i].occupancy == HM_Cell::Occupancy::Occupied)
  143. ret.append(data[i].original);
  144. }
  145. return ret;
  146. }
  147. value_type get_object(key_type key, u64 hash_val) {
  148. s32 index = get_index_of_living_cell_if_it_exists(key, hash_val);
  149. if (index != -1) {
  150. return data[index].object;
  151. }
  152. return 0;
  153. }
  154. value_type get_object(key_type key) {
  155. return get_object(key, hm_hash((key_type)key));
  156. }
  157. value_type* get_object_ptr(key_type key, u64 hash_val) {
  158. s32 index = get_index_of_living_cell_if_it_exists(key, hash_val);
  159. if (index != -1) {
  160. return &(data[index].object);
  161. }
  162. return 0;
  163. }
  164. value_type* get_object_ptr(key_type key) {
  165. return get_object_ptr(key, hm_hash((key_type)key));
  166. }
  167. void delete_object(key_type key) {
  168. s32 index = get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key));
  169. if (index != -1) {
  170. data[index].occupancy = HM_Cell::Occupancy::Deleted;
  171. }
  172. }
  173. void set_object(key_type key, value_type obj, u64 hash_val) {
  174. u32 index = hash_val & (current_capacity - 1);
  175. /* if we the desired cell is avaliable, write to it and done :) */
  176. if (data[index].occupancy == HM_Cell::Occupancy::Avaliable) {
  177. /* insert new cell into desired slot */
  178. ++cell_count;
  179. } else {
  180. if (hm_objects_match(key, data[index].original)) {
  181. /* overwrite object with same key, dont increment cell */
  182. /* count */
  183. } else {
  184. /* collision, check resize */
  185. if ((cell_count*1.0f / current_capacity) > 0.666f) {
  186. auto old_data = data;
  187. data = (HM_Cell*)ftb_calloc(current_capacity*4, sizeof(HM_Cell));
  188. cell_count = 0;
  189. current_capacity *= 4;
  190. /* insert all old items again */
  191. for (u32 i = 0; i < current_capacity/4; ++i) {
  192. auto cell = old_data[i];
  193. if (cell.occupancy == HM_Cell::Occupancy::Occupied) {
  194. set_object(cell.original, cell.object, cell.hash);
  195. }
  196. }
  197. ftb_free(old_data);
  198. index = hash_val & (current_capacity - 1);
  199. }
  200. ++cell_count;
  201. /* search for empty slot for new cell starting at desired index; */
  202. /* preventing gotos using lambdas! */
  203. [&]{
  204. for (u32 i = index; i < current_capacity; ++i) {
  205. if (data[i].occupancy == HM_Cell::Occupancy::Avaliable ||
  206. hm_objects_match(data[i].original, key))
  207. {
  208. index = i;
  209. return;
  210. }
  211. }
  212. for (u32 i = 0; i < index; ++i) {
  213. if (data[i].occupancy == HM_Cell::Occupancy::Avaliable ||
  214. hm_objects_match(data[i].original, key))
  215. {
  216. index = i;
  217. return;
  218. }
  219. }
  220. }();
  221. }
  222. }
  223. data[index].occupancy = HM_Cell::Occupancy::Occupied;
  224. data[index].original = key;
  225. data[index].hash = hash_val;
  226. data[index].object = obj;
  227. }
  228. void set_object(key_type key, value_type obj) {
  229. u64 hash_val = hm_hash((key_type)key);
  230. set_object(key, obj, hash_val);
  231. }
  232. void dump_occupancy(const char* path) {
  233. FILE* out = fopen(path, "w");
  234. defer { fclose(out); };
  235. for (u32 i = 0; i < current_capacity; ++i) {
  236. if (data[i].occupancy == HM_Cell::Occupancy::Avaliable) {
  237. fprintf(out, "%04u [FREE]\n", i);
  238. } else if (data[i].occupancy == HM_Cell::Occupancy::Deleted) {
  239. fprintf(out, "%04u [DELETED] hash: %llu (wants to be %llu)\n", i, data[i].hash, data[i].hash & (current_capacity - 1));
  240. } else {
  241. fprintf(out, "%04u [OCCUPIED] hash: %llu (wants to be %llu)\n", i, data[i].hash, data[i].hash & (current_capacity - 1));
  242. }
  243. }
  244. }
  245. };