-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTable.lua
More file actions
327 lines (268 loc) · 7.21 KB
/
Table.lua
File metadata and controls
327 lines (268 loc) · 7.21 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
-- Various Table Classes
-- Table
do
Table = { }
-- Private key to track the number of entries in the table
local key_n = {}
-- Metatable to make tables inherit from Table
local metatable = {
__index = Table,
}
-- Create a new table
function Table.New(self)
local ret = {
[key_n] = 0,
}
setmetatable(ret,metatable)
return ret
end
-- Get table size
function Table.GetN(t)
-- See if the table has an "n" element
if t[key_n] == nil then
-- If it doesn't, count until we find nil
local i = 0
while t[i+1] ~= nil do i=i+1 end
t[key_n] = i
end
return t[key_n]
end
Table.Size = Table.GetN
-- Set the table size
function Table.SetN(t,size)
-- Get the table size
local len = Table.GetN(t)
-- Check if we're reducing the size
if size < len then
-- Remove all elements after size
for i=len,size+1,-1 do
t[i]=nil
end
end
-- Set n
t[key_n]=size
return t
end
-- Add an element to a table
function Table.Insert(t,pos,item)
-- Check if we were given 3 arguments
if item == nil then
-- Nope, only 2, 2nd one is the item
item = pos
pos = nil
end
-- Get the number of items we're adding
local num = 1
if type(item) == "table" and item[key_n] ~= nil then
num = Table.GetN(item)
end
-- Save the number of items in t
local tEnd = Table.GetN(t)
-- Increase number of items
t[key_n] = Table.GetN(t) + num
-- Check if we have a position
if pos == nil then
-- Set pos to the end
pos = tEnd + 1
end
-- Shift everything forward
local i
for i=t[key_n],pos+num,-1 do
t[i]=t[i-num]
end
-- Insert the item(s)
if type(item) == "table" and item[key_n] ~= nil then
-- Loop through the table to be merged
for i=1,Table.GetN(item) do
-- Merge them in
t[pos+i-1]=item[i]
end
else
t[pos]=item
end
return t
end
-- Add an element to the end of a table
function Table.Append(t,item)
-- Just the same as insert without specifying position
return Table.Insert(t,item,nil)
end
-- Remove an element from a table
function Table.Remove(t,pos,num)
-- Check if we have num
if num == nil or num < 0 then
num = 1
elseif num == 0 then
return
end
-- Check if the item exists
if Table.GetN(t) < pos then
return
end
-- Shift everything forward
for i=pos,t[key_n]-num do
t[i]=t[i+num]
t[i+num]=nil
end
-- Remove the previous elements
t[key_n]=t[key_n]-num
return t
end
--[[
-- Remove elements from the table, based on a function evaluating their values
function Table.sieveValues(t,f,reversed)
-- Make sure we have a sieving function
if type(f) ~= "function" then
return t
end
-- Make sure reversed is a boolean
if type(reversed) ~= "boolean" then
reversed = false
end
-- Loop through each element in the table
local i,max = 1,Table.getn(t)
while i <= max do
-- Check if the element should be ignored
local ret = f(t[i])
if type(ret) ~= "boolean" then
ret = false
end
if ret ~= reversed then
-- Element should be removed
Table.remove(t,i)
-- Reduce the max
max = Table.getn(t)
else
-- Element should stay
i = i + 1
end
end
return t
end
--]]
-- Shallow copy a table (just create a new one)
function Table.ShallowCopy(t,into,overwrite)
local copy = into or {}
-- Loop through all elements of the table
for k,v in t do
if copy[k] == nil or overwrite then
copy[k] = v
end
end
-- Set the same metatable
local mt = getmetatable(t)
if type(mt) == "table" then
setmetatable(copy,mt)
end
return copy
end
-- Deep copy a table (subtables are also copied to new ones)
do
local function do_deep_copy(v,saved)
if not saved[v] then
local t = type(v)
if t == "table" then
-- Deep copy the table
saved[v] = Table.DeepCopy(v,nil,nil,saved)
else
-- Everything else
saved[v] = v
end
end
return saved[v]
end
function Table.DeepCopy(t,copy,overwrite,saved)
-- Create a saved table
saved = saved or setmetatable({},{__mode="k",})
-- Check if this table has already been copied
if saved[t] then
return saved[t]
end
-- Create a copy table
local copy = copy or {}
-- Add this table to the saved list
saved[t] = copy
-- Loop through all elements of the table
for k,v in pairs(t) do
local new_k = do_deep_copy(k,saved)
if copy[new_k] == nil or overwrite then
copy[new_k] = do_deep_copy(v,saved)
end
end
-- Check for a metatable
local mt = getmetatable(t)
if type(mt) == "table" then
local deep_mt = getmetatable(copy)
setmetatable(copy,Table.DeepCopy(mt,deep_mt,overwrite,saved))
end
-- Return the copy
return copy
end
end
end
-- List
do
-- List based on code from http://www.lua.org/pil/11.4.html
List = {}
local metatable = {
__index = List
}
function List.New()
local ret = { first = 0, last = -1 }
setmetatable(ret,metatable)
return ret
end
function List.PushLeft (list, value)
list.first = list.first - 1
list[list.first] = value;
end
function List.PushRight (list, value)
list.last = list.last + 1
list[list.last] = value
end
function List.PopLeft (list)
if list.first > list.last then
return nil
end
local value = list[list.first]
list[list.first] = nil -- to allow garbage collection
list.first = list.first + 1
return value
end
function List.PopRight (list)
if list.first > list.last then
return nil
end
local value = list[list.last]
list[list.last] = nil
list.last = list.last - 1
return value
end
function List.Clear (list)
for i=list.first,list.last do
list[i] = nil
end
list.first = 0
list.last = -1
end
function List.Size (list)
return list.last - list.first + 1
end
end
-- Function to selectively return values from pairs based on index name
function FindPairs(t,pattern,init,plain)
-- Get the results from pairs
local iter,t,first = pairs(t)
-- Return a custom function with the table and first item from pairs()
return function(t,idx)
-- Loop through the values from iter()
local value
repeat
-- Get the next index and skill from pairs()'s iterator
idx,value = iter(t,idx)
-- Loop until no pairs left or an index matches the pattern
until idx == nil or string.find(tostring(idx),pattern,init,plain) ~= nil
-- Return the next index and value
return idx,value
end,t,first
end