scannerConnectors.hh

Go to the documentation of this file.
1 
6 #ifndef scannerConnectors_hh
7 #define scannerConnectors_hh
8 
9 #include <vector>
10 #include "basics/typedefs.hh"
11 #include "basics/outputOperator.hh"
12 #include "basics/debug.hh"
13 #include "basics/exceptions.hh"
14 #include "toolbox/array.hh"
15 
16 // debugging Joiner
17 #define JoinerConstr_D 0
18 #define JoinerDestr_D 0
19 
20 namespace concepts {
21 
22  // ****************************************************************** Scan **
23 
28  template <class T>
29  class Scan {
30  public:
31  virtual ~Scan() {};
32 
34  operator int() { return !eos(); }
35 
37  virtual bool eos() const = 0;
39  virtual T& operator++(int) = 0;
41  virtual Scan* clone() const = 0;
42  };
43 
44  // ************************************************************* Joiner **
45 
46  template<class T, unsigned nlnk>
47  class Joiner;
48 
49  template<class T, unsigned nlnk>
50  std::ostream& operator<<(std::ostream& os,
51  const Joiner<T, nlnk>& j);
52 
72  template<class T, unsigned nlnk>
73  class Joiner {
74  friend std::ostream& operator<< <>(std::ostream& os,
75  const Joiner<T, nlnk>& j);
76  public:
83  inline Joiner(const T& val, Joiner* lnk = 0);
84 
88  inline ~Joiner() {}
89 
95  static void destructor(Joiner<T, nlnk>*& j, bool values = true);
96 
101  inline Joiner*& operator[](uint i) {
102  conceptsAssert(i < nlnk, Assertion());
103  return lnk_[i];
104  }
105 
107  inline T& value() { return val_; }
108 
109  private:
112 
114  T val_;
115 
117  Joiner(const Joiner&);
118  void operator =(const Joiner&);
119 
121  static void destructor_(Joiner<T, nlnk>* j, bool values = true);
122  };
123 
124  template <class T, unsigned nlnk>
126  val_(val) {
127  DEBUGL(JoinerConstr_D, "start.");
128  for(uint i = 0; i < nlnk - 1; ++i)
129  lnk_[i] = 0;
130  lnk_[nlnk - 1] = lnk;
131  DEBUGL(JoinerConstr_D, "done.");
132  }
133 
134  template <class T, unsigned nlnk>
136  DEBUGL(JoinerDestr_D, "start.");
137  if (nlnk > 1) {
138  if (j) {destructor_(j); j = 0;}
139  }
140  else {
141  while (j) {
142  Joiner<T, 1>* foo = j;
143  j = j->lnk_[0];
144  if (values) delete foo->val_;
145  delete foo;
146  }
147  }
148  DEBUGL(JoinerDestr_D, "done.");
149  }
150 
151  template <class T, unsigned nlnk>
153  for(uint i = 0; i < nlnk; i++) {
154  if (j->lnk_[i]) destructor_(j->lnk_[i]);
155  }
156  if (values) delete j->val_;
157  delete j;
158  }
159 
160  template<class T, unsigned nlnk>
161  std::ostream& operator<<(std::ostream& os, const Joiner<T, nlnk>& j) {
162  os << "J(" << j.val_ << " -> [" << std::flush;
163  for(uint i = 0; i < nlnk-1; ++i)
164  if (j.lnk_[i])
165  os << *j.lnk_[i] << ", " << std::flush;
166  if (j.lnk_[nlnk-1])
167  os << *j.lnk_[nlnk-1] << std::flush;
168  return os << "])" << std::flush;
169  }
170 
171  // ************************************************************* PListScan **
172 
175  template <class T>
176  class PListScan : public Scan<T> {
179 
182 
184  bool eos_;
185  public:
190  head_(&cnr), current_(&cnr), eos_(head_ ? false : true) {}
191 
192  bool eos() const { return eos_; }
193 
194  PListScan<T>* clone() const { return new PListScan<T>(*this); }
195 
196  T& operator++(int);
197  };
198 
199  template <class T>
201 
202  T* tmp = 0;
203  if (!eos_) {
204  tmp = current_->value();
205  current_ = (*current_)[0];
206  if ((eos_ = current_ == 0)) current_ = head_;
207  }
208  return *tmp;
209  }
210 
211  // ************************************************************** ListScan **
212 
215  template <class T>
216  class ListScan : public Scan<T> {
219 
222 
224  bool eos_;
225  public:
230  head_(&cnr), current_(&cnr), eos_(head_ ? false : true) {}
231 
232  bool eos() const { return eos_; }
233 
234  ListScan<T>* clone() const { return new ListScan<T>(*this); }
235 
236  T& operator++(int);
237  };
238 
239  template <class T>
241 
242  T& tmp = current_->value();
243 
244  if (!eos_) {
245  current_ = (*current_)[0];
246  if ((eos_ = current_ == 0)) current_ = head_;
247  }
248 
249  return tmp;
250  }
251 
252  // ******************************************************** PStlVectorScan **
253 
256  template<class T>
257  class PStlVectorScan : public Scan<T> {
258  public:
259  inline PStlVectorScan(typename std::vector<T*>& v)
260  : i_(v.begin()), last_(v.end()) {}
261  inline PStlVectorScan(typename std::vector<T*>::iterator first,
262  typename std::vector<T*>::const_iterator last)
263  : i_(first), last_(last) {}
264  inline PStlVectorScan(const PStlVectorScan<T> &scan)
265  : i_(scan.i_), last_(scan.last_) {}
266  inline bool eos() const { return i_ == last_; }
267  inline T& operator++(int) { return **i_++; }
268  inline PStlVectorScan<T>* clone() const { return new PStlVectorScan(*this); }
269  private:
270  typename std::vector<T*>::iterator i_;
271  typename std::vector<T*>::const_iterator last_;
272  };
273 
274  // ********************************************************* StlVectorScan **
275 
280  template <class T, class ItType = typename std::vector<T*>::const_iterator >
281  class StlVectorScan : public Scan<T> {
282  typedef std::vector<T*> Vector;
283 
284  const Vector& vec;
285  ItType it;
286 
287 
288  public:
293  StlVectorScan(const Vector& vec, ItType it)
294  : vec(vec), it(it)
295  {}
296 
301  : vec(vec), it(vec.begin())
302  {}
303 
304  bool eos() const { return it == vec.end(); }
305 
306  StlVectorScan* clone() const { return new StlVectorScan(*this); }
307 
308  T& operator*() {
309  return **it;
310  }
311 
313  T& operator++(int) {
314  T* old = *it;
315  ++it;
316  return *old;
317  }
318 
320  T& operator++() {
321  return *(++it);
322  }
323 
324  };
325 
326  // ************************************************************* ArrayScan **
327 
332  template<class T>
333  class ArrayScan : public Scan<T> {
334  public:
335  inline ArrayScan(Array<T>& container)
336  : idx_(0), container_(container) {}
337  inline ArrayScan(const ArrayScan& scan) :
338  idx_(scan.idx_), container_(scan.container_) {}
339  inline bool eos() const { return idx_ == container_.size(); }
340  inline T& operator++(int) { return container_[idx_++]; }
341  inline Scan<T>* clone() const { return new ArrayScan(*this); }
342  private:
343  uint idx_;
345  };
346 
347  // *********************************************************** SingletonScan **
348 
351  template<class T>
352  class SingletonScan : public concepts::Scan<T> {
353  public:
354  SingletonScan(T& singleton) :
355  singleton_(singleton), eos_(false) {}
356 
357  SingletonScan(const SingletonScan& other) :
358  singleton_(other.singleton_), eos_(other.eos_) {}
359 
360  bool eos() const /*override*/ { return eos_; }
361 
362  T& operator++ (int) /*override*/ {
364  eos_ = true;
365  return singleton_;
366  }
367 
368  SingletonScan* clone () const /*override*/ {
369  return new SingletonScan(*this);
370  }
371 
372  private:
374  bool eos_;
375  };
376 
377 } // namespace concepts
378 
379 #endif // scannerConnectors_hh
uint size() const
Returns the requested size of the array.
Definition: array.hh:259
T & operator++(int)
Returns the next element in the scanned set.
A scanner over a single element.
Scanner working on std::vector elements.
std::vector< T * > Vector
#define JoinerDestr_D
bool eos_
Is true if the end of the list is reached.
virtual bool eos() const =0
Returns true if the end of the scanned set is reached.
bool eos() const
Returns true if the end of the scanned set is reached.
Joiner< T *, 1 > * head_
Pointer to the head of the list.
ListScan(Joiner< T, 1 > &cnr)
Constructor.
bool eos() const
Returns true if the end of the scanned set is reached.
PStlVectorScan< T > * clone() const
Returns a clone of the scanner.
Joiner class with multiple successors, i.e.
T & operator++()
pre-increment operator
Scanner for a list.
StlVectorScan * clone() const
Returns a clone of the scanner.
bool eos() const
Returns true if the end of the scanned set is reached.
ArrayScan(Array< T > &container)
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
Joiner *& operator[](uint i)
Index operator for the container.
virtual T & operator++(int)=0
Returns the next element in the scanned set.
Scanner for a STL container std::vector of pointers.
std::vector< T * >::iterator i_
bool eos() const
Returns true if the end of the scanned set is reached.
#define DEBUGL(doit, msg)
Scanner for an Array.
Joiner(const Joiner &)
Copy constructor.
T & operator++(int)
Returns the next element in the scanned set.
T & operator++(int)
Returns the next element in the scanned set.
std::vector< T * >::const_iterator last_
Joiner(const T &val, Joiner *lnk=0)
Constructor.
Joiner< T, 1 > * head_
Pointer to the head of the list.
Exception class for assertions.
Definition: exceptions.hh:258
PStlVectorScan(typename std::vector< T * >::iterator first, typename std::vector< T * >::const_iterator last)
bool eos() const
Returns true if the end of the scanned set is reached.
PStlVectorScan(typename std::vector< T * > &v)
T & operator++(int)
post-increment operator
~Joiner()
Empty destructor.
std::ostream & operator<<(std::ostream &os, const Level< dim > &c)
virtual Scan * clone() const =0
Returns a clone of the scanner.
An abstract class for scanning a mesh (a set of cells) or a space (a set of elements).
#define JoinerConstr_D
Joiner< T, nlnk > * lnk_[nlnk]
Array of the successors.
T val_
The content of the container.
PListScan< T > * clone() const
Returns a clone of the scanner.
StlVectorScan(Vector &vec)
Constructor.
PStlVectorScan(const PStlVectorScan< T > &scan)
Joiner< T, 1 > * current_
Pointer to the current element.
Joiner< T *, 1 > * current_
Pointer to the current element.
T & operator++(int)
Returns the next element in the scanned set.
PListScan(Joiner< T *, 1 > &cnr)
Constructor.
T & value()
Returns the content of the container.
SingletonScan(const SingletonScan &other)
static void destructor(Joiner< T, nlnk > *&j, bool values=true)
Static function to delete the list/tree.
bool eos() const
Returns true if the end of the scanned set is reached.
SingletonScan * clone() const
Returns a clone of the scanner.
static void destructor_(Joiner< T, nlnk > *j, bool values=true)
Recursive destructor.
void operator=(const Joiner &)
StlVectorScan(const Vector &vec, ItType it)
Constructor.
bool eos_
Is true if the end of the list is reached.
T & operator++(int)
Returns the next element in the scanned set.
Scan< T > * clone() const
Returns a clone of the scanner.
ArrayScan(const ArrayScan &scan)
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Scanner for a list of pointers.
ListScan< T > * clone() const
Returns a clone of the scanner.
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich