dynamic_map_internal.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package runtime
  2. import "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. source_code_location_hash :: proc(s: Source_Code_Location) -> uintptr {
  119. hash := _fnv64a(transmute([]byte)s.file_path);
  120. hash = hash ~ (u64(s.line) * 0x100000001b3);
  121. hash = hash ~ (u64(s.column) * 0x100000001b3);
  122. return uintptr(hash);
  123. }
  124. __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
  125. header := Map_Header{m = (^Raw_Map)(m)};
  126. Entry :: struct {
  127. hash: uintptr,
  128. next: int,
  129. key: K,
  130. value: V,
  131. };
  132. header.equal = intrinsics.type_equal_proc(K);
  133. header.entry_size = size_of(Entry);
  134. header.entry_align = align_of(Entry);
  135. header.key_offset = offset_of(Entry, key);
  136. header.key_size = size_of(K);
  137. header.value_offset = offset_of(Entry, value);
  138. header.value_size = size_of(V);
  139. return header;
  140. }
  141. __slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: Allocator, loc := #caller_location) -> bool {
  142. array := (^Raw_Slice)(array_);
  143. if new_count < array.len {
  144. return true;
  145. }
  146. assert(allocator.procedure != nil);
  147. old_size := array.len*size_of(T);
  148. new_size := new_count*size_of(T);
  149. new_data := mem_resize(array.data, old_size, new_size, align_of(T), allocator, loc);
  150. if new_data == nil {
  151. return false;
  152. }
  153. array.data = new_data;
  154. array.len = new_count;
  155. return true;
  156. }
  157. __dynamic_map_reserve :: proc(using header: Map_Header, cap: int, loc := #caller_location) {
  158. __dynamic_array_reserve(&m.entries, entry_size, entry_align, cap, loc);
  159. old_len := len(m.hashes);
  160. __slice_resize(&m.hashes, cap, m.entries.allocator, loc);
  161. for i in old_len..<len(m.hashes) {
  162. m.hashes[i] = -1;
  163. }
  164. }
  165. __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #caller_location) #no_bounds_check {
  166. new_header: Map_Header = header;
  167. nm := Raw_Map{};
  168. nm.entries.allocator = m.entries.allocator;
  169. new_header.m = &nm;
  170. c := context;
  171. if m.entries.allocator.procedure != nil {
  172. c.allocator = m.entries.allocator;
  173. }
  174. context = c;
  175. new_count := new_count;
  176. new_count = max(new_count, 2*m.entries.len);
  177. __dynamic_array_reserve(&nm.entries, entry_size, entry_align, m.entries.len, loc);
  178. __slice_resize(&nm.hashes, new_count, m.entries.allocator, loc);
  179. for i in 0 ..< new_count {
  180. nm.hashes[i] = -1;
  181. }
  182. for i in 0 ..< m.entries.len {
  183. if len(nm.hashes) == 0 {
  184. __dynamic_map_grow(new_header, loc);
  185. }
  186. entry_header := __dynamic_map_get_entry(header, i);
  187. entry_hash := __get_map_hash_from_entry(header, entry_header);
  188. fr := __dynamic_map_find(new_header, entry_hash);
  189. j := __dynamic_map_add_entry(new_header, entry_hash, loc);
  190. if fr.entry_prev < 0 {
  191. nm.hashes[fr.hash_index] = j;
  192. } else {
  193. e := __dynamic_map_get_entry(new_header, fr.entry_prev);
  194. e.next = j;
  195. }
  196. e := __dynamic_map_get_entry(new_header, j);
  197. __dynamic_map_copy_entry(header, e, entry_header);
  198. e.next = fr.entry_index;
  199. if __dynamic_map_full(new_header) {
  200. __dynamic_map_grow(new_header, loc);
  201. }
  202. }
  203. delete(m.hashes, m.entries.allocator, loc);
  204. free(m.entries.data, m.entries.allocator, loc);
  205. header.m^ = nm;
  206. }
  207. __dynamic_map_get :: proc(h: Map_Header, hash: Map_Hash) -> rawptr {
  208. index := __dynamic_map_find(h, hash).entry_index;
  209. if index >= 0 {
  210. data := uintptr(__dynamic_map_get_entry(h, index));
  211. return rawptr(data + h.value_offset);
  212. }
  213. return nil;
  214. }
  215. __dynamic_map_set :: proc(h: Map_Header, hash: Map_Hash, value: rawptr, loc := #caller_location) #no_bounds_check {
  216. index: int;
  217. assert(value != nil);
  218. if len(h.m.hashes) == 0 {
  219. __dynamic_map_reserve(h, INITIAL_MAP_CAP, loc);
  220. __dynamic_map_grow(h, loc);
  221. }
  222. fr := __dynamic_map_find(h, hash);
  223. if fr.entry_index >= 0 {
  224. index = fr.entry_index;
  225. } else {
  226. index = __dynamic_map_add_entry(h, hash, loc);
  227. if fr.entry_prev >= 0 {
  228. entry := __dynamic_map_get_entry(h, fr.entry_prev);
  229. entry.next = index;
  230. } else {
  231. h.m.hashes[fr.hash_index] = index;
  232. }
  233. }
  234. {
  235. e := __dynamic_map_get_entry(h, index);
  236. e.hash = hash.hash;
  237. key := rawptr(uintptr(e) + h.key_offset);
  238. mem_copy(key, hash.key_ptr, h.key_size);
  239. val := rawptr(uintptr(e) + h.value_offset);
  240. mem_copy(val, value, h.value_size);
  241. }
  242. if __dynamic_map_full(h) {
  243. __dynamic_map_grow(h, loc);
  244. }
  245. }
  246. __dynamic_map_grow :: proc(using h: Map_Header, loc := #caller_location) {
  247. // TODO(bill): Determine an efficient growing rate
  248. new_count := max(4*m.entries.cap + 7, INITIAL_MAP_CAP);
  249. __dynamic_map_rehash(h, new_count, loc);
  250. }
  251. __dynamic_map_full :: #force_inline proc "contextless" (using h: Map_Header) -> bool {
  252. return int(0.75 * f64(len(m.hashes))) <= m.entries.cap;
  253. }
  254. __dynamic_map_hash_equal :: proc "contextless" (h: Map_Header, a, b: Map_Hash) -> bool {
  255. if a.hash == b.hash {
  256. return h.equal(a.key_ptr, b.key_ptr);
  257. }
  258. return false;
  259. }
  260. __dynamic_map_find :: proc(using h: Map_Header, hash: Map_Hash) -> Map_Find_Result #no_bounds_check {
  261. fr := Map_Find_Result{-1, -1, -1};
  262. if n := uintptr(len(m.hashes)); n > 0 {
  263. fr.hash_index = int(hash.hash % n);
  264. fr.entry_index = m.hashes[fr.hash_index];
  265. for fr.entry_index >= 0 {
  266. entry := __dynamic_map_get_entry(h, fr.entry_index);
  267. entry_hash := __get_map_hash_from_entry(h, entry);
  268. if __dynamic_map_hash_equal(h, entry_hash, hash) {
  269. return fr;
  270. }
  271. fr.entry_prev = fr.entry_index;
  272. fr.entry_index = entry.next;
  273. }
  274. }
  275. return fr;
  276. }
  277. __dynamic_map_add_entry :: proc(using h: Map_Header, hash: Map_Hash, loc := #caller_location) -> int {
  278. prev := m.entries.len;
  279. c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align, loc);
  280. if c != prev {
  281. end := __dynamic_map_get_entry(h, c-1);
  282. end.hash = hash.hash;
  283. mem_copy(rawptr(uintptr(end) + key_offset), hash.key_ptr, key_size);
  284. end.next = -1;
  285. }
  286. return prev;
  287. }
  288. __dynamic_map_delete_key :: proc(using h: Map_Header, hash: Map_Hash) {
  289. fr := __dynamic_map_find(h, hash);
  290. if fr.entry_index >= 0 {
  291. __dynamic_map_erase(h, fr);
  292. }
  293. }
  294. __dynamic_map_get_entry :: proc(using h: Map_Header, index: int) -> ^Map_Entry_Header {
  295. assert(0 <= index && index < m.entries.len);
  296. return (^Map_Entry_Header)(uintptr(m.entries.data) + uintptr(index*entry_size));
  297. }
  298. __dynamic_map_copy_entry :: proc "contextless" (h: Map_Header, new, old: ^Map_Entry_Header) {
  299. mem_copy(new, old, h.entry_size);
  300. }
  301. __dynamic_map_erase :: proc(using h: Map_Header, fr: Map_Find_Result) #no_bounds_check {
  302. if fr.entry_prev < 0 {
  303. m.hashes[fr.hash_index] = __dynamic_map_get_entry(h, fr.entry_index).next;
  304. } else {
  305. prev := __dynamic_map_get_entry(h, fr.entry_prev);
  306. curr := __dynamic_map_get_entry(h, fr.entry_index);
  307. prev.next = curr.next;
  308. }
  309. if (fr.entry_index == m.entries.len-1) {
  310. // NOTE(bill): No need to do anything else, just pop
  311. } else {
  312. old := __dynamic_map_get_entry(h, fr.entry_index);
  313. end := __dynamic_map_get_entry(h, m.entries.len-1);
  314. __dynamic_map_copy_entry(h, old, end);
  315. old_hash := __get_map_hash_from_entry(h, old);
  316. if last := __dynamic_map_find(h, old_hash); last.entry_prev >= 0 {
  317. last_entry := __dynamic_map_get_entry(h, last.entry_prev);
  318. last_entry.next = fr.entry_index;
  319. } else {
  320. m.hashes[last.hash_index] = fr.entry_index;
  321. }
  322. }
  323. m.entries.len -= 1;
  324. }