-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray.ahk
365 lines (359 loc) · 14.8 KB
/
Array.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
/*
Name: Array.ahk
Version 0.4.1 (02.09.24)
Created: 27.08.22
Author: Descolada
Description:
A compilation of useful array methods.
Array.Slice(start:=1, end:=0, step:=1) => Returns a section of the array from 'start' to 'end',
optionally skipping elements with 'step'.
Array.Swap(a, b) => Swaps elements at indexes a and b.
Array.Map(func, arrays*) => Applies a function to each element in the array.
Array.ForEach(func) => Calls a function for each element in the array.
Array.Filter(func) => Keeps only values that satisfy the provided function
Array.Reduce(func, initialValue?) => Applies a function cumulatively to all the values in
the array, with an optional initial value.
Array.IndexOf(value, start:=1) => Finds a value in the array and returns its index.
Array.Find(func, &match?, start:=1) => Finds a value satisfying the provided function and returns the index.
match will be set to the found value.
Array.Reverse() => Reverses the array.
Array.Count(value) => Counts the number of occurrences of a value.
Array.Sort(OptionsOrCallback?, Key?) => Sorts an array, optionally by object values.
Array.Shuffle() => Randomizes the array.
Array.Unique() => Returns a set of the values in the array
Array.Join(delim:=",") => Joins all the elements to a string using the provided delimiter.
Array.Flat() => Turns a nested array into a one-level array.
Array.Extend(enums*) => Adds the values of other arrays or enumerables to the end of this one.
*/
class Array2 {
static __New() => (Array2.base := Array.Prototype.base, Array.Prototype.base := Array2)
/**
* Returns a section of the array from 'start' to 'end', optionally skipping elements with 'step'.
* Modifies the original array.
* @param start Optional: index to start from. Default is 1.
* @param end Optional: index to end at. Can be negative. Default is 0 (includes the last element).
* @param step Optional: an integer specifying the incrementation. Default is 1.
* @returns {Array}
*/
static Slice(start:=1, end:=0, step:=1) {
len := this.Length, i := start < 1 ? len + start : start, j := Min(end < 1 ? len + end : end, len), r := [], reverse := False
if step = 0
Throw ValueError("Slice: step cannot be 0", -1)
if len = 0
return []
if i < 1
i := 1
r.Length := r.Capacity := Abs(j+1-i) // step
if step < 0 {
while i >= j {
if this.Has(i)
r[A_Index] := this[i]
i += step
}
} else {
while i <= j {
if this.Has(i)
r[A_Index] := this[i]
i += step
}
}
return this := r
}
/**
* Swaps elements at indexes a and b
* @param a First elements index to swap
* @param b Second elements index to swap
* @returns {Array}
*/
static Swap(a, b) {
temp := this.Has(b) ? this[b] : unset
this.Has(a) ? (this[b] := this[a]) : this.Delete(b)
IsSet(temp) ? (this[a] := temp) : this.Delete(a)
return this
}
/**
* Applies a function to each element in the array (mutates the array).
* @param func The mapping function that accepts one argument.
* @param arrays Additional arrays to be accepted in the mapping function
* @returns {Array}
*/
static Map(func, arrays*) {
if !HasMethod(func)
throw ValueError("Map: func must be a function", -1)
for i, v in this {
bf := func.Bind(v?)
for _, vv in arrays
bf := bf.Bind(vv.Has(i) ? vv[i] : unset)
try bf := bf()
this[i] := bf
}
return this
}
/**
* Applies a function to each element in the array.
* @param func The callback function with arguments Callback(value[, index, array]).
* @returns {Array}
*/
static ForEach(func) {
if !HasMethod(func)
throw ValueError("ForEach: func must be a function", -1)
for i, v in this
func(v?, i, this)
return this
}
/**
* Keeps only values that satisfy the provided function
* @param func The filter function that accepts one argument.
* @returns {Array}
*/
static Filter(func) {
if !HasMethod(func)
throw ValueError("Filter: func must be a function", -1)
r := []
for v in this
if func(v?)
r.Push(v)
return this := r
}
/**
* Applies a function cumulatively to all the values in the array, with an optional initial value.
* @param func The function that accepts two arguments and returns one value
* @param initialValue Optional: the starting value. If omitted, the first value in the array is used.
* @returns {func return type}
* @example
* [1,2,3,4,5].Reduce((a,b) => (a+b)) ; returns 15 (the sum of all the numbers)
*/
static Reduce(func, initialValue?) {
if !HasMethod(func)
throw ValueError("Reduce: func must be a function", -1)
len := this.Length + 1
if len = 1
return initialValue ?? ""
if IsSet(initialValue)
out := initialValue, i := 0
else
out := this[1], i := 1
while ++i < len {
out := func(out?, this.Has(i) ? this[i] : unset)
}
return out
}
/**
* Finds a value in the array and returns its index.
* @param value The value to search for.
* @param start Optional: the index to start the search from, negative start reverses the search. Default is 1.
*/
static IndexOf(value?, start:=1) {
local len := this.Length, reverse := false
if !IsInteger(start)
throw ValueError("IndexOf: start value must be an integer", -1)
if start < 0
reverse := true, start := len+1+start
if start < 1 || start > len
return 0
if reverse {
++start
if IsSet(value) {
while --start > 0
if this.Has(start) && (this[start] == value)
return start
} else {
while --start > 0
if !this.Has(start)
return start
}
} else {
--start
if IsSet(value) {
while ++start <= len
if this.Has(start) && (this[start] == value)
return start
} else {
while ++start <= len
if !this.Has(start)
return start
}
}
return 0
}
/**
* Finds a value satisfying the provided function and returns its index.
* @param func The condition function that accepts one argument.
* @param match Optional: is set to the found value
* @param start Optional: the index to start the search from, negative start reverses the search. Default is 1.
* @example
* [1,2,3,4,5].Find((v) => (Mod(v,2) == 0)) ; returns 2
*/
static Find(func, &match?, start:=1) {
local reverse := false
if !HasMethod(func)
throw ValueError("Find: func must be a function", -1)
local len := this.Length
if start < 0
reverse := true, start := len+1+start
if start < 1 || start > len
return 0
if reverse {
++start
while --start > 0
if ((v := (this.Has(start) ? this[start] : unset)), func(v?))
return ((match := v ?? unset), start)
} else {
--start
while ++start <= len
if ((v := (this.Has(start) ? this[start] : unset)), func(v?))
return ((match := v ?? unset), start)
}
return 0
}
/**
* Reverses the array.
* @example
* [1,2,3].Reverse() ; returns [3,2,1]
*/
static Reverse() {
local len := this.Length + 1, max := (len // 2), i := 0
while ++i <= max
this.Swap(i, len - i)
return this
}
/**
* Counts the number of occurrences of a value
* @param value The value to count. Can also be a function.
*/
static Count(value?) {
count := 0
if !IsSet(value) {
Loop this.Length
if this.Has(A_Index)
count++
} else if HasMethod(value) {
for _, v in this
if value(v?)
count++
} else
for _, v in this
if v == value
count++
return count
}
/**
* Sorts an array, optionally by object keys
* @param OptionsOrCallback Optional: either a callback function, or one of the following:
*
* N => array is considered to consist of only numeric values. This is the default option.
* C, C1 or COn => case-sensitive sort of strings
* C0 or COff => case-insensitive sort of strings
*
* The callback function should accept two parameters elem1 and elem2 and return an integer:
* Return integer < 0 if elem1 less than elem2
* Return 0 is elem1 is equal to elem2
* Return > 0 if elem1 greater than elem2
* @param Key Optional: Omit it if you want to sort a array of primitive values (strings, numbers etc).
* If you have an array of objects, specify here the key by which contents the object will be sorted.
* @returns {Array}
*/
static Sort(optionsOrCallback:="N", key?) {
static sizeofFieldType := 16 ; Same on both 32-bit and 64-bit
if HasMethod(optionsOrCallback)
pCallback := CallbackCreate(CustomCompare.Bind(optionsOrCallback), "F Cdecl", 2), optionsOrCallback := ""
else {
if InStr(optionsOrCallback, "N")
pCallback := CallbackCreate(IsSet(key) ? NumericCompareKey.Bind(key) : NumericCompare, "F CDecl", 2)
if RegExMatch(optionsOrCallback, "i)C(?!0)|C1|COn")
pCallback := CallbackCreate(IsSet(key) ? StringCompareKey.Bind(key,,True) : StringCompare.Bind(,,True), "F CDecl", 2)
if RegExMatch(optionsOrCallback, "i)C0|COff")
pCallback := CallbackCreate(IsSet(key) ? StringCompareKey.Bind(key) : StringCompare, "F CDecl", 2)
if InStr(optionsOrCallback, "Random")
pCallback := CallbackCreate(RandomCompare, "F CDecl", 2)
if !IsSet(pCallback)
throw ValueError("No valid options provided!", -1)
}
mFields := NumGet(ObjPtr(this) + (8 + (VerCompare(A_AhkVersion, "<2.1-") > 0 ? 3 : 5)*A_PtrSize), "Ptr") ; in v2.0: 0 is VTable. 2 is mBase, 3 is mFields, 4 is FlatVector, 5 is mLength and 6 is mCapacity
DllCall("msvcrt.dll\qsort", "Ptr", mFields, "UInt", this.Length, "UInt", sizeofFieldType, "Ptr", pCallback, "Cdecl")
CallbackFree(pCallback)
if RegExMatch(optionsOrCallback, "i)R(?!a)")
this.Reverse()
if InStr(optionsOrCallback, "U")
this := this.Unique()
return this
CustomCompare(compareFunc, pFieldType1, pFieldType2) => (ValueFromFieldType(pFieldType1, &fieldValue1), ValueFromFieldType(pFieldType2, &fieldValue2), compareFunc(fieldValue1, fieldValue2))
NumericCompare(pFieldType1, pFieldType2) => (ValueFromFieldType(pFieldType1, &fieldValue1), ValueFromFieldType(pFieldType2, &fieldValue2), (fieldValue1 > fieldValue2) - (fieldValue1 < fieldValue2))
NumericCompareKey(key, pFieldType1, pFieldType2) => (ValueFromFieldType(pFieldType1, &fieldValue1), ValueFromFieldType(pFieldType2, &fieldValue2), (f1 := fieldValue1.HasProp("__Item") ? fieldValue1[key] : fieldValue1.%key%), (f2 := fieldValue2.HasProp("__Item") ? fieldValue2[key] : fieldValue2.%key%), (f1 > f2) - (f1 < f2))
StringCompare(pFieldType1, pFieldType2, casesense := False) => (ValueFromFieldType(pFieldType1, &fieldValue1), ValueFromFieldType(pFieldType2, &fieldValue2), StrCompare(fieldValue1 "", fieldValue2 "", casesense))
StringCompareKey(key, pFieldType1, pFieldType2, casesense := False) => (ValueFromFieldType(pFieldType1, &fieldValue1), ValueFromFieldType(pFieldType2, &fieldValue2), StrCompare(fieldValue1.%key% "", fieldValue2.%key% "", casesense))
RandomCompare(pFieldType1, pFieldType2) => (Random(0, 1) ? 1 : -1)
ValueFromFieldType(pFieldType, &fieldValue?) {
static SYM_STRING := 0, PURE_INTEGER := 1, PURE_FLOAT := 2, SYM_MISSING := 3, SYM_OBJECT := 5
switch SymbolType := NumGet(pFieldType + 8, "Int") {
case PURE_INTEGER: fieldValue := NumGet(pFieldType, "Int64")
case PURE_FLOAT: fieldValue := NumGet(pFieldType, "Double")
case SYM_STRING: fieldValue := StrGet(NumGet(pFieldType, "Ptr")+2*A_PtrSize)
case SYM_OBJECT: fieldValue := ObjFromPtrAddRef(NumGet(pFieldType, "Ptr"))
case SYM_MISSING: return
}
}
}
/**
* Randomizes the array. Slightly faster than Array.Sort(,"Random N")
* @returns {Array}
*/
static Shuffle() {
len := this.Length
Loop len-1
this.Swap(A_index, Random(A_index, len))
return this
}
/**
* Returns a set of values in array
*/
static Unique() {
unique := Map()
for v in this
unique[v] := 1
return [unique*]
}
/**
* Joins all the elements to a string using the provided delimiter.
* @param delim Optional: the delimiter to use. Default is comma.
* @returns {String}
*/
static Join(delim:=",") {
result := ""
for v in this
result .= (v ?? "") delim
return (len := StrLen(delim)) ? SubStr(result, 1, -len) : result
}
/**
* Turns a nested array into a one-level array
* @returns {Array}
* @example
* [1,[2,[3]]].Flat() ; returns [1,2,3]
*/
static Flat() {
r := []
for v in this {
if !IsSet(v)
r.Length += 1
else if v is Array
r.Push(v.Flat()*)
else
r.Push(v)
}
return this := r
}
/**
* Adds the contents of another array to the end of this one.
* @param enums The arrays or other enumerables that are used to extend this one.
* @returns {Array}
*/
static Extend(enums*) {
for enum in enums {
if !HasMethod(enum, "__Enum")
throw ValueError("Extend: arr must be an iterable")
for _, v in enum
this.Push(v)
}
return this
}
}