queue.odin 6.1 KB

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