-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFUtils_Tables.lua
More file actions
177 lines (151 loc) · 4.69 KB
/
SFUtils_Tables.lua
File metadata and controls
177 lines (151 loc) · 4.69 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
-- LibSFUtils is already defined in prior loaded file
LibSFUtils = LibSFUtils or {}
local sfutil = LibSFUtils
-- debug convenience function
function sfutil.dTable(vtable, depth, name)
if type(vtable) ~= "table" then
return sfutil.str(vtable)
end
local function appendVal(tbl, val)
tbl[#tbl+1] = val
return tbl
end
local arg = {}
local vt
if depth == nil or depth < 1 then return table.concat(arg) end
for k, v in pairs(vtable) do
vt = type(v)
if vt == "function" then
appendVal(arg, sfutil.str(name, " : ", k, " -> (function), \n"))
elseif vt == "table" then
appendVal(arg, (sfutil.dTable(v, depth - 1, name.." - ["..tostring(k).."]")))
else
appendVal(arg, sfutil.str(name, " : ", k, " -> ", v, ", \n"))
end
end
return table.concat(arg)
end
--[[ ---------------------
Recursively initialize missing values in a table from
a defaults table. Existing values in the svtable will
remain unchanged.
--]]
function sfutil.defaultMissing(svtable, defaulttable)
if svtable == nil then return sfutil.deepCopy(defaulttable) end
if type(svtable) ~= 'table' or type(defaulttable) ~= 'table' then return sfutil.safeTable(svtable) end
for k,v in pairs(defaulttable) do
if svtable[k] == nil then
if type( defaulttable[k] )=='table' then
svtable[k] = {}
sfutil.defaultMissing( svtable[k], defaulttable[k])
else
svtable[k] = defaulttable[k]
end
end
end
return svtable
end
--[[ ---------------------
Recursively copy contents of a table into a new table
for table/object it returns a copy of the table/object
for anything else, it returns the value of the orig.
The resulting table also gets a reference to the metatable
from the orig table.
The seen argument is typically nii when called from
outside, and a table of already seen tables when
called internally (recursively). This is so that
an orig table that contains multiple copies of another
table will copy the first instance of that table to the result
table and after that set all other instance of that table to
reference the first full copy.
--]]
function sfutil.deepCopy(orig, seen)
seen = sfutil.safeTable(seen)
if type(orig) ~= 'table' then return orig end
if seen[orig] then
return seen[orig]
end
local tcopy = {}
seen[orig] = tcopy
local dcpy = sfutil.deepCopy -- alias
for orig_key, orig_value in pairs(orig) do
if orig_key then
tcopy[dcpy(orig_key, seen)] = dcpy(orig_value, seen)
end
end
local mt = getmetatable(orig)
if mt then
setmetatable(tcopy, mt)
end
return tcopy
end
--[[ ---------------------
given a (supposed) table variable
either return the table variable (reference)
or return a new empty table if the table variable was nil
--]]
function sfutil.safeTable(tbl)
if tbl == nil or type(tbl) ~= "table" then
return {}
end
return tbl
end
--[[ ---------------------
given a (supposed) table variable
either return the same table variable after discarding the contents
or return an empty table if the table variable was nil
The difference between this and ZO_ClearTable is the initial safety check
and that we return the empty table (which might have been created if
the parameter was not a proper table).
--]]
function sfutil.safeClearTable(tbl)
if tbl == nil or type(tbl) ~= "table" then
return {}
end
for k,v in pairs(tbl) do
tbl[k] = nil
end
return tbl
end
--[[ ---------------------
Return only listA (all) items that are not in listB (known)
Note: Compares listA values to listB keys, returns listA values as keys in remains with values of 1. Kinda wierd.
--]]
function sfutil.RemainsInList(listA, listB)
local newList = {}
if listA == nil then return newList end
for _, v in pairs(listA) do
if not listB[v] then
newList[v] = 1
end
end
return newList
end
--[[ ---------------------
Return a count of the number of items in the table.
Return nil if the supposed "table" is not a table
Handles non-contiguous. More safe version of ZOS's NonContiguousCount()
--]]
function sfutil.GetSize(tbl)
if tbl == nil or type(tbl) ~= "table" then
return 0
end
local count = 0
for _ in pairs(tbl) do
count = count + 1
end
return count
end
--[[ ---------------------
Return true or false if the table is empty or not.
Return nil if the supposed "table" is not a table
(Adds the type() check above what ZO_IsTableEmpty() does.
Also, I regard a nil "tbl" to be not an "empty" table as it
is not a table at all. ZOS seems to disagree.)
--]]
function sfutil.isEmpty(tbl)
if tbl == nil or type(tbl) ~= "table" then
return nil
end
return next(tbl) == nil
end