π Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io π

# glennj's solution

## to Circular Buffer in the Lua Track

Published at Mar 12 2019 · 0 comments
Instructions
Test suite
Solution

A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.

A circular buffer first starts empty and of some predefined length. For example, this is a 7-element buffer:

``````[ ][ ][ ][ ][ ][ ][ ]
``````

Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):

``````[ ][ ][ ][1][ ][ ][ ]
``````

Then assume that two more elements are added β 2 & 3 β which get appended after the 1:

``````[ ][ ][ ][1][2][3][ ]
``````

If two elements are then removed from the buffer, the oldest values inside the buffer are removed. The two elements removed, in this case, are 1 & 2, leaving the buffer with just a 3:

``````[ ][ ][ ][ ][ ][3][ ]
``````

If the buffer has 7 elements then it is completely full:

``````[6][7][8][9][3][4][5]
``````

When the buffer is full an error will be raised, alerting the client that further writes are blocked until a slot becomes free.

When the buffer is full, the client can opt to overwrite the oldest data with a forced write. In this case, two more elements β A & B β are added and they overwrite the 3 & 4:

``````[6][7][8][9][A][B][5]
``````

3 & 4 have been replaced by A & B making 5 now the oldest data in the buffer. Finally, if two elements are removed then what would be returned is 5 & 6 yielding the buffer:

``````[ ][7][8][9][A][B][ ]
``````

Because there is space available, if the client again uses overwrite to store C & D then the space where 5 & 6 were stored previously will be used not the location of 7 & 8. 7 is still the oldest element and the buffer is once again full.

``````[D][7][8][9][A][B][C]
``````

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

## Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### circular-buffer_spec.lua

``````local CircularBuffer = require('circular-buffer')

describe('circular-buffer', function()
it('reading an empty buffer throws a BufferEmptyException', function()
local buffer = CircularBuffer:new(1)
assert.has_error((function() buffer:read() end), 'buffer is empty')
end)

it('write and read back one item', function()
local buffer = CircularBuffer:new(1)
buffer:write('1')
assert.has_error((function() buffer:read() end), 'buffer is empty')
end)

it('write and read back multiple items', function()
local buffer = CircularBuffer:new(2)
buffer:write('1')
buffer:write('2')
assert.has_error((function() buffer:read() end), 'buffer is empty')
end)

it('clearing a buffer', function()
local buffer = CircularBuffer:new(2)
buffer:write('1')
buffer:write('2')
buffer:clear()
assert.has_error((function() buffer:read() end), 'buffer is empty')
buffer:write('3')
buffer:write('4')
end)

local buffer = CircularBuffer:new(2)
buffer:write('1')
buffer:write('2')
end)

local buffer = CircularBuffer:new(3)
buffer:write('1')
buffer:write('2')
buffer:write('3')
end)

it('writes of nil do not occupy buffer', function()
local buffer = CircularBuffer:new(3)
buffer:write(nil)
for i = 1, 3 do
buffer:write(tostring(i))
end
end)

it('writing to a full buffer throws a BufferFullException', function()
local buffer = CircularBuffer:new(2)
buffer:write('1')
buffer:write('2')
assert.has_error((function() buffer:write('A') end), 'buffer is full')
end)

it('forced writes over write oldest item in a full buffer', function()
local buffer = CircularBuffer:new(2)
buffer:write('1')
buffer:write('2')
buffer:forceWrite('A')
assert.has_error((function() buffer:read() end), 'buffer is empty')
end)

it('forced writes act like normal writes in a non-full buffer', function()
local buffer = CircularBuffer:new(2)
buffer:write('1')
buffer:forceWrite('2')
assert.has_error((function() buffer:read() end), 'buffer is empty')
end)

it('alternate force write and read into full buffer', function()
local buffer = CircularBuffer:new(5)
for i = 1, 3 do
buffer:write(tostring(i))
end
buffer:write('4')
for i = 5, 8 do
buffer:write(tostring(i))
end
buffer:forceWrite('A')
buffer:forceWrite('B')
assert.has_error((function() buffer:read() end), 'buffer is empty')
end)
end)``````
``````local CircularBuffer = {}
CircularBuffer.__index = CircularBuffer

--[[
This can easily be implemented with an array, where
the read pointer is fixed to the head of the array and
the write pointer is fixed to the tail of the array.
But let's implement a proper circular tape with a read
pointer and write pointer that move along the tape.

I explicitly check a value is or isn't nil to allow
`false` as valid data.
---]]

function CircularBuffer:new(size)
local buf = { size = size }
setmetatable(buf, self)
buf:clear()
return buf
end

function CircularBuffer:clear()
self.data = {}
self.pointer = {read = 1, write = 1}
return self
end

function CircularBuffer:is_empty()
end

function CircularBuffer:is_full()
and not self:is_empty()
end

assert(not self:is_empty(), 'buffer is empty')
return val
end

function CircularBuffer:write(data)
-- It is unspecified what to do when we write nil to a
-- full buffer: ignore or error. I choose ignore.
if data ~= nil then
assert(not self:is_full(), 'buffer is full')
self.data[self.pointer.write] = data
self:incr_pointer('write')
end
return self
end

function CircularBuffer:forceWrite(data)
-- again unspecified: I do nothing for force write nil
-- to a full buffer
if data ~= nil then
if self:is_full() then
end
self:write(data)
end
return self
end

function CircularBuffer:incr_pointer(ptype)
assert(ptype == 'read' or ptype == 'write')
self.pointer[ptype] = (self.pointer[ptype] % self.size) + 1
end

return CircularBuffer``````