-
Notifications
You must be signed in to change notification settings - Fork 71
/
Array.ahk
110 lines (105 loc) · 3.01 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
; Array Lib - temp01 - http://www.autohotkey.com/forum/viewtopic.php?t=49736
Array(p1="……", p2="……", p3="……", p4="……", p5="……", p6="……"){
static ArrBase
If !ArrBase
ArrBase := Object("len", "Array_Length", "indexOf", "Array_indexOf", "join", "Array_Join"
, "append", "Array_Append", "insert", "Array_Insert", "delete", "Array_Delete"
, "sort", "Array_sort", "reverse", "Array_Reverse", "unique", "Array_Unique"
, "extend", "Array_Extend", "copy", "Array_Copy", "pop", "Array_Pop")
arr := Object("base", ArrBase)
While (_:=p%A_Index%)!="……" && A_Index<=6
arr[A_Index] := _
Return arr
}
Array_indexOf(arr, val, opts="", startpos=1){
P := !!InStr(opts, "P"), C := !!InStr(opts, "C")
If A := !!InStr(opts, "A")
matches := Array()
Loop % arr.len()
If(A_Index>=startpos)
If(match := InStr(arr[A_Index], val, C)) and (P or StrLen(arr[A_Index])=StrLen(val))
If A
matches.append(A_Index)
Else
Return A_Index
If A
Return matches
Else
Return 0
}
Array_Join(arr, sep="`n"){
Loop, % arr.len()
str .= arr[A_Index] sep
StringTrimRight, str, str, % StrLen(sep)
return str
}
Array_Copy(arr){
Return Array().extend(arr)
}
Array_Append(arr, p1="……", p2="……", p3="……", p4="……", p5="……", p6="……"){
Return arr.insert(arr.len()+1, p1, p2, p3, p4, p5, p6)
}
Array_Insert(arr, index, p1="……", p2="……", p3="……", p4="……", p5="……", p6="……"){
While (_:=p%A_Index%)!="……" && A_Index<=6
arr._Insert(index + (A_Index-1), _)
Return arr
}
Array_Reverse(arr){
arr2 := Array()
Loop, % len:=arr.len()
arr2[len-(A_Index-1)] := arr[A_Index]
Return arr2
}
Array_Sort(arr, func="Array_CompareFunc"){
n := arr.len(), swapped := true
while swapped {
swapped := false
Loop, % n-1 {
i := A_Index
if %func%(arr[i], arr[i+1], 1) > 0 ; standard ahk syntax for sort callout functions
arr.insert(i, arr[i+1]).delete(i+2), swapped := true
}
n--
}
Return arr
}
Array_Unique(arr, func="Array_CompareFunc"){ ; by infogulch
i := 0
while ++i < arr.len(), j := i + 1
while j <= arr.len()
if !%func%(arr[i], arr[j], i-j)
arr.delete(j) ; j comes after
else
j++ ; only increment to next element if not removing the current one
Return arr
}
Array_CompareFunc(a, b, c){
; this setup is compatible with the sort command's syntax
; if a > b return positive
; if a = b return false
; if a < b return negative
; c is element offset (a.pos - b.pos)
return a > b ? 1 : a = b ? 0 : -1
}
Array_Extend(arr, p1="……", p2="……", p3="……", p4="……", p5="……", p6="……"){
While (_:=p%A_Index%)!="……" && A_Index<=6
If IsObject(_)
Loop, % _.len()
arr.append(_[A_Index])
Else
Loop, % %_%0
arr.append(%_%%A_Index%)
Return arr
}
Array_Pop(arr){
Return arr.delete(arr.len())
}
Array_Delete(arr, p1="……", p2="……", p3="……", p4="……", p5="……", p6="……"){
While (_:=p%A_Index%)!="……" && A_Index<=6
arr._Remove(_)
Return arr
}
Array_Length(arr){
len := arr._MaxIndex()
Return len="" ? 0 : len
}