forked from ryunp/ahk-array
-
Notifications
You must be signed in to change notification settings - Fork 4
/
export.ahk
509 lines (374 loc) · 9.5 KB
/
export.ahk
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
; Apply "native" support, redefines Array() to add custom _Array base object
Array(prms*) {
; Since prms is already an array of the parameters, just give it a
; new base object and return it. Using this method, _Array.__New()
; is not called and any instance variables are not initialized.
prms.base := _Array
return prms
}
; Define the base class.
class _Array {
concat(arrays*) {
result := []
; First add the values from the instance being called on
result := this.Clone()
; Second, add arrays given in parameter
for key, array in arrays {
if (IsObject(array)) {
for key2, element in array {
result.push(element)
}
} else {
result.push(array)
}
}
return result
}
every(callback) {
for index, element in this
if !callback.Call(element, index, this)
return false
return true
}
fill(value, start:=0, end:=0) {
len := this.Count()
; START: Adjust 1 based index, check signage, set defaults
if (start > 0)
begin := start - 1 ; Include starting index going forward
else if (start < 0)
begin := len + start ; Count backwards from end
else
begin := start
; END: Check signage and set defaults
if (end > 0)
last := end
else if (end < 0)
last := len + end ; Count backwards from end
else
last := len
loop, % (last - begin)
this[begin + A_Index] := value
return this
}
filter(callback) {
result := []
for index, element in this
if (callback.Call(element, index, this))
result.push(element)
return result
}
; Modified to return "" instead of 'undefined'
find(callback) {
for index, element in this
if (callback.Call(element, index, this))
return element
return ""
}
findIndex(callback) {
for index, value in this
if (callback.Call(value, index, this))
return index
return -1
}
forEach(callback) {
for index, element in this
callback.Call(element, index, this)
return ""
}
includes(searchElement, fromIndex:=0) {
return this.indexOf(searchElement, fromIndex) > 0 ? true : false
}
indexOf(searchElement, fromIndex:=0) {
len := this.Count()
if (fromIndex > 0)
start := fromIndex - 1 ; Include starting index going forward
else if (fromIndex < 0)
start := len + fromIndex ; Count backwards from end
else
start := fromIndex
loop, % len - start
if (this[start + A_Index] = searchElement)
return start + A_Index
return -1
}
join(separator:=",") {
result := ""
for index, element in this {
result .= element (index < this.Count() ? separator : "")
}
return result
}
keys() {
result := []
for key, value in this {
result.push(key)
}
return result
}
; if the provided index is negative, the array is still searched from front to back
; - Are we not able to return the first found starting from the back?
lastIndexOf(searchElement, fromIndex:=0) {
len := this.Count()
foundIdx := -1
if (fromIndex > len)
return foundIdx
if (fromIndex > 0)
start := fromIndex - 1 ; Include starting index going forward
else if (fromIndex < 0)
start := len + fromIndex ; Count backwards from end
else
start := fromIndex
loop, % len - start
if (this[start + A_Index] = searchElement)
foundIdx := start + A_Index
return foundIdx
}
map(callback) {
result := []
for index, element in this
result.push(callback.Call(element, index, this))
return result
}
reduce(callback, initialValue:="__NULL__") {
len := this.Count()
; initialValue not defined
if (initialValue == "__NULL__") {
if (len < 1) {
; Empty array with no intial value
return
}
else if (len == 1) {
; Single item array with no initial value
return this[1]
}
; Starting value is last element
initialValue := this[1]
; Loop n-1 times (start at 2nd element)
iterations := len - 1
; Set index A_Index+1 each iteration
idxOffset := 1
} else {
; initialValue defined
if (len == 0) {
; Empty array with initial value
return initialValue
}
; Loop n times (starting at 1st element)
iterations := len
; Set index A_Index each iteration
idxOffset := 0
}
; if no initialValue is passed, use first index as starting value and reduce
; array starting at index n+1. Otherwise, use initialValue as staring value
; and start at arrays first index.
Loop, % iterations
{
adjIndex := A_Index + idxOffset
initialValue := callback.Call(initialValue, this[adjIndex], adjIndex, this)
}
return initialValue
}
reduceRight(callback, initialValue:="__NULL__") {
len := this.Count()
; initialValue not defined
if (initialValue == "__NULL__") {
if (len < 1) {
; Empty array with no intial value
return
}
else if (len == 1) {
; Single item array with no initial value
return this[1]
}
; Starting value is last element
initialValue := this[len]
; Loop n-1 times (starting at n-1 element)
iterations := len - 1
; Set index A_Index-1 each iteration
idxOffset := 0
} else {
; initialValue defined
if (len == 0)
; Empty array with initial value
return initialValue
; Loop n times (start at n element)
iterations := len
; Set index A_Index each iteration
idxOffset := 1
}
; If no initialValue is passed, use last index as starting value and reduce
; array starting at index n-1. Otherwise, use initialValue as starting value
; and start at arrays last index.
Loop, % iterations
{
adjIndex := len - (A_Index - idxOffset)
initialValue := callback.Call(initialValue, this[adjIndex], adjIndex, this)
}
return initialValue
}
reverse() {
len := this.Count()
Loop, % (len // 2)
{
idxFront := A_Index
idxBack := len - (A_Index - 1)
tmp := this[idxFront]
this[idxFront] := this[idxBack]
this[idxBack] := tmp
}
return this
}
shift() {
return this.RemoveAt(1)
}
slice(start:=0, end:=0) {
len := this.Count()
; START: Adjust 1 based index, check signage, set defaults
if (start > 0)
begin := start - 1 ; Include starting index going forward
else if (start < 0)
begin := len + start ; Count backwards from end
else
begin := start
; END: Check signage and set defaults
; MDN States: "to end (end not included)" so subtract one from end
if (end > 0)
last := end - 1
else if (end < 0)
last := len + end ; Count backwards from end
else
last := len
result := []
loop, % last - begin
result.push(this[begin + A_Index])
if (result.Count() == 0) {
return ""
}
return result
}
some(callback) {
for index, value in this
if callback.Call(value, index, this)
return true
return false
}
sort(compare_fn:=0) {
; Quicksort
this._Call(this, compare_fn)
return this
}
splice(start, deleteCount:=-1, args*) {
len := this.Count()
exiting := []
; Determine starting index
if (start > len)
start := len
if (start < 0)
start := len + start
; deleteCount unspecified or out of bounds, set count to start through end
if ((deleteCount < 0) || (len <= (start + deleteCount))) {
deleteCount := len - start + 1
}
; Remove elements
Loop, % deleteCount
{
exiting.push(this[start])
this.RemoveAt(start)
}
; Inject elements
Loop, % args.Count()
{
curIndex := start + (A_Index - 1)
this.InsertAt(curIndex, args[1])
args.removeAt(1)
}
return exiting
}
toString() {
str := ""
for i,v in this
str .= v (i < this.Count() ? "," : "")
return str
}
unshift(args*) {
for index, value in args
this.InsertAt(A_Index, value)
return this.Count()
}
values() {
result := []
for key, value in this {
result.push(value)
}
return result
}
; Internal functions
_compare_alphanum(a, b) {
; return 0 if a and b are the same
; return -1 if b is "" or undefined
; return 1 if a is greater than b
; return -1 if a is less than b
return a == b ? 0 : b == "" ? -1 : a > b ? 1 : a < b ? -1
}
_sort(array, compare_fn, left, right) {
if (array.Count() > 1) {
centerIdx := this._partition(array, compare_fn, left, right)
if (left < centerIdx - 1) {
this._sort(array, compare_fn, left, centerIdx - 1)
}
if (centerIdx < right) {
this._sort(array, compare_fn, centerIdx, right)
}
}
}
_partition(array, compare_fn, left, right) {
pivot := array[floor(left + (right - left) / 2)]
i := left
j := right
while (i <= j) {
while (compare_fn.Call(array[i], pivot) < 0) { ;array[i] < pivot
i++
}
while (compare_fn.Call(array[j], pivot) > 0) { ;array[j] > pivot
j--
}
if (i <= j) {
this._swap(array, i, j)
i++
j--
}
}
return i
}
_swap(array, idx1, idx2) {
tempVar := array[idx1]
array[idx1] := array[idx2]
array[idx2] := tempVar
}
; Left/Right remain from a standard implementation, but the adaption is
; ported from JS which doesn't expose these parameters.
;
; To expose left/right: Call(array, compare_fn:=0, left:=0, right:=0), but
; this would require passing a falsey value to compare_fn when only
; positioning needs altering: Call(myArr, <false/0/"">, 2, myArr.Count())
_Call(array, compare_fn:=0) {
; Default comparator
if !(compare_fn) {
compare_fn := objBindMethod(this, "_compare_alphanum")
}
; Default start/end index
left := left ? left : 1
right := right ? right : array.Count()
; Perform in-place sort
this._sort(array, compare_fn, left, right)
; Return object ref for method chaining
return array
}
; Redirect to newer calling standard
__Call(method, args*) {
if (method = "")
return this._Call(args*)
if (IsObject(method))
return this._Call(method, args*)
}
}