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" |
17 |
#include "ScriptVM.h" |
#include "ScriptVM.h" |
18 |
|
#include "../common/RTMath.h" |
19 |
|
|
20 |
namespace LinuxSampler { |
namespace LinuxSampler { |
21 |
|
|
72 |
VMFnResult* CoreVMFunction_message::exec(VMFnArgs* args) { |
VMFnResult* CoreVMFunction_message::exec(VMFnArgs* args) { |
73 |
if (!args->argsCount()) return errorResult(); |
if (!args->argsCount()) return errorResult(); |
74 |
|
|
75 |
|
uint64_t usecs = RTMath::unsafeMicroSeconds(RTMath::real_clock); |
76 |
|
|
77 |
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(args->arg(0)); |
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(args->arg(0)); |
78 |
if (strExpr) { |
if (strExpr) { |
79 |
std::cout << "[ScriptVM] " << strExpr->evalStr() << "\n"; |
printf("[ScriptVM %.3f] %s\n", usecs/1000000.f, strExpr->evalStr().c_str()); |
80 |
return successResult(); |
return successResult(); |
81 |
} |
} |
82 |
|
|
83 |
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
84 |
if (intExpr) { |
if (intExpr) { |
85 |
std::cout << "[ScriptVM] " << intExpr->evalInt() << "\n"; |
printf("[ScriptVM %.3f] %d\n", usecs/1000000.f, intExpr->evalInt()); |
86 |
return successResult(); |
return successResult(); |
87 |
} |
} |
88 |
|
|
234 |
return successResult(l > r ? l : r); |
return successResult(l > r ? l : r); |
235 |
} |
} |
236 |
|
|
237 |
|
/////////////////////////////////////////////////////////////////////////// |
238 |
|
// built-in script function: array_equal() |
239 |
|
|
240 |
|
VMFnResult* CoreVMFunction_array_equal::exec(VMFnArgs* args) { |
241 |
|
VMIntArrayExpr* l = args->arg(0)->asIntArray(); |
242 |
|
VMIntArrayExpr* r = args->arg(1)->asIntArray(); |
243 |
|
if (l->arraySize() != r->arraySize()) { |
244 |
|
wrnMsg("array_equal(): the two arrays differ in size"); |
245 |
|
return successResult(0); // false |
246 |
|
} |
247 |
|
const int n = l->arraySize(); |
248 |
|
for (int i = 0; i < n; ++i) |
249 |
|
if (l->evalIntElement(i) != r->evalIntElement(i)) |
250 |
|
return successResult(0); // false |
251 |
|
return successResult(1); // true |
252 |
|
} |
253 |
|
|
254 |
|
/////////////////////////////////////////////////////////////////////////// |
255 |
|
// built-in script function: search() |
256 |
|
|
257 |
|
ExprType_t CoreVMFunction_search::argType(int iArg) const { |
258 |
|
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
259 |
|
} |
260 |
|
|
261 |
|
bool CoreVMFunction_search::acceptsArgType(int iArg, ExprType_t type) const { |
262 |
|
if (iArg == 0) |
263 |
|
return type == INT_ARR_EXPR; |
264 |
|
else |
265 |
|
return type == INT_EXPR; |
266 |
|
} |
267 |
|
|
268 |
|
VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) { |
269 |
|
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
270 |
|
const int needle = args->arg(1)->asInt()->evalInt(); |
271 |
|
const int n = a->arraySize(); |
272 |
|
for (int i = 0; i < n; ++i) |
273 |
|
if (a->evalIntElement(i) == needle) |
274 |
|
return successResult(i); |
275 |
|
return successResult(-1); // not found |
276 |
|
} |
277 |
|
|
278 |
|
/////////////////////////////////////////////////////////////////////////// |
279 |
|
// built-in script function: sort() |
280 |
|
|
281 |
|
ExprType_t CoreVMFunction_sort::argType(int iArg) const { |
282 |
|
return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR; |
283 |
|
} |
284 |
|
|
285 |
|
bool CoreVMFunction_sort::acceptsArgType(int iArg, ExprType_t type) const { |
286 |
|
if (iArg == 0) |
287 |
|
return type == INT_ARR_EXPR; |
288 |
|
else |
289 |
|
return type == INT_EXPR; |
290 |
|
} |
291 |
|
|
292 |
|
struct ArrElemPOD { |
293 |
|
VMIntArrayExpr* m_array; |
294 |
|
int m_index; |
295 |
|
}; |
296 |
|
|
297 |
|
static inline void swap(class ArrElemRef a, class ArrElemRef b); |
298 |
|
|
299 |
|
class ArrElemRef : protected ArrElemPOD { |
300 |
|
public: |
301 |
|
ArrElemRef() { |
302 |
|
m_array = NULL; |
303 |
|
m_index = 0; |
304 |
|
} |
305 |
|
ArrElemRef(VMIntArrayExpr* a, int index) { |
306 |
|
m_array = a; |
307 |
|
m_index = index; |
308 |
|
} |
309 |
|
inline ArrElemRef& operator=(const ArrElemRef& e) { |
310 |
|
setValue(e.getValue()); |
311 |
|
return *this; |
312 |
|
} |
313 |
|
inline ArrElemRef& operator=(int val) { |
314 |
|
setValue(val); |
315 |
|
return *this; |
316 |
|
} |
317 |
|
inline bool operator==(const ArrElemRef& e) const { |
318 |
|
if (m_index == e.m_index) |
319 |
|
return true; |
320 |
|
return getValue() == e.getValue(); |
321 |
|
} |
322 |
|
inline bool operator==(int val) const { |
323 |
|
return getValue() == val; |
324 |
|
} |
325 |
|
inline bool operator!=(const ArrElemRef& e) const { |
326 |
|
return !(operator==(e)); |
327 |
|
} |
328 |
|
inline bool operator!=(int val) const { |
329 |
|
return !(operator==(val)); |
330 |
|
} |
331 |
|
inline bool operator<(const ArrElemRef& e) const { |
332 |
|
if (m_index == e.m_index) |
333 |
|
return false; |
334 |
|
return getValue() < e.getValue(); |
335 |
|
} |
336 |
|
inline bool operator<(int val) const { |
337 |
|
return getValue() < val; |
338 |
|
} |
339 |
|
inline bool operator>(const ArrElemRef& e) const { |
340 |
|
if (m_index == e.m_index) |
341 |
|
return false; |
342 |
|
return getValue() > e.getValue(); |
343 |
|
} |
344 |
|
inline bool operator>(int val) const { |
345 |
|
return getValue() > val; |
346 |
|
} |
347 |
|
inline bool operator<=(const ArrElemRef& e) const { |
348 |
|
if (m_index == e.m_index) |
349 |
|
return true; |
350 |
|
return getValue() <= e.getValue(); |
351 |
|
} |
352 |
|
inline bool operator<=(int val) const { |
353 |
|
return getValue() <= val; |
354 |
|
} |
355 |
|
inline bool operator>=(const ArrElemRef& e) const { |
356 |
|
if (m_index == e.m_index) |
357 |
|
return true; |
358 |
|
return getValue() >= e.getValue(); |
359 |
|
} |
360 |
|
inline bool operator>=(int val) const { |
361 |
|
return getValue() >= val; |
362 |
|
} |
363 |
|
inline operator int() const { |
364 |
|
return getValue(); |
365 |
|
} |
366 |
|
protected: |
367 |
|
inline int getValue() const { |
368 |
|
return m_array->evalIntElement(m_index); |
369 |
|
} |
370 |
|
inline void setValue(int value) { |
371 |
|
m_array->assignIntElement(m_index, value); |
372 |
|
} |
373 |
|
|
374 |
|
friend void swap(class ArrElemRef a, class ArrElemRef b); |
375 |
|
}; |
376 |
|
|
377 |
|
class ArrElemPtr : protected ArrElemPOD { |
378 |
|
public: |
379 |
|
ArrElemPtr() { |
380 |
|
m_array = NULL; |
381 |
|
m_index = 0; |
382 |
|
} |
383 |
|
ArrElemPtr(VMIntArrayExpr* a, int index) { |
384 |
|
m_array = a; |
385 |
|
m_index = index; |
386 |
|
} |
387 |
|
inline ArrElemRef operator*() { |
388 |
|
return *(ArrElemRef*)this; |
389 |
|
} |
390 |
|
}; |
391 |
|
|
392 |
|
static inline void swap(ArrElemRef a, ArrElemRef b) { |
393 |
|
int valueA = a.getValue(); |
394 |
|
int valueB = b.getValue(); |
395 |
|
a.setValue(valueB); |
396 |
|
b.setValue(valueA); |
397 |
|
} |
398 |
|
|
399 |
|
class ArrExprIter : public ArrElemPOD { |
400 |
|
public: |
401 |
|
typedef std::random_access_iterator_tag iterator_category; |
402 |
|
typedef int value_type; |
403 |
|
typedef ssize_t difference_type; |
404 |
|
typedef ArrElemPtr pointer; |
405 |
|
typedef ArrElemRef reference; |
406 |
|
|
407 |
|
ArrExprIter(VMIntArrayExpr* a, int index) { |
408 |
|
m_array = a; |
409 |
|
m_index = index; |
410 |
|
} |
411 |
|
inline ArrElemRef operator*() { |
412 |
|
return *(ArrElemRef*)this; |
413 |
|
} |
414 |
|
inline ArrExprIter& operator++() { // prefix increment |
415 |
|
++m_index; |
416 |
|
return *this; |
417 |
|
} |
418 |
|
inline ArrExprIter& operator--() { // prefix decrement |
419 |
|
--m_index; |
420 |
|
return *this; |
421 |
|
} |
422 |
|
inline ArrExprIter operator++(int) { // postfix increment |
423 |
|
ArrExprIter it = *this; |
424 |
|
++m_index; |
425 |
|
return it; |
426 |
|
} |
427 |
|
inline ArrExprIter operator--(int) { // postfix decrement |
428 |
|
ArrExprIter it = *this; |
429 |
|
--m_index; |
430 |
|
return it; |
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 bool 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 difference_type operator-(const ArrExprIter& other) const { |
454 |
|
return m_index - other.m_index; |
455 |
|
} |
456 |
|
inline ArrExprIter operator-(difference_type d) const { |
457 |
|
return ArrExprIter(m_array, m_index - d); |
458 |
|
} |
459 |
|
inline ArrExprIter operator+(difference_type d) const { |
460 |
|
return ArrExprIter(m_array, m_index + d); |
461 |
|
} |
462 |
|
inline ArrExprIter operator*(difference_type factor) const { |
463 |
|
return ArrExprIter(m_array, m_index * factor); |
464 |
|
} |
465 |
|
}; |
466 |
|
|
467 |
|
struct DescArrExprSorter { |
468 |
|
inline bool operator()(const int& a, const int& b) const { |
469 |
|
return a > b; |
470 |
|
} |
471 |
|
}; |
472 |
|
|
473 |
|
VMFnResult* CoreVMFunction_sort::exec(VMFnArgs* args) { |
474 |
|
VMIntArrayExpr* a = args->arg(0)->asIntArray(); |
475 |
|
bool bAscending = |
476 |
|
(args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt(); |
477 |
|
int n = a->arraySize(); |
478 |
|
ArrExprIter itBegin(a, 0); |
479 |
|
ArrExprIter itEnd(a, n); |
480 |
|
if (bAscending) { |
481 |
|
std::sort(itBegin, itEnd); |
482 |
|
} else { |
483 |
|
DescArrExprSorter sorter; |
484 |
|
std::sort(itBegin, itEnd, sorter); |
485 |
|
} |
486 |
|
return successResult(); |
487 |
|
} |
488 |
|
|
489 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |