dynamic_map_internal.odin 13 KB

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