tilde~
Puredata Qt-based GUI
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ceammc_atomlist.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2016 Serge Poltavsky. All rights reserved.
3  *
4  * This file may be distributed under the terms of GNU Public License version
5  * 3 (GPL v3) as defined by the Free Software Foundation (FSF). A copy of the
6  * license should have been included with this file, or the project in which
7  * this file belongs to. You may also find the details of GPL v3 at:
8  * http://www.gnu.org/licenses/gpl-3.0.txt
9  *
10  * If you have any questions regarding the use of this file, feel free to
11  * contact the author of this file, or the owner of the project in which
12  * this file belongs to.
13  *****************************************************************************/
14 #ifndef CEAMMC_ATOMLIST_H
15 #define CEAMMC_ATOMLIST_H
16 
17 #include "ceammc_atom.h"
18 #include <deque>
19 #include <string>
20 #include <vector>
21 
22 namespace ceammc {
23 
24 typedef std::vector<t_float> FloatList;
25 typedef bool (*AtomPredicate)(const Atom& a);
26 typedef Atom (*AtomGenerator)();
27 typedef Atom (*AtomMapFunction)(const Atom& a);
28 
29 class AtomList {
30  std::vector<Atom> atoms_;
31  typedef std::vector<Atom> atom_list;
32  typedef atom_list::const_iterator const_atom_iterator;
33  typedef atom_list::iterator atom_iterator;
34  typedef atom_list::reverse_iterator atom_riterator;
35 
36  static bool calc_rel_idx(int pos, size_t* dest, size_t sz);
37  bool getRelativeIdx(int pos, size_t* idx) const;
38 
39 public:
40  AtomList();
41  AtomList(const Atom& a);
42  AtomList(const Atom& a, const Atom& b);
43  AtomList(size_t n, t_atom* lst);
44  explicit AtomList(int n, t_atom* lst);
45  size_t size() const;
46  bool empty() const;
47 
54  Atom& at(size_t pos);
55  const Atom& at(size_t pos) const;
56  Atom& operator[](size_t pos);
57  const Atom& operator[](size_t pos) const;
58 
65  Atom* relativeAt(int pos);
66  const Atom* relativeAt(int pos) const;
67 
74  Atom* clipAt(int pos);
75  const Atom* clipAt(int pos) const;
76 
83  Atom* wrapAt(int pos);
84  const Atom* wrapAt(int pos) const;
85 
92  Atom* foldAt(int pos);
93  const Atom* foldAt(int pos) const;
94 
101  void resizePad(size_t n, const Atom& v);
102 
110  void resizeClip(size_t n);
111 
119  void resizeWrap(size_t n);
120 
128  void resizeFold(size_t n);
129 
136  bool property(const std::string& name, Atom* dest) const;
137 
141  std::deque<AtomList> properties() const;
142 
146  bool hasProperty(const std::string& name) const;
147 
148  AtomList slice(int start) const;
149  AtomList slice(int start, int end, size_t step = 1) const;
150 
151  void fromPdData(size_t n, t_atom* lst);
152  void fromPdData(int n, t_atom* lst);
153  t_atom* toPdData() const;
154  void append(const Atom& a);
155  void append(const AtomList& l);
156  bool insert(size_t pos, const Atom& a);
157  bool insert(size_t pos, const AtomList& l);
158  bool remove(size_t pos);
159  void removeAll(const Atom& a);
160  void removeAll(AtomPredicate pred);
161  void replaceAll(const Atom& old_value, const Atom& new_value);
162  void replaceAll(AtomPredicate pred, const Atom& new_value);
163  void clear();
164  void fill(const Atom& a);
165  void fill(const Atom& a, size_t sz);
166  Atom* first();
167  Atom* last();
168  const Atom* first() const;
169  const Atom* last() const;
170 
171  void sort();
172  void shuffle();
173  void reverse();
174 
175  AtomList subList(int begin, int end);
176  AtomList filtered(AtomPredicate pred) const;
177 
178  const Atom* min() const;
179  const Atom* max() const;
180  bool range(Atom& min, Atom& max) const;
181  const Atom* find(const Atom& a) const;
182  const Atom* find(AtomPredicate pred) const;
183  const Atom* findLast(const Atom& a) const;
184  const Atom* findLast(AtomPredicate pred) const;
185  Atom* min();
186  Atom* max();
187  Atom* find(const Atom& a);
188  Atom* find(AtomPredicate pred);
189  Atom* findLast(const Atom& a);
190  Atom* findLast(AtomPredicate pred);
191 
195  float sum() const;
196 
200  float product() const;
201 
202  bool contains(const Atom& a) const;
203  int findPos(const Atom& a) const;
204  int findPos(AtomPredicate pred) const;
205  size_t count(const Atom& a) const;
206  size_t count(AtomPredicate pred) const;
207 
208  bool allOff(AtomPredicate pred) const;
209  bool anyOff(AtomPredicate pred) const;
210  bool noneOff(AtomPredicate pred) const;
211 
212  FloatList asFloats() const;
213  size_t asSizeT(size_t defaultValue = 0) const;
214 
219  void outputAtoms(t_outlet* x) const;
220 
225  void output(t_outlet* x) const;
226 
230  void outputAsAny(t_outlet* x) const;
231 
237  void outputAsAny(t_outlet* x, t_symbol* s) const;
238 
240  MINSIZE = 0, // result of min size
241  PADZERO, // result of max size, min list padded with zeroes
242  CLIP, // result of max size, min list clipped with last value
243  WRAP, // result of max size, min list wraped
244  FOLD // result of max size, min list wraped
245  };
246 
247  template <typename T>
248  T reduce(T init, T (*fn)(const Atom&, const Atom&)) const;
249  t_float reduceFloat(t_float init, t_float def, t_float (*fn)(t_float, t_float)) const;
250 
251  bool normalizeFloats();
252 
253 public:
254  static AtomList zeroes(size_t n);
255  static AtomList ones(size_t n);
256  static AtomList filled(const Atom& a, size_t n);
257  static AtomList values(size_t n, ...);
258 
265  static AtomList add(const AtomList& a, const AtomList& b, NonEqualLengthBehaivor lb = MINSIZE);
266 
274  static AtomList sub(const AtomList& a, const AtomList& b, NonEqualLengthBehaivor lb = MINSIZE);
275 
279  AtomList& operator+=(double v);
280  AtomList& operator-=(double v);
281  AtomList& operator*=(double v);
282  AtomList& operator/=(double v);
283 
284  AtomList operator+(double v) const;
285  AtomList operator-(double v) const;
286  AtomList operator*(double v) const;
287  AtomList operator/(double v) const;
288 
289 public:
290  friend bool operator==(const AtomList& l1, const AtomList& l2);
291  friend bool operator!=(const AtomList& l1, const AtomList& l2);
292 };
293 
294 template <typename T>
295 T AtomList::reduce(T init, T (*fn)(const Atom&, const Atom&)) const
296 {
297  T accum(init);
298  AtomList::const_atom_iterator it;
299  for (it = atoms_.begin(); it != atoms_.end(); ++it) {
300  accum = fn(accum, *it);
301  }
302 
303  return accum;
304 }
305 
306 bool operator==(const AtomList& l1, const AtomList& l2);
307 bool operator!=(const AtomList& l1, const AtomList& l2);
308 std::ostream& operator<<(std::ostream& os, const AtomList& l);
309 
310 void to_outlet(t_outlet* x, const AtomList& a);
311 
312 template <typename T>
313 static Atom atomFrom(T v) { return Atom(v); }
314 
315 Atom atomFrom(const std::string& v);
316 
317 template <typename T>
318 static AtomList listFrom(T v)
319 {
320  AtomList res;
321  res.append(atomFrom<T>(v));
322  return res;
323 }
324 
325 AtomList listFrom(bool v);
326 AtomList listFrom(const std::string& v);
327 AtomList listFrom(const std::vector<std::string>& v);
328 AtomList listFrom(const AtomList& v);
329 
330 template <typename T>
331 static T atomlistToValue(const AtomList&, const T& def) { return def; }
332 
333 template <>
334 bool atomlistToValue(const AtomList& l, const bool& def)
335 {
336  if (l.empty())
337  return def;
338 
339  if (l[0].isFloat())
340  return l[0].asFloat(0.f) != 0.f;
341 
342  if (l[0].isSymbol())
343  return l[0].asSymbol() == gensym("true");
344 
345  return false;
346 }
347 
348 template <>
349 float atomlistToValue(const AtomList& l, const float& def)
350 {
351  if (l.empty())
352  return def;
353 
354  return l[0].asFloat(def);
355 }
356 
357 template <>
358 double atomlistToValue(const AtomList& l, const double& def)
359 {
360  if (l.empty())
361  return def;
362 
363  return static_cast<double>(l[0].asFloat(static_cast<float>(def)));
364 }
365 
366 template <>
367 int atomlistToValue(const AtomList& l, const int& def)
368 {
369  if (l.empty())
370  return def;
371 
372  return static_cast<int>(l[0].asFloat(def));
373 }
374 
375 template <>
376 size_t atomlistToValue(const AtomList& l, const size_t& def)
377 {
378  if (l.empty())
379  return def;
380 
381  t_float v = def;
382  if (!l[0].getFloat(&v))
383  return def;
384 
385  if (v < 0)
386  return def;
387 
388  return static_cast<size_t>(v);
389 }
390 
391 template <>
392 t_symbol* atomlistToValue(const AtomList& l, t_symbol* const& def)
393 {
394  if (l.empty())
395  return const_cast<t_symbol*>(def);
396 
397  if (!l[0].isSymbol())
398  return const_cast<t_symbol*>(def);
399 
400  return l[0].asSymbol();
401 }
402 
403 template <>
404 Atom atomlistToValue(const AtomList& l, const Atom& def)
405 {
406  if (l.empty())
407  return def;
408 
409  return l[0];
410 }
411 
412 } // namespace ceammc
413 
414 #endif // CEAMMC_ATOMLIST_H
void replaceAll(const Atom &old_value, const Atom &new_value)
void removeAll(const Atom &a)
AtomList & operator+=(double v)
const Atom * min() const
Definition: ceammc_atom.h:23
Atom * foldAt(int pos)
bool operator!=(const Atom &a1, const Atom &a2)
bool normalizeFloats()
Definition: ceammc_atomlist.h:240
size_t asSizeT(size_t defaultValue=0) const
static AtomList zeroes(size_t n)
Definition: m_pd.h:179
bool noneOff(AtomPredicate pred) const
Definition: ceammc_atomlist.h:242
float sum() const
Definition: ceammc_atomlist.h:241
static AtomList filled(const Atom &a, size_t n)
bool property(const std::string &name, Atom *dest) const
AtomList operator-(double v) const
void output(t_outlet *x) const
AtomList & operator*=(double v)
static AtomList add(const AtomList &a, const AtomList &b, NonEqualLengthBehaivor lb=MINSIZE)
returns new list that is a sum of original list values and new list ("l") values
void resizeClip(size_t n)
void outputAtoms(t_outlet *x) const
output list atoms separatly, one by one
const Atom * max() const
void fill(const Atom &a)
bool empty() const
void append(const Atom &a)
Atom(* AtomMapFunction)(const Atom &a)
Definition: ceammc_atomlist.h:27
void outputAsAny(t_outlet *x) const
static AtomList sub(const AtomList &a, const AtomList &b, NonEqualLengthBehaivor lb=MINSIZE)
returns new list that contains difference from given lists
size_t count(const Atom &a) const
EXTERN t_symbol * gensym(const char *s)
int findPos(const Atom &a) const
Definition: ceammc_atomlist.h:244
static AtomList values(size_t n,...)
static AtomList ones(size_t n)
Definition: ceammc_atomlist.h:243
bool(* AtomPredicate)(const Atom &a)
Definition: ceammc_atomlist.h:25
void fromPdData(size_t n, t_atom *lst)
NonEqualLengthBehaivor
Definition: ceammc_atomlist.h:239
bool anyOff(AtomPredicate pred) const
friend bool operator==(const AtomList &l1, const AtomList &l2)
const Atom * find(const Atom &a) const
AtomList & operator/=(double v)
AtomList operator+(double v) const
AtomList operator/(double v) const
t_atom * toPdData() const
AtomList filtered(AtomPredicate pred) const
Atom & at(size_t pos)
returns reference to element at specified position
AtomList operator*(double v) const
bool contains(const Atom &a) const
Atom * relativeAt(int pos)
Definition: ceammc_atomlist.h:29
friend bool operator!=(const AtomList &l1, const AtomList &l2)
PD_FLOATTYPE t_float
Definition: m_pd.h:107
t_float reduceFloat(t_float init, t_float def, t_float(*fn)(t_float, t_float)) const
#define t_outlet
Definition: m_pd.h:189
void resizeFold(size_t n)
Atom(* AtomGenerator)()
Definition: ceammc_atomlist.h:26
Definition: m_pd.h:110
bool to_outlet(t_outlet *x, const Atom &a)
bool range(Atom &min, Atom &max) const
void resizePad(size_t n, const Atom &v)
AtomList subList(int begin, int end)
bool operator==(const Atom &a1, const Atom &a2)
size_t size() const
Atom * clipAt(int pos)
std::ostream & operator<<(std::ostream &os, const Atom &a)
Atom & operator[](size_t pos)
AtomList & operator-=(double v)
bool allOff(AtomPredicate pred) const
T reduce(T init, T(*fn)(const Atom &, const Atom &)) const
Definition: ceammc_atomlist.h:295
const Atom * findLast(const Atom &a) const
bool hasProperty(const std::string &name) const
std::deque< AtomList > properties() const
bool insert(size_t pos, const Atom &a)
FloatList asFloats() const
AtomList slice(int start) const
float product() const
void resizeWrap(size_t n)
std::vector< t_float > FloatList
Definition: ceammc_atomlist.h:24
Atom * wrapAt(int pos)