Skip to content

Instantly share code, notes, and snippets.

@JlnWntr
Created August 16, 2018 10:22
Show Gist options
  • Save JlnWntr/2cf0c15073121234b93a88b8517c3e2a to your computer and use it in GitHub Desktop.
Save JlnWntr/2cf0c15073121234b93a88b8517c3e2a to your computer and use it in GitHub Desktop.
Most simple ringbuffer/queue in Lua (5.3.4)
MAX_SIZE = 4
Ring = {}
function insert(ring, a)
table.insert(ring, a)
if #ring > MAX_SIZE then
table.remove(ring, 1)
end
end
------------------------TEST--------------------------------------------
insert(Ring, 1)
insert(Ring, 2)
insert(Ring, 3)
insert(Ring, 4)
insert(Ring, 5)
for k,v in pairs(Ring) do
print(v)
end
insert(Ring, 6)
insert(Ring, 7)
insert(Ring, 8)
print()
for k,v in pairs(Ring) do
print(v)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment