sequence.hh

Go to the documentation of this file.
1 
8 #ifndef sequence_hh
9 #define sequence_hh
10 
11 #include <vector>
12 #include <algorithm>
13 #include <set>
14 #include <queue>
15 #include <stdarg.h>
16 #include <initializer_list>
17 #include "basics/typedefs.hh"
18 #include "basics/outputOperator.hh"
19 #include "basics/exceptions.hh"
20 #include "basics/pointerOutput.hh"
21 
22 #define SeqInput_D 0
23 
24 namespace concepts {
25 
26  // forward declaration
27  template<class F>
28  class BaseSequence;
29 
30  // ************************************************************** Sequence **
31 
38  template<class F>
39  class Sequence : public BaseSequence<F> {
40  public:
41  Sequence() : BaseSequence<F>() {}
42  Sequence(const int N) : BaseSequence<F>(N) {}
43  template<class G>
44  Sequence(const G& seq) : BaseSequence<F>(seq) {}
45  virtual ~Sequence() {}
46  };
47 
48  template<>
49  class Sequence<bool>;
50 
51 
52  // *********************************************************** BaseSequence **
53 
58  template<class F>
59  class BaseSequence : public std::vector<F>, public concepts::OutputOperator {
60  public:
61  BaseSequence() : std::vector<F>() {}
67  BaseSequence(const std::string& str);
68 
70  BaseSequence(std::queue<F> q);
71 
73  BaseSequence(const int N);
74 
76  template<class G>
77  BaseSequence(const G& seq)
78  : std::vector<F>(seq) {}
79  virtual ~BaseSequence() {}
80 
81 
83  inline operator Sequence<F>() const { return *this; }
84 
85  inline operator std::set<F>() const {
86  std::set<F> set;
87  for(typename std::vector<F>::
88  const_iterator i = this->begin(); i != this->end(); ++i)
89  set.insert(*i);
90  return set;
91  }
92 
96  template<class G, class H>
97  inline Sequence<G> operator()(G (H::*fun)() const) const;
101  template<class G, class H>
102  inline Sequence<G*> operator()(G& (H::*fun)() const) const;
106  template<class G, class H, class I, class J>
107  inline Sequence<G> operator()(G (H::*fun)(I) const, J i) const;
108 
109  // Return set union
110  inline std::vector<bool> operator||(const Sequence<F>& set) const;
111  // Return set intersection
112  inline std::vector<bool> operator&&(const Sequence<F>& set) const;
113 
114 
115  // Return a sequence with appended \c seq
116  inline Sequence<F> operator+(const Sequence<F>& seq) const;
117  // Return sequence, where \c val is not inside
118  inline Sequence<F> operator-(const F& val) const;
119  // Return sequence, where are elements of \c seq are not inside
120  inline Sequence<F> operator-(const Sequence<F>& seq) const;
122  inline std::vector<bool> operator==(const F val) const;
124  inline Sequence<F> operator()(const BaseSequence<bool>& seq) const;
125 
127  inline bool exist(F val) const;
128  protected:
129  virtual std::ostream& info(std::ostream& os) const;
130  };
131 
132  template<class F>
133  std::istream& operator>>(std::istream& is, BaseSequence<F>& seq) {
134  F val;
135  while(!is.eof()) {
136  is >> val;
137  DEBUGL(SeqInput_D, "val = " << val);
138  if (!is.fail())
139  seq.push_back(val);
140  }
141  return is;
142  }
143 
144  template<class F>
145  BaseSequence<F>::BaseSequence(const std::string& str) {
146  if (str.size() > 0) {
147  std::stringstream s;
148  s << str;
149  s >> *this;
150  }
151  }
152 
153  template<class F>
154  BaseSequence<F>::BaseSequence(std::queue<F> q) {
155  while(!q.empty()) {
156  // take the first element of the queue
157  this->push_back(q.front());
158  // delete the first element
159  q.pop();
160  }
161  }
162 
163 
164  template<class F>
166  {
167  this->resize(N);
168  }
169 
170  // e.g. concepts::Key::key()
171  template<class F>
172  template<class G, class H>
173  Sequence<G> BaseSequence<F>::operator()(G (H::*fun)() const) const {
174  std::vector<G> seq;
175  for(typename std::vector<F>::
176  const_iterator i = this->begin(); i != this->end(); ++i) {
177  G val = ((*i)->*fun)();
178  seq.push_back(val);
179  }
180  return seq;
181  }
182  // e.g. concepts::Connector1::key()
183  template<class F>
184  template<class G, class H>
185  Sequence<G*> BaseSequence<F>::operator()(G& (H::*fun)() const) const {
186  std::vector<G*> seq;
187  for(typename std::vector<F>::
188  const_iterator i = this->begin(); i != this->end();++i) {
189  G* val = &((*i)->*fun)();
190  seq.push_back(val);
191  }
192  return seq;
193  }
194  // e.g. concepts::Connector1::vertex(uint i)
195  template<class F>
196  template<class G, class H, class I, class J>
197  Sequence<G> BaseSequence<F>::operator()(G (H::*fun)(I) const, J i) const {
198  std::vector<G> seq;
199  for(typename std::vector<F>::
200  const_iterator is = this->begin(); is != this->end();++is) {
201  G val = ((*is)->*fun)(i);
202  seq.push_back(val);
203  }
204  return seq;
205  }
206 
207  template<class F>
208  std::vector<bool> BaseSequence<F>::operator||(const Sequence<F>& seq) const
209  {
210  conceptsAssert(this->size() == seq.size(), Assertion());
211  std::vector<bool> result(this->size(), false);
212 
213  std::vector<bool>::iterator i = result.begin();
214  typename std::vector<F>::
215  const_iterator it = this->begin(), is = seq.begin();
216  for(; it != this->end(); ++it, ++is, ++i)
217  *i = *it || *is;
218  return result;
219  }
220 
221  template<class F>
222  std::vector<bool> BaseSequence<F>::operator&&(const Sequence<F>& seq) const
223  {
224  conceptsAssert(this->size() == seq.size(), Assertion());
225  std::vector<bool> result(this->size(), false);
226 
227  std::vector<bool>::iterator i = result.begin();
228  typename std::vector<F>::
229  const_iterator it = this->begin(), is = seq.begin();
230  for(; it != this->end(); ++it, ++is, ++i)
231  *i = *it && *is;
232  return result;
233  }
234 
235  template<class F>
237  std::set<F> set = seq;
238  std::vector<F> result;
239  result.reserve(this->size());
240  for(typename std::vector<F>::
241  const_iterator i = this->begin(); i != this->end(); ++i)
242  if (set.find(*i) == set.end()) result.push_back(*i);
243  return result;
244  }
245 
246  template<class F>
248  std::vector<F> result;
249  result.reserve(this->size());
250  for(typename std::vector<F>::
251  const_iterator i = this->begin(); i != this->end(); ++i)
252  if (!(*i == val)) result.push_back(*i);
253  return result;
254  }
255 
256  template<class F>
258  std::vector<F> result(this->size() + seq.size());
259  typename std::vector<F>::iterator i = result.begin();
260  typename std::vector<F>::
261  const_iterator it = this->begin(), is = seq.begin();
262  for(; it != this->end(); ++it, ++i) *i = *it;
263  for(; is != seq.end(); ++is, ++i) *i = *is;
264  return result;
265  }
266 
267  template<class F>
268  std::vector<bool> BaseSequence<F>::operator==(const F val) const {
269  std::vector<bool> result(this->size());
270 // uint i = 0;
271 // for(typename std::vector<F>::
272 // const_iterator is = this->begin(); is != this->end(); ++is, ++i)
273 // if (*is == val) result.push_back(i);
274  std::vector<bool>::iterator i = result.begin();
275  for(typename std::vector<F>::
276  const_iterator it = this->begin(); it != this->end(); ++it, ++i)
277  *i = *it == val;
278  return result;
279  }
280 
281 // template<class F>
282 // Sequence<F> BaseSequence<F>::operator()(const Sequence<uint>& seq) const {
283 // std::vector<F> result(seq.size());
284 // std::vector<uint> sortedIdx = seq;
285 // sort(sortedIdx.begin(), sortedIdx.end());
286 // sortedIdx.erase(unique(sortedIdx.begin(), sortedIdx.end()),
287 // sortedIdx.end());
288 
289 // uint i = 0;
290 // std::vector<uint>::const_iterator is = sortedIdx.begin();
291 // typename std::vector<F>::const_iterator it = this->begin();
292 // typename std::vector<F>::iterator ir = result.begin();
293 // while(is != sortedIdx.end() && it != this->end()) {
294 // if (i == *is) {
295 // *ir = *it;
296 // ++ir;
297 // ++is;
298 // }
299 // ++it; ++i;
300 // }
301 // conceptsAssert(is == sortedIdx.end(), Assertion());
302 // return result;
303 // }
304 
305  template<class F>
307  conceptsAssert(this->size() == seq.size(), Assertion());
308 
309  std::vector<F> result(count(seq.begin(), seq.end(), true));
310  if (result.size() > 0) {
311  std::vector<bool>::const_iterator is = seq.begin();
312  typename std::vector<F>::const_iterator it = this->begin();
313  typename std::vector<F>::iterator i = result.begin();
314  for(; it != this->end(); ++it, ++is)
315  if (*is) {
316  *i = *it;
317  ++i;
318  }
319  }
320  return result;
321  }
322 
323  template<class F>
324  bool BaseSequence<F>::exist(F val) const {
325  for(typename std::vector<F>::
326  const_iterator i = this->begin(); i != this->end(); ++i)
327  {
328  if (*i == val) return true;
329  }
330  return false;
331  // return this->find(val) != this->end();
332  }
333 
334  template<class F>
335  std::ostream& BaseSequence<F>::info(std::ostream& os) const {
336  os << concepts::typeOf(*this)<<"(";
337  for(typename std::vector<F>::
338  const_iterator i = this->begin(); i != this->end();) {
339  pointerOutput(os, *i);
340  if (++i != this->end()) os << ", ";
341  }
342  return os << ")";
343  }
344 
345  // ******************************************************** Sequence<bool> **
346 
347  template<>
348  class Sequence<bool> : public BaseSequence<bool> {
349  public:
351  Sequence() : BaseSequence<bool>() {}
353  template<class F>
354  Sequence(const F& seq) : BaseSequence<bool>(seq) {}
355  virtual ~Sequence() {}
356 
363  };
364 
365  // ************************************************************ makeSequence **
366 
374  template<class F>
375  Sequence<F> makeSequence(uint n, const F& first, ...) {
376  Sequence<F> data(n);
377  data[0] = first;
378 
379  va_list param;
380  va_start(param, first);
381  for(uint i = 1; i < n; ++i)
382  data[i] = va_arg(param, F);
383  va_end(param);
384  return data;
385  }
386 
394  template<class F>
395  Sequence<F> makeSequence(std::initializer_list<F> list) {
396  uint n = list.size();
397  Sequence<F> data(n);
398  uint idx = 0;
399  for( F elem : list )
400  data[idx++] = elem;
401  return data;
402  }
403 
404  // ************************************************* makeEquidistantSequence **
405 
406  template<class F>
407  Sequence<F> makeEquidistantSequence(uint n, const F& first, const F& diff)
408  {
409  Sequence<F> data(n);
410  typename Sequence<F>::iterator i = data.begin();
411  F current = *i++ = first;
412  for(; i != data.end(); ++i)
413  *i++ = (current+=diff);
414  return data;
415  }
416 
417  // ****************************************************** makeRangeSequence **
418 
421 
423  Sequence<int> makeRangeSequence(int start, int afterlast);
424 
428  Sequence<int> makeRangeSequence(int start, int afterlast, uint dist);
429 
430  // ************************************************************ operator*() **
431 
432  template<class F, class G>
434  operator*(const Sequence<F> seq1, const Sequence<G> seq2)
435  {
436  conceptsAssert(seq1.size() == seq2.size(), Assertion());
437 
439  typename Sequence<F>::const_iterator i1 = seq1.begin();
440  typename Sequence<F>::const_iterator i2 = seq2.begin();
441  for(; i1 != seq1.end(); )
442  seq.push_back( *i1++ * *i2++);
443  return seq;
444  }
445 
446  template<class F, class G>
447  Sequence<typename Combtype<F,G>::type>
448  operator*(const Sequence<F> seq1, const G factor)
449  {
451  typename Sequence<F>::const_iterator i1 = seq1.begin();
452  for(; i1 != seq1.end(); )
453  seq.push_back( *i1++ * factor);
454  return seq;
455  }
456 
457  // ************************************************************ operator/() **
458 
459  template<class F, class G>
460  Sequence<typename Combtype<F,G>::type>
461  operator/(const Sequence<F> seq1, const Sequence<G> seq2)
462  {
463  conceptsAssert(seq1.size() == seq2.size(), Assertion());
464 
466  typename Sequence<F>::const_iterator i1 = seq1.begin();
467  typename Sequence<F>::const_iterator i2 = seq2.begin();
468  for(; i1 != seq1.end(); )
469  seq.push_back( *i1++ / *i2++);
470  return seq;
471  }
472 
473  template<class F, class G>
474  Sequence<typename Combtype<F,G>::type>
475  operator/(const Sequence<F> seq1, const G divisor)
476  {
478  typename Sequence<F>::const_iterator i1 = seq1.begin();
479  for(; i1 != seq1.end(); )
480  seq.push_back( *i1++ / divisor);
481  return seq;
482  }
483 
484 } // namespace concepts
485 
486 #endif // sequence_hh
bool exist(F val) const
Returns true, if a value is in the seq.
Definition: sequence.hh:324
Sequence(const int N)
Definition: sequence.hh:42
Sequence< bool > operator|(const Sequence< bool > &seq) const
Set each bit, which is set in \seq.
ElementFormulaContainer< Real > operator/(const ElementFormulaContainer< Real > frm1, const ElementFormulaContainer< Real > frm2)
Division of a element formulas by a scalar element formula via /-operator.
void pointerOutput(std::ostream &os, const F &val)
virtual std::ostream & info(std::ostream &os) const
Definition: sequence.hh:335
Point< typename Combtype< F, Real >::type, dim > operator*(const Real x, const Point< F, dim > &y)
Sequence(const F &seq)
Constructor, which uses the constructor of the base class BaseSequence.
Definition: sequence.hh:354
Sequence< F > makeEquidistantSequence(uint n, const F &first, const F &diff)
Definition: sequence.hh:407
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
std::istream & operator>>(std::istream &is, BaseSequence< F > &seq)
Definition: sequence.hh:133
BaseSequence(const std::string &str)
Constructor, sequence is defined by a string.
Definition: sequence.hh:145
Sequence< F > operator-(const Sequence< F > &seq) const
Definition: sequence.hh:236
Sequence()
Standard Constructor.
Definition: sequence.hh:351
#define DEBUGL(doit, msg)
Sequence< F > operator+(const Sequence< F > &seq) const
Definition: sequence.hh:257
virtual ~Sequence()
Definition: sequence.hh:45
Sequence< G > operator()(G(H::*fun)() const) const
Returns element wise application of a member function, e.g.
Definition: sequence.hh:173
Sequence< G * > operator()(G &(H::*fun)() const) const
Returns element wise application of a member function, e.g.
Definition: sequence.hh:185
Exception class for assertions.
Definition: exceptions.hh:258
Sequence< bool > operator~() const
Returns copy of this sequence with negate bits.
BaseSequence(std::queue< F > q)
Constructor taking a queue (takes a copy)
Definition: sequence.hh:154
Sequence with operations, output operator, and method of the particular element types.
Definition: sequence.hh:39
std::vector< bool > operator==(const F val) const
Returns true for the elements with are equal to val.
Definition: sequence.hh:268
Sequence< F > operator()(const BaseSequence< bool > &seq) const
Returns subsequence which indices are true in seq.
Definition: sequence.hh:306
std::vector< bool > operator&&(const Sequence< F > &set) const
Definition: sequence.hh:222
BaseSequence(const G &seq)
Constructor, which uses the constructor of the base class std::vector.
Definition: sequence.hh:77
virtual ~BaseSequence()
Definition: sequence.hh:79
#define SeqInput_D
Definition: sequence.hh:22
Sequence< F > operator-(const F &val) const
Definition: sequence.hh:247
std::vector< bool > operator||(const Sequence< F > &set) const
Definition: sequence.hh:208
Sequence with operations and output operator.
Definition: sequence.hh:28
Sequence< int > makeRangeSequence(uint n)
Returns the sequence 0,1,...,n-1.
std::string typeOf(const T &t)
Return the typeid name of a class object.
Definition: output.hh:43
Sequence< bool > operator&(const Sequence< bool > &seq) const
Deletes each bit, which is not set in \seq.
BaseSequence(const int N)
Constructor taking an integer.
Definition: sequence.hh:165
Sequence< G > operator()(G(H::*fun)(I) const, J i) const
Returns element wise application of a member function, e.g.
Definition: sequence.hh:197
Class providing an output operator.
Sequence< F > makeSequence(uint n, const F &first,...)
Creates an sequence of length from a comma separated list of values.
Definition: sequence.hh:375
Sequence(const G &seq)
Definition: sequence.hh:44
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich