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
|
#ifndef UDO_ARRAYTOOLS
#define UDO_ARRAYTOOLS ##
/*
Array tools
This file is part of the SONICS UDO collection by Richard Knight 2021
License: GPL-2.0-or-later
http://1bpm.net
*/
giassocarrayvalues[] init 1
gSassocarraykeys[] init 1
/*
Get a random value from an array
ivalue arr_random iarray[]
kvalue arr_random iarray[]
Svalue arr_random Sarray[]
ivalue selected value
kvalue selected value
iarray[] array to evaluate
Sarray[] string array to evaluate
*/
opcode arr_random, i, i[]
iarray[] xin
ivalue = iarray[round(random(0, lenarray(iarray) - 1))]
xout ivalue
endop
opcode arr_random, S, S[]
Sarray[] xin
Svalue = Sarray[round(random(0, lenarray(Sarray) - 1))]
xout Svalue
endop
opcode arr_random, k, i[]
iarray[] xin
kvalue = iarray[round:k(random:k(0, lenarray(iarray) - 1))]
xout kvalue
endop
/*
Append to global associative array
*/
opcode asarr_append, 0, Si
Skey, ivalue xin
index = 0
ilen = lenarray(giassocarrayvalues)
Snewkeys[] init (ilen + 1)
inewvals[] init (ilen + 1)
while (index < ilen) do
Snewkeys[index] = gSassocarraykeys[index]
inewvals[index] = giassocarrayvalues[index]
index += 1
od
Snewkeys[index] = Skey
inewvals[index] = ivalue
giassocarrayvalues = inewvals
gSassocarraykeys = Snewkeys
endop
opcode asarr_get, i, S
Skey xin
index = 0
ivalue = -1
ilen = lenarray(giassocarrayvalues)
while (index < ilen) do
isame strcmp Skey, gSassocarraykeys[index]
if (isame == 0) then
ivalue = giassocarrayvalues[index]
goto output
endif
index += 1
od
output:
xout ivalue
endop
/*
Get the nearest value to kvalue in the one-dimensional array iarray, which should be sorted
knearest nearest iarray, kvalue
knearest the nearest value found
*/
opcode arr_nearest, k, i[]k
iarray[], kvalue xin
knearest = 0
kindex = 0
while (kindex < lenarray(iarray)) do
ktest = iarray[kindex]
if (abs(kvalue - ktest) < abs(kvalue - knearest)) then
knearest = ktest
endif
kindex += 1
od
xout knearest
endop
; Snew[] arrayappend Sarray, Svalue
; extend a S array with a given value
opcode arr_append, S[], S[]S
Sarray[], Svalue xin
index = 0
ilen = lenarray(Sarray)
Snew[] init (ilen + 1)
while (index < ilen) do
Snew[index] = Sarray[index]
index += 1
od
Snew[index] = Svalue
xout Snew
endop
; inew[] arrayappend iarray, ivalue
; extend an i array with a given value
opcode arr_append, i[], i[]i
iarray[], ivalue xin
index = 0
ilen = lenarray(iarray)
inew[] init (ilen + 1)
while (index < ilen) do
inew[index] = iarray[index]
index += 1
od
inew[index] = ivalue
xout inew
endop
; array, asfloat
opcode arr_serialise, S, i[]p
iarray[], iasfloat xin
ilen = lenarray(iarray)
index = 0
SprintfChar = (iasfloat == 1) ? "%f" : "%d"
Sout = "["
while (index < ilen) do
if (index != 0) then
Sout = strcat(Sout, ",")
endif
Sout = strcat(Sout, sprintf(SprintfChar, iarray[index]))
index += 1
od
Sout = strcat(Sout, "]")
xout Sout
endop
; array
opcode arr_serialise, S, S[]
Sarray[] xin
ilen = lenarray(Sarray)
index = 0
Sout = "["
while (index < ilen) do
if (index != 0) then
Sout = strcat(Sout, ",")
endif
Sout = strcat(Sout, sprintf("\"%s\"", Sarray[index]))
index += 1
od
Sout = strcat(Sout, "]")
xout Sout
endop
opcode arr_unserialise, S[], S
Sdata xin
ilen = strlen(Sdata)
ichar = 0
inarray = 0
instring = 0
iitems = 0
while (ichar < ilen) do
Schar = strsub(Sdata, ichar, ichar + 1)
if (instring == 0 && strcmp(Schar, "[") == 0) then
inarray = 1
elseif (inarray == 1 && instring == 0 && strcmp(Schar, "]") == 0) then
inarray = 0
elseif (inarray == 1 && instring == 0 && strcmp(Schar, ",") == 0) then
iitems += 1
elseif (inarray == 1 && strcmp(Schar, "\"") == 0) then
instring = 1 - instring
endif
ichar += 1
od
Sarray[] init iitems + 1
ichar = 0
inarray = 0
instring = 0
istringstart = -1
index = 0
while (ichar < ilen) do
Schar = strsub(Sdata, ichar, ichar + 1)
if (instring == 0 && strcmp(Schar, "[") == 0) then
inarray = 1
elseif (inarray == 1 && instring == 0 && strcmp(Schar, "]") == 0) then
inarray = 0
elseif (inarray == 1 && instring == 0 && strcmp(Schar, ",") == 0) then
index += 1
elseif (inarray == 1 && strcmp(Schar, "\"") == 0) then
if (instring == 0) then
instring = 1
istringstart = ichar + 1
else
instring = 0
Sarray[index] = strsub(Sdata, istringstart, ichar)
istringstart = -1
endif
endif
ichar += 1
od
xout Sarray
endop
#end
|