dynamic_map_internal.odin 12 KB

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