Ravelin
ColumnIterator.h
1 /****************************************************************************
2  * Copyright 2013 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef COLUMN_ITERATOR
8 #error This class is not to be included by the user directly. Use ColumnIteratorf.h or ColumnIteratord.h instead.
9 #endif
10 
12 class COLUMN_ITERATOR : public std::iterator<std::random_access_iterator_tag, REAL>
13 {
14  friend class MATRIXN;
15  friend class VECTORN;
16  friend class VECTOR3;
17  friend class ORIGIN3;
18  friend class VECTOR2;
19  friend class ORIGIN2;
20  friend class MATRIX3;
21  friend class MATRIX2;
22  friend class SHAREDVECTORN;
23  friend class SHAREDMATRIXN;
24  friend class CONST_SHAREDVECTORN;
25  friend class CONST_SHAREDMATRIXN;
26  friend class SVECTOR6;
27  friend class SFORCE;
28  friend class SVELOCITY;
29  friend class SACCEL;
30  friend class CONST_COLUMN_ITERATOR;
31 
32  public:
34  {
35  _data_start = _current_data = NULL;
36  _count = 0;
37  _sz = 0;
38  _rows = 0;
39  _ld = 0;
40  _columns = 0;
41  }
42 
43  COLUMN_ITERATOR end() const
44  {
45  if (_count <= _sz)
46  return *this + (_sz - _count);
47  else
48  return *this - (_count - _sz);
49  }
50 
51  COLUMN_ITERATOR& operator+=(int n)
52  {
53  // if there are no rows, verify that n = 0
54  if (_rows == 0)
55  {
56  assert(n == 0);
57  return *this;
58  }
59 
60  // update the count
61  _count += n;
62 
63  // update the current data
64  _current_data = _data_start + (_count / _rows)*_ld + (_count % _rows);
65 
66  return *this;
67  }
68 
69  COLUMN_ITERATOR& operator-=(int n)
70  {
71  // if there are no rows, verify that n = 0
72  if (_rows == 0)
73  {
74  assert(n == 0);
75  return *this;
76  }
77 
78  // update the count
79  _count -= n;
80 
81  // update the current data
82  _current_data = _data_start + (_count / _rows)*_ld + (_count % _rows);
83 
84  return *this;
85  }
86 
87  REAL& operator[](int i) const
88  {
89  // verify user not doing something wrong
90  assert (_rows > 0);
91 
92  int j = i + _count;
93  if (j > _sz || j < 0)
94  throw std::runtime_error("Data outside of scope!");
95  return _data_start[(j / _rows)*_ld + (j % _rows)];
96  }
97 
98  COLUMN_ITERATOR operator+(int n) const
99  {
100  COLUMN_ITERATOR b = *this;
101  b += n;
102  return b;
103  }
104 
105  COLUMN_ITERATOR operator-(int n) const
106  {
107  COLUMN_ITERATOR b = *this;
108  b -= n;
109  return b;
110  }
111 
112  bool operator<(const COLUMN_ITERATOR& j) const
113  {
114  return _count < j._count;
115  }
116 
117  bool operator>(const COLUMN_ITERATOR& j) const
118  {
119  return _count > j._count;
120  }
121 
122  REAL& operator*() const
123  {
124  if (_count < 0 || _count >= _sz)
125  throw std::runtime_error("Iterator outside of range!");
126  return *_current_data;
127  }
128 
129  int operator-(const COLUMN_ITERATOR& b) const
130  {
131  return _count - b._count;
132  }
133 
134  bool operator==(const COLUMN_ITERATOR& b) const
135  {
136  // verify that we're not comparing two dissimilar iterators
137  assert(_data_start == b._data_start &&
138  _sz == b._sz &&
139  _ld == b._ld &&
140  _rows == b._rows &&
141  _columns == b._columns);
142 
143  return (_count == b._count);
144  }
145 
146  bool operator!=(const COLUMN_ITERATOR& j) const { return !operator==(j); }
147 
148  // prefix--
149  COLUMN_ITERATOR& operator--()
150  {
151  // verify user not doing something wrong
152  assert(_rows > 0);
153 
154  _count--;
155  _current_data--;
156  if (_count % _rows == 0)
157  _current_data -= (_ld - _rows);
158 
159  return *this;
160  }
161 
162  // prefix++
163  COLUMN_ITERATOR& operator++()
164  {
165  // verify user not doing something wrong
166  assert(_rows > 0);
167 
168  _count++;
169  _current_data++;
170  if (_count % _rows == 0)
171  _current_data += (_ld - _rows);
172 
173  return *this;
174  }
175 
176  // postfix--
177  COLUMN_ITERATOR operator--(int)
178  {
179  COLUMN_ITERATOR b = *this;
180  this->operator--();
181  return b;
182  }
183 
184  // postfix++
185  COLUMN_ITERATOR operator++(int)
186  {
187  COLUMN_ITERATOR b = *this;
188  this->operator++();
189  return b;
190  }
191 
192  // assignment operator
193  COLUMN_ITERATOR& operator=(const COLUMN_ITERATOR& i)
194  {
195  _current_data = i._current_data;
196  _count = i._count;
197  _sz = i._sz;
198  _data_start = i._data_start;
199  _ld = i._ld;
200  _columns = i._columns;
201  _rows = i._rows;
202  return *this;
203  }
204 
205  protected:
206  int _count, _sz;
207  REAL* _data_start;
208  REAL* _current_data;
209  unsigned _ld;
210  unsigned _columns;
211  unsigned _rows;
212 }; // end class
213 
215 class CONST_COLUMN_ITERATOR : public std::iterator<std::random_access_iterator_tag, REAL>
216 {
217  friend class MATRIXN;
218  friend class VECTORN;
219  friend class VECTOR3;
220  friend class ORIGIN3;
221  friend class VECTOR2;
222  friend class ORIGIN2;
223  friend class MATRIX3;
224  friend class MATRIX2;
225  friend class SHAREDVECTORN;
226  friend class SHAREDMATRIXN;
227  friend class CONST_SHAREDVECTORN;
228  friend class CONST_SHAREDMATRIXN;
229  friend class SVECTOR6;
230  friend class SFORCE;
231  friend class SVELOCITY;
232  friend class SACCEL;
233 
234  public:
236  {
237  _data_start = _current_data = NULL;
238  _count = 0;
239  _sz = 0;
240  _rows = 0;
241  _ld = 0;
242  _columns = 0;
243  }
244 
247  {
248  _data_start = i._data_start;
249  _current_data = i._current_data;
250  _count = i._count;
251  _sz = i._sz;
252  _rows = i._rows;
253  _ld = i._ld;
254  _columns = i._columns;
255  }
256 
259  {
260  if (_count <= _sz)
261  return *this + (_sz - _count);
262  else
263  return *this - (_count - _sz);
264  }
265 
266  CONST_COLUMN_ITERATOR& operator+=(int n)
267  {
268  // if there are no rows, verify that n = 0
269  if (_rows == 0)
270  {
271  assert(n == 0);
272  return *this;
273  }
274 
275  // update the count
276  _count += n;
277 
278  // update the current data
279  _current_data = _data_start + (_count / _rows)*_ld + (_count % _rows);
280 
281  return *this;
282  }
283 
284  CONST_COLUMN_ITERATOR& operator-=(int n)
285  {
286  // if there are no rows, verify that n = 0
287  if (_rows == 0)
288  {
289  assert(n == 0);
290  return *this;
291  }
292 
293  // update the count
294  _count -= n;
295 
296  // update the current data
297  _current_data = _data_start + (_count / _rows)*_ld + (_count % _rows);
298 
299  return *this;
300  }
301 
302  const REAL& operator[](int i) const
303  {
304  // verify user not doing something wrong
305  assert(_rows > 0);
306 
307  int j = i + _count;
308  if (j > _sz || j < 0)
309  throw std::runtime_error("Data outside of scope!");
310  return _data_start[(j / _rows)*_ld + (j % _rows)];
311  }
312 
313  CONST_COLUMN_ITERATOR operator+(int n) const
314  {
315  CONST_COLUMN_ITERATOR b = *this;
316  b += n;
317  return b;
318  }
319 
320  CONST_COLUMN_ITERATOR operator-(int n) const
321  {
322  CONST_COLUMN_ITERATOR b = *this;
323  b -= n;
324  return b;
325  }
326 
327  bool operator<(const CONST_COLUMN_ITERATOR& j) const
328  {
329  return _count < j._count;
330  }
331 
332  bool operator>(const CONST_COLUMN_ITERATOR& j) const
333  {
334  return _count > j._count;
335  }
336 
337  const REAL& operator*() const
338  {
339  if (_count < 0 || _count >= _sz)
340  throw std::runtime_error("Iterator outside of range!");
341  return *_current_data;
342  }
343 
344  int operator-(const CONST_COLUMN_ITERATOR& b) const
345  {
346  return _count - b._count;
347  }
348 
349  bool operator==(const CONST_COLUMN_ITERATOR& b) const
350  {
351  // verify that we're not comparing two dissimilar iterators
352  assert(_data_start == b._data_start &&
353  _sz == b._sz &&
354  _ld == b._ld &&
355  _columns == b._columns &&
356  _rows == b._rows);
357 
358  return (_count == b._count);
359  }
360 
361  bool operator!=(const CONST_COLUMN_ITERATOR& j) const { return !operator==(j); }
362 
363  // prefix--
364  CONST_COLUMN_ITERATOR& operator--()
365  {
366  // verify user not doing something wrong
367  assert(_rows > 0);
368 
369  _count--;
370  _current_data--;
371  if (_count % _rows == 0)
372  _current_data -= (_ld - _rows);
373 
374  return *this;
375  }
376 
377  // prefix++
378  CONST_COLUMN_ITERATOR& operator++()
379  {
380  // verify user not doing something wrong
381  assert(_rows > 0);
382 
383  _count++;
384  _current_data++;
385  if (_count % _rows == 0)
386  _current_data += (_ld - _rows);
387 
388  return *this;
389  }
390 
391  // postfix--
392  CONST_COLUMN_ITERATOR operator--(int n)
393  {
394  // verify user not doing something wrong
395  assert(_rows > 0);
396 
397  CONST_COLUMN_ITERATOR b = *this;
398  this->operator--();
399  return b;
400  }
401 
402  // postfix++
403  CONST_COLUMN_ITERATOR operator++(int n)
404  {
405  CONST_COLUMN_ITERATOR b = *this;
406  this->operator++();
407  return b;
408  }
409 
410  // assignment operator
411  CONST_COLUMN_ITERATOR& operator=(const CONST_COLUMN_ITERATOR& i)
412  {
413  _current_data = i._current_data;
414  _count = i._count;
415  _sz = i._sz;
416  _data_start = i._data_start;
417  _ld = i._ld;
418  _columns = i._columns;
419  _rows = i._rows;
420  return *this;
421  }
422 
423  protected:
424  int _count, _sz;
425  const REAL* _data_start;
426  const REAL* _current_data;
427  unsigned _ld;
428  unsigned _columns;
429  unsigned _rows;
430 }; // end class
431 
432 
CONST_COLUMN_ITERATOR end() const
Gets the iterator at the end of this block.
Definition: ColumnIterator.h:258
A general 2x2 matrix.
Definition: Matrix2.h:16
A two-dimensional floating point vector used for computational geometry calculations and with associa...
Definition: Vector2.h:15
SACCEL operator-() const
Returns the negation of this vector.
Definition: SAccel.h:96
A generic, possibly non-square matrix.
Definition: MatrixN.h:18
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:77
A generic, possibly non-square matrix using shared data.
Definition: SharedMatrixN.h:59
A spatial velocity (a twist)
Definition: SVelocity.h:15
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:15
A 6-dimensional floating-point vector for use with spatial algebra.
Definition: SVector6.h:22
A spatial (six dimensional) acceleration.
Definition: SAccel.h:14
A generic N-dimensional floating point vector.
Definition: VectorN.h:16
A three-dimensional floating point vector used for representing points and vectors in 3D with associa...
Definition: Vector3.h:15
A spatial force (a wrench)
Definition: SForce.h:14
A three-dimensional floating point vector used for representing points and vectors in 3D and without ...
Definition: Origin3.h:16
A construct for iterating over a rectangular block of a matrix.
Definition: ColumnIterator.h:12
A generic, possibly non-square matrix using constant shared data.
Definition: SharedMatrixN.h:19
A construct for iterating over a rectangular block of a matrix.
Definition: ColumnIterator.h:215
A two-dimensional floating point vector used for computational geometry calculations and without asso...
Definition: Origin2.h:14
A 3x3 matrix that may be used for orientation, inertia tensors, etc.
Definition: Matrix3.h:20
CONST_COLUMN_ITERATOR(COLUMN_ITERATOR i)
Converts a non-constant column iterator to a constant one.
Definition: ColumnIterator.h:246