queue.odin 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package container_queue
  2. import "base:builtin"
  3. import "base:runtime"
  4. _ :: runtime
  5. // Dynamically resizable double-ended queue/ring-buffer
  6. Queue :: struct($T: typeid) {
  7. data: [dynamic]T,
  8. len: uint,
  9. offset: uint,
  10. }
  11. DEFAULT_CAPACITY :: 16
  12. // Procedure to initialize a queue
  13. init :: proc(q: ^$Q/Queue($T), capacity := DEFAULT_CAPACITY, allocator := context.allocator) -> runtime.Allocator_Error {
  14. if q.data.allocator.procedure == nil {
  15. q.data.allocator = allocator
  16. }
  17. clear(q)
  18. return reserve(q, capacity)
  19. }
  20. // Procedure to initialize a queue from a fixed backing slice.
  21. // The contents of the `backing` will be overwritten as items are pushed onto the `Queue`.
  22. // Any previous contents are not available.
  23. init_from_slice :: proc(q: ^$Q/Queue($T), backing: []T) -> bool {
  24. clear(q)
  25. q.data = transmute([dynamic]T)runtime.Raw_Dynamic_Array{
  26. data = raw_data(backing),
  27. len = builtin.len(backing),
  28. cap = builtin.len(backing),
  29. allocator = {procedure=runtime.nil_allocator_proc, data=nil},
  30. }
  31. return true
  32. }
  33. // Procedure to initialize a queue from a fixed backing slice.
  34. // Existing contents are preserved and available on the queue.
  35. init_with_contents :: proc(q: ^$Q/Queue($T), backing: []T) -> bool {
  36. clear(q)
  37. q.data = transmute([dynamic]T)runtime.Raw_Dynamic_Array{
  38. data = raw_data(backing),
  39. len = builtin.len(backing),
  40. cap = builtin.len(backing),
  41. allocator = {procedure=runtime.nil_allocator_proc, data=nil},
  42. }
  43. q.len = builtin.len(backing)
  44. return true
  45. }
  46. // Procedure to destroy a queue
  47. destroy :: proc(q: ^$Q/Queue($T)) {
  48. delete(q.data)
  49. }
  50. // The length of the queue
  51. len :: proc(q: $Q/Queue($T)) -> int {
  52. return int(q.len)
  53. }
  54. // The current capacity of the queue
  55. cap :: proc(q: $Q/Queue($T)) -> int {
  56. return builtin.len(q.data)
  57. }
  58. // Remaining space in the queue (cap-len)
  59. space :: proc(q: $Q/Queue($T)) -> int {
  60. return builtin.len(q.data) - int(q.len)
  61. }
  62. // Reserve enough space for at least the specified capacity
  63. reserve :: proc(q: ^$Q/Queue($T), capacity: int) -> runtime.Allocator_Error {
  64. if capacity > space(q^) {
  65. return _grow(q, uint(capacity))
  66. }
  67. return nil
  68. }
  69. get :: proc(q: ^$Q/Queue($T), #any_int i: int, loc := #caller_location) -> T {
  70. runtime.bounds_check_error_loc(loc, i, builtin.len(q.data))
  71. idx := (uint(i)+q.offset)%builtin.len(q.data)
  72. return q.data[idx]
  73. }
  74. front :: proc(q: ^$Q/Queue($T)) -> T {
  75. return q.data[q.offset]
  76. }
  77. front_ptr :: proc(q: ^$Q/Queue($T)) -> ^T {
  78. return &q.data[q.offset]
  79. }
  80. back :: proc(q: ^$Q/Queue($T)) -> T {
  81. idx := (q.offset+uint(q.len - 1))%builtin.len(q.data)
  82. return q.data[idx]
  83. }
  84. back_ptr :: proc(q: ^$Q/Queue($T)) -> ^T {
  85. idx := (q.offset+uint(q.len - 1))%builtin.len(q.data)
  86. return &q.data[idx]
  87. }
  88. set :: proc(q: ^$Q/Queue($T), #any_int i: int, val: T, loc := #caller_location) {
  89. runtime.bounds_check_error_loc(loc, i, builtin.len(q.data))
  90. idx := (uint(i)+q.offset)%builtin.len(q.data)
  91. q.data[idx] = val
  92. }
  93. get_ptr :: proc(q: ^$Q/Queue($T), #any_int i: int, loc := #caller_location) -> ^T {
  94. runtime.bounds_check_error_loc(loc, i, builtin.len(q.data))
  95. idx := (uint(i)+q.offset)%builtin.len(q.data)
  96. return &q.data[idx]
  97. }
  98. peek_front :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> ^T {
  99. runtime.bounds_check_error_loc(loc, 0, builtin.len(q.data))
  100. idx := q.offset%builtin.len(q.data)
  101. return &q.data[idx]
  102. }
  103. peek_back :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> ^T {
  104. runtime.bounds_check_error_loc(loc, int(q.len - 1), builtin.len(q.data))
  105. idx := (uint(q.len - 1)+q.offset)%builtin.len(q.data)
  106. return &q.data[idx]
  107. }
  108. // Push an element to the back of the queue
  109. push_back :: proc(q: ^$Q/Queue($T), elem: T) -> (ok: bool, err: runtime.Allocator_Error) {
  110. if space(q^) == 0 {
  111. _grow(q) or_return
  112. }
  113. idx := (q.offset+uint(q.len))%builtin.len(q.data)
  114. q.data[idx] = elem
  115. q.len += 1
  116. return true, nil
  117. }
  118. // Push an element to the front of the queue
  119. push_front :: proc(q: ^$Q/Queue($T), elem: T) -> (ok: bool, err: runtime.Allocator_Error) {
  120. if space(q^) == 0 {
  121. _grow(q) or_return
  122. }
  123. q.offset = uint(q.offset - 1 + builtin.len(q.data)) % builtin.len(q.data)
  124. q.len += 1
  125. q.data[q.offset] = elem
  126. return true, nil
  127. }
  128. // Pop an element from the back of the queue
  129. pop_back :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> (elem: T) {
  130. assert(condition=q.len > 0, loc=loc)
  131. q.len -= 1
  132. idx := (q.offset+uint(q.len))%builtin.len(q.data)
  133. elem = q.data[idx]
  134. return
  135. }
  136. // Safely pop an element from the back of the queue
  137. pop_back_safe :: proc(q: ^$Q/Queue($T)) -> (elem: T, ok: bool) {
  138. if q.len > 0 {
  139. q.len -= 1
  140. idx := (q.offset+uint(q.len))%builtin.len(q.data)
  141. elem = q.data[idx]
  142. ok = true
  143. }
  144. return
  145. }
  146. // Pop an element from the front of the queue
  147. pop_front :: proc(q: ^$Q/Queue($T), loc := #caller_location) -> (elem: T) {
  148. assert(condition=q.len > 0, loc=loc)
  149. elem = q.data[q.offset]
  150. q.offset = (q.offset+1)%builtin.len(q.data)
  151. q.len -= 1
  152. return
  153. }
  154. // Safely pop an element from the front of the queue
  155. pop_front_safe :: proc(q: ^$Q/Queue($T)) -> (elem: T, ok: bool) {
  156. if q.len > 0 {
  157. elem = q.data[q.offset]
  158. q.offset = (q.offset+1)%builtin.len(q.data)
  159. q.len -= 1
  160. ok = true
  161. }
  162. return
  163. }
  164. // Push multiple elements to the back of the queue
  165. push_back_elems :: proc(q: ^$Q/Queue($T), elems: ..T) -> (ok: bool, err: runtime.Allocator_Error) {
  166. n := uint(builtin.len(elems))
  167. if space(q^) < int(n) {
  168. _grow(q, q.len + n) or_return
  169. }
  170. sz := uint(builtin.len(q.data))
  171. insert_from := (q.offset + q.len) % sz
  172. insert_to := n
  173. if insert_from + insert_to > sz {
  174. insert_to = sz - insert_from
  175. }
  176. copy(q.data[insert_from:], elems[:insert_to])
  177. copy(q.data[:insert_from], elems[insert_to:])
  178. q.len += n
  179. return true, nil
  180. }
  181. // Consume `n` elements from the front of the queue
  182. consume_front :: proc(q: ^$Q/Queue($T), n: int, loc := #caller_location) {
  183. assert(condition=int(q.len) >= n, loc=loc)
  184. if n > 0 {
  185. nu := uint(n)
  186. q.offset = (q.offset + nu) % builtin.len(q.data)
  187. q.len -= nu
  188. }
  189. }
  190. // Consume `n` elements from the back of the queue
  191. consume_back :: proc(q: ^$Q/Queue($T), n: int, loc := #caller_location) {
  192. assert(condition=int(q.len) >= n, loc=loc)
  193. if n > 0 {
  194. q.len -= uint(n)
  195. }
  196. }
  197. append_elem :: push_back
  198. append_elems :: push_back_elems
  199. push :: proc{push_back, push_back_elems}
  200. append :: proc{push_back, push_back_elems}
  201. // Clear the contents of the queue
  202. clear :: proc(q: ^$Q/Queue($T)) {
  203. q.len = 0
  204. q.offset = 0
  205. }
  206. // Internal growing procedure
  207. _grow :: proc(q: ^$Q/Queue($T), min_capacity: uint = 0) -> runtime.Allocator_Error {
  208. new_capacity := max(min_capacity, uint(8), uint(builtin.len(q.data))*2)
  209. n := uint(builtin.len(q.data))
  210. builtin.resize(&q.data, int(new_capacity)) or_return
  211. if q.offset + q.len > n {
  212. diff := n - q.offset
  213. copy(q.data[new_capacity-diff:], q.data[q.offset:][:diff])
  214. q.offset += new_capacity - n
  215. }
  216. return nil
  217. }