-
-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve ordering of generated Lua code
This fixes #1645, and (partially) reverts 68ac28.
- Loading branch information
1 parent
23a1ad3
commit 59da849
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
-- borrowed from the Lua users wiki, at http://lua-users.org/wiki/SortedIteration | ||
|
||
function __genOrderedIndex( t ) | ||
local orderedIndex = {} | ||
for key in pairs(t) do | ||
table.insert( orderedIndex, key ) | ||
end | ||
table.sort( orderedIndex ) | ||
return orderedIndex | ||
end | ||
|
||
function orderedNext(t, state) | ||
-- Equivalent of the next function, but returns the keys in the alphabetic | ||
-- order. We use a temporary ordered key table that is stored in the | ||
-- table being iterated. | ||
|
||
local key = nil | ||
--print("orderedNext: state = "..tostring(state) ) | ||
if state == nil then | ||
-- the first time, generate the index | ||
t.__orderedIndex = __genOrderedIndex( t ) | ||
key = t.__orderedIndex[1] | ||
else | ||
-- fetch the next value | ||
for i = 1,table.getn(t.__orderedIndex) do | ||
if t.__orderedIndex[i] == state then | ||
key = t.__orderedIndex[i+1] | ||
end | ||
end | ||
end | ||
|
||
if key then | ||
return key, t[key] | ||
end | ||
|
||
-- no more value to return, cleanup | ||
t.__orderedIndex = nil | ||
return | ||
end | ||
|
||
function orderedPairs(t) | ||
-- Equivalent of the pairs() function on tables. Allows to iterate | ||
-- in order | ||
return orderedNext, t, nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters