Sha256: cb88dbd2298e7af157db67458cb61223b4ec9ff6108665f7ad5baea513eee514
Contents?: true
Size: 870 Bytes
Versions: 396
Compression:
Stored size: 870 Bytes
Contents
local CircularBuffer = {} CircularBuffer.__index = CircularBuffer function CircularBuffer:read() if self.head == self.tail then error('buffer is empty') end self.tail = self.tail + 1 return self.items[self.tail - 1] end function CircularBuffer:write(item) if item == nil then return end if (self.head - self.tail) == self.capacity then error('buffer is full') end table.insert(self.items, self.head, item) self.head = self.head + 1 end function CircularBuffer:forceWrite(item) if item == nil then return end if (self.head - self.tail) == self.capacity then self.tail = self.tail + 1 end self:write(item) end function CircularBuffer:clear() self.items = {} self.head = 1 self.tail = 1 end return { new = function(_, capacity) local self = setmetatable({ capacity = capacity }, CircularBuffer) self:clear() return self end }
Version data entries
396 entries across 396 versions & 1 rubygems