Anonymous

Module:FunList/Iterators: Difference between revisions

From Melvor Idle
no edit summary
No edit summary
No edit summary
Line 63: Line 63:
     self.tbl = tbl
     self.tbl = tbl
     self.state = 0
     self.state = 0
     -- We base the behaviour of moveNext on self.index!
     self.startIndex = self.index
    -- If self.index is set to 0, it's ipairs, otherwise it's pairs
 
    if self.index == 0 then
        self.moveNext = TableEnumerator.pairsMoveNext
    else
        self.moveNext = TableEnumerator.ipairsMoveNext
    end
     return self
     return self
end
end


function TableEnumerator:moveNextPairs()
function TableEnumerator:moveNext()
if self.state == 0 then
if self.state == 0 then
self.state = 1
self.state = 1
end
end
-- Grab the next index for the internal table.
-- if startIndex is 0, it means we are dealing with a ipairs() iteration since the initial index for this
local key = self.index
-- iteration has to start at 0. Otherwise, we are dealing with a pairs() iteration.
key = next(self.tbl, key)
if self.startIndex == 0 then
self.index = key
-- Grab the next index for the internal table.
self.index = self.index + 1
-- If the index exist, we have succesfuly moved to the next.
self.current = self.tbl[self.index]
if key ~= nil then
-- If the index exist, we have succesfuly moved to the next.
self.current = self.tbl[key]
if self.current ~= nil then
return true
return true
end
else
-- Grab the next index for the internal table.
local key = self.index
key = next(self.tbl, key)
self.index = key
if key ~= nil then
self.current = self.tbl[key]
return true
end
end
end
return false
end


function TableEnumerator:moveNextIPairs()
if self.state == 0 then
self.state = 1
end
-- Grab the next index for the internal table.
self.index = self.index + 1
self.current = self.tbl[self.index]
-- If the index exist, we have succesfuly moved to the next.
if self.current ~= nil then
return true
end
return false
return false
end
end
2,873

edits