matrixIterator.hh

Go to the documentation of this file.
1 
6 #ifndef matrixIterator_hh
7 #define matrixIterator_hh
8 
9 #include <cstddef>
10 #include <bits/stl_iterator_base_types.h>
11 #include "basics/exceptions.hh"
12 #include "basics/outputOperator.hh"
13 
14 namespace concepts {
15 
16  // forward declaration
17  template<class F>
18  class Matrix;
19 
20  // ************************************************* _Matrix_iterator_base **
21 
27  template <class _Tp, class _Ref, class _Ptr>
28  class _Matrix_iterator_base : public virtual OutputOperator {
29  public:
32 
37  template<class _Tp_>
38  struct ReturnType;
39 
40  typedef _Tp value_type;
41  typedef _Ptr pointer;
42  typedef _Ref reference;
43  typedef size_t size_type;
44  typedef ptrdiff_t difference_type;
47 
55  _Matrix_iterator_base(const unsigned int nofRows, const unsigned int nofCols,
56  const unsigned int r, const unsigned int c);
63  template<class _RefR, class _PtrR>
65  : nofRows_(__x.nofRows()), nofCols_(__x.nofCols()),
66  row_(__x.row()), col_(__x.col()), last_(__x.last()) {}
67 
69  inline const unsigned int nofRows() const { return nofRows_; }
71  inline const unsigned int nofCols() const { return nofCols_; }
73  inline unsigned int row() const { return row_; }
75  inline unsigned int col() const { return col_; }
77  inline bool last() const { return last_; }
78 
80  _Self& operator=(const iterator& __x) {
81  conceptsAssert(nofRows_ == __x.nofRows() && nofCols_ == __x.nofCols(),
82  Assertion());
83  row_ = __x.row();
84  col_ = __x.col();
85  last_ = __x.last();
86  return *this;
87  }
88  protected:
89  virtual std::ostream& info(std::ostream& os) const;
91  const unsigned int nofRows_, nofCols_;
93  unsigned int row_, col_;
95  bool last_;
96  };
97 
101  template <class _Tp, class _Ref, class _Ptr>
102  template<class _Tp_>
103  struct _Matrix_iterator_base<_Tp, _Ref, _Ptr>::ReturnType {
104  typedef _Tp_ type;
105  };
106 
110  template <class _Tp, class _Ref, class _Ptr>
111  template<class _Tp_>
112  struct _Matrix_iterator_base<_Tp, _Ref, _Ptr>::ReturnType<const _Tp_&> {
113  typedef const _Tp_ type;
114  };
115 
116  template <class _Tp, class _Ref, class _Ptr>
117  std::ostream&
119  os << "_Matrix_iterator_base(pos = ";
120  if (!last_)
121  os << "(" << row_ << ", " << col_ << ")";
122  else os << "end)";
123  return os;
124  }
125 
126  // *************
127 
128  template <class _Tp, class _Ref, class _Ptr>
129  inline bool
132  {
133  return (__x.last() && __y.last()) ||
134  (!__x.last() && !__y.last() &&
135  __x.row() == __y.row() && __x.col() == __y.col());
136  }
137 
138  template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
139  inline bool
142  {
143  return (__x.last() && __y.last()) ||
144  (!__x.last() && !__y.last() &&
145  __x.row() == __y.row() && __x.col() == __y.col());
146  }
147 
148  template <class _Tp, class _Ref, class _Ptr>
149  inline bool
152  {
153  return !(__x == __y);
154  }
155 
156  template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
157  inline bool
160  {
161  return !(__x == __y);
162  }
163 
164  template <class _Tp, class _Ref, class _Ptr>
165  inline bool
168  {
169  return !__x.last() &&
170  (__y.last() || ((!__y.last() && __x.row() < __y.row()) ||
171  (__x.row() == __y.row() && __x.col() < __y.col())));
172  }
173 
174  template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
175  inline bool
178  {
179  return !__x.last() &&
180  (__y.last() || (!__y.last() && __x.row() < __y.row()) ||
181  (__x.row() == __y.row() && __x.col() < __y.col()));
182  }
183 
184  template <class _Tp, class _Ref, class _Ptr>
185  inline bool
188  {
189  return __y < __x;
190  }
191 
192  template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
193  inline bool
196  {
197  return __y < __x;
198  }
199 
200  template <class _Tp, class _Ref, class _Ptr>
201  inline bool
204  {
205  return !(__y < __x);
206  }
207 
208  template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
209  inline bool
212  {
213  return !(__y < __x);
214  }
215 
216  template <class _Tp, class _Ref, class _Ptr>
217  inline bool
220  {
221  return !(__x < __y);
222  }
223 
224  template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR>
225  inline bool
228  {
229  return !(__x < __y);
230  }
231 
232  template <typename _Tp, typename _RefL, typename _PtrL,
233  typename _RefR, typename _PtrR>
237  {
238  if (__x.last())
239  if (__y.last()) return 0;
240  else return __y.nofRows() * (__y.nofCols() - __y.row()) - __y.col();
241  else if (__y.last())
242  return __x.nofRows() * (__x.nofCols() - __x.row()) - __x.col();
244  (__x.col() - __y.col() + __x.nofCols() * (__x.row() - __y.row()));
245  }
246 
247  template <class _Tp, class _Ref, class _Ptr>
248  inline _Matrix_iterator_base<_Tp, _Ref, _Ptr>
250  {
251  return __x + __n;
252  }
253 
254  // ****************************************************** securePointer **
255 
261  template<class F, class G>
262  F* securePointer(F& value, G* matrix) { return &value; }
263 
264  template<class F, class G>
265  F* securePointer(const F value, const G* matrix) {
267  ("inapplicable for constant matrices"));
268  return 0;
269  }
270 
271  // ****************************************************** _Matrix_iterator **
272 
277  template <class _Tp, class _Ref, class _Ptr>
278  class _Matrix_iterator : public _Matrix_iterator_base<_Tp, _Ref, _Ptr> {
279  public:
282 
283  template<class _Tp_, class _Ref_, class _Ptr_>
284  struct MatrixType;
285 
286  typedef std::random_access_iterator_tag iterator_category;
287  typedef _Tp value_type;
288  typedef _Ptr pointer;
289  typedef _Ref reference;
290  typedef size_t size_type;
291  typedef ptrdiff_t difference_type;
295  typedef typename _Base::return_type return_type;
296 
303  _Matrix_iterator(matrix_type& m, const unsigned int r = 0, const unsigned int c = 0);
307  template<class _RefR, class _PtrR>
309  : _Base(__x), matrix_(__x.matrix()) {}
310 
314  return (*matrix_)(this->row_, this->col_);
315  }
322  pointer operator->() const {
324  return securePointer((*matrix_)(this->row_, this->col_), matrix_);
325  }
326 
328  inline matrix_type* matrix() const { return matrix_; }
329 
331  _Self& operator=(const iterator& __x) {
332  conceptsAssert(matrix_ == __x.matrix(), Assertion());
333  _Base::operator=(__x);
334  return *this;
335  }
336 
338  if (!this->last_) {
339  if (++this->col_ >= this->nofCols_) {
340  ++this->row_;
341  this->col_ = 0;
342  }
343  this->last_ = this->row_ >= this->nofRows_;
344  }
345  return *this;
346  }
347  _Self operator++(int) {
348  _Self __tmp = *this;
349  ++*this;
350  return __tmp;
351  }
352 
354  if (this->last_) {
356  this->col_ = this->nofCols_ - 1;
357  this->row_ = this->nofRows_ - 1;
358  this->last_ = false;
359  } else {
360  if (this->col_ > 0) --this->col_;
361  else {
362  conceptsAssert(this->row_ > 0, Assertion());
363  --this->row_;
364  this->col_ = this->nofCols_ - 1;
365  }
366  }
367  return *this;
368  }
369  _Self operator--(int) {
370  _Self __tmp = *this;
371  --*this;
372  return __tmp;
373  }
374 
376  {
377  if (this->last_ && __n < 0) {
378  this->col_ = 0;
379  this->row_ = this->nofRows_;
380  this->last_ = false;
381  }
382  if (!this->last_) {
384  int col = __n + this->col_;
385  int row = col / (int)this->nofCols_ + this->row_;
386  conceptsAssert(row >= 0 , Assertion());
387  this->row_ = row;
388  this->col_ = col % this->nofCols_;
389  this->last_ = this->row_ >= this->nofRows_;
390  }
391  return *this;
392  }
393 
394  _Self operator+(difference_type __n) const
395  {
396  _Self __tmp = *this;
397  return __tmp += __n;
398  }
399 
400  _Self& operator-=(difference_type __n) { return *this += -__n; }
401 
403  _Self __tmp = *this;
404  return __tmp -= __n;
405  }
406 
408  return *(*this + __n);
409  }
410  protected:
411  virtual std::ostream& info(std::ostream& os) const;
412  private:
415  };
416 
418  template <class _Tp, class _Ref, class _Ptr>
419  template<class _Tp_, class _Ref_, class _Ptr_>
420  struct _Matrix_iterator<_Tp, _Ref, _Ptr>::MatrixType {
422  };
423 
425  template <class _Tp, class _Ref, class _Ptr>
426  template<class _Tp_>
427  struct _Matrix_iterator<_Tp, _Ref, _Ptr>::
428  MatrixType<_Tp_, const _Tp_&, const _Tp_*>
429  {
430  typedef const Matrix<_Tp_> type;
431  };
432 
433  // *************
434 
435 // template <class _Tp, class _Ref, class _Ptr, class G>
436 // inline bool
437 // operator==(const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __x,
438 // const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __y)
439 // {
440 // return __x.last() && __y.last() ||
441 // (!__x.last() && !__y.last() &&
442 // __x.row() == __y.row() && __x.col() == __y.col());
443 // }
444 
445 // template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR,
446 // class G>
447 // inline bool
448 // operator==(const _Matrix_iterator<_Tp, _RefL, _PtrL, G>& __x,
449 // const _Matrix_iterator<_Tp, _RefR, _PtrR, G>& __y)
450 // {
451 // return __x.last() && __y.last() ||
452 // (!__x.last() && !__y.last() &&
453 // __x.row() == __y.row() && __x.col() == __y.col());
454 // }
455 
456 // template <class _Tp, class _Ref, class _Ptr, class G>
457 // inline bool
458 // operator!=(const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __x,
459 // const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __y)
460 // {
461 // return !(__x == __y);
462 // }
463 
464 // template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR,
465 // class G>
466 // inline bool
467 // operator!=(const _Matrix_iterator<_Tp, _RefL, _PtrL, G>& __x,
468 // const _Matrix_iterator<_Tp, _RefR, _PtrR, G>& __y)
469 // {
470 // return !(__x == __y);
471 // }
472 
473 // template <class _Tp, class _Ref, class _Ptr, class G>
474 // inline bool
475 // operator<(const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __x,
476 // const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __y)
477 // {
478 // return !__x.last() &&
479 // (__y.last() || (!__y.last() && __x.row() < __y.row() ||
480 // (__x.row() == __y.row() && __x.col() < __y.col())));
481 // }
482 
483 // template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR,
484 // class G>
485 // inline bool
486 // operator<(const _Matrix_iterator<_Tp, _RefL, _PtrL, G>& __x,
487 // const _Matrix_iterator<_Tp, _RefR, _PtrR, G>& __y)
488 // {
489 // return !__x.last() &&
490 // (__y.last() || (!__y.last() && __x.row() < __y.row() ||
491 // (__x.row() == __y.row() && __x.col() < __y.col())));
492 // }
493 
494 // template <class _Tp, class _Ref, class _Ptr, class G>
495 // inline bool
496 // operator>(const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __x,
497 // const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __y)
498 // {
499 // return __y < __x;
500 // }
501 
502 // template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR,
503 // class G>
504 // inline bool
505 // operator>(const _Matrix_iterator<_Tp, _RefL, _PtrL, G>& __x,
506 // const _Matrix_iterator<_Tp, _RefR, _PtrR, G>& __y)
507 // {
508 // return __y < __x;
509 // }
510 
511 // template <class _Tp, class _Ref, class _Ptr, class G>
512 // inline bool
513 // operator<=(const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __x,
514 // const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __y)
515 // {
516 // return !(__y < __x);
517 // }
518 
519 // template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR,
520 // class G>
521 // inline bool
522 // operator<=(const _Matrix_iterator<_Tp, _RefL, _PtrL, G>& __x,
523 // const _Matrix_iterator<_Tp, _RefR, _PtrR, G>& __y)
524 // {
525 // return !(__y < __x);
526 // }
527 
528 // template <class _Tp, class _Ref, class _Ptr, class G>
529 // inline bool
530 // operator>=(const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __x,
531 // const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __y)
532 // {
533 // return !(__x < __y);
534 // }
535 
536 // template <class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR,
537 // class G>
538 // inline bool
539 // operator>=(const _Matrix_iterator<_Tp, _RefL, _PtrL, G>& __x,
540 // const _Matrix_iterator<_Tp, _RefR, _PtrR, G>& __y)
541 // {
542 // return !(__x < __y);
543 // }
544 
545 // template <typename _Tp, typename _RefL, typename _PtrL,
546 // typename _RefR, typename _PtrR, typename G>
547 // inline typename _Matrix_iterator<_Tp, _RefL, _PtrL, G>::difference_type
548 // operator-(const _Matrix_iterator<_Tp, _RefL, _PtrL, G>& __x,
549 // const _Matrix_iterator<_Tp, _RefR, _PtrR, G>& __y)
550 // {
551 // if (__x.last())
552 // if (__y.last()) return 0;
553 // else return __y.nofRows() * (__y.nofCols() - __y.row()) - __y.col();
554 // else if (__y.last())
555 // return __x.nofRows() * (__x.nofCols() - __x.row()) - __x.col();
556 // return _Matrix_iterator<_Tp, _RefL, _PtrL, G>::difference_type
557 // (__x.col() - __y.col() + __x.nofCols() * (__x.row() - __y.row()));
558 // }
559 
560 // template <class _Tp, class _Ref, class _Ptr, class G>
561 // inline _Matrix_iterator<_Tp, _Ref, _Ptr, G>
562 // operator+(ptrdiff_t __n, const _Matrix_iterator<_Tp, _Ref, _Ptr, G>& __x)
563 // {
564 // return __x + __n;
565 // }
566 
567 } // namespace concepts
568 
569 #endif // matrixIterator_hh
_Matrix_iterator< _Tp, _Tp &, _Tp * > iterator
Return Type reference (_Tp&) for non-constant iterator constant copy (const _Tp) for constant iterato...
std::random_access_iterator_tag iterator_category
_Self & operator=(const iterator &__x)
Assignment.
STL iterator for matrices.
#define conceptsException(exc)
Prepares an exception for throwing.
Definition: exceptions.hh:344
bool operator>(const _Matrix_iterator_base< _Tp, _Ref, _Ptr > &__x, const _Matrix_iterator_base< _Tp, _Ref, _Ptr > &__y)
_Matrix_iterator_base()
Constructor. Iterator stands at the end of any matrix.
bool operator>=(const _Matrix_iterator_base< _Tp, _Ref, _Ptr > &__x, const _Matrix_iterator_base< _Tp, _Ref, _Ptr > &__y)
_Self operator+(difference_type __n) const
const unsigned int nofCols() const
Number of columns.
ElementFormulaContainer< Real > operator-(const ElementFormulaContainer< Real > frm, const Real a)
Simple subtracting of a element formulas and a constant via –operator.
_Self & operator+=(difference_type __n)
_Matrix_iterator_base(const unsigned int nofRows, const unsigned int nofCols, const unsigned int r, const unsigned int c)
Constructor.
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
unsigned int row_
Current row and column.
_Matrix_iterator_base< _Tp, _Ref, _Ptr > _Base
_Matrix_iterator_base< _Tp, _Tp &, _Tp * > iterator
const unsigned int nofRows_
Number of rows and columns.
pointer operator->() const
Returns a pointer to the value, but only valid for non-constant matrices.
unsigned int row() const
Row.
F * securePointer(F &value, G *matrix)
Templated function, which prevent a pointer to a temporary value got from constant matrices with inde...
Exception class for assertions.
Definition: exceptions.hh:258
_Matrix_iterator< _Tp, const _Tp &, const _Tp * > const_iterator
_Base::return_type return_type
_Matrix_iterator_base(const _Matrix_iterator_base< _Tp, _RefR, _PtrR > &__x)
Copy constructor.
bool operator<=(const _Matrix_iterator_base< _Tp, _Ref, _Ptr > &__x, const _Matrix_iterator_base< _Tp, _Ref, _Ptr > &__y)
_Matrix_iterator_base< _Tp, const _Tp &, const _Tp * > const_iterator
_Self & operator=(const iterator &__x)
Assignment.
_Matrix_iterator(const _Matrix_iterator< _Tp, _RefR, _PtrR > &__x)
Copy constructor. Also from iterator to constant iterator.
Abstract class for an operator.
Definition: compositions.hh:31
matrix_type * matrix_
Matrix.
ReturnType< _Ref >::type return_type
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
_Matrix_iterator_base _Self
return_type operator[](difference_type __n) const
Base class for STL like iterator for matrices.
_Matrix_iterator(matrix_type &m, const unsigned int r=0, const unsigned int c=0)
Constructor.
bool operator<(const Cell &cell_x, const Cell &cell_y)
<-operator could be useful for sorting, e.g. in std::set.
bool operator!=(const _Matrix_iterator_base< _Tp, _Ref, _Ptr > &__x, const _Matrix_iterator_base< _Tp, _Ref, _Ptr > &__y)
unsigned int col() const
Column.
bool operator==(const Point< F, dim > &x, const Point< F, dim > &y)
Exception class to express a missing feature.
Definition: exceptions.hh:206
Non-constant matrix for non-constant iterator.
matrix_type * matrix() const
Pointer to the matrix, needed for copy constructor.
const unsigned int nofRows() const
Number of rows.
Frm_Sum< Real > operator+(const Formula< Real > &frm, const Real a)
Simple adding two formulas by +-operator.
_Matrix_iterator()
Constructor. Iterator stands at the end of any matrix.
bool last_
Iterator is behind the last entrance.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
_Self & operator-=(difference_type __n)
bool last() const
Returns true, if iterator is behind the last entry.
Class providing an output operator.
MatrixType< _Tp, _Ref, _Ptr >::type matrix_type
return_type operator*() const
Dereferencation.
_Self operator-(difference_type __n) const
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