Simbody
3.5
|
00001 #ifndef SimTK_SIMMATRIX_VECTORITERATOR_H_ 00002 #define SimTK_SIMMATRIX_VECTORITERATOR_H_ 00003 00004 /* -------------------------------------------------------------------------- * 00005 * Simbody(tm): SimTKcommon * 00006 * -------------------------------------------------------------------------- * 00007 * This is part of the SimTK biosimulation toolkit originating from * 00008 * Simbios, the NIH National Center for Physics-Based Simulation of * 00009 * Biological Structures at Stanford, funded under the NIH Roadmap for * 00010 * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. * 00011 * * 00012 * Portions copyright (c) 2005-13 Stanford University and the Authors. * 00013 * Authors: Peter Eastman * 00014 * Contributors: Michael Sherman * 00015 * * 00016 * Licensed under the Apache License, Version 2.0 (the "License"); you may * 00017 * not use this file except in compliance with the License. You may obtain a * 00018 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * 00019 * * 00020 * Unless required by applicable law or agreed to in writing, software * 00021 * distributed under the License is distributed on an "AS IS" BASIS, * 00022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 00023 * See the License for the specific language governing permissions and * 00024 * limitations under the License. * 00025 * -------------------------------------------------------------------------- */ 00026 00031 #include <cstddef> 00032 00033 namespace SimTK { 00034 00035 //============================================================================== 00036 // VECTOR ITERATOR 00037 //============================================================================== 00051 template <class ELT, class VECTOR_CLASS> 00052 class VectorIterator { 00053 public: 00054 typedef ELT value_type; 00055 typedef ptrdiff_t difference_type; 00056 typedef ELT& reference; 00057 typedef ELT* pointer; 00058 typedef std::random_access_iterator_tag iterator_category; 00059 VectorIterator(VECTOR_CLASS& vector, ptrdiff_t index) 00060 : vector(vector), index(index) {} 00061 VectorIterator(const VectorIterator& iter) 00062 : vector(iter.vector), index(iter.index) {} 00063 VectorIterator& operator=(const VectorIterator& iter) { 00064 vector = iter.vector; 00065 index = iter.index; 00066 return *this; 00067 } 00068 ELT& operator*() { 00069 assert (index >= 0 && index < vector.size()); 00070 return vector[(int)index]; 00071 } 00072 ELT& operator[](ptrdiff_t i) { 00073 assert (i >= 0 && i < vector.size()); 00074 return vector[(int)i]; 00075 } 00076 VectorIterator operator++() { 00077 assert (index < vector.size()); 00078 ++index; 00079 return *this; 00080 } 00081 VectorIterator operator++(int) { 00082 assert (index < vector.size()); 00083 VectorIterator current = *this; 00084 ++index; 00085 return current; 00086 } 00087 VectorIterator operator--() { 00088 assert (index > 0); 00089 --index; 00090 return *this; 00091 } 00092 VectorIterator operator--(int) { 00093 assert (index > 0); 00094 VectorIterator current = *this; 00095 --index; 00096 return current; 00097 } 00098 VectorIterator operator+=(ptrdiff_t n) { 00099 assert (0 <= index+n && index+n <= vector.size()); 00100 index += n; 00101 return *this; 00102 } 00103 VectorIterator operator-=(ptrdiff_t n) { 00104 assert (0 <= index-n && index-n <= vector.size()); 00105 index -= n; 00106 return *this; 00107 } 00108 bool operator<(VectorIterator iter) const { 00109 return (index < iter.index); 00110 } 00111 bool operator>(VectorIterator iter) const { 00112 return (index > iter.index); 00113 } 00114 bool operator<=(VectorIterator iter) const { 00115 return (index <= iter.index); 00116 } 00117 bool operator>=(VectorIterator iter) const { 00118 return (index >= iter.index); 00119 } 00120 ptrdiff_t operator-(VectorIterator iter) const { 00121 return (index - iter.index); 00122 } 00123 VectorIterator operator-(ptrdiff_t n) const { 00124 return VectorIterator(vector, index-n); 00125 } 00126 VectorIterator operator+(ptrdiff_t n) const { 00127 return VectorIterator(vector, index+n); 00128 } 00129 bool operator==(VectorIterator iter) const { 00130 return (index == iter.index); 00131 } 00132 bool operator!=(VectorIterator iter) const { 00133 return (index != iter.index); 00134 } 00135 private: 00136 VECTOR_CLASS& vector; 00137 ptrdiff_t index; 00138 }; 00139 00140 } //namespace SimTK 00141 00142 #endif // SimTK_SIMMATRIX_VECTORITERATOR_H_