- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtable.lua
More file actions
Latest commit
405 lines (364 loc) · 8.82 KB
/
Copy pathtable.lua
File metadata and controls
405 lines (364 loc) · 8.82 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
localtable= {}
fork,vinpairs(require'table') dotable[k] =vend
table.__index=table
functiontable.new(...)
returnsetmetatable({}, table):union(...)
end
setmetatable(table, {
__call=function(t, ...)
returntable.new(...)
end
})
-- 5.2 or 5.3 compatible
table.unpack=table.unpackorunpack
-- [[ how about table.unpack(t) defaults to table.unpack(t, 1, t.n) if t.n is present?
-- for cohesion with table.pack?
-- already table.unpack's default is #t, but this doesn't account for nils
-- this might break compatability somewhere ...
localorigTableUnpack=table.unpack
functiontable.unpack(...)
localnargs=select('#', ...)
localt, i, j=...
ifnargs<3andt.n~=nilthen
returnorigTableUnpack(t, ior1, t.n)
end
returnorigTableUnpack(...)
end
--]]
-- 5.1 compatible
ifnottable.packthen
functiontable.pack(...)
localt= {...}
t.n=select('#', ...)
returnsetmetatable(t, table)
end
else
localoldpack=table.pack
functiontable.pack(...)
returnsetmetatable(oldpack(...), table)
end
end
-- non-5.1 compat:
ifnottable.maxnthen
functiontable.maxn(t)
localmax=0
fork,vinpairs(t) do
iftype(k) =='number' then
max=math.max(max, k)
end
end
returnmax
end
end
-- applies to the 'self' table
-- same behavior as new
functiontable:union(...)
fori=1,select('#', ...) do
localo=select(i, ...)
ifothen
fork,vinpairs(o) do
self[k] =v
end
end
end
returnself
end
-- something to consider:
-- mapvalue() returns a new table
-- but append() modifies the current table
-- for consistency shouldn't append() create a new one as well?
functiontable:append(...)
fori=1,select('#', ...) do
localu=select(i, ...)
ifuthen
for_,vinipairs(u) do
table.insert(self, v)
end
end
end
returnself
end
functiontable:removeKeys(...)
fori=1,select('#', ...) do
localv=select(i, ...)
self[v] =nil
end
end
-- cb(value, key, newtable) returns newvalue[, newkey]
-- nil newkey means use the old key
functiontable:map(cb)
localt=table()
fork,vinpairs(self) do
localnv, nk=cb(v,k,t)
ifnk==nilthennk=kend
t[nk] =nv
end
returnt
end
-- cb(value, key, newtable) returns newvalue[, newkey]
-- nil newkey means use the old key
functiontable:mapi(cb)
localt=table()
fork=1,#selfdo
localv=self[k]
localnv, nk=cb(v,k,t)
ifnk==nilthennk=kend
t[nk] =nv
end
returnt
end
-- this excludes keys that don't pass the callback function
-- if the key is an ineteger then it is table.remove'd
-- currently the handling of integer keys is the only difference between this
-- and calling table.map and returning nil kills on filtered items
functiontable:filter(f)
localt=table()
fork,vinpairs(self) do
iff(v,k) then
-- TODO now that i made filteri, should this only ever always directly map keys
-- even if the key is an integer?
-- or should it still insert integer keys?
iftype(k) =='string' then
t[k] =v
else
t:insert(v)
end
end
end
returnt
end
-- like filter but only works on ipairs entries
functiontable:filteri(f)
localt=table()
fork,vinipairs(self) do
iff(v,k) then
t:insert(v)
end
end
returnt
end
functiontable:keys()
localt=table()
fork,_inpairs(self) do
t:insert(k)
end
returnt
end
functiontable:values()
localt=table()
for_,vinpairs(self) do
t:insert(v)
end
returnt
end
-- should we also return value, key to match map, sup, and inf?
-- that seems redundant if it's find-by-value ...
functiontable:find(value, eq)
ifeqthen
fork,vinpairs(self) do
ifeq(v, value) thenreturnk, vend
end
else
fork,vinpairs(self) do
ifv==valuethenreturnk, vend
end
end
end
functiontable:findi(value, eq)
ifeqthen
fork,vinipairs(self) do
ifeq(v, value) thenreturnk, vend
end
else
fork,vinipairs(self) do
ifv==valuethenreturnk, vend
end
end
end
-- should insertUnique only operate on the pairs() ?
-- especially when insert() itself is an ipairs() operation
functiontable:insertUnique(value, eq)
ifnottable.find(self, value, eq) thentable.insert(self, value) end
end
functiontable:removeObject(...)
localremovedKeys=table()
locallen=#self
localk=table.find(self, ...)
whilek~=nildo
iftype(k) =='number' andtonumber(k) <=lenthen
table.remove(self, k)
else
self[k] =nil
end
removedKeys:insert(k)
k=table.find(self, ...)
end
returntable.unpack(removedKeys)
end
functiontable:kvpairs()
localt=table()
fork,vinpairs(self) do
table.insert(t, {[k]=v})
end
returnt
end
-- TODO - math instead of table?
-- TODO - have cmp default to operator> just like inf and sort?
functiontable:sup(cmp)
localbestk, bestv
ifcmpthen
fork,vinpairs(self) do
ifbestv==nilorcmp(v, bestv) thenbestk, bestv=k, vend
end
else
fork,vinpairs(self) do
ifbestv==nilorv>bestvthenbestk, bestv=k, vend
end
end
returnbestv, bestk
end
-- TODO - math instead of table?
functiontable:inf(cmp)
localbestk, bestv
ifcmpthen
fork,vinpairs(self) do
ifbestv==nilorcmp(v, bestv) thenbestk, bestv=k, vend
end
else
fork,vinpairs(self) do
ifbestv==nilorv<bestvthenbestk, bestv=k, vend
end
end
returnbestv, bestk
end
-- combine elements of
functiontable:combine(callback)
locals
for_,vinpairs(self) do
ifs==nilthen
s=v
else
s=callback(s, v)
end
end
returns
end
localop=require'ext.op'
functiontable:sum()
returntable.combine(self, op.add)
end
functiontable:product()
returntable.combine(self, op.mul)
end
functiontable:last()
returnself[#self]
end
-- just like string subset
functiontable.sub(t,i,j)
ifi<0theni=math.max(1, #t+i+1) end
--if i < 0 then i = math.max(1, #t + i + 1) else i = math.max(1, i) end -- TODO this is affecting symmath edge cases somewhere ...
j=jor#t
j=math.min(j, #t)
ifj<0thenj=math.min(#t, #t+j+1) end
--if j < 0 then j = math.min(#t, #t + j + 1) else j = math.max(1, j) end -- TODO this is affecting symmath edge cases somewhere ...
localres= {}
fork=i,jdo
res[k-i+1] =t[k]
end
setmetatable(res, table)
returnres
end
functiontable.reverse(t)
localr=table()
fori=#t,1,-1do
r:insert(t[i])
end
returnr
end
functiontable.rep(t,n)
localc=table()
fori=1,ndo
c:append(t)
end
returnc
end
-- in-place sort is fine, but it returns nothing. for kicks I'd like to chain methods
localoldsort=require'table'.sort
functiontable:sort(...)
oldsort(self, ...)
returnself
end
-- returns a shuffled duplicate of the ipairs in table 't'
functiontable.shuffle(t)
t=table(t)
-- https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
fori=#t,2,-1do
localj=math.random(i-1)
t[i], t[j] =t[j], t[i]
end
returnt
end
functiontable.pickRandom(t)
returnt[math.random(#t)]
end
-- keys = options, values = probs
functiontable.pickWeighted(t)
localtotal=table.values(t):sum()
localx=math.random() *total
fork,vinpairs(t) do
x=x-v
ifx<=0then
returnk
end
end
-- this should never be reached unless you have some not-a-number's as values
end
-- where to put this ...
-- I want to convert iterators into tables
-- it looks like a coroutine but it is made for functions returned from coroutine.wrap
-- also, what to do with multiple-value iterators (like ipairs)
-- do I only wrap the first value?
-- do I wrap both values in a double table?
-- do I do it optionally based on the # args returned?
-- how about I ask for a function to convert the iterator to the table?
-- this is looking very similar to table.map
-- I'll just wrap it with table.wrap and then let the caller use :mapi to transform the results
-- usage: table.wrapfor(ipairs(t))
-- if you want to wrap a 'for=' loop then just use range(a,b[,c])
-- ok at this point I should just start using lua-fun ...
functiontable.wrapfor(f, s, var)
localt=table()
whiletruedo
localvars=table.pack(f(s, var))
localvar_1=vars[1]
ifvar_1==nilthenbreakend
var=var_1
t:insert(vars)
end
returnt
end
-- https://www.lua.org/pil/9.3.html
localfunctionpermgen(t, n)
ifn<1then
coroutine.yield(t)
else
fori=n,1,-1do
-- put i-th element as the last one
t[n], t[i] =t[i], t[n]
-- generate all permutations of the other elements
permgen(t, n-1)
-- restore i-th element
t[n], t[i] =t[i], t[n]
end
end
end
-- return iterator of permutations of the table
functiontable.permutations(t)
returncoroutine.wrap(function()
permgen(t, table.maxn(t))
end)
end
-- I won't add table.getmetatable because, as a member method, that will always return 'table'
-- if you use this as a member method then know that you can't use it a second time (unless the metatable you set it to has a __index that has 'setmetatable' defined)
table.setmetatable=setmetatable
returntable