Simbody  3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-13 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  VectorIterator(VECTOR_CLASS& vector, ptrdiff_t index)
60  : vector(vector), index(index) {}
62  : vector(iter.vector), index(iter.index) {}
64  vector = iter.vector;
65  index = iter.index;
66  return *this;
67  }
68  ELT& operator*() {
69  assert (index >= 0 && index < vector.size());
70  return vector[(int)index];
71  }
72  ELT& operator[](ptrdiff_t i) {
73  assert (i >= 0 && i < vector.size());
74  return vector[(int)i];
75  }
77  assert (index < vector.size());
78  ++index;
79  return *this;
80  }
82  assert (index < vector.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 <= vector.size());
100  index += n;
101  return *this;
102  }
103  VectorIterator operator-=(ptrdiff_t n) {
104  assert (0 <= index-n && index-n <= vector.size());
105  index -= n;
106  return *this;
107  }
108  bool operator<(VectorIterator iter) const {
109  return (index < iter.index);
110  }
111  bool operator>(VectorIterator iter) const {
112  return (index > iter.index);
113  }
114  bool operator<=(VectorIterator iter) const {
115  return (index <= iter.index);
116  }
117  bool operator>=(VectorIterator iter) const {
118  return (index >= iter.index);
119  }
120  ptrdiff_t operator-(VectorIterator iter) const {
121  return (index - iter.index);
122  }
123  VectorIterator operator-(ptrdiff_t n) const {
124  return VectorIterator(vector, index-n);
125  }
126  VectorIterator operator+(ptrdiff_t n) const {
127  return VectorIterator(vector, index+n);
128  }
129  bool operator==(VectorIterator iter) const {
130  return (index == iter.index);
131  }
132  bool operator!=(VectorIterator iter) const {
133  return (index != iter.index);
134  }
135 private:
136  VECTOR_CLASS& vector;
137  ptrdiff_t index;
138 };
139 
140 } //namespace SimTK
141 
142 #endif // SimTK_SIMMATRIX_VECTORITERATOR_H_
VectorIterator operator--()
Definition: VectorIterator.h:87
ELT & reference
Definition: VectorIterator.h:56
VectorIterator(const VectorIterator &iter)
Definition: VectorIterator.h:61
bool operator>(VectorIterator iter) const
Definition: VectorIterator.h:111
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
bool operator>=(VectorIterator iter) const
Definition: VectorIterator.h:117
VectorIterator operator+(ptrdiff_t n) const
Definition: VectorIterator.h:126
bool operator<=(VectorIterator iter) const
Definition: VectorIterator.h:114
std::random_access_iterator_tag iterator_category
Definition: VectorIterator.h:58
VectorIterator & operator=(const VectorIterator &iter)
Definition: VectorIterator.h:63
VectorIterator operator+=(ptrdiff_t n)
Definition: VectorIterator.h:98
ELT value_type
Definition: VectorIterator.h:54
bool operator!=(VectorIterator iter) const
Definition: VectorIterator.h:132
bool operator==(VectorIterator iter) const
Definition: VectorIterator.h:129
ELT * pointer
Definition: VectorIterator.h:57
ELT & operator*()
Definition: VectorIterator.h:68
ptrdiff_t difference_type
Definition: VectorIterator.h:55
VectorIterator operator-(ptrdiff_t n) const
Definition: VectorIterator.h:123
ptrdiff_t operator-(VectorIterator iter) const
Definition: VectorIterator.h:120
bool operator<(VectorIterator iter) const
Definition: VectorIterator.h:108
VectorIterator(VECTOR_CLASS &vector, ptrdiff_t index)
Definition: VectorIterator.h:59
VectorIterator operator++(int)
Definition: VectorIterator.h:81
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:92
ELT & operator[](ptrdiff_t i)
Definition: VectorIterator.h:72
VectorIterator operator-=(ptrdiff_t n)
Definition: VectorIterator.h:103
VectorIterator operator++()
Definition: VectorIterator.h:76