forked from pure-data/pure-data
/
ceammc_atomlist_view.h
558 lines (485 loc) · 17.6 KB
/
ceammc_atomlist_view.h
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*****************************************************************************
* Copyright 2020 Serge Poltavsky. All rights reserved.
*
* This file may be distributed under the terms of GNU Public License version
* 3 (GPL v3) as defined by the Free Software Foundation (FSF). A copy of the
* license should have been included with this file, or the project in which
* this file belongs to. You may also find the details of GPL v3 at:
* http://www.gnu.org/licenses/gpl-3.0.txt
*
* If you have any questions regarding the use of this file, feel free to
* contact the author of this file, or the owner of the project in which
* this file belongs to.
*****************************************************************************/
#ifndef CEAMMC_ATOMLIST_VIEW_H
#define CEAMMC_ATOMLIST_VIEW_H
#include "ceammc_atom.h"
#include <algorithm>
#include <functional>
#include <initializer_list>
#include <boost/container/small_vector.hpp>
#include <boost/optional.hpp>
namespace ceammc {
using MaybeFloat = boost::optional<t_float>;
class AtomList;
enum class AtomListMapType {
FILTER,
KEEP
};
class AtomListView {
const Atom* data_;
size_t n_;
public:
using iterator = const Atom*;
using const_iterator = const Atom*;
public:
AtomListView() noexcept;
AtomListView(const Atom* a, size_t n) noexcept;
AtomListView(const Atom& a) noexcept;
AtomListView(const AtomList& l) noexcept;
/**
* @note ceammc atom datatypes are not recognized:
* use only for raw pd datatypes
*/
AtomListView(const t_atom* a, size_t n) noexcept;
/**
* @param b - binbuf pointer
* @note ceammc atom datatypes are not recognized:
* use only for raw pd datatypes
*/
explicit AtomListView(const t_binbuf* b) noexcept;
void set(t_atom* a, size_t n);
void set(const Atom& a);
void set(const AtomList& l);
bool get(t_atom*& a, int& n);
bool operator==(const AtomListView& v) const;
bool operator!=(const AtomListView& v) const { return !operator==(v); }
bool operator==(const AtomList& l) const;
bool operator!=(const AtomList& l) const { return !operator==(l); }
bool operator==(bool v) const { return isBool() && v == asBool(); }
bool operator!=(bool v) const { return !operator==(v); }
bool operator==(t_float v) const;
bool operator!=(t_float v) const { return !operator==(v); }
bool operator==(int v) const { return isInteger() && asInt() == v; }
bool operator!=(int v) const { return !operator==(v); }
bool operator==(t_symbol* s) const { return isSymbol() && asSymbol() == s; }
bool operator!=(t_symbol* s) const { return !operator==(s); }
bool operator==(const Atom& a) const { return isAtom() && front() == a; }
bool operator!=(const Atom& a) const { return !operator==(a); }
// containter checks
inline bool empty() const { return n_ == 0; }
inline size_t size() const { return n_; }
inline bool isNull() const { return data_ == nullptr; }
// type checks
bool isBool() const;
bool isFloat() const { return data_ && n_ == 1 && atom().a_type == A_FLOAT; }
bool isInteger() const;
bool isSymbol() const { return data_ && n_ == 1 && atom().a_type == A_SYMBOL; }
bool isAtom() const { return data_ && n_ == 1; }
bool isData() const { return data_ && n_ == 1 && data_->isData(); }
bool isDataType(int t) const { return data_ && n_ == 1 && data_->isDataType(t); }
template <typename T>
bool isA() const { return n_ == 1 && data_ && data_->isA<T>(); }
/// value access
/**
* @brief returns pointer to element at specified relative position
* @param pos - positive value means position from the begining, negative position - from end.
* @example pos == -1 means last element (if exists)
* @return pointer to element, or NULL if no element at given position
*/
const Atom* relativeAt(int pos) const;
/**
* Same as at(), but values for index greater than the size of the
* List will be clipped to the last index.
* @param pos - position index
* @return pointer to element, or NULL if empty
*/
const Atom* clipAt(int pos) const;
/**
* Same as at(), but values for index greater than the size of the
* List will be wrapped around to 0.
* @param pos - position index
* @return pointer to element, or NULL if empty
*/
const Atom* wrapAt(int pos) const;
/**
* Same as at(), but values for index greater than the size of the
* List will be folded back.
* @param pos - position index
* @return pointer to element, or NULL if empty
*/
const Atom* foldAt(int pos) const;
/**
* Convert atomlist to parametrised type
* @throw std::logic_error on error
* @see Atom::asT
*/
template <typename T>
T asT() const
{
if (n_ == 1 && data_)
return data_->asT<T>();
else
throw std::logic_error("not a single atom list view");
}
/**
* Returns pointer to data
* @warning no type checks are done
*/
template <typename T>
const T* asD() const
{
if (n_ == 1 && data_)
return data_->asD<T>();
else
return nullptr;
}
/**
* Get list value
* @return def value if list contains other type then specified
*/
template <typename T>
T toT(T def) const
{
if (isA<T>())
return asT<T>();
else
return def;
}
// iterators
const_iterator begin() const { return data_; }
const_iterator end() const { return data_ + n_; }
// no range checks!!!
/** returns first atom in view, no range check */
const Atom& front() const { return *data_; }
/** returns last atom in view, no range check */
const Atom& back() const { return data_[n_ - 1]; }
/** returns atom at specified position in view, no range check */
const Atom& at(size_t pos) const { return data_[pos]; }
/** returns atom at specified position in view, no range check */
const Atom& operator[](size_t pos) const { return data_[pos]; }
const Atom& relativeAt(long pos) const;
inline bool asBool(bool def = false) const { return empty() ? def : front().asBool(def); }
inline t_symbol* asSymbol(t_symbol* def = &s_) const { return empty() ? def : front().asSymbol(def); }
inline t_float asFloat(t_float def = 0) const { return empty() ? def : front().asFloat(def); }
inline t_int asInt(t_int def = 0) const { return empty() ? def : static_cast<t_int>(front().asFloat(def)); }
// with range checks
/**
* Returns boolean value at specified position or default if not found
* @param pos - position
* @param def - default value if not found
* @return true or false
*/
bool boolAt(size_t pos, bool def) const;
/**
* Returns float value at specified position or default if not found
* @param pos - position
* @param def - default value if not found
* @return float value
*/
t_float floatAt(size_t pos, t_float def) const;
/**
* Returns int value at specified position or default if not found
* @param pos - position
* @param def - default value if not found
* @return int value
*/
int intAt(size_t pos, int def) const;
/**
* Returns symbol value at specified position or default if not found
* @param pos - position
* @param def - default value if not found
* @return symbol value
*/
t_symbol* symbolAt(size_t pos, t_symbol* def) const;
/**
* Returns atom value at specified position or default if not found
* @param pos - position
* @param def - default value if not found
* @return atom value
*/
const Atom& atomAt(size_t pos, const Atom& def) const { return pos >= n_ ? def : at(pos); }
/**
* Returns float at specified position that is greater
* then specified value or default if index out of range or value not satisfied
* @param pos - position
* @param min - lower bound
* @param def - default value if not found or not satisfied
* @return float value
*/
t_float floatGreaterThenAt(size_t pos, t_float min, t_float def) const;
/**
* Returns float at specified position that is greater
* or equal to specified value or default if index out of range or value not satisfied
* @param pos - position
* @param min - lower bound
* @param def - default value if not found or not satisfied
* @return float value
*/
t_float floatGreaterEqualAt(size_t pos, t_float min, t_float def) const;
/**
* Returns float at specified position that is less
* then specified value or default if index out of range or value not satisfied
* @param pos - position
* @param max - upper bound
* @param def - default value if not found or not satisfied
* @return float value
*/
t_float floatLessThenAt(size_t pos, t_float max, t_float def) const;
/**
* Returns float at specified position that is less
* or equal to specified value or default if index out of range or value not satisfied
* @param pos - position
* @param max - upper bound
* @param def - default value if not found or not satisfied
* @return int value
*/
t_float floatLessEqualAt(size_t pos, t_float max, t_float def) const;
/**
* Returns int at specified position that is greater
* then specified value or default if index out of range or value not satisfied
* @param pos - position
* @param min - lower bound
* @param def - default value if not found or not satisfied
* @return int value
*/
t_int intGreaterThenAt(size_t pos, t_int min, t_int def) const;
/**
* Returns int at specified position that is greater
* or equal to specified value or default if index out of range or value not satisfied
* @param pos - position
* @param min - lower bound
* @param def - default value if not found or not satisfied
* @return int value
*/
t_int intGreaterEqualAt(size_t pos, t_int min, t_int def) const;
/**
* Returns int at specified position that is less
* then specified value or default if index out of range or value not satisfied
* @param pos - position
* @param max - upper bound
* @param def - default value if not found or not satisfied
* @return int value
*/
t_int intLessThenAt(size_t pos, t_int max, t_int def) const;
/**
* Returns int at specified position that is less
* or equal to specified value or default if index out of range or value not satisfied
* @param pos - position
* @param max - upper bound
* @param def - default value if not found or not satisfied
* @return int value
*/
t_int intLessEqualAt(size_t pos, t_int max, t_int def) const;
/**
* Returns int at specified position that is lin specified closed range
* or default if index out of range or value not satisfied
* @param pos - position
* @param min - lower bound
* @param max - upper bound
* @param def - default value if not found or not satisfied
* @return int value
*/
t_int intInClosedIntervalAt(size_t pos, t_int min, t_int max, t_int def) const;
/**
* Returns subview from specified position
*/
AtomListView subView(size_t from) const;
/**
* Returns subview from specified position and length
*/
AtomListView subView(size_t from, size_t len) const;
/**
* Returns subview form specified position and before first property element
*/
AtomListView argSubView(size_t from) const;
/**
* Returns true if contains specified atom, otherwise false
* @param a - atom value
*/
bool contains(const Atom& a) const;
/**
* Check if all atoms satisfies to predicate
* @param pred - predicate function
* @return true if for all atoms predicate returns true
*/
bool allOf(std::function<bool(const Atom&)> pred) const;
/**
* Check if at least one atom satisfies to predicate
* @param pred - predicate function
* @return true if for at least one atom predicate returns true
*/
bool anyOf(std::function<bool(const Atom&)> pred) const;
/**
* Check if no one atom satisfies to predicate
* @param pred - predicate function
* @return true if for all atoms predicate returns false
*/
bool noneOf(std::function<bool(const Atom&)> pred) const;
friend class AtomList;
/** pointer to pd raw t_atoms */
t_atom* toPdData() const { return reinterpret_cast<t_atom*>(const_cast<Atom*>(data_)); }
/**
* Returns sum of floats in list or boost::none if empty
*/
MaybeFloat sum() const noexcept;
/**
* Returns product of floats in list or boost::none if empty
*/
MaybeFloat product() const noexcept;
/**
* Reduce to t_float
* @param init - init value
* @param fn - reduce function
* @return - result, or boost::none if no floats in list
*/
MaybeFloat reduceFloat(t_float init, std::function<t_float(t_float, t_float)> fn) const;
/**
* Get min and max elements from list
* @param min - min destination
* @param max - max destination
* @return false if empty
*/
bool range(Atom& min, Atom& max) const noexcept;
/**
* Map atom values
* @param fn - atom map function
* @param out - map output
*/
template <typename T>
void map(const AtomMapFunction& fn, T& out) const
{
if (!fn)
return;
out.reserve(n_);
for (auto& a : *this)
out.push_back(fn(a));
}
/**
* Map float values
* @param fn - function
* @param out - map output
* @param t - AtomListMapType::KEEP - non-float values are left untouched,
* if AtomListMapType::FILTER - non-float values are removed
*/
template <typename T>
void mapFloat(const FloatMapFunction& fn, T& out, AtomListMapType t = AtomListMapType::KEEP) const
{
if (!fn)
return;
out.reserve(n_);
for (auto& a : *this) {
if (a.isFloat())
out.push_back(fn(a.asT<t_float>()));
else if (t == AtomListMapType::KEEP)
out.push_back(a);
}
}
/**
* Map symbol values
* @param fn - function
* @param out - map output
* @param t - AtomListMapType::KEEP - non-symbol values are left untouched,
* if AtomListMapType::FILTER - non-symbol values are removed
*/
template <typename T>
void mapSymbol(const SymbolMapFunction& fn, T& out, AtomListMapType t = AtomListMapType::KEEP) const
{
if (!fn)
return;
out.reserve(n_);
for (auto& a : *this) {
if (a.isSymbol())
out.push_back(fn(a.asT<t_symbol*>()));
else if (t == AtomListMapType::KEEP)
out.push_back(a);
}
}
/**
* Select values for which atom predicate returns true
*/
template <typename T>
void filter(const AtomPredicate& fn, T& out) const
{
out.reserve(n_);
for (auto& a : *this) {
if (!fn || fn(a))
out.push_back(a);
}
}
/**
* Select values for which float predicate returns true
*/
template <typename T>
void filterFloat(const FloatPredicate& fn, T& out) const
{
out.reserve(n_);
for (auto& a : *this) {
if (!fn || (a.isFloat() && fn(a.asT<t_float>())))
out.push_back(a);
}
}
/**
* Select values for which symbol predicate returns true
*/
template <typename T>
void filterSymbol(const SymbolPredicate& fn, T& out) const
{
out.reserve(n_);
for (auto& a : *this) {
if (!fn || (a.isSymbol() && fn(a.asT<t_symbol*>())))
out.push_back(a);
}
}
/**
* search given property and return subview with property value
* @param name - target property name
* @param res - result to store
* @return if property was found
*/
bool getProperty(t_symbol* name, AtomListView& res) const;
enum class InvalidDollarArgPolicy {
KEEP_RAW,
DEFAULT_ARG,
DROP_ARG
};
/**
* @brief expandDollars
* @param cnv
* @param res - result to append atoms (not cleared!)
* @param policy - what todo if dollar argument not exists in canvas
* @return
*/
bool expandDollarArgs(const t_canvas* cnv,
AtomList& res,
InvalidDollarArgPolicy policy = InvalidDollarArgPolicy::DEFAULT_ARG,
const Atom& def = Atom(0.)) const;
/**
* Convert symbols to Atom primitives:
* @example ";" goes to a Atom::SEMICOLON, "," to Atom::COMMA, "$.." to
* Atom::DOLLAR or Atom::DOLLAR_SYMBOL
* @param res - result to append atoms (not cleared!)
*/
void restorePrimitives(AtomList& res) const;
/**
* Split view by specified atom
*/
void split(const Atom& sep, const std::function<void(const AtomListView&)>& msg) const;
private:
inline const t_atom& atom() const { return data_->atom(); }
};
std::ostream& operator<<(std::ostream& os, const AtomListView& l);
template <>
inline bool AtomListView::isA<bool>() const { return isBool(); }
template <>
inline bool AtomListView::isA<t_float>() const { return isFloat(); }
template <>
inline bool AtomListView::isA<int>() const { return isInteger(); }
template <>
inline bool AtomListView::isA<t_symbol*>() const { return isSymbol(); }
template <>
inline bool AtomListView::isA<Atom>() const { return isAtom(); }
template <>
inline bool AtomListView::isA<AtomList>() const { return true; }
}
#endif // CEAMMC_ATOMLIST_VIEW_H