|
| 1 | +local M = {} |
| 2 | + |
| 3 | +--- @class commons.RingBuffer |
| 4 | +--- @field pos integer |
| 5 | +--- @field queue any[] |
| 6 | +--- @field size integer |
| 7 | +--- @field maxsize integer |
| 8 | +local RingBuffer = {} |
| 9 | + |
| 10 | +--- @param maxsize integer? |
| 11 | +--- @return commons.RingBuffer |
| 12 | +function RingBuffer:new(maxsize) |
| 13 | + assert(type(maxsize) == "number" and maxsize > 0) |
| 14 | + local o = { |
| 15 | + pos = 0, |
| 16 | + queue = {}, |
| 17 | + size = 0, |
| 18 | + maxsize = maxsize, |
| 19 | + } |
| 20 | + setmetatable(o, self) |
| 21 | + self.__index = self |
| 22 | + return o |
| 23 | +end |
| 24 | + |
| 25 | +--- @param idx integer |
| 26 | +--- @return integer |
| 27 | +function RingBuffer:_inc(idx) |
| 28 | + if idx == self.maxsize then |
| 29 | + return 1 |
| 30 | + else |
| 31 | + return idx + 1 |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +--- @param idx integer |
| 36 | +--- @return integer |
| 37 | +function RingBuffer:_dec(idx) |
| 38 | + if idx == 1 then |
| 39 | + return self.maxsize |
| 40 | + else |
| 41 | + return idx - 1 |
| 42 | + end |
| 43 | +end |
| 44 | + |
| 45 | +--- @param item any |
| 46 | +--- @return integer |
| 47 | +function RingBuffer:push(item) |
| 48 | + assert(self.size >= 0 and self.size <= self.maxsize) |
| 49 | + |
| 50 | + if self.size < self.maxsize then |
| 51 | + table.insert(self.queue, item) |
| 52 | + self.pos = self:_inc(self.pos) |
| 53 | + self.size = self.size + 1 |
| 54 | + else |
| 55 | + self.pos = self:_inc(self.pos) |
| 56 | + self.queue[self.pos] = item |
| 57 | + end |
| 58 | + return self.pos |
| 59 | +end |
| 60 | + |
| 61 | +--- @return any? |
| 62 | +function RingBuffer:pop() |
| 63 | + if self.size <= 0 then |
| 64 | + return nil |
| 65 | + end |
| 66 | + |
| 67 | + local old = self.queue[self.pos] |
| 68 | + self.queue[self.pos] = nil |
| 69 | + self.size = self.size - 1 |
| 70 | + self.pos = self:_dec(self.pos) |
| 71 | + return old |
| 72 | +end |
| 73 | + |
| 74 | +--- @return any? |
| 75 | +function RingBuffer:peek() |
| 76 | + if self.size <= 0 then |
| 77 | + return nil |
| 78 | + end |
| 79 | + return self.queue[self.pos] |
| 80 | +end |
| 81 | + |
| 82 | +--- @return integer |
| 83 | +function RingBuffer:clear() |
| 84 | + local old = self.size |
| 85 | + self.pos = 0 |
| 86 | + self.queue = {} |
| 87 | + self.size = 0 |
| 88 | + return old |
| 89 | +end |
| 90 | + |
| 91 | +-- RingBufferIterator { |
| 92 | + |
| 93 | +-- usage: |
| 94 | +-- |
| 95 | +-- ```lua |
| 96 | +-- local it = ringbuf:iterator() |
| 97 | +-- local item = nil |
| 98 | +-- repeat |
| 99 | +-- item = it:next() |
| 100 | +-- if item then |
| 101 | +-- -- consume item data |
| 102 | +-- end |
| 103 | +-- until item |
| 104 | +-- ``` |
| 105 | +-- |
| 106 | +--- @class commons._RingBufferIterator |
| 107 | +--- @field ringbuf commons.RingBuffer |
| 108 | +--- @field index integer |
| 109 | +--- @field initial_index integer |
| 110 | +local _RingBufferIterator = {} |
| 111 | + |
| 112 | +--- @param ringbuf commons.RingBuffer |
| 113 | +--- @param index integer |
| 114 | +--- @return commons._RingBufferIterator |
| 115 | +function _RingBufferIterator:new(ringbuf, index) |
| 116 | + assert(type(ringbuf) == "table") |
| 117 | + |
| 118 | + local o = { |
| 119 | + ringbuf = ringbuf, |
| 120 | + index = index, |
| 121 | + initial_index = index, |
| 122 | + } |
| 123 | + setmetatable(o, self) |
| 124 | + self.__index = self |
| 125 | + return o |
| 126 | +end |
| 127 | + |
| 128 | +--- @return boolean |
| 129 | +function _RingBufferIterator:has_next() |
| 130 | + if self.ringbuf.size == 0 then |
| 131 | + return false |
| 132 | + end |
| 133 | + if self.index <= 0 or self.index > self.ringbuf.size then |
| 134 | + return false |
| 135 | + end |
| 136 | + if self.index ~= self.initial_index and self.ringbuf:_inc(self.index) == self.initial_index then |
| 137 | + return false |
| 138 | + end |
| 139 | + |
| 140 | + return true |
| 141 | +end |
| 142 | + |
| 143 | +--- @return any? |
| 144 | +function _RingBufferIterator:next() |
| 145 | + assert(self:has_next()) |
| 146 | + assert(self.index >= 1 and self.index <= self.ringbuf.maxsize) |
| 147 | + local item = self.ringbuf.queue[self.index] |
| 148 | + self.index = self.ringbuf:_inc(self.index) |
| 149 | + return item |
| 150 | +end |
| 151 | + |
| 152 | +-- RingBufferIterator } |
| 153 | + |
| 154 | +-- RingBufferRIterator { |
| 155 | + |
| 156 | +--- @class commons._RingBufferRIterator |
| 157 | +--- @field ringbuf commons.RingBuffer |
| 158 | +--- @field index integer |
| 159 | +--- @field initial_index integer |
| 160 | +local _RingBufferRIterator = {} |
| 161 | + |
| 162 | +--- @param ringbuf commons.RingBuffer |
| 163 | +--- @param index integer |
| 164 | +--- @return commons._RingBufferRIterator |
| 165 | +function _RingBufferRIterator:new(ringbuf, index) |
| 166 | + assert(type(ringbuf) == "table") |
| 167 | + |
| 168 | + local o = { |
| 169 | + ringbuf = ringbuf, |
| 170 | + index = index, |
| 171 | + initial_index = index, |
| 172 | + } |
| 173 | + setmetatable(o, self) |
| 174 | + self.__index = self |
| 175 | + return o |
| 176 | +end |
| 177 | + |
| 178 | +--- @return boolean |
| 179 | +function _RingBufferRIterator:has_next() |
| 180 | + if self.ringbuf.size == 0 then |
| 181 | + return false |
| 182 | + end |
| 183 | + if self.index <= 0 or self.index > self.ringbuf.size then |
| 184 | + return false |
| 185 | + end |
| 186 | + if self.index ~= self.initial_index and self.ringbuf:_dec(self.index) == self.initial_index then |
| 187 | + return false |
| 188 | + end |
| 189 | + |
| 190 | + return true |
| 191 | +end |
| 192 | + |
| 193 | +--- @return any? |
| 194 | +function _RingBufferRIterator:next() |
| 195 | + assert(self:has_next()) |
| 196 | + assert(self.index >= 1 and self.index <= self.ringbuf.maxsize) |
| 197 | + |
| 198 | + local item = self.ringbuf.queue[self.index] |
| 199 | + self.index = self.ringbuf:_dec(self.index) |
| 200 | + return item |
| 201 | +end |
| 202 | + |
| 203 | +-- RingBufferRIterator } |
| 204 | + |
| 205 | +--- @return commons._RingBufferIterator |
| 206 | +function RingBuffer:iterator() |
| 207 | + if self.size < self.maxsize then |
| 208 | + return _RingBufferIterator:new(self, 0) |
| 209 | + else |
| 210 | + return _RingBufferIterator:new(self, self:_inc(self.pos)) |
| 211 | + end |
| 212 | +end |
| 213 | + |
| 214 | +--- @return commons._RingBufferRIterator |
| 215 | +function RingBuffer:riterator() |
| 216 | + return _RingBufferRIterator:new(self, self.pos) |
| 217 | +end |
| 218 | + |
| 219 | +M.RingBuffer = RingBuffer |
| 220 | + |
| 221 | +return M |
0 commit comments