Simbody  3.5
VectorIterator.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATRIX_VECTORITERATOR_H_
2 #define SimTK_SIMMATRIX_VECTORITERATOR_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2005-15 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: Michael Sherman *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
31 #include <cstddef>
32 
33 namespace SimTK {
34 
35 //==============================================================================
36 // VECTOR ITERATOR
37 //==============================================================================
51 template <class ELT, class VECTOR_CLASS>
52 class VectorIterator {
53 public:
54  typedef ELT value_type;
55  typedef ptrdiff_t difference_type;
56  typedef ELT& reference;
57  typedef ELT* pointer;
58  typedef std::random_access_iterator_tag iterator_category;
59 
62  VectorIterator(VECTOR_CLASS& vector, ptrdiff_t index)
63  : vectorp(&vector), index(index) {}
64 
65  // Default copy constructor, copy assignment, destructor.
66  // No default constructor.
67 
68  ELT& operator*() {
69  assert (index >= 0 && index < vectorp->size());
70  return (*vectorp)[(int)index];
71  }
72  ELT& operator[](ptrdiff_t i) {
73  assert (i >= 0 && i < vectorp->size());
74  return (*vectorp)[(int)i];
75  }
77  assert (index < vectorp->size());
78  ++index;
79  return *this;
80  }
82  assert (index < vectorp->size());
83  VectorIterator current = *this;
84  ++index;
85  return current;
86  }
88  assert (index > 0);
89  --index;
90  return *this;
91  }
93  assert (index > 0);
94  VectorIterator current = *this;
95  --index;
96  return current;
97  }
98  VectorIterator& operator+=(ptrdiff_t n) {
99  assert (0 <= index+n && index+n <= vectorp->size());
100  index += n;
101  return *this;
102  }
103  VectorIterator& operator-=(ptrdiff_t n) {
104  assert (0 <= index-n && index-n <= vectorp->size());
105  index -= n;
106  return *this;
107  }
108  bool operator<(const VectorIterator& iter) const {
109  return (index < iter.index);
110  }
111  bool operator>(const VectorIterator& iter) const {
112  return (index > iter.index);
113  }
114  bool operator<=(const VectorIterator& iter) const {
115  return (index <= iter.index);
116  }
117  bool operator>=(const VectorIterator& iter) const {
118  return (index >= iter.index);
119  }
120  ptrdiff_t operator-(const VectorIterator& iter) const {
121  return (index - iter.index);
122  }
123  VectorIterator operator-(ptrdiff_t n) const {
124  return VectorIterator(*vectorp, index-n);
125  }
126  VectorIterator operator+(ptrdiff_t n) const {
127  return VectorIterator(*vectorp, index+n);
128  }
129  bool operator==(const VectorIterator& iter) const {
130  return (index == iter.index);
131  }
132  bool operator!=(const VectorIterator& iter) const {
133  return (index != iter.index);
134  }
135 private:
136  VECTOR_CLASS* vectorp;
137  ptrdiff_t index;
138 };
139 
140 } //namespace SimTK
141 
142 #endif // SimTK_SIMMATRIX_VECTORITERATOR_H_
ptrdiff_t operator-(const VectorIterator &iter) const
Definition: VectorIterator.h:120
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
std::random_access_iterator_tag iterator_category
Definition: VectorIterator.h:58
VectorIterator operator-(ptrdiff_t n) const
Definition: VectorIterator.h:123
bool operator>=(const VectorIterator &iter) const
Definition: VectorIterator.h:117
VectorIterator(VECTOR_CLASS &vector, ptrdiff_t index)
Create an iterator for the supplied `vector` and set it to refer to the element at `index`...
Definition: VectorIterator.h:62
VectorIterator & operator+=(ptrdiff_t n)
Definition: VectorIterator.h:98
bool operator>(const VectorIterator &iter) const
Definition: VectorIterator.h:111
VectorIterator operator+(ptrdiff_t n) const
Definition: VectorIterator.h:126
This is an iterator for iterating over the elements of a Vector_ or Vec object.
Definition: BigMatrix.h:176
VectorIterator operator++(int)
Definition: VectorIterator.h:81
VectorIterator operator--(int)
Definition: VectorIterator.h:92
bool operator<=(const VectorIterator &iter) const
Definition: VectorIterator.h:114
bool operator!=(const VectorIterator &iter) const
Definition: VectorIterator.h:132
ELT & operator[](ptrdiff_t i)
Definition: VectorIterator.h:72
bool operator==(const VectorIterator &iter) const
Definition: VectorIterator.h:129
VectorIterator & operator--()
Definition: VectorIterator.h:87
ELT & operator*()
Definition: VectorIterator.h:68
VectorIterator & operator-=(ptrdiff_t n)
Definition: VectorIterator.h:103
bool operator<(const VectorIterator &iter) const
Definition: VectorIterator.h:108
ELT value_type
Definition: VectorIterator.h:54
VectorIterator & operator++()
Definition: VectorIterator.h:76
ptrdiff_t difference_type
Definition: VectorIterator.h:55
ELT & reference
Definition: VectorIterator.h:56
ELT * pointer
Definition: VectorIterator.h:57