Lua implementation of ring buffer
Put ringer in your project and call it via require
local Ringer = require "ringer"
local ring_buffer = Ringer({"a", "b", "c", "d"})
for i = 1, 9 do
local data = ring_buffer:next()
io.write(tostring(i) .. " = " .. data, " ")
end
-- print: 1 = a 2 = b 3 = c 4 = d 5 = a 6 = b 7 = c 8 = d 9 = a
Ringer([data])
Create new ringer
object
Arguments:
[optional]
data
(table)
- The data to fill the buffer
Returns:
Ringer
(table)
- Theringer
object
:insert(item)
Add new item to buffer
Arguments:
item
(any)
- The data to fill the buffer
:remove(item)
Remove concrete item from buffer
Arguments:
item
(any)
- The data for deletion
If item does not exist, ringer
raise error
remove(): item not available
:get()
Get buffer head
Returns:
(any)
- Current head object
If buffer is empty, ringer raise error:
get(): buffer is empty!
:next()
Return current head object and move the head to the next element
Returns:
(any)
- Current head object
If buffer is empty, ringer raise error:
next(): buffer is empty!
:is_empty()
Check if buffer is empty
Returns:
(boolean)
- Returntrue
if buffer is empty
:is_exist(item)
checks if the item
is already in the buffer
Arguments:
item
(any)
- The data for deletion
Returns:
(boolean)
- Returntrue
if item is exist in bufer
Also, ringer implements some metamethods:
#ringer
- return lenght of bufferprint(ringer)
- pretty print buffer content like[a, b, c, d]
Tests defined with busted
test farmework. To run the suite, install busted and simply execute busted
in the ringer directory.