dynamic_map_internal.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package runtime
  2. import "core:intrinsics"
  3. _ :: intrinsics;
  4. INITIAL_MAP_CAP :: 16;
  5. // Temporary data structure for comparing hashes and keys
  6. Map_Hash :: struct {
  7. hash: uintptr,
  8. key_ptr: rawptr, // address of Map_Entry_Header.key
  9. }
  10. __get_map_hash :: proc "contextless" (k: ^$K) -> (map_hash: Map_Hash) {
  11. hasher := intrinsics.type_hasher_proc(K);
  12. map_hash.key_ptr = k;
  13. map_hash.hash = hasher(k, 0);
  14. return;
  15. }
  16. __get_map_hash_from_entry :: proc "contextless" (h: Map_Header, entry: ^Map_Entry_Header) -> (hash: Map_Hash) {
  17. hash.hash = entry.hash;
  18. hash.key_ptr = rawptr(uintptr(entry) + h.key_offset);
  19. return;
  20. }
  21. Map_Find_Result :: struct {
  22. hash_index: int,
  23. entry_prev: int,
  24. entry_index: int,
  25. }
  26. Map_Entry_Header :: struct {
  27. hash: uintptr,
  28. next: int,
  29. /*
  30. key: Key_Value,
  31. value: Value_Type,
  32. */
  33. }
  34. Map_Header :: struct {
  35. m: ^Raw_Map,
  36. equal: Equal_Proc,
  37. entry_size: int,
  38. entry_align: int,
  39. key_offset: uintptr,
  40. key_size: int,
  41. value_offset: uintptr,
  42. value_size: int,
  43. }
  44. INITIAL_HASH_SEED :: 0xcbf29ce484222325;
  45. _fnv64a :: proc "contextless" (data: []byte, seed: u64 = INITIAL_HASH_SEED) -> u64 {
  46. h: u64 = seed;
  47. for b in data {
  48. h = (h ~ u64(b)) * 0x100000001b3;
  49. }
  50. return h;
  51. }
  52. default_hash :: #force_inline proc "contextless" (data: []byte) -> uintptr {
  53. return uintptr(_fnv64a(data));
  54. }
  55. default_hash_string :: #force_inline proc "contextless" (s: string) -> uintptr {
  56. return default_hash(transmute([]byte)(s));
  57. }
  58. default_hash_ptr :: #force_inline proc "contextless" (data: rawptr, size: int) -> uintptr {
  59. s := Raw_Slice{data, size};
  60. return default_hash(transmute([]byte)(s));
  61. }
  62. @(private)
  63. _default_hasher_const :: #force_inline proc "contextless" (data: rawptr, seed: uintptr, $N: uint) -> uintptr where N <= 16 {
  64. h := u64(seed) + 0xcbf29ce484222325;
  65. p := uintptr(data);
  66. #unroll for _ in 0..<N {
  67. b := u64((^byte)(p)^);
  68. h = (h ~ b) * 0x100000001b3;
  69. p += 1;
  70. }
  71. return uintptr(h);
  72. }
  73. default_hasher_n :: #force_inline proc "contextless" (data: rawptr, seed: uintptr, N: int) -> uintptr {
  74. h := u64(seed) + 0xcbf29ce484222325;
  75. p := uintptr(data);
  76. for _ in 0..<N {
  77. b := u64((^byte)(p)^);
  78. h = (h ~ b) * 0x100000001b3;
  79. p += 1;
  80. }
  81. return uintptr(h);
  82. }
  83. // NOTE(bill): There are loads of predefined ones to improve optimizations for small types
  84. default_hasher1 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 1); }
  85. default_hasher2 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 2); }
  86. default_hasher3 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 3); }
  87. default_hasher4 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 4); }
  88. default_hasher5 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 5); }
  89. default_hasher6 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 6); }
  90. default_hasher7 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 7); }
  91. default_hasher8 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 8); }
  92. default_hasher9 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 9); }
  93. default_hasher10 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 10); }
  94. default_hasher11 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 11); }
  95. default_hasher12 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 12); }
  96. default_hasher13 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 13); }
  97. default_hasher14 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 14); }
  98. default_hasher15 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 15); }
  99. default_hasher16 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return #force_inline _default_hasher_const(data, seed, 16); }
  100. default_hasher_string :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr {
  101. h := u64(seed) + 0xcbf29ce484222325;
  102. str := (^[]byte)(data)^;
  103. for b in str {
  104. h = (h ~ u64(b)) * 0x100000001b3;
  105. }
  106. return uintptr(h);
  107. }
  108. default_hasher_cstring :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr {
  109. h := u64(seed) + 0xcbf29ce484222325;
  110. ptr := (^uintptr)(data)^;
  111. for (^byte)(ptr)^ != 0 {
  112. b := (^byte)(ptr)^;
  113. h = (h ~ u64(b)) * 0x100000001b3;
  114. ptr += 1;
  115. }
  116. return uintptr(h);
  117. }
  118. __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
  119. header := Map_Header{m = (^Raw_Map)(m)};
  120. Entry :: struct {
  121. hash: uintptr,
  122. next: int,
  123. key: K,
  124. value: V,
  125. };
  126. header.equal = intrinsics.type_equal_proc(K);
  127. header.entry_size = size_of(Entry);
  128. header.entry_align = align_of(Entry);
  129. header.key_offset = offset_of(Entry, key);
  130. header.key_size = size_of(K);
  131. header.value_offset = offset_of(Entry, value);
  132. header.value_size = size_of(V);
  133. return header;
  134. }
  135. __slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: Allocator, loc := #caller_location) -> bool {
  136. array := (^Raw_Slice)(array_);
  137. if new_count < array.len {
  138. return true;
  139. }
  140. assert(allocator.procedure != nil);
  141. old_size := array.len*size_of(T);
  142. new_size := new_count*size_of(T);
  143. new_data, err := mem_resize(array.data, old_size, new_size, align_of(T), allocator, loc);
  144. if new_data == nil || err != nil {
  145. return false;
  146. }
  147. array.data = new_data;
  148. array.len = new_count;
  149. return true;
  150. }
  151. __dynamic_map_reserve :: proc(using header: Map_Header, cap: int, loc := #caller_location) {
  152. __dynamic_array_reserve(&m.entries, entry_size, entry_align, cap, loc);
  153. old_len := len(m.hashes);
  154. __slice_resize(&m.hashes, cap, m.entries.allocator, loc);
  155. for i in old_len..<len(m.hashes) {
  156. m.hashes[i] = -1;
  157. }
  158. }
  159. __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #caller_location) #no_bounds_check {
  160. c := context;
  161. if m.entries.allocator.procedure != nil {
  162. c.allocator = m.entries.allocator;
  163. }
  164. context = c;
  165. nm := Raw_Map{};
  166. nm.entries.allocator = m.entries.allocator;
  167. nm.hashes = m.hashes;
  168. new_header: Map_Header = header;
  169. new_header.m = &nm;
  170. new_count := new_count;
  171. new_count = max(new_count, 2*m.entries.len);
  172. __slice_resize(&nm.hashes, new_count, m.entries.allocator, loc);
  173. for i in 0 ..< new_count {
  174. nm.hashes[i] = -1;
  175. }
  176. __dynamic_array_reserve(&nm.entries, entry_size, entry_align, m.entries.len, loc);
  177. for i in 0 ..< m.entries.len {
  178. if len(nm.hashes) == 0 {
  179. __dynamic_map_grow(new_header, loc);
  180. }
  181. entry_header := __dynamic_map_get_entry(header, i);
  182. entry_hash := __get_map_hash_from_entry(header, entry_header);
  183. fr := __dynamic_map_find(new_header, entry_hash);
  184. j := __dynamic_map_add_entry(new_header, entry_hash, loc);
  185. if fr.entry_prev < 0 {
  186. nm.hashes[fr.hash_index] = j;
  187. } else {
  188. e := __dynamic_map_get_entry(new_header, fr.entry_prev);
  189. e.next = j;
  190. }
  191. e := __dynamic_map_get_entry(new_header, j);
  192. __dynamic_map_copy_entry(header, e, entry_header);
  193. e.next = fr.entry_index;
  194. if __dynamic_map_full(new_header) {
  195. __dynamic_map_grow(new_header, loc);
  196. }
  197. }
  198. free(m.entries.data, m.entries.allocator, loc);
  199. header.m^ = nm;
  200. }
  201. __dynamic_map_get :: proc(h: Map_Header, hash: Map_Hash) -> rawptr {
  202. index := __dynamic_map_find(h, hash).entry_index;
  203. if index >= 0 {
  204. data := uintptr(__dynamic_map_get_entry(h, index));
  205. return rawptr(data + h.value_offset);
  206. }
  207. return nil;
  208. }
  209. __dynamic_map_set :: proc(h: Map_Header, hash: Map_Hash, value: rawptr, loc := #caller_location) -> ^Map_Entry_Header #no_bounds_check {
  210. index: int;
  211. assert(value != nil);
  212. if len(h.m.hashes) == 0 {
  213. __dynamic_map_reserve(h, INITIAL_MAP_CAP, loc);
  214. __dynamic_map_grow(h, loc);
  215. }
  216. fr := __dynamic_map_find(h, hash);
  217. if fr.entry_index >= 0 {
  218. index = fr.entry_index;
  219. } else {
  220. index = __dynamic_map_add_entry(h, hash, loc);
  221. if fr.entry_prev >= 0 {
  222. entry := __dynamic_map_get_entry(h, fr.entry_prev);
  223. entry.next = index;
  224. } else {
  225. h.m.hashes[fr.hash_index] = index;
  226. }
  227. }
  228. e := __dynamic_map_get_entry(h, index);
  229. e.hash = hash.hash;
  230. key := rawptr(uintptr(e) + h.key_offset);
  231. mem_copy(key, hash.key_ptr, h.key_size);
  232. val := rawptr(uintptr(e) + h.value_offset);
  233. mem_copy(val, value, h.value_size);
  234. if __dynamic_map_full(h) {
  235. __dynamic_map_grow(h, loc);
  236. index = __dynamic_map_find(h, hash).entry_index;
  237. assert(index >= 0);
  238. }
  239. return __dynamic_map_get_entry(h, index);
  240. }
  241. __dynamic_map_grow :: proc(using h: Map_Header, loc := #caller_location) {
  242. // TODO(bill): Determine an efficient growing rate
  243. new_count := max(4*m.entries.cap + 7, INITIAL_MAP_CAP);
  244. __dynamic_map_rehash(h, new_count, loc);
  245. }
  246. __dynamic_map_full :: #force_inline proc "contextless" (using h: Map_Header) -> bool {
  247. return int(0.75 * f64(len(m.hashes))) <= m.entries.len;
  248. }
  249. __dynamic_map_hash_equal :: proc "contextless" (h: Map_Header, a, b: Map_Hash) -> bool {
  250. if a.hash == b.hash {
  251. return h.equal(a.key_ptr, b.key_ptr);
  252. }
  253. return false;
  254. }
  255. __dynamic_map_find :: proc(using h: Map_Header, hash: Map_Hash) -> Map_Find_Result #no_bounds_check {
  256. fr := Map_Find_Result{-1, -1, -1};
  257. if n := uintptr(len(m.hashes)); n > 0 {
  258. fr.hash_index = int(hash.hash % n);
  259. fr.entry_index = m.hashes[fr.hash_index];
  260. for fr.entry_index >= 0 {
  261. entry := __dynamic_map_get_entry(h, fr.entry_index);
  262. entry_hash := __get_map_hash_from_entry(h, entry);
  263. if __dynamic_map_hash_equal(h, entry_hash, hash) {
  264. return fr;
  265. }
  266. fr.entry_prev = fr.entry_index;
  267. fr.entry_index = entry.next;
  268. }
  269. }
  270. return fr;
  271. }
  272. __dynamic_map_add_entry :: proc(using h: Map_Header, hash: Map_Hash, loc := #caller_location) -> int {
  273. prev := m.entries.len;
  274. c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align, loc);
  275. if c != prev {
  276. end := __dynamic_map_get_entry(h, c-1);
  277. end.hash = hash.hash;
  278. mem_copy(rawptr(uintptr(end) + key_offset), hash.key_ptr, key_size);
  279. end.next = -1;
  280. }
  281. return prev;
  282. }
  283. __dynamic_map_delete_key :: proc(using h: Map_Header, hash: Map_Hash) {
  284. fr := __dynamic_map_find(h, hash);
  285. if fr.entry_index >= 0 {
  286. __dynamic_map_erase(h, fr);
  287. }
  288. }
  289. __dynamic_map_get_entry :: proc(using h: Map_Header, index: int) -> ^Map_Entry_Header {
  290. assert(0 <= index && index < m.entries.len);
  291. return (^Map_Entry_Header)(uintptr(m.entries.data) + uintptr(index*entry_size));
  292. }
  293. __dynamic_map_copy_entry :: proc "contextless" (h: Map_Header, new, old: ^Map_Entry_Header) {
  294. mem_copy(new, old, h.entry_size);
  295. }
  296. __dynamic_map_erase :: proc(using h: Map_Header, fr: Map_Find_Result) #no_bounds_check {
  297. if fr.entry_prev < 0 {
  298. m.hashes[fr.hash_index] = __dynamic_map_get_entry(h, fr.entry_index).next;
  299. } else {
  300. prev := __dynamic_map_get_entry(h, fr.entry_prev);
  301. curr := __dynamic_map_get_entry(h, fr.entry_index);
  302. prev.next = curr.next;
  303. }
  304. if fr.entry_index == m.entries.len-1 {
  305. // NOTE(bill): No need to do anything else, just pop
  306. } else {
  307. old := __dynamic_map_get_entry(h, fr.entry_index);
  308. end := __dynamic_map_get_entry(h, m.entries.len-1);
  309. __dynamic_map_copy_entry(h, old, end);
  310. old_hash := __get_map_hash_from_entry(h, old);
  311. if last := __dynamic_map_find(h, old_hash); last.entry_prev >= 0 {
  312. last_entry := __dynamic_map_get_entry(h, last.entry_prev);
  313. last_entry.next = fr.entry_index;
  314. } else {
  315. m.hashes[last.hash_index] = fr.entry_index;
  316. }
  317. }
  318. m.entries.len -= 1;
  319. }