Herly Quijano
Newcomer
- Joined
- Mar 19, 2021
- Messages
- 77
- Reaction score
- 10
As well as exists the function "next" to get the next value in a table, is there an equivalent to the previous value if I set the index?
[email protected] t table
[email protected] i integer if is nil this will return the last value
[email protected] any,any
function prev(t,i)
if t then
if not i then return #t,t[#t] end
local k1,v1
for k,v in pairs(t) do
if v==i then return k1,v1 end
k1,v1=k,v
end
end
return nil
end
local Next={[0]=0}
local Prev={[0]=0}
function InsertToList(node,replace)
Next[node]=replace
Prev[node]=Prev[replace]
Next[Prev[node]]=node
Prev[replace]=node
end
function AddToList(node)
InsertToList(node,0)
end
function RemoveFromList(node)
Next[Prev[node]]=Next[node]
Prev[Next[node]]=Prev[node]
end
function Iteration()
local node=Next[0]
while node~=0 do
--Do stuff
node=Next[node]
end
end