git clone https://git.lucas.co/hou-control.git
vex/include/sidefxlabs_datax.h (6K)
1 #ifndef __sidefxlabs_data_h__
2 #define __sidefxlabs_data_h__
3
4
5 // Float to String
6
7 string labs_ftoa(const float f; const int decimal_places)
8 {
9 return sprintf("%.*g", decimal_places + (int)log10(abs(f)) + (abs(f) >= 1.0), f);
10 }
11
12 string labs_ftoa(const float f)
13 {
14 return labs_ftoa(f, 3);
15 }
16
17
18 // Binary Search
19
20 float labs_binarysearch(const int array[], target_value; export int success)
21 {
22 success = 0;
23 int n = len(array);
24 if (n == 0) return -1.0;
25
26 int l = 0;
27 int r = n - 1;
28 int m = -1;
29
30 while (l <= r)
31 {
32 m = (l + r) / 2;
33
34 if (array[m] < target_value)
35 {
36 l = m + 1;
37 }
38 else if (array[m] > target_value)
39 {
40 r = m - 1;
41 }
42 else
43 {
44 success = 1;
45 break;
46 }
47 }
48
49 // Only happens when the left index is 1 less than the right
50 // index and the target value is between the two values
51 if (l > r) m = r;
52
53 // If the target value is greater than a few duplicated values
54 // but less than the next value, the middle index will always
55 // move to the last of the duplicates
56
57 float index = m;
58
59 if (!success)
60 {
61 if (target_value < array[0] || target_value > array[n - 1])
62 index = -1.0;
63 else if (m + 1 < n)
64 // In VEX, division by zero is allowed. It just returns zero.
65 index += float(target_value - array[m]) / (array[m + 1] - array[m]);
66 }
67
68 return index;
69 }
70
71 float labs_binarysearch(const float array[], target_value; export int success)
72 {
73 success = 0;
74 int n = len(array);
75 if (n == 0) return -1.0;
76
77 int l = 0;
78 int r = n - 1;
79 int m = -1;
80
81 // Forces the target value to have the same floating-point precision as the array elements
82 float target_value_safe = set(target_value, 0)[0];
83
84 while (l <= r)
85 {
86 m = (l + r) / 2;
87
88 if (array[m] < target_value_safe)
89 {
90 l = m + 1;
91 }
92 else if (array[m] > target_value_safe)
93 {
94 r = m - 1;
95 }
96 else
97 {
98 success = 1;
99 break;
100 }
101 }
102
103 // Only happens when the left index is 1 less than the right
104 // index and the target value is between the two values
105 if (l > r) m = r;
106
107 // If the target value is greater than a few duplicated values
108 // but less than the next value, the middle index will always
109 // move to the last of the duplicates
110
111 float index = m;
112
113 if (!success)
114 {
115 if (target_value_safe < array[0] || target_value_safe > array[n - 1])
116 index = -1.0;
117 else if (m + 1 < n)
118 // In VEX, division by zero is allowed. It just returns zero.
119 index += float(target_value_safe - array[m]) / (array[m + 1] - array[m]);
120 }
121
122 return index;
123 }
124
125 int labs_binarysearch(const string array[], target_value; export int success)
126 {
127 success = 0;
128 int n = len(array);
129 if (n == 0) return -1;
130
131 int l = 0;
132 int r = n - 1;
133 int m = -1;
134
135 while (l <= r)
136 {
137 m = (l + r) / 2;
138
139 if (array[m] < target_value)
140 {
141 l = m + 1;
142 }
143 else if (array[m] > target_value)
144 {
145 r = m - 1;
146 }
147 else
148 {
149 success = 1;
150 break;
151 }
152 }
153
154 // Only happens when the left index is 1 less than the right
155 // index and the target value is between the two values
156 if (l > r) m = r;
157
158 // If the target value is greater than a few duplicated values
159 // but less than the next value, the middle index will always
160 // move to the last of the duplicates
161
162 int index = m;
163
164 if (!success)
165 {
166 if (target_value < array[0] || target_value > array[n - 1])
167 index = -1;
168 }
169
170 return index;
171 }
172
173
174 // Append Unique
175
176 void labs_append_unique(export int array[]; const int value)
177 {
178 if (find(array, value) < 0)
179 append(array, value);
180 }
181
182 void labs_append_unique(export float array[]; const float value)
183 {
184 if (find(array, value) < 0)
185 append(array, value);
186 }
187
188 void labs_append_unique(export vector2 array[]; const vector2 value)
189 {
190 if (find(array, value) < 0)
191 append(array, value);
192 }
193
194 void labs_append_unique(export vector array[]; const vector value)
195 {
196 if (find(array, value) < 0)
197 append(array, value);
198 }
199
200 void labs_append_unique(export vector4 array[]; const vector4 value)
201 {
202 if (find(array, value) < 0)
203 append(array, value);
204 }
205
206 void labs_append_unique(export string array[]; const string value)
207 {
208 if (find(array, value) < 0)
209 append(array, value);
210 }
211
212 void labs_append_unique(export string str; const string value)
213 {
214 if (find(str, value) < 0)
215 append(str, value);
216 }
217
218
219 // Make Array Unique
220
221 int[] labs_array_unique(const int array[])
222 {
223 int array_unique[] = {};
224
225 foreach (int value; array)
226 labs_append_unique(array_unique, value);
227
228 return array_unique;
229 }
230
231 float[] labs_array_unique(const float array[])
232 {
233 float array_unique[] = {};
234
235 foreach (float value; array)
236 labs_append_unique(array_unique, value);
237
238 return array_unique;
239 }
240
241 vector2[] labs_array_unique(const vector2 array[])
242 {
243 vector2 array_unique[] = {};
244
245 foreach (vector2 value; array)
246 labs_append_unique(array_unique, value);
247
248 return array_unique;
249 }
250
251 vector[] labs_array_unique(const vector array[])
252 {
253 vector array_unique[] = {};
254
255 foreach (vector value; array)
256 labs_append_unique(array_unique, value);
257
258 return array_unique;
259 }
260
261 vector4[] labs_array_unique(const vector4 array[])
262 {
263 vector4 array_unique[] = {};
264
265 foreach (vector4 value; array)
266 labs_append_unique(array_unique, value);
267
268 return array_unique;
269 }
270
271 string[] labs_array_unique(const string array[])
272 {
273 string array_unique[] = {};
274
275 foreach (string value; array)
276 labs_append_unique(array_unique, value);
277
278 return array_unique;
279 }
280
281 string labs_array_unique(const string str)
282 {
283 string str_unique = "";
284
285 foreach (string value; str)
286 labs_append_unique(str_unique, value);
287
288 return str_unique;
289 }
290
291
292 #endif