Simbody  3.6
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 
66  VectorIterator() = delete;
67 
70  VectorIterator(const VectorIterator& source) = default;
71 
74  VectorIterator& operator=(const VectorIterator& source) = default;
75 
76  ELT& operator*() {
77  assert (index >= 0 && index < vectorp->size());
78  return (*vectorp)[(int)index];
79  }
80  ELT& operator[](ptrdiff_t i) {
81  assert (i >= 0 && i < vectorp->size());
82  return (*vectorp)[(int)i];
83  }
85  assert (index < vectorp->size());
86  ++index;
87  return *this;
88  }
90  assert (index < vectorp->size());
91  VectorIterator current = *this;
92  ++index;
93  return current;
94  }
96  assert (index > 0);
97  --index;
98  return *this;
99  }
101  assert (index > 0);
102  VectorIterator current = *this;
103  --index;
104  return current;
105  }
106  VectorIterator& operator+=(ptrdiff_t n) {
107  assert (0 <= index+n && index+n <= vectorp->size());
108  index += n;
109  return *this;
110  }
111  VectorIterator& operator-=(ptrdiff_t n) {
112  assert (0 <= index-n && index-n <= vectorp->size());
113  index -= n;
114  return *this;
115  }
116  bool operator<(const VectorIterator& iter) const {
117  return (index < iter.index);
118  }
119  bool operator>(const VectorIterator& iter) const {
120  return (index > iter.index);
121  }
122  bool operator<=(const VectorIterator& iter) const {
123  return (index <= iter.index);
124  }
125  bool operator>=(const VectorIterator& iter) const {
126  return (index >= iter.index);
127  }
128  ptrdiff_t operator-(const VectorIterator& iter) const {
129  return (index - iter.index);
130  }
131  VectorIterator operator-(ptrdiff_t n) const {
132  return VectorIterator(*vectorp, index-n);
133  }
134  VectorIterator operator+(ptrdiff_t n) const {
135  return VectorIterator(*vectorp, index+n);
136  }
137  bool operator==(const VectorIterator& iter) const {
138  return (index == iter.index);
139  }
140  bool operator!=(const VectorIterator& iter) const {
141  return (index != iter.index);
142  }
143 private:
144  VECTOR_CLASS* vectorp;
145  ptrdiff_t index;
146 };
147 
148 } //namespace SimTK
149 
150 #endif // SimTK_SIMMATRIX_VECTORITERATOR_H_
bool operator!=(const VectorIterator &iter) const
Definition: VectorIterator.h:140
bool operator>=(const VectorIterator &iter) const
Definition: VectorIterator.h:125
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
VectorIterator operator-(ptrdiff_t n) const
Definition: VectorIterator.h:131
std::random_access_iterator_tag iterator_category
Definition: VectorIterator.h:58
bool operator>(const VectorIterator &iter) const
Definition: VectorIterator.h:119
bool operator<=(const VectorIterator &iter) const
Definition: VectorIterator.h:122
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:106
ptrdiff_t operator-(const VectorIterator &iter) const
Definition: VectorIterator.h:128
VectorIterator operator+(ptrdiff_t n) const
Definition: VectorIterator.h:134
VectorIterator()=delete
No default constructor.
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:89
bool operator<(const VectorIterator &iter) const
Definition: VectorIterator.h:116
VectorIterator operator--(int)
Definition: VectorIterator.h:100
VectorIterator & operator=(const VectorIterator &source)=default
Copy assignment makes this iterator refer to the same element of the same vector as the source iterat...
ELT & operator[](ptrdiff_t i)
Definition: VectorIterator.h:80
VectorIterator & operator--()
Definition: VectorIterator.h:95
ELT & operator*()
Definition: VectorIterator.h:76
VectorIterator & operator-=(ptrdiff_t n)
Definition: VectorIterator.h:111
ELT value_type
Definition: VectorIterator.h:54
VectorIterator & operator++()
Definition: VectorIterator.h:84
ptrdiff_t difference_type
Definition: VectorIterator.h:55
ELT & reference
Definition: VectorIterator.h:56
ELT * pointer
Definition: VectorIterator.h:57
bool operator==(const VectorIterator &iter) const
Definition: VectorIterator.h:137