set.odin 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package container
  2. Set :: struct {
  3. hash: Array(int),
  4. entries: Array(Set_Entry),
  5. }
  6. Set_Entry :: struct {
  7. key: u64,
  8. next: int,
  9. }
  10. /*
  11. set_init :: proc{
  12. set_init_none,
  13. set_init_cap,
  14. }
  15. set_delete
  16. set_in
  17. set_not_in
  18. set_add
  19. set_remove
  20. set_reserve
  21. set_clear
  22. */
  23. set_init :: proc{set_init_none, set_init_cap}
  24. set_init_none :: proc(m: ^Set, allocator := context.allocator) {
  25. m.hash.allocator = allocator
  26. m.entries.allocator = allocator
  27. }
  28. set_init_cap :: proc(m: ^Set, cap: int, allocator := context.allocator) {
  29. m.hash.allocator = allocator
  30. m.entries.allocator = allocator
  31. set_reserve(m, cap)
  32. }
  33. set_delete :: proc(m: Set) {
  34. array_delete(m.hash)
  35. array_delete(m.entries)
  36. }
  37. set_in :: proc(m: Set, key: u64) -> bool {
  38. return _set_find_or_fail(m, key) >= 0
  39. }
  40. set_not_in :: proc(m: Set, key: u64) -> bool {
  41. return _set_find_or_fail(m, key) < 0
  42. }
  43. set_add :: proc(m: ^Set, key: u64) {
  44. if array_len(m.hash) == 0 {
  45. _set_grow(m)
  46. }
  47. _ = _set_find_or_make(m, key)
  48. if _set_full(m^) {
  49. _set_grow(m)
  50. }
  51. }
  52. set_remove :: proc(m: ^Set, key: u64) {
  53. fr := _set_find_key(m^, key)
  54. if fr.entry_index >= 0 {
  55. _set_erase(m, fr)
  56. }
  57. }
  58. set_reserve :: proc(m: ^Set, new_size: int) {
  59. nm: Set
  60. set_init(&nm, m.hash.allocator)
  61. array_resize(&nm.hash, new_size)
  62. array_reserve(&nm.entries, array_len(m.entries))
  63. for i in 0..<new_size {
  64. array_set(&nm.hash, i, -1)
  65. }
  66. for i in 0..<array_len(m.entries) {
  67. e := array_get(m.entries, i)
  68. set_add(&nm, e.key)
  69. }
  70. set_delete(m^)
  71. m^ = nm
  72. }
  73. set_clear :: proc(m: ^Set) {
  74. array_clear(&m.hash)
  75. array_clear(&m.entries)
  76. }
  77. set_equal :: proc(a, b: Set) -> bool {
  78. a_entries := array_slice(a.entries)
  79. b_entries := array_slice(b.entries)
  80. if len(a_entries) != len(b_entries) {
  81. return false
  82. }
  83. for e in a_entries {
  84. if set_not_in(b, e.key) {
  85. return false
  86. }
  87. }
  88. return true
  89. }
  90. /// Internal
  91. _set_add_entry :: proc(m: ^Set, key: u64) -> int {
  92. e: Set_Entry
  93. e.key = key
  94. e.next = -1
  95. idx := array_len(m.entries)
  96. array_push(&m.entries, e)
  97. return idx
  98. }
  99. _set_erase :: proc(m: ^Set, fr: Map_Find_Result) {
  100. if fr.entry_prev < 0 {
  101. array_set(&m.hash, fr.hash_index, array_get(m.entries, fr.entry_index).next)
  102. } else {
  103. array_get_ptr(m.entries, fr.entry_prev).next = array_get(m.entries, fr.entry_index).next
  104. }
  105. if fr.entry_index == array_len(m.entries)-1 {
  106. array_pop_back(&m.entries)
  107. return
  108. }
  109. array_set(&m.entries, fr.entry_index, array_get(m.entries, array_len(m.entries)-1))
  110. last := _set_find_key(m^, array_get(m.entries, fr.entry_index).key)
  111. if last.entry_prev < 0 {
  112. array_get_ptr(m.entries, last.entry_prev).next = fr.entry_index
  113. } else {
  114. array_set(&m.hash, last.hash_index, fr.entry_index)
  115. }
  116. }
  117. _set_find_key :: proc(m: Set, key: u64) -> Map_Find_Result {
  118. fr: Map_Find_Result
  119. fr.hash_index = -1
  120. fr.entry_prev = -1
  121. fr.entry_index = -1
  122. if array_len(m.hash) == 0 {
  123. return fr
  124. }
  125. fr.hash_index = int(key % u64(array_len(m.hash)))
  126. fr.entry_index = array_get(m.hash, fr.hash_index)
  127. for fr.entry_index >= 0 {
  128. it := array_get_ptr(m.entries, fr.entry_index)
  129. if it.key == key {
  130. return fr
  131. }
  132. fr.entry_prev = fr.entry_index
  133. fr.entry_index = it.next
  134. }
  135. return fr
  136. }
  137. _set_find_entry :: proc(m: ^Set, e: ^Set_Entry) -> Map_Find_Result {
  138. fr: Map_Find_Result
  139. fr.hash_index = -1
  140. fr.entry_prev = -1
  141. fr.entry_index = -1
  142. if array_len(m.hash) == 0 {
  143. return fr
  144. }
  145. fr.hash_index = int(e.key % u64(array_len(m.hash)))
  146. fr.entry_index = array_get(m.hash, fr.hash_index)
  147. for fr.entry_index >= 0 {
  148. it := array_get_ptr(m.entries, fr.entry_index)
  149. if it == e {
  150. return fr
  151. }
  152. fr.entry_prev = fr.entry_index
  153. fr.entry_index = it.next
  154. }
  155. return fr
  156. }
  157. _set_find_or_fail :: proc(m: Set, key: u64) -> int {
  158. return _set_find_key(m, key).entry_index
  159. }
  160. _set_find_or_make :: proc(m: ^Set, key: u64) -> int {
  161. fr := _set_find_key(m^, key)
  162. if fr.entry_index >= 0 {
  163. return fr.entry_index
  164. }
  165. i := _set_add_entry(m, key)
  166. if fr.entry_prev < 0 {
  167. array_set(&m.hash, fr.hash_index, i)
  168. } else {
  169. array_get_ptr(m.entries, fr.entry_prev).next = i
  170. }
  171. return i
  172. }
  173. _set_make :: proc(m: ^Set, key: u64) -> int {
  174. fr := _set_find_key(m^, key)
  175. i := _set_add_entry(m, key)
  176. if fr.entry_prev < 0 {
  177. array_set(&m.hash, fr.hash_index, i)
  178. } else {
  179. array_get_ptr(m.entries, fr.entry_prev).next = i
  180. }
  181. array_get_ptr(m.entries, i).next = fr.entry_index
  182. return i
  183. }
  184. _set_full :: proc(m: Set) -> bool {
  185. // TODO(bill): Determine good max load factor
  186. return array_len(m.entries) >= (array_len(m.hash) / 4)*3
  187. }
  188. _set_grow :: proc(m: ^Set) {
  189. new_size := array_len(m.entries) * 4 + 7 // TODO(bill): Determine good grow rate
  190. set_reserve(m, new_size)
  191. }