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.2.1.180 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.179 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.178 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.177 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.176 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.175 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.174 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.173 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.172 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.171 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.170 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.169 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.167 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.166 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.165 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.164 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.163 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.162 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.161 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.160 tracks/lua/exercises/circular-buffer/example.lua