-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexArray.au3
More file actions
224 lines (148 loc) · 6.73 KB
/
IndexArray.au3
File metadata and controls
224 lines (148 loc) · 6.73 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
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.16.1
Author(s): Zvend
Discord(s): zvend
Script Function:
_IndexArray_Init(Const $nSize = 32) -> IndexArray
_IndexArray_IsIndexArray(Const ByRef $aArray) -> Bool
_IndexArray_IsIndexValid(Const ByRef $aArray, Const $nIndex, Const $bSkipCheck = False) -> Bool
_IndexArray_GetSize(Const ByRef $aArray) -> UInt32
_IndexArray_Resize(ByRef $aArray, Const $nNewSize) -> Boolean
_IndexArray_Get(Const ByRef $aArray, Const $nIndex, Const $vDefaultValue = Null) -> Variant
_IndexArray_Set(ByRef $aArray, Const $nIndex, Const $vValue) -> Bool
_IndexArray_Reset(ByRef $aArray, Const $nSize = 32) -> Bool
Description:
IndexArrays are meant to be index controlled. So instead of adding the new value to the end of an array it will always
require where to write the array and if necessary it will resize the array accordingly.
This is very useful for arrays with fixed IDs that have to be set on an index that does not (yet) exist.
#ce ----------------------------------------------------------------------------
#cs - Guide --------------------------------------------------------------------
How To Use:
Initialize your global array like:
Global $g_aMyArray = _IndexArray_Init(4)
This will create an array of size 4 indexes (1-4) and index 0 is always the current size of the array.
Empty fields will always return Null.
Now Set your values like:
_IndexArray_Set($g_aMyArray, 1, "I am a value")
_IndexArray_Set($g_aMyArray, 2, "Look another value!")
_IndexArray_Set($g_aMyArray, 4, "I am the end of this array!")
_IndexArray_Set($g_aMyArray, 8, "No you are not!")
If you sharpened your eyes you see that setting on index 8 should not be possible, since we gave the array a size of 4.
But it will auto resize the array by calculating the size like 'Floor(size * 1.5)' until the index 8 fits in the array.
So 4 * 1.5 = 6 and 6 * 1.5 = 9. The array will be resized to size 9.
You should always get the values by _IndexArray_Get(). it will error check and prevent autoit from crashing if index is out of bounds.
If you need to completely reset your array (Nulling all values) + resizing to a default value, you can use:
_IndexArray_Reset($g_aMyArray, 4)
All values will be Null again. You could also just call:
$g_aMyArray = _IndexArray_Init(4)
Since it would do the exact same like Reset BUT using _IndexArray_Reset() is by far more readable.
#ce ----------------------------------------------------------------------------
#include-once
#include ".\Integer.au3"
#include ".\UnitTest.au3"
#include <Array.au3>
Global Enum _
$INDEXARRAY_ERR_NONE , _
$INDEXARRAY_ERR_BAD_SIZE , _
$INDEXARRAY_ERR_BAD_INDEXARRAY, _
$INDEXARRAY_ERR_INVALID_INDEX
Global Enum _
$__INDEXARRAY_IDENTIFIER, _
$__INDEXARRAY_SIZE, _
$__INDEXARRAY_PARAMS
Func _IndexArray_Init(Const $nSize = 32) ;-> IndexArray
If Not IsInt($nSize) Or $nSize <= 0 Then
Return SetError($INDEXARRAY_ERR_BAD_SIZE, 0, Null)
EndIf
Local $aArray[$nSize + $__INDEXARRAY_PARAMS]
$aArray[$__INDEXARRAY_IDENTIFIER] = "IndexArray"
$aArray[$__INDEXARRAY_SIZE] = $nSize
For $i = $__INDEXARRAY_PARAMS To $nSize + $__INDEXARRAY_PARAMS - 1
$aArray[$i] = Null
Next
Return $aArray
EndFunc
Func _IndexArray_IsIndexArray(Const ByRef $aArray) ;-> Bool
If Not IsArray($aArray) Then
Return SetError($INDEXARRAY_ERR_BAD_INDEXARRAY, 0, 0)
EndIf
If UBound($aArray) < $__INDEXARRAY_PARAMS Then
Return SetError($INDEXARRAY_ERR_BAD_INDEXARRAY, 0, 0)
EndIf
If Not ($aArray[$__INDEXARRAY_IDENTIFIER] == "IndexArray") Then
Return SetError($INDEXARRAY_ERR_BAD_INDEXARRAY, 0, 0)
EndIf
Return 1
EndFunc
Func _IndexArray_IsIndexValid(Const ByRef $aArray, Const $nIndex, Const $bSkipCheck = False) ;-> Bool
If Not $bSkipCheck And Not _IndexArray_IsIndexArray($aArray) Then
Return SetError(@error, 0, 0)
EndIf
If Not _Integer_IsInRange($nIndex, 0, $aArray[$__INDEXARRAY_SIZE] - 1) Then
Return SetError($INDEXARRAY_ERR_INVALID_INDEX, 0, 0)
EndIf
Return 1
EndFunc
Func _IndexArray_GetSize(Const ByRef $aArray) ;-> UInt32
If Not _IndexArray_IsIndexArray($aArray) Then
Return SetError(@error, 0, 0)
EndIf
Return $aArray[$__INDEXARRAY_SIZE]
EndFunc
Func _IndexArray_Resize(ByRef $aArray, Const $nNewSize) ;-> Boolean
If Not _IndexArray_IsIndexArray($aArray) Then
Return SetError(@error, 0, 0)
EndIf
If Not IsInt($nNewSize) Or $nNewSize <= 0 Then
Return SetError($INDEXARRAY_ERR_BAD_SIZE, 0, 0)
EndIf
If $nNewSize = $aArray[$__INDEXARRAY_SIZE] Then
Return 1
EndIf
If $nNewSize < $aArray[$__INDEXARRAY_SIZE] Then
Redim $aArray[$nNewSize + $__INDEXARRAY_PARAMS]
$aArray[$__INDEXARRAY_SIZE] = $nNewSize
Return 1
EndIf
Local $nCalcSize = $aArray[$__INDEXARRAY_SIZE]
Local $nOldSize = $aArray[$__INDEXARRAY_SIZE]
While $nCalcSize <= $nNewSize
$nCalcSize = Floor($nCalcSize * 1.5)
WEnd
ReDim $aArray[$nCalcSize + $__INDEXARRAY_PARAMS]
$aArray[$__INDEXARRAY_SIZE] = $nCalcSize
For $i = $__INDEXARRAY_PARAMS + $nOldSize To $nCalcSize + $__INDEXARRAY_PARAMS - 1
$aArray[$i] = Null
Next
Return 1
EndFunc
Func _IndexArray_Get(Const ByRef $aArray, Const $nIndex, Const $vDefaultValue = Null) ;-> Variant
If Not _IndexArray_IsIndexValid($aArray, $nIndex, False) Then
Return SetError(@error, 0, $vDefaultValue)
EndIf
Return $aArray[$nIndex + $__INDEXARRAY_PARAMS]
EndFunc
Func _IndexArray_Set(ByRef $aArray, Const $nIndex, Const $vValue) ;-> Bool
If Not _IndexArray_IsIndexArray($aArray) Then
Return SetError(@error, 0, 0)
EndIf
If Not IsInt($nIndex) Or $nIndex < 0 Then
Return SetError($INDEXARRAY_ERR_INVALID_INDEX, 0, 0)
EndIf
If $nIndex >= $aArray[$__INDEXARRAY_SIZE] Then
_IndexArray_Resize($aArray, $nIndex)
EndIf
$aArray[$nIndex + $__INDEXARRAY_PARAMS] = $vValue
Return 1
EndFunc
Func _IndexArray_Reset(ByRef $aArray, Const $nSize = 32) ;-> Bool
If Not _IndexArray_IsIndexArray($aArray) Then
Return SetError(1, 0, 0)
EndIf
Local $aTemp = _IndexArray_Init($nSize)
If $aTemp = Null Or @error Then
Return SetError(@error, 0, 0)
EndIf
$aArray = $aTemp
Return 1
EndFunc