forked from kimburgess/netlinx-common-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.axi
339 lines (298 loc) · 7.48 KB
/
array.axi
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
program_name='array'
#if_not_defined __NCL_LIB_ARRAY
#define __NCL_LIB_ARRAY
include 'math'
include 'io'
define_function array_function_deprecated(char old[64], char new[64]) {
println("'Warning, the array function ',old,'() is deprecated',
', please use ', new, '()'");
}
/**
* Finds the index for any matching device entry in an array.
* @deprecated
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_index(dev item, dev list[])
{
array_function_deprecated('array_index', 'array_device_index');
return array_device_index(item, list);
}
/**
* Finds the index for any matching device entry in an array.
*
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_device_index(dev item, dev list[])
{
stack_var integer i
for (i = 1; i <= max_length_array(list); i++) {
if (item == list[i]) {
return i
}
}
return 0
}
/**
* Finds the index for any matching integer entry in an array.
*
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_integer_index(integer item, integer list[])
{
stack_var integer i
for (i = 1; i <= max_length_array(list); i++) {
if (item == list[i]) {
return i
}
}
return 0
}
/**
* Finds the index for any matching double entry in an array.
*
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_double_index(double item, double list[])
{
stack_var integer i
for (i = 1; i <= max_length_array(list); i++) {
if (item == list[i]) {
return i
}
}
return 0
}
/**
* Finds the index for any matching string entry in an array.
*
* @param item item to find in the array
* @param list array of items
*
* @return the index of the matching value, or 0 if no match
*/
define_function integer array_string_index(char item[], char list[][])
{
stack_var integer i
for (i = 1; i <= max_length_array(list); i++) {
if (item == list[i]) {
return i
}
}
return 0
}
/**
* Finds the minimum value in an integer array
*
* @param list array of integers
*
* @return the minimum value in the array
*/
define_function integer min(integer list[])
{
stack_var integer i
stack_var integer val
val = 65535;
for (i = 1; i <= max_length_array(list); i++) {
val = min_value(val, list[i]);
}
return val;
}
/**
* Finds the maximum value in an integer array
*
* @param list array of integers
*
* @return the maximum value in the array
*/
define_function integer max(integer list[])
{
stack_var integer i
stack_var integer val
val = 0;
for (i = 1; i <= max_length_array(list); i++) {
val = max_value(val, list[i]);
}
return val;
}
/**
* Finds the minimum value in a double array
*
* @param list array of doubles
*
* @return the minimum value in the array
*/
define_function double mind(double list[])
{
stack_var integer i
stack_var double val
val = MATH_POSITIVE_INFINITY;
for (i = 1; i <= max_length_array(list); i++) {
if (list[i] < val) {
val = list[i];
}
}
return val;
}
/**
* Finds the maximum value in a double array
*
* @param list array of doubles
*
* @return the maximum value in the array
*/
define_function double maxd(double list[])
{
stack_var integer i
stack_var double val
val = MATH_NEGATIVE_INFINITY;
for (i = 1; i <= max_length_array(list); i++) {
if (list[i] > val) {
val = list[i];
}
}
return val;
}
/**
* In-place sorting of an integer array using a non-recursive quicksort method.
*
* @param list array of integers
*
* @return nothing
*/
define_function quicksort(integer list[])
{
stack_var integer stack_count // current stack size
stack_var integer stack[16][2] // stack size with log2(N) slots required: N = max allowable number
stack_var integer pivot // current pivot value
stack_var integer lo, hi // current lower and upper indices
stack_count = 1
stack[stack_count][1] = 1
stack[stack_count][2] = length_array(list)
while (stack_count) {
lo = stack[stack_count][1]
hi = stack[stack_count][2]
if (lo < hi) {
pivot = list[lo]
while (lo < hi) {
while (list[hi] >= pivot && lo < hi) {
hi--
}
if (lo < hi) {
list[lo] = list[hi]
lo++
}
while (list[lo] <= pivot && lo < hi) {
lo++
}
if (lo < hi) {
list[hi] = list[lo]
hi--
}
}
list[lo] = pivot
stack_count++
stack[stack_count][1] = lo + 1
stack[stack_count][2] = stack[stack_count - 1][2]
stack[stack_count - 1][2] = lo
}
else {
stack_count--
}
}
}
/**
* In-place sorting of an integer array using the insertion sort method.
* Efficient for small data sets.
* Crude testing on a NI-900 suggests that insertion sort is faster than the
* non-recursive quicksort for random integer arrays up to a size of about 25
* elements.
*
* @param list array of integers
*
* @return nothing
*/
define_function insertionsort(integer list[])
{
stack_var integer length
stack_var integer n
stack_var integer val
stack_var integer hole_pos
length = length_array(list)
for (n = 2; n <= length; n++) {
val = list[n]
hole_pos = n
while (val < list[hole_pos - 1]) {
list[hole_pos] = list[hole_pos - 1]
hole_pos--
if (hole_pos == 1) {
break
}
}
list[hole_pos] = val
}
}
/**
* In-place partitioning of an integer array. It partitions the portion of the
* array between indices left and right inclusive by moving all elements less
* than list[pivot_index] before the pivot and those equal or greater after it.
*
* @param list array of integers
* @param left index of first element of the sub-array to partition
* @param right index of second element of the sub-array to partition
* @param pivot_index index of the pivot element
*
* @return nothing
*/
define_function integer partition(integer list[], integer left, integer right, integer pivot_index)
{
stack_var integer pivot_value
stack_var integer store_index
stack_var integer n
store_index = 0
if (left <= pivot_index && pivot_index <= right && left && right <= length_array(list)) {
pivot_value = list[pivot_index]
swap(list, pivot_index, right)
store_index = left
for (n = left; n < right; n++) {
if (list[n] <= pivot_value) {
swap(list, n, store_index)
store_index++
}
}
swap(list, store_index, right)
}
return store_index
}
/**
* In-place swap of two elements in an integer array
*
* @param list array of integers
* @param index1 index of the first element to swap
* @param index2 index of the second element to swap
*
* @return nothing
*/
define_function swap(integer list[], integer index1, integer index2)
{
stack_var integer length
stack_var integer temp_store
length = length_array(list)
if (index1 && index1 <= length && index2 && index2 <= length) {
temp_store = list[index1];
list[index1] = list[index2]
list[index2] = temp_store
}
}
#end_if