dynamic_map_internal.odin 13 KB

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