diff --git a/lua/libk/3rdparty/circular_queue.lua b/lua/libk/3rdparty/circular_queue.lua index 9a63fd4..7db1200 100644 --- a/lua/libk/3rdparty/circular_queue.lua +++ b/lua/libk/3rdparty/circular_queue.lua @@ -10,6 +10,12 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]-- +--[[ +- vugi99 / Voltaism + +This was not working when the list was expanded. I fixed it by adding count inside the table to avoid problems. +]]-- + --[[ = Circular Queue = @@ -30,83 +36,65 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI ]]-- local META = {} - META.__index = META - -function META:Add( entry ) - local index = self.writeIndex - - -- Catched up with readIndex - if ( self.readIndex == index and self[index] != nil ) then - local size = self.capacity - local toCopy = size - index - - -- Copy the remairing data to the end of the queue - for offset = 0, toCopy do - self[size +offset +1] = self[index + offset] - self[index +offset] = nil - end - - self.readIndex = size +1 - self.capacity = self.capacity + toCopy +1 - end - - -- Set - self[index] = entry - - -- Increase (Wrap around) index - index = index +1 - - if ( index > self.capacity ) then - index = 1 - end - - self.writeIndex = index +META.__index = META + +function META:Add(entry) + if self.count == self.capacity then + local oldCapacity = self.capacity + local newCapacity = oldCapacity * 2 + + local newData = {} + for i = 0, self.count - 1 do + local oldIndex = (self.readIndex + i - 1) % oldCapacity + 1 + newData[i + 1] = self[oldIndex] + self[oldIndex] = nil + end + + for i = 1, self.count do + self[i] = newData[i] + end + + self.readIndex = 1 + self.writeIndex = self.count + 1 + self.capacity = newCapacity + end + + self[self.writeIndex] = entry + self.writeIndex = (self.writeIndex % self.capacity) + 1 + self.count = self.count + 1 end - + function META:Peek() - return self[ self.readIndex ] + if self.count == 0 then return nil end + return self[self.readIndex] end - + function META:IsEmpty() - return self.readIndex == self.writeIndex + return self.count == 0 end - + function META:Pop() - if ( self:IsEmpty() ) then return end - - local index = self.readIndex - - -- Pop - local value = self[ index ] - self[ index ] = nil - - -- Increase (Wrap around) index - index = index +1 - - if ( index > self.capacity ) then - index = 1 - end - - self.readIndex = index - - -- Return popped - return value + if self.count == 0 then return nil end + + local val = self[self.readIndex] + self[self.readIndex] = nil + + self.readIndex = (self.readIndex % self.capacity) + 1 + self.count = self.count - 1 + + return val end - -function META:Count() - if ( self.writeIndex < self.readIndex ) then - return self.writeIndex + self.capacity - self.readIndex - end - - return self.writeIndex - self.readIndex + +function META:Count() + return self.count end - -function CircularQueue( size ) - local obj = {} - obj.readIndex = 1 - obj.writeIndex = 1 - - obj.capacity = size or 8 - - return setmetatable( obj, META ) + +function CircularQueue(size) + local obj = { + readIndex = 1, + writeIndex = 1, + capacity = size or 8, + count = 0 + } + return setmetatable(obj, META) end \ No newline at end of file