2,875
edits
No edit summary |
No edit summary |
||
Line 63: | Line 63: | ||
self.tbl = tbl | self.tbl = tbl | ||
self.state = 0 | self.state = 0 | ||
self.startIndex = self.index | |||
return self | return self | ||
end | end | ||
function TableEnumerator: | 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 | ||
-- iteration has to start at 0. Otherwise, we are dealing with a pairs() iteration. | |||
if self.startIndex == 0 then | |||
-- Grab the next index for the internal table. | |||
self.index = self.index + 1 | |||
self.current = self.tbl[self.index] | |||
if key ~= nil then | -- If the index exist, we have succesfuly moved to the next. | ||
if self.current ~= nil then | |||
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 | return false | ||
end | end |
edits