Ravelin
RowIterator.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 ROW_ITERATOR
8 #error This class is not to be included by the user directly. Use RowIteratorf.h or RowIteratord.h instead.
9 #endif
10 
12 class ROW_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_ROW_ITERATOR;
31 
32  public:
33  ROW_ITERATOR()
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  ROW_ITERATOR end() const
44  {
45  if (_count <= _sz)
46  return *this + (_sz - _count);
47  else
48  return *this - (_count - _sz);
49  }
50 
51  ROW_ITERATOR& operator+=(int n)
52  {
53  // if there are no columns, verify that n = 0
54  if (_columns == 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 / _columns) + (_count % _columns)*_ld;
65 
66  return *this;
67  }
68 
69  ROW_ITERATOR& operator-=(int n)
70  {
71  // if there are no columns, verify that n = 0
72  if (_columns == 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 / _columns) + (_count % _columns)*_ld;
83 
84  return *this;
85  }
86 
87  REAL& operator[](int i) const
88  {
89  assert(_columns > 0);
90  int j = i + _count;
91  if (j < 0 && j > _sz)
92  throw std::runtime_error("Data outside of scope!");
93  return _data_start[(j / _columns) + (j % _columns)*_ld];
94  }
95 
96  ROW_ITERATOR operator+(int n) const
97  {
98  ROW_ITERATOR b = *this;
99  b += n;
100  return b;
101  }
102 
103  ROW_ITERATOR operator-(int n) const
104  {
105  ROW_ITERATOR b = *this;
106  b -= n;
107  return b;
108  }
109 
110  bool operator<(const ROW_ITERATOR& j) const
111  {
112  return _count < j._count;
113  }
114 
115  bool operator>(const ROW_ITERATOR& j) const
116  {
117  return _count > j._count;
118  }
119 
120  REAL& operator*() const
121  {
122  if (_count < 0 || _count >= _sz)
123  throw std::runtime_error("Iterator outside of range!");
124  return *_current_data;
125  }
126 
127  int operator-(const ROW_ITERATOR& b) const
128  {
129  return _count - b._count;
130  }
131 
132  bool operator==(const ROW_ITERATOR& b) const
133  {
134  // verify that we're not comparing two dissimilar iterators
135  assert(_data_start == b._data_start &&
136  _sz == b._sz &&
137  _ld == b._ld &&
138  _rows == b._rows &&
139  _columns == b._columns);
140 
141  return (_count == b._count);
142  }
143 
144  bool operator!=(const ROW_ITERATOR& j) const { return !operator==(j); }
145 
146  // prefix--
147  ROW_ITERATOR& operator--()
148  {
149  assert(_columns > 0);
150  if (--_count % _columns == 0)
151  _current_data = _data_start + (_count / _columns);
152  else
153  _current_data -= _ld;
154 
155  return *this;
156  }
157 
158  // prefix++
159  ROW_ITERATOR& operator++()
160  {
161  assert(_columns > 0);
162  if (++_count % _columns == 0)
163  _current_data = _data_start + (_count / _columns);
164  else
165  _current_data += _ld;
166 
167  return *this;
168  }
169 
170  // postfix--
171  ROW_ITERATOR operator--(int)
172  {
173  ROW_ITERATOR b = *this;
174  this->operator--();
175  return b;
176  }
177 
178  // postfix++
179  ROW_ITERATOR operator++(int)
180  {
181  ROW_ITERATOR b = *this;
182  this->operator++();
183  return b;
184  }
185 
186  // assignment operator
187  ROW_ITERATOR& operator=(const ROW_ITERATOR& i)
188  {
189  _current_data = i._current_data;
190  _count = i._count;
191  _sz = i._sz;
192  _data_start = i._data_start;
193  _ld = i._ld;
194  _columns = i._columns;
195  _rows = i._rows;
196  return *this;
197  }
198 
199  protected:
200  int _count; // number of iterator increments
201  int _sz; // size of the data block
202  REAL* _data_start; // pointer to the start of the data block
203  REAL* _current_data; // pointer to data corresponding to iterator state
204  unsigned _ld; // leading dimension of matrix
205  unsigned _columns; // columns of the matrix
206  unsigned _rows; // rows of the matrix
207 }; // end class
208 
210 class CONST_ROW_ITERATOR : public std::iterator<std::random_access_iterator_tag, REAL>
211 {
212  friend class MATRIXN;
213  friend class VECTORN;
214  friend class VECTOR3;
215  friend class ORIGIN3;
216  friend class VECTOR2;
217  friend class ORIGIN2;
218  friend class MATRIX3;
219  friend class MATRIX2;
220  friend class SHAREDVECTORN;
221  friend class SHAREDMATRIXN;
222  friend class CONST_SHAREDVECTORN;
223  friend class CONST_SHAREDMATRIXN;
224  friend class SVECTOR6;
225  friend class SFORCE;
226  friend class SVELOCITY;
227  friend class SACCEL;
228 
229  public:
231  {
232  _data_start = _current_data = NULL;
233  _count = 0;
234  _sz = 0;
235  _rows = 0;
236  _ld = 0;
237  _columns = 0;
238  }
239 
242  {
243  _data_start = i._data_start;
244  _current_data = i._current_data;
245  _count = i._count;
246  _sz = i._sz;
247  _rows = i._rows;
248  _ld = i._ld;
249  _columns = i._columns;
250  }
251 
254  {
255  if (_count <= _sz)
256  return *this + (_sz - _count);
257  else
258  return *this - (_count - _sz);
259  }
260 
261  CONST_ROW_ITERATOR& operator+=(int n)
262  {
263  // if there are no columns, verify that n = 0
264  if (_columns == 0)
265  {
266  assert(n == 0);
267  return *this;
268  }
269 
270  // update the count
271  _count += n;
272 
273  // update the current data
274  _current_data = _data_start + (_count / _columns) + (_count % _columns)*_ld;
275 
276  return *this;
277  }
278 
279  CONST_ROW_ITERATOR& operator-=(int n)
280  {
281  // if there are no columns, verify that n = 0
282  if (_columns == 0)
283  {
284  assert(n == 0);
285  return *this;
286  }
287 
288  // update the count
289  _count -= n;
290 
291  // update the current data
292  _current_data = _data_start + (_count / _columns) + (_count % _columns)*_ld;
293 
294  return *this;
295  }
296 
297  const REAL& operator[](int i) const
298  {
299  int j = i + _count;
300  if (j < 0 && j > _sz)
301  throw std::runtime_error("Data outside of scope!");
302  return _data_start[(j / _columns) + (j % _columns)*_ld];
303  }
304 
305  CONST_ROW_ITERATOR operator+(int n) const
306  {
307  CONST_ROW_ITERATOR b = *this;
308  b += n;
309  return b;
310  }
311 
312  CONST_ROW_ITERATOR operator-(int n) const
313  {
314  CONST_ROW_ITERATOR b = *this;
315  b -= n;
316  return b;
317  }
318 
319  bool operator<(const CONST_ROW_ITERATOR& j) const
320  {
321  return _count < j._count;
322  }
323 
324  bool operator>(const CONST_ROW_ITERATOR& j) const
325  {
326  return _count > j._count;
327  }
328 
329  const REAL& operator*() const
330  {
331  if (_count >= _sz || _count < 0)
332  throw std::runtime_error("Iterator outside of range!");
333  return *_current_data;
334  }
335 
336  int operator-(const CONST_ROW_ITERATOR& b) const
337  {
338  return _count - b._count;
339  }
340 
341  bool operator==(const CONST_ROW_ITERATOR& b) const
342  {
343  // verify that we're not comparing two dissimilar iterators
344  assert(_data_start == b._data_start &&
345  _sz == b._sz &&
346  _ld == b._ld &&
347  _columns == b._columns &&
348  _rows == b._rows);
349 
350  return (_count == b._count);
351  }
352 
353  bool operator!=(const CONST_ROW_ITERATOR& j) const { return !operator==(j); }
354 
355  // prefix--
356  CONST_ROW_ITERATOR& operator--()
357  {
358  assert(_columns > 0);
359 
360  if (--_count % _columns == 0)
361  _current_data = _data_start + (_count / _columns);
362  else
363  _current_data -= _ld;
364 
365  return *this;
366  }
367 
368  // prefix++
369  CONST_ROW_ITERATOR& operator++()
370  {
371  assert(_columns > 0);
372 
373  if (++_count % _columns == 0)
374  _current_data = _data_start + (_count / _columns);
375  else
376  _current_data += _ld;
377 
378  return *this;
379  }
380 
381  // postfix--
382  CONST_ROW_ITERATOR operator--(int)
383  {
384  CONST_ROW_ITERATOR b = *this;
385  this->operator--();
386  return b;
387  }
388 
389  // postfix++
390  CONST_ROW_ITERATOR operator++(int)
391  {
392  CONST_ROW_ITERATOR b = *this;
393  this->operator++();
394  return b;
395  }
396 
397  // assignment operator
398  CONST_ROW_ITERATOR& operator=(const CONST_ROW_ITERATOR& i)
399  {
400  _current_data = i._current_data;
401  _count = i._count;
402  _sz = i._sz;
403  _data_start = i._data_start;
404  _ld = i._ld;
405  _columns = i._columns;
406  _rows = i._rows;
407  return *this;
408  }
409 
410  protected:
411  int _count; // number of iterator increments
412  int _sz; // size of the data block
413  const REAL* _data_start; // pointer to the start of the data block
414  const REAL* _current_data; // pointer to data corresponding to iterator state
415  unsigned _ld; // leading dimension of matrix
416  unsigned _columns; // columns of the matrix
417  unsigned _rows; // rows of the matrix
418 }; // end class
419 
420 
A construct for iterating over a rectangular block of a matrix.
Definition: RowIterator.h:210
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
CONST_ROW_ITERATOR end() const
Gets the iterator at the end of this block.
Definition: RowIterator.h:253
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
CONST_ROW_ITERATOR(ROW_ITERATOR i)
Converts a non-constant row iterator to a constant one.
Definition: RowIterator.h:241
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 generic, possibly non-square matrix using constant shared data.
Definition: SharedMatrixN.h:19
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
A construct for iterating over a rectangular block of a matrix.
Definition: RowIterator.h:12