forked from pure-data/pure-data
/
ceammc_atomlist_view.cpp
474 lines (386 loc) · 10.1 KB
/
ceammc_atomlist_view.cpp
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
/*****************************************************************************
* 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.
*****************************************************************************/
#include "ceammc_atomlist_view.h"
#include "ceammc_atomlist.h"
#include "ceammc_convert.h"
#include "ceammc_numeric.h"
#include <cmath>
#include <functional>
namespace ceammc {
AtomListView::AtomListView() noexcept
: data_(nullptr)
, n_(0)
{
}
AtomListView::AtomListView(const t_atom* a, size_t n) noexcept
: data_(n ? reinterpret_cast<const Atom*>(a) : nullptr)
, n_(n)
{
}
AtomListView::AtomListView(const Atom* a, size_t n) noexcept
: data_(n ? a : nullptr)
, n_(n)
{
}
AtomListView::AtomListView(const Atom& a) noexcept
: data_(&a)
, n_(1)
{
}
AtomListView::AtomListView(const AtomList& l) noexcept
: AtomListView(l.toPdData(), l.size())
{
}
AtomListView::AtomListView(const t_binbuf* b) noexcept
{
if (b) {
n_ = binbuf_getnatom(b);
data_ = n_ ? reinterpret_cast<const Atom*>(binbuf_getvec(b)) : nullptr;
}
}
void AtomListView::set(t_atom* a, size_t n)
{
data_ = reinterpret_cast<Atom*>(a);
n_ = n;
}
void AtomListView::set(const Atom& a)
{
data_ = &a;
n_ = 1;
}
void AtomListView::set(const AtomList& l)
{
set(l.toPdData(), l.size());
}
bool AtomListView::get(t_atom*& a, int& n)
{
if (!data_)
return false;
a = const_cast<t_atom*>(&data_->atom());
n = static_cast<int>(n_);
return true;
}
bool AtomListView::operator==(const AtomList& l) const
{
if (n_ != l.size())
return false;
if (l.atoms_.data() == data_)
return true;
for (size_t i = 0; i < n_; i++) {
if (data_[i] != l[i])
return false;
}
return true;
}
bool AtomListView::operator==(t_float v) const
{
return isFloat() && math::float_compare(asFloat(), v);
}
bool AtomListView::operator==(const AtomListView& v) const
{
if (v.n_ != n_)
return false;
if (v.data_ == data_)
return true;
if (data_ == nullptr)
return true;
for (size_t i = 0; i < n_; i++) {
if (data_[i] != v.data_[i])
return false;
}
return true;
}
bool AtomListView::isBool() const
{
return (isFloat() && (asFloat() == 1 || asFloat() == 0))
|| ((isSymbol() && (asSymbol() == gensym("true") || asSymbol() == gensym("false"))));
}
bool AtomListView::isInteger() const
{
return isFloat() && math::is_integer(asFloat());
}
const Atom& AtomListView::relativeAt(long pos) const
{
return at(relativeIndex(pos, n_));
}
bool AtomListView::boolAt(size_t pos, bool def) const
{
if (pos >= n_)
return def;
else
return at(pos).asBool(def);
}
t_float AtomListView::floatAt(size_t pos, t_float def) const
{
if (pos >= n_)
return def;
else
return at(pos).asFloat(def);
}
int AtomListView::intAt(size_t pos, int def) const
{
if (pos >= n_)
return def;
else
return at(pos).asInt(def);
}
t_symbol* AtomListView::symbolAt(size_t pos, t_symbol* def) const
{
if (pos >= n_)
return def;
else if (at(pos).isSymbol())
return at(pos).asSymbol();
else
return def;
}
t_float AtomListView::floatGreaterThenAt(size_t pos, t_float min, t_float def) const
{
if (pos >= n_)
return def;
else
return at(pos).asFloatGreaterThen(min, def);
}
t_float AtomListView::floatGreaterEqualAt(size_t pos, t_float min, t_float def) const
{
if (pos >= n_)
return def;
else
return at(pos).asFloatGreaterEqual(min, def);
}
t_float AtomListView::floatLessThenAt(size_t pos, t_float max, t_float def) const
{
if (pos >= n_)
return def;
else
return at(pos).asFloatLessThen(max, def);
}
t_float AtomListView::floatLessEqualAt(size_t pos, t_float max, t_float def) const
{
if (pos >= n_)
return def;
else
return at(pos).asFloatLessEqual(max, def);
}
t_int AtomListView::intGreaterThenAt(size_t pos, t_int min, t_int def) const
{
if (pos >= n_)
return def;
else
return at(pos).asIntGreaterThen(min, def);
}
t_int AtomListView::intGreaterEqualAt(size_t pos, t_int min, t_int def) const
{
if (pos >= n_)
return def;
else
return at(pos).asIntGreaterEqual(min, def);
}
t_int AtomListView::intLessThenAt(size_t pos, t_int max, t_int def) const
{
if (pos >= n_)
return def;
else
return at(pos).asIntLessThen(max, def);
}
t_int AtomListView::intLessEqualAt(size_t pos, t_int max, t_int def) const
{
if (pos >= n_)
return def;
else
return at(pos).asIntLessEqual(max, def);
}
t_int AtomListView::intInClosedIntervalAt(size_t pos, t_int min, t_int max, t_int def) const
{
if (pos >= n_)
return def;
else
return at(pos).asIntInClosedInterval(min, max, def);
}
AtomListView AtomListView::subView(size_t from) const
{
return subView(from, n_);
}
AtomListView AtomListView::subView(size_t from, size_t len) const
{
if (!data_)
return {};
if (from >= n_)
return {};
return AtomListView(&data_[from].atom(), std::min(n_ - from, len));
}
AtomListView AtomListView::argSubView(size_t from) const
{
if (!data_)
return {};
if (from >= n_)
return {};
for (size_t i = 0; i < n_; i++) {
auto& a = data_[i];
if (a.isProperty())
return (i < from) ? AtomListView {} : AtomListView(&data_[from].atom(), i - from);
}
return AtomListView(&data_[from].atom(), n_ - from);
}
bool AtomListView::contains(const Atom& a) const
{
if (empty() || isNull())
return false;
return std::find(data_, data_ + n_, a) != (data_ + n_);
}
bool AtomListView::allOf(std::function<bool(const Atom&)> pred) const
{
return std::all_of(begin(), end(), pred);
}
bool AtomListView::anyOf(std::function<bool(const Atom&)> pred) const
{
return std::any_of(begin(), end(), pred);
}
bool AtomListView::noneOf(std::function<bool(const Atom&)> pred) const
{
return std::none_of(begin(), end(), pred);
}
MaybeFloat AtomListView::sum() const noexcept
{
return reduceFloat(0, [](t_float a, t_float b) { return a + b; });
}
MaybeFloat AtomListView::product() const noexcept
{
return reduceFloat(1, [](t_float a, t_float b) { return a * b; });
}
MaybeFloat AtomListView::reduceFloat(t_float init, std::function<t_float(t_float, t_float)> fn) const
{
t_float accum = init;
size_t n = 0;
for (size_t i = 0; i < n_; i++) {
auto& el = data_[i];
if (el.isFloat()) {
accum = fn(accum, el.asFloat());
n++;
}
}
return (n > 0) ? MaybeFloat(accum) : boost::none;
}
const Atom* AtomListView::relativeAt(int pos) const
{
auto idx = relativeIndex<int>(pos, n_);
if (idx < 0)
return nullptr;
return data_ + static_cast<size_t>(idx);
}
const Atom* AtomListView::clipAt(int pos) const
{
if (n_ == 0)
return nullptr;
auto idx = static_cast<size_t>(clip<long>(pos, 0, long(n_ - 1)));
return data_ + idx;
}
const Atom* AtomListView::wrapAt(int pos) const
{
if (n_ == 0)
return nullptr;
return data_ + wrapInteger<long>(pos, n_);
}
const Atom* AtomListView::foldAt(int pos) const
{
if (n_ == 0)
return nullptr;
return data_ + foldInteger<long>(pos, n_);
}
bool AtomListView::range(Atom& min, Atom& max) const noexcept
{
if (!n_ || !data_)
return false;
auto res = std::minmax_element(begin(), end());
min = *res.first;
max = *res.second;
return true;
}
bool AtomListView::getProperty(t_symbol* name, AtomListView& res) const
{
constexpr size_t NPOS = std::numeric_limits<size_t>::max();
size_t pos = NPOS;
for (size_t i = 0; i < n_; i++) {
if (data_[i] == name) {
pos = i;
break;
}
}
if (pos == NPOS)
return false;
for (size_t i = pos + 1; i < n_; i++) {
if (data_[i].isProperty()) {
res = subView(pos + 1, i - (pos + 1));
return true;
}
}
res = subView(pos + 1);
return true;
}
bool AtomListView::expandDollarArgs(const t_canvas* cnv, AtomList& res, InvalidDollarArgPolicy policy, const Atom& def) const
{
res.reserve(res.size() + n_);
for (size_t i = 0; i < n_; i++) {
auto atom = data_[i].expandDollarArgs(cnv, true);
switch (policy) {
case InvalidDollarArgPolicy::DEFAULT_ARG:
res.append(atom ? *atom : def);
break;
case InvalidDollarArgPolicy::KEEP_RAW:
res.append(atom ? *atom : data_[i]);
break;
case InvalidDollarArgPolicy::DROP_ARG:
default:
break;
}
}
return true;
}
void AtomListView::restorePrimitives(AtomList& res) const
{
if (empty())
return;
res.reserve(res.size() + n_);
auto bb = binbuf_new();
if (!bb)
return;
binbuf_restore(bb, n_, toPdData());
for (int i = 0; i < binbuf_getnatom(bb); i++)
res.append(binbuf_getvec(bb)[i]);
binbuf_free(bb);
}
void AtomListView::split(const Atom& sep, const std::function<void(const AtomListView&)>& msg) const
{
if (!msg)
return;
size_t start = 0;
for (size_t i = 0; i < n_; i++) {
if (data_[i] == sep) {
msg(subView(start, i - start));
start = i + 1;
}
}
msg(subView(start));
}
std::ostream& operator<<(std::ostream& os, const AtomListView& l)
{
os << "( ";
for (size_t i = 0; i < l.size(); i++) {
if (i != 0)
os << " ";
os << l.at(i);
}
os << " )";
return os;
}
}