Mercurial > wow > reaction
comparison ReAction.lua @ 241:09c8e9baa35a
Collect table utility functions
| author | Flick |
|---|---|
| date | Fri, 25 Mar 2011 16:50:43 -0700 |
| parents | 704f4a05a1d7 |
| children | b56cff349bd6 |
comparison
equal
deleted
inserted
replaced
| 240:98d7ad4a1158 | 241:09c8e9baa35a |
|---|---|
| 9 LoadAddOn("LibKeyBound-1.0") | 9 LoadAddOn("LibKeyBound-1.0") |
| 10 LKB = LibStub("LibKeyBound-1.0") | 10 LKB = LibStub("LibKeyBound-1.0") |
| 11 end | 11 end |
| 12 | 12 |
| 13 ------ Utility ------ | 13 ------ Utility ------ |
| 14 local tcopy | 14 -- make a deep copy of a table |
| 15 do | 15 local function tcopy(x) |
| 16 function tcopy(x) | 16 if type(x) ~= "table" then |
| 17 if type(x) ~= "table" then | 17 return x |
| 18 return x | 18 end |
| 19 end | 19 local r = {} |
| 20 local r = {} | 20 for k,v in pairs(x) do |
| 21 for k,v in pairs(x) do | 21 r[k] = tcopy(v) |
| 22 r[k] = tcopy(v) | 22 end |
| 23 end | 23 return r |
| 24 return r | 24 end |
| 25 end | 25 |
| 26 end | 26 -- traverse a table tree by key list and fetch the result or first nil |
| 27 local function tfetch(t, ...) | |
| 28 for i = 1, select('#', ...) do | |
| 29 t = t and t[select(i, ...)] | |
| 30 end | |
| 31 return t | |
| 32 end | |
| 33 | |
| 34 -- traverse a table tree by key list and build tree as necessary | |
| 35 local function tbuild(t, ...) | |
| 36 for i = 1, select('#', ...) do | |
| 37 local key = select(i, ...) | |
| 38 if not t[key] then t[key] = { } end | |
| 39 t = t[key] | |
| 40 end | |
| 41 return t | |
| 42 end | |
| 43 | |
| 44 -- return a new array of keys of table 't', sorted by comparing | |
| 45 -- sub-fields (obtained via tfetch) of the table values | |
| 46 local function fieldsort( t, ... ) | |
| 47 local r = { } | |
| 48 for k in pairs(t) do | |
| 49 table.insert(r,k) | |
| 50 end | |
| 51 local path = { ... } | |
| 52 table.sort(r, function(lhs, rhs) | |
| 53 local olhs = tfetch(t[lhs], unpack(path)) or 0 | |
| 54 local orhs = tfetch(t[rhs], unpack(path)) or 0 | |
| 55 return olhs < orhs | |
| 56 end) | |
| 57 return r | |
| 58 end | |
| 59 | |
| 60 -- store in the addon table | |
| 61 addonTable.tcopy = tcopy | |
| 62 addonTable.tfetch = tfetch | |
| 63 addonTable.tbuild = tbuild | |
| 64 addonTable.fieldsort = fieldsort | |
| 27 | 65 |
| 28 ------ Core ------ | 66 ------ Core ------ |
| 29 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction", | 67 local ReAction = LibStub("AceAddon-3.0"):NewAddon( "ReAction", |
| 30 "AceEvent-3.0" | 68 "AceEvent-3.0" |
| 31 ) | 69 ) |
