comparison classes/State.lua @ 241:09c8e9baa35a

Collect table utility functions
author Flick
date Fri, 25 Mar 2011 16:50:43 -0700
parents 98d7ad4a1158
children b56cff349bd6
comparison
equal deleted inserted replaced
240:98d7ad4a1158 241:09c8e9baa35a
9 local L = ReAction.L 9 local L = ReAction.L
10 local _G = _G 10 local _G = _G
11 local format = string.format 11 local format = string.format
12 local InCombatLockdown = InCombatLockdown 12 local InCombatLockdown = InCombatLockdown
13 local RegisterStateDriver = RegisterStateDriver 13 local RegisterStateDriver = RegisterStateDriver
14 local tfetch = addonTable.tfetch
15 local tbuild = addonTable.tbuild
14 16
15 -- module declaration 17 -- module declaration
16 local moduleID = "State" 18 local moduleID = "State"
17 local module = ReAction:NewModule( moduleID, "AceEvent-3.0" ) 19 local module = ReAction:NewModule( moduleID, "AceEvent-3.0" )
18 20
19 -- Utility -- 21 -- Utility --
20 22
21 -- traverse a table tree by key list and fetch the result or first nil
22 local function tfetch(t, ...)
23 for i = 1, select('#', ...) do
24 t = t and t[select(i, ...)]
25 end
26 return t
27 end
28
29 -- traverse a table tree by key list and build tree as necessary
30 local function tbuild(t, ...)
31 for i = 1, select('#', ...) do
32 local key = select(i, ...)
33 if not t[key] then t[key] = { } end
34 t = t[key]
35 end
36 return t
37 end
38
39 -- return a new array of keys of table 't', sorted by comparing
40 -- sub-fields (obtained via tfetch) of the table values
41 local function fieldsort( t, ... )
42 local r = { }
43 for k in pairs(t) do
44 table.insert(r,k)
45 end
46 local path = { ... }
47 table.sort(r, function(lhs, rhs)
48 local olhs = tfetch(t[lhs], unpack(path)) or 0
49 local orhs = tfetch(t[rhs], unpack(path)) or 0
50 return olhs < orhs
51 end)
52 return r
53 end
54 23
55 24
56 local ApplyStates, CleanupStates, SetProperty, GetProperty 25 local ApplyStates, CleanupStates, SetProperty, GetProperty
57 26
58 -- PRIVATE -- 27 -- PRIVATE --