-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays.lua
308 lines (270 loc) · 6.64 KB
/
arrays.lua
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
local M = {}
local function pred_default(a)
if a then
return true
end
return false
end
local function _to_norm_range(arr, start, stop)
if not start then
start = 1
elseif start < 0 then
start = #arr + start + 1
end
start = math.max(1, start)
if not stop then
stop = #arr
elseif stop < 0 then
stop = #arr + stop + 1
end
stop = math.min(#arr, stop)
return start, stop
end
local function _del_from(arr, f, start, stop)
assert(start <= stop)
local base = start
for i = stop + 1, #arr do
local e = arr[i]
if f then
f(e)
end
arr[i] = nil
arr[base] = e
base = base + 1
end
for i = base, #arr do
arr[i] = nil
end
end
local function _do_from(items, f, start, stop)
if (start <= 0 and stop <= 0) or (start > #items and stop > #items) then
return
end
if start <= stop then
start = math.max(1, start)
else
start = math.min(#items, start)
end
local inc = start <= stop and 1 or -1
for i = start, stop, inc do
local e = items[i]
if e == nil then
break
end
f(items[i])
end
end
local function _del_if(items, f, pred, invert)
local base = 1
-- int-based iteration is important! its not safe in lua
-- to mutate while iterating using pairs() or ipairs()
for i = 1, #items do
local item = items[i]
items[i] = nil
local is_match = pred(item)
if (not invert and is_match) or (invert and not is_match) then
if f then
f(item)
end
else
items[base] = item
base = base + 1
end
end
end
local function _do_if(items, f, pred, invert)
for _, item in ipairs(items) do
local is_match = pred(item)
if (not invert and is_match) or (invert and not is_match) then
f(item)
end
end
end
local function _cloned(o, deep)
deep = deep or false
local o2 = o
if type(o) == "table" then
o2 = {}
for k, v in pairs(o) do
if deep then
o2[k] = _cloned(v, deep)
else
o2[k] = v
end
end
end
local meta = getmetatable(o)
if deep then
meta = _cloned(meta, deep)
end
setmetatable(o2, meta)
return o2
end
function M.is_array(o)
if type(o) ~= "table" then
return false
end
local max_idx = 0
for i, _ in pairs(o) do
max_idx = i
end
return max_idx == #o
end
---@param items any[] array to read from
---@param start? integer start index
---@param stop? integer stop index (inclusive)
---@return any[] got results within range
---@nodiscard
function M.get_from(items, start, stop)
start, stop = _to_norm_range(items, start, stop)
local results = {}
_do_from(items, function(e)
table.insert(results, e)
end, start, stop)
return results
end
---@param items any[] items to extract from
---@param pred? fun(any): boolean true if should extract
---@param invert? boolean switch behavior of predicate
---@return any[] items all items satisfying the predicate
---@nodiscard
function M.get_if(items, pred, invert)
pred = pred or pred_default
invert = invert or false
local results = {}
_do_if(items, function(e)
table.insert(results, e)
end, pred, invert)
return results
end
---@param items any[] items to extract from
---@param start? integer start index
---@param stop? integer stop index (inclusive)
---@return any[] extracted items extracted within range
function M.remove_from(items, start, stop)
start, stop = _to_norm_range(items, start, stop)
local results = {}
_del_from(items, function(e)
table.insert(results, e)
end, start, stop)
return results
end
---@param items any[] items to extract from
---@param pred? fun(any): boolean true if should extract
---@param invert? boolean switch behavior of predicate
---@return any[] items all items satisfying the predicate
function M.remove_if(items, pred, invert)
pred = pred or pred_default
invert = invert or false
local results = {}
_del_if(items, function(e)
table.insert(results, e)
end, pred, invert)
return results
end
---@param items any[] array to clone
---@param start? integer start index
---@param stop? integer stop index (inclusive)
---@param deep? boolean do deep clone
---@return any[] clone cloned items
---@nodiscard
function M.clone(items, start, stop, deep)
start, stop = _to_norm_range(items, start, stop)
local results = {}
_do_from(items, function(e)
if deep then
e = _cloned(e, deep)
end
table.insert(results, e)
end, start, stop)
return results
end
---@param items any[] array to make unique
---@param key? fun(any): any how to compare for equivalence
function M.uniqify(items, key)
local seen = {}
local base = 1
-- int-based iteration is important! its not safe in lua to
-- mutate while iterating using pairs() or ipairs()
for i = 1, #items do
local e = items[i]
items[i] = nil
local k = e
if key then
k = key(e)
end
if not seen[k] then
seen[k] = true
items[base] = e
base = base + 1
end
end
for i = base + 1, #items do
items[i] = nil
end
end
---@param items any[] array to extend
---@param more any[] items to append
function M.extend(items, more)
for _, e in ipairs(more) do
table.insert(items, e)
end
end
---@param items any[] list to iterate over
---@param f fun(any) function to apply
---@param start? integer index to start at
---@param stop? integer index to stop at (inclusive)
function M.apply(items, f, start, stop)
start, stop = _to_norm_range(items, start, stop)
_do_from(items, f, start, stop)
end
---@param items any[] items to filter
---@param pred? fun(any): boolean true if should keep
---@param invert? boolean switch behavior of predicate
function M.filter(items, pred, invert)
invert = invert or false
M.remove_if(items, pred, not invert)
end
---@param items? any[] items to transform
---@param f? fun(a: any): any transformation to apply
function M.transform(items, f)
if items == nil or f == nil then
return
end
local idx = 1
for i = 1, #items do
local value = f(items[i])
if value ~= nil then
items[idx] = value
idx = idx + 1
end
end
end
---@param items any[] items to remove from
---@param value any value to remove
---@param invert? boolean true to reverse behavior
function M.remove(items, value, invert)
_del_if(items, nil, function(o)
return o == value
end, invert)
end
---@param items any[] items to reorder as an array
function M.canonicalize(items)
local keys = {}
for k, _ in pairs(items) do
table.insert(keys, k)
end
table.sort(keys)
local old = {}
local base = 1
for _, key in ipairs(keys) do
old[base] = items[base]
if old[key] ~= nil then
items[base] = old[key]
else
items[base] = items[key]
end
base = base + 1
end
end
return M