-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_pairs.lua
More file actions
52 lines (44 loc) · 1.26 KB
/
ordered_pairs.lua
File metadata and controls
52 lines (44 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
--[[
Ordered table iterator, allow to iterate on the natural order of the keys of a
table.
Example:
]]
local function __gen_ordered_index( t )
local orderedIndex = {}
for key in pairs(t) do
table.insert( orderedIndex, key )
end
table.sort( orderedIndex )
return orderedIndex
end
local function ordered_next(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("ordered_next: state = "..tostring(state) )
if state == nil then
-- the first time, generate the index
t.__orderedIndex = __gen_ordered_index( 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
local function ordered_pairs(t)
-- Equivalent of the pairs() function on tables. Allows to iterate
-- in order
return ordered_next, t, nil
end
return ordered_pairs