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

247 строки
8.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. current_capacity = initial_capacity;
  57. cell_count = 0;
  58. data = (HM_Cell*)calloc(initial_capacity, sizeof(HM_Cell));
  59. }
  60. void dealloc() {
  61. free(data);
  62. data = nullptr;
  63. }
  64. s32 get_index_of_living_cell_if_it_exists(key_type key, u64 hash_val) {
  65. // s32 index = hash_val & (current_capacity - 1);
  66. s32 index = hash_val % current_capacity;
  67. HM_Cell cell = data[index];
  68. /* test if there is or was something there */
  69. if (cell.occupancy != HM_Cell::Occupancy::Avaliable) {
  70. /* check if objects match */
  71. if (hm_objects_match(key, cell.original)) {
  72. /* we found it, now check it it is deleted: */
  73. if (cell.occupancy == HM_Cell::Occupancy::Deleted) {
  74. /* we found it but it was deleted, we */
  75. /* dont have to check for collisions then */
  76. return -1;
  77. } else {
  78. /* we found it and it is not deleted */
  79. return index;
  80. }
  81. } else {
  82. /* objects dont match, this means we have */
  83. /* a collision. We just search forward */
  84. for (u32 i = 0; i < current_capacity; ++i) {
  85. u32 new_idx = (i + index) % current_capacity;
  86. cell = data[new_idx];
  87. /* If we find a avaliable cell while looking */
  88. /* forward, the object is not in the hm */
  89. if (cell.occupancy == HM_Cell::Occupancy::Avaliable)
  90. return -1;
  91. /* If the objects don't match, keep looking */
  92. if (!hm_objects_match(key, cell.original))
  93. continue;
  94. /* TODO(Felix): If the objects do match, */
  95. /* and it is deleted, we should return -1? */
  96. if (cell.occupancy == HM_Cell::Occupancy::Deleted)
  97. continue;
  98. return new_idx;
  99. }
  100. /* not or only deleted cells found */
  101. return -1;
  102. }
  103. } else {
  104. /* no cell exists at this index so the item was never in the */
  105. /* hashmap. Either it would be there or be ther and 'deleted' */
  106. /* or another item would be there and therefore a collistion */
  107. /* would exist */
  108. return -1;
  109. }
  110. }
  111. bool key_exists(key_type key) {
  112. return get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key)) != -1;
  113. }
  114. key_type search_key_to_object(value_type v) {
  115. for (u32 i = 0; i < current_capacity; ++i) {
  116. if (data[i].object == v &&
  117. data[i].occupancy == HM_Cell::Occupancy::Occupied)
  118. {
  119. return data[i].original;
  120. }
  121. }
  122. return nullptr;
  123. }
  124. Array_List<key_type> get_all_keys() {
  125. Array_List<key_type> ret;
  126. ret.alloc();
  127. // QUESTION(Felix): Does it make sense to
  128. // ret.reserve(this->cell_count)?
  129. for (u32 i = 0; i < current_capacity; ++i) {
  130. if (data[i].occupancy == HM_Cell::Occupancy::Occupied)
  131. ret.append(data[i].original);
  132. }
  133. return ret;
  134. }
  135. value_type get_object(key_type key, u64 hash_val) {
  136. s32 index = get_index_of_living_cell_if_it_exists(key, hash_val);
  137. if (index != -1) {
  138. return data[index].object;
  139. }
  140. return 0;
  141. }
  142. value_type get_object(key_type key) {
  143. return get_object(key, hm_hash((key_type)key));
  144. }
  145. value_type* get_object_ptr(key_type key, u64 hash_val) {
  146. s32 index = get_index_of_living_cell_if_it_exists(key, hash_val);
  147. if (index != -1) {
  148. return &(data[index].object);
  149. }
  150. return 0;
  151. }
  152. value_type* get_object_ptr(key_type key) {
  153. return get_object_ptr(key, hm_hash((key_type)key));
  154. }
  155. void delete_object(key_type key) {
  156. s32 index = get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key));
  157. if (index != -1) {
  158. data[index].occupancy = HM_Cell::Occupancy::Deleted;
  159. }
  160. }
  161. void set_object(key_type key, value_type obj, u64 hash_val) {
  162. u32 index = hash_val % current_capacity;
  163. /* if we the desired cell is avaliable, write to it and done :) */
  164. if (data[index].occupancy == HM_Cell::Occupancy::Avaliable) {
  165. /* insert new cell into desired slot */
  166. ++cell_count;
  167. } else {
  168. if (hm_objects_match(key, data[index].original)) {
  169. /* overwrite object with same key, dont increment cell */
  170. /* count */
  171. } else {
  172. /* collision, check resize */
  173. if ((cell_count*1.0f / current_capacity) > 0.666f) {
  174. auto old_data = data;
  175. data = (HM_Cell*)calloc(current_capacity*4, sizeof(HM_Cell));
  176. cell_count = 0;
  177. current_capacity *= 4;
  178. /* insert all old items again */
  179. for (u32 i = 0; i < current_capacity/4; ++i) {
  180. auto cell = old_data[i];
  181. if (cell.occupancy == HM_Cell::Occupancy::Occupied) {
  182. set_object(cell.original, cell.object, cell.hash);
  183. }
  184. }
  185. free(old_data);
  186. index = hash_val % current_capacity;
  187. }
  188. ++cell_count;
  189. /* search for empty slot for new cell starting at desired index; */
  190. /* preventing gotos using lambdas! */
  191. [&]{
  192. for (u32 i = index; i < current_capacity; ++i) {
  193. if (data[i].occupancy == HM_Cell::Occupancy::Avaliable ||
  194. hm_objects_match(data[i].original, key))
  195. {
  196. index = i;
  197. return;
  198. }
  199. }
  200. for (u32 i = 0; i < index; ++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. }();
  209. }
  210. }
  211. data[index].occupancy = HM_Cell::Occupancy::Occupied;
  212. data[index].original = key;
  213. data[index].hash = hash_val;
  214. data[index].object = obj;
  215. }
  216. void set_object(key_type key, value_type obj) {
  217. u64 hash_val = hm_hash((key_type)key);
  218. set_object(key, obj, hash_val);
  219. }
  220. };