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

Version Path
trackler-2.0.4.0 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.9 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.8 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.7 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.6 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.5 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.4 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.3 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.2 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.1 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.3.0 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.2.0 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.1.2 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.1.1 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.1.0 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.0.10 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.0.9 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.0.8 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.0.7 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.0.0.6 tracks/lua/exercises/circular-buffer/example.lua