2,875
edits
No edit summary |
No edit summary |
||
Line 37: | Line 37: | ||
-- Hooks the moveNext function into the Lua 'pairs' function | -- Hooks the moveNext function into the Lua 'pairs' function | ||
function Enumerator:overridePairs(startIndex) | function Enumerator:overridePairs(startIndex) | ||
-- Get or create clean enumerator | -- Get or create clean enumerator. This ensures the state is 0. | ||
local enum = self:getEnumerator() | local enum = self:getEnumerator() | ||
enum.current = nil | enum.current = nil | ||
Line 63: | Line 63: | ||
self.tbl = tbl | self.tbl = tbl | ||
self.state = 0 | self.state = 0 | ||
self.startIndex = | self.startIndex = nil | ||
return self | return self | ||
Line 69: | Line 69: | ||
function TableEnumerator:moveNext() | function TableEnumerator:moveNext() | ||
if self.state == 0 then | |||
self.state = 1 | |||
self.startIndex = self.index | |||
end | |||
if self.startIndex == 0 then | |||
-- Iterate using ipairs, starting from index 1 | |||
self.index = self.index + 1 | |||
self.current = self.tbl[self.index] | |||
return self.current ~= nil | |||
else | |||
-- Iterate using pairs | |||
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 | |||
return false | |||
end | end | ||
edits