1 |
/* |
/* |
2 |
* Copyright (c) 2014-2015 Christian Schoenebeck |
* Copyright (c) 2014-2017 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
10 |
#include "CoreVMFunctions.h" |
#include "CoreVMFunctions.h" |
11 |
|
|
12 |
#include <iostream> |
#include <iostream> |
13 |
|
#include <algorithm> // for std::sort() |
14 |
#include <math.h> |
#include <math.h> |
15 |
#include <stdlib.h> |
#include <stdlib.h> |
16 |
#include "tree.h" |
#include "tree.h" |
231 |
return successResult(l > r ? l : r); |
return successResult(l > r ? l : r); |
232 |
} |
} |
233 |
|
|
234 |
|
/////////////////////////////////////////////////////////////////////////// |
235 |
|
// built-in script function: array_equal() |
236 |
|
|
237 |
|
VMFnResult* CoreVMFunction_array_equal::exec(VMFnArgs* args) { |
238 |
|
VMIntArrayExpr* l = args->arg(0)->asIntArray(); |
239 |
|
VMIntArrayExpr* r = args->arg(1)->asIntArray(); |
240 |
|
if (l->arraySize() != r->arraySize()) { |
241 |
|
wrnMsg("array_equal(): the two arrays differ in size"); |
242 |
|
return successResult(0); // false |
243 |
|
} |
244 |
|
const int n = l->arraySize(); |
245 |
|
for (int i = 0; i < n; ++i) |
246 |
|
if (l->evalIntElement(i) != r->evalIntElement(i)) |
247 |
|
return successResult(0); // false |
248 |
|
return successResult(1); // true |
249 |
|
} |
250 |
|
|
251 |
|
/////////////////////////////////////////////////////////////////////////// |
252 |
|
// built-in script function: search() |
253 |
|
|
254 |
|
ExprType_t CoreVMFunction_search::argType(int iArg) const { |
255 |
|
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
256 |
|
} |
257 |
|
|
258 |
|
bool CoreVMFunction_search::acceptsArgType(int iArg, ExprType_t type) const { |
259 |
|
if (iArg == 0) |
260 |
|
return type == INT_ARR_EXPR; |
261 |
|
else |
262 |
|
return type == INT_EXPR; |
263 |
|
} |
264 |
|
|
265 |
|
VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) { |
266 |
|
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
267 |
|
const int needle = args->arg(1)->asInt()->evalInt(); |
268 |
|
const int n = a->arraySize(); |
269 |
|
for (int i = 0; i < n; ++i) |
270 |
|
if (a->evalIntElement(i) == needle) |
271 |
|
return successResult(i); |
272 |
|
return successResult(-1); // not found |
273 |
|
} |
274 |
|
|
275 |
|
/////////////////////////////////////////////////////////////////////////// |
276 |
|
// built-in script function: sort() |
277 |
|
|
278 |
|
ExprType_t CoreVMFunction_sort::argType(int iArg) const { |
279 |
|
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
280 |
|
} |
281 |
|
|
282 |
|
bool CoreVMFunction_sort::acceptsArgType(int iArg, ExprType_t type) const { |
283 |
|
if (iArg == 0) |
284 |
|
return type == INT_ARR_EXPR; |
285 |
|
else |
286 |
|
return type == INT_EXPR; |
287 |
|
} |
288 |
|
|
289 |
|
struct ArrElemPOD { |
290 |
|
VMIntArrayExpr* m_array; |
291 |
|
int m_index; |
292 |
|
}; |
293 |
|
|
294 |
|
static inline void swap(class ArrElemRef a, class ArrElemRef b); |
295 |
|
|
296 |
|
class ArrElemRef : protected ArrElemPOD { |
297 |
|
public: |
298 |
|
ArrElemRef() { |
299 |
|
m_array = NULL; |
300 |
|
m_index = 0; |
301 |
|
} |
302 |
|
ArrElemRef(VMIntArrayExpr* a, int index) { |
303 |
|
m_array = a; |
304 |
|
m_index = index; |
305 |
|
} |
306 |
|
inline ArrElemRef& operator=(const ArrElemRef& e) { |
307 |
|
setValue(e.getValue()); |
308 |
|
return *this; |
309 |
|
} |
310 |
|
inline ArrElemRef& operator=(int val) { |
311 |
|
setValue(val); |
312 |
|
return *this; |
313 |
|
} |
314 |
|
inline bool operator==(const ArrElemRef& e) const { |
315 |
|
if (m_index == e.m_index) |
316 |
|
return true; |
317 |
|
return getValue() == e.getValue(); |
318 |
|
} |
319 |
|
inline bool operator==(int val) const { |
320 |
|
return getValue() == val; |
321 |
|
} |
322 |
|
inline bool operator!=(const ArrElemRef& e) const { |
323 |
|
return !(operator==(e)); |
324 |
|
} |
325 |
|
inline bool operator!=(int val) const { |
326 |
|
return !(operator==(val)); |
327 |
|
} |
328 |
|
inline bool operator<(const ArrElemRef& e) const { |
329 |
|
if (m_index == e.m_index) |
330 |
|
return false; |
331 |
|
return getValue() < e.getValue(); |
332 |
|
} |
333 |
|
inline bool operator<(int val) const { |
334 |
|
return getValue() < val; |
335 |
|
} |
336 |
|
inline bool operator>(const ArrElemRef& e) const { |
337 |
|
if (m_index == e.m_index) |
338 |
|
return false; |
339 |
|
return getValue() > e.getValue(); |
340 |
|
} |
341 |
|
inline bool operator>(int val) const { |
342 |
|
return getValue() > val; |
343 |
|
} |
344 |
|
inline bool operator<=(const ArrElemRef& e) const { |
345 |
|
if (m_index == e.m_index) |
346 |
|
return true; |
347 |
|
return getValue() <= e.getValue(); |
348 |
|
} |
349 |
|
inline bool operator<=(int val) const { |
350 |
|
return getValue() <= val; |
351 |
|
} |
352 |
|
inline bool operator>=(const ArrElemRef& e) const { |
353 |
|
if (m_index == e.m_index) |
354 |
|
return true; |
355 |
|
return getValue() >= e.getValue(); |
356 |
|
} |
357 |
|
inline bool operator>=(int val) const { |
358 |
|
return getValue() >= val; |
359 |
|
} |
360 |
|
inline operator int() const { |
361 |
|
return getValue(); |
362 |
|
} |
363 |
|
protected: |
364 |
|
inline int getValue() const { |
365 |
|
return m_array->evalIntElement(m_index); |
366 |
|
} |
367 |
|
inline void setValue(int value) { |
368 |
|
m_array->assignIntElement(m_index, value); |
369 |
|
} |
370 |
|
|
371 |
|
friend void swap(class ArrElemRef a, class ArrElemRef b); |
372 |
|
}; |
373 |
|
|
374 |
|
class ArrElemPtr : protected ArrElemPOD { |
375 |
|
public: |
376 |
|
ArrElemPtr() { |
377 |
|
m_array = NULL; |
378 |
|
m_index = 0; |
379 |
|
} |
380 |
|
ArrElemPtr(VMIntArrayExpr* a, int index) { |
381 |
|
m_array = a; |
382 |
|
m_index = index; |
383 |
|
} |
384 |
|
inline ArrElemRef operator*() { |
385 |
|
return *(ArrElemRef*)this; |
386 |
|
} |
387 |
|
}; |
388 |
|
|
389 |
|
static inline void swap(ArrElemRef a, ArrElemRef b) { |
390 |
|
int valueA = a.getValue(); |
391 |
|
int valueB = b.getValue(); |
392 |
|
a.setValue(valueB); |
393 |
|
b.setValue(valueA); |
394 |
|
} |
395 |
|
|
396 |
|
class ArrExprIter : public ArrElemPOD { |
397 |
|
public: |
398 |
|
typedef std::random_access_iterator_tag iterator_category; |
399 |
|
typedef int value_type; |
400 |
|
typedef ssize_t difference_type; |
401 |
|
typedef ArrElemPtr pointer; |
402 |
|
typedef ArrElemRef reference; |
403 |
|
|
404 |
|
ArrExprIter(VMIntArrayExpr* a, int index) { |
405 |
|
m_array = a; |
406 |
|
m_index = index; |
407 |
|
} |
408 |
|
inline ArrElemRef operator*() { |
409 |
|
return *(ArrElemRef*)this; |
410 |
|
} |
411 |
|
inline ArrExprIter& operator++() { // prefix increment |
412 |
|
++m_index; |
413 |
|
return *this; |
414 |
|
} |
415 |
|
inline ArrExprIter& operator--() { // prefix decrement |
416 |
|
--m_index; |
417 |
|
return *this; |
418 |
|
} |
419 |
|
inline ArrExprIter operator++(int) { // postfix increment |
420 |
|
ArrExprIter it = *this; |
421 |
|
++m_index; |
422 |
|
return it; |
423 |
|
} |
424 |
|
inline ArrExprIter operator--(int) { // postfix decrement |
425 |
|
ArrExprIter it = *this; |
426 |
|
--m_index; |
427 |
|
return it; |
428 |
|
} |
429 |
|
inline bool operator==(const ArrExprIter& other) const { |
430 |
|
return m_index == other.m_index; |
431 |
|
} |
432 |
|
inline bool operator!=(const ArrExprIter& other) const { |
433 |
|
return m_index != other.m_index; |
434 |
|
} |
435 |
|
inline bool operator<(const ArrExprIter& other) const { |
436 |
|
return m_index < other.m_index; |
437 |
|
} |
438 |
|
inline bool operator>(const ArrExprIter& other) const { |
439 |
|
return m_index > other.m_index; |
440 |
|
} |
441 |
|
inline bool operator<=(const ArrExprIter& other) const { |
442 |
|
return m_index <= other.m_index; |
443 |
|
} |
444 |
|
inline bool operator>=(const ArrExprIter& other) const { |
445 |
|
return m_index >= other.m_index; |
446 |
|
} |
447 |
|
inline difference_type operator+(const ArrExprIter& other) const { |
448 |
|
return m_index + other.m_index; |
449 |
|
} |
450 |
|
inline difference_type operator-(const ArrExprIter& other) const { |
451 |
|
return m_index - other.m_index; |
452 |
|
} |
453 |
|
inline ArrExprIter operator-(difference_type d) const { |
454 |
|
return ArrExprIter(m_array, m_index - d); |
455 |
|
} |
456 |
|
inline ArrExprIter operator+(difference_type d) const { |
457 |
|
return ArrExprIter(m_array, m_index + d); |
458 |
|
} |
459 |
|
inline ArrExprIter operator*(difference_type factor) const { |
460 |
|
return ArrExprIter(m_array, m_index * factor); |
461 |
|
} |
462 |
|
}; |
463 |
|
|
464 |
|
struct DescArrExprSorter { |
465 |
|
inline bool operator()(const int& a, const int& b) const { |
466 |
|
return a > b; |
467 |
|
} |
468 |
|
}; |
469 |
|
|
470 |
|
VMFnResult* CoreVMFunction_sort::exec(VMFnArgs* args) { |
471 |
|
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
472 |
|
bool bAscending = |
473 |
|
(args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt(); |
474 |
|
int n = a->arraySize(); |
475 |
|
ArrExprIter itBegin(a, 0); |
476 |
|
ArrExprIter itEnd(a, n); |
477 |
|
if (bAscending) { |
478 |
|
std::sort(itBegin, itEnd); |
479 |
|
} else { |
480 |
|
DescArrExprSorter sorter; |
481 |
|
std::sort(itBegin, itEnd, sorter); |
482 |
|
} |
483 |
|
return successResult(); |
484 |
|
} |
485 |
|
|
486 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |