1. 4FipS.com
  2. Photos
  3. Videos
  4. Code
  5. Forums
  6. pfQuizzz
  7. About

4FipS.com Forums

Forums dedicated to the projects hosted at 4FipS.com

Skip to content

A simple circular buffer with reverse iterator (Lua)

> About coding, hacking, photography, and such...

A simple circular buffer with reverse iterator (Lua)

Postby FipS on Wed Nov 23, 2016 10:22 pm

This is just a small code snippet showing a simple circular buffer with reverse iterator that I've coded up for my forthcoming IoT NodeMcu-based project. Its purpose is to provide fixed and efficient storage for data coming from sensors, so in my case it basically represents history of N most recent values with additional convenience of the reverse iterator for easy FIFO access. I haven't coded in Lua for some time, so it was quite refreshing to come up with this. I especially like how iterators can make use of closures for tracking state, which makes the implementation pretty simple end easy to read. What I don't like about Lua that much is the infamous 1-based indexing, which feels unnatural to me, especially when dealing with modular arithmetic for index wrapping.

Code: Select all
--[CODE BY FIPS @ 4FIPS.COM, (c) 2016 FILIP STOKLAS, MIT-LICENSED]

History = {}

function History.new(max_size)
   local hist = { __index = History }
   setmetatable(hist, hist)
   hist.max_size = max_size
   hist.size = 0
   hist.cursor = 1
   return hist
end

function History:push(value)
  if self.size < self.max_size then
    table.insert(self, value)
    self.size = self.size + 1
  else
    self[self.cursor] = value
    self.cursor = self.cursor % self.max_size + 1
  end
end

function History:iterator()
  local i = 0
  return function()
    i = i + 1
    if i <= self.size then
      return self[(self.cursor - i - 1) % self.size + 1]
    end
  end
end

hist = History.new(5)
hist:push(10)
hist:push(20)
hist:push(30)
hist:push(40)
hist:push(50)
hist:push(60)
hist:push(70)

print("Raw (wrapped) values:")
for i, v in ipairs(hist) do
  print(v)
end

print("")

print("History values:")
for v in hist:iterator() do
  print(v)
end

--[[
output:
Raw (wrapped) values:
60
70
30
40
50

History values:
70
60
50
40
30
--]]
User avatar
FipS
Site Admin
 
Posts: 146
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague

A simple circular buffer with reverse iterator (Lua)

Sponsor

Sponsor
 

Return to 4FipS.com General Discussion & Blog

Who is online

Users browsing this forum: No registered users and 2 guests