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.159 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.158 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.157 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.156 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.155 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.154 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.153 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.152 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.151 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.150 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.149 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.148 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.147 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.146 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.145 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.144 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.143 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.142 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.141 tracks/lua/exercises/circular-buffer/example.lua
trackler-2.2.1.140 tracks/lua/exercises/circular-buffer/example.lua