dynamic_map_internal.odin 12 KB

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