Changeset 1062 for trunk/yat/utility/Iterator.h
 Timestamp:
 Feb 10, 2008, 7:41:06 PM (15 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/utility/Iterator.h
r1040 r1062 5 5 6 6 /* 7 Copyright (C) 2007 Peter Johansson7 Copyright (C) 2007, 2008 Peter Johansson 8 8 9 9 This file is part of the yat library, http://trac.thep.lu.se/yat … … 36 36 namespace utility { 37 37 38 class VectorBase;39 40 38 /** 41 39 @brief Iterator 42 40 */ 43 template<typename T>41 template<typename Container, typename T> 44 42 class Iterator 45 43 { … … 48 46 typedef double value_type; 49 47 typedef size_t difference_type; 48 typedef T reference; 50 49 typedef double* pointer; 51 typedef typename T::result_type reference; 52 typedef typename T::container_type Container ; 53 50 54 51 private: 55 typedef Iterator< T> self;52 typedef Iterator<Container, T> self; 56 53 57 54 public: … … 65 62 66 63 \param container iterator points to 67 \param index telling which element iterator points to 64 \param row telling which row iterator points to 65 \param colun telling which column iterator points to 68 66 */ 69 Iterator(Container& container, difference_type index)70 : container_(&container), index_( index) {}67 Iterator(Container& container, difference_type row, difference_type column) 68 : container_(&container), index_(row*container.columns()+column) {} 71 69 72 70 /** … … 75 73 reference operator*(void) const 76 74 { 77 yat_assert<std::out_of_range>(index_ <container_>size(),75 yat_assert<std::out_of_range>(index_ < this>size(), 78 76 "Iterator::operator*"); 79 return container_>operator()( index_);77 return container_>operator()(this>row(index_), this>column(index_)); 80 78 } 81 79 … … 85 83 reference operator[](difference_type n) const 86 84 { 87 yat_assert<std::out_of_range>(index_+n < container_>size(),85 yat_assert<std::out_of_range>(index_+n < this>size(), 88 86 "Iterator::operator[]"); 89 return container_>operator()( index_+n);87 return container_>operator()(this>row(index_+n), this>column(index_+n)); 90 88 } 91 89 … … 139 137 \return copy of resulting iterator 140 138 */ 141 friend Iterator operator+(const Iterator& lhs, size_tn)139 friend Iterator operator+(const Iterator& lhs, difference_type n) 142 140 { return self(*lhs.container_, lhs.index_+n); } 143 141 … … 147 145 \return copy of resulting iterator 148 146 */ 149 friend Iterator operator(const Iterator& lhs, size_tn)147 friend Iterator operator(const Iterator& lhs, difference_type n) 150 148 { return self(*lhs.container_, lhs.index_n); } 151 149 … … 156 154 */ 157 155 friend difference_type operator(const Iterator& lhs, const Iterator& rhs) 158 { return lhs.index_rhs.index_; } 156 { 157 yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 158 "Iterator::operator"); 159 return lhs.index_rhs.index_; 160 } 159 161 160 162 … … 165 167 */ 166 168 friend bool operator==(const self& lhs, const self& rhs) 167 { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; } 169 { 170 yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 171 "Iterator::operator=="); 172 return lhs.index_==rhs.index_; 173 } 168 174 169 175 /** … … 172 178 \return False if \a lhs and \a rhs are pointing to same element 173 179 */ 174 friend bool operator!=(const Iterator& lhs, 175 const Iterator& rhs) 176 { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); } 180 friend bool operator!=(const Iterator& lhs, const Iterator& rhs) 181 { 182 yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 183 "Iterator::operator!="); 184 return !(lhs==rhs); 185 } 177 186 178 187 /** … … 180 189 */ 181 190 friend bool operator<(const self& lhs, const self& rhs) 182 { return lhs.index_<rhs.index_; } 191 { 192 yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 193 "Iterator::operator<"); 194 return lhs.index_<rhs.index_; 195 } 183 196 184 197 /** … … 186 199 */ 187 200 friend bool operator<=(const self& lhs, const self& rhs) 188 { return lhs.index_<=rhs.index_; } 201 { 202 yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 203 "Iterator::operator<="); 204 return lhs.index_<=rhs.index_; 205 } 189 206 190 207 /** 191 208 \brief Larger operator 192 209 */ 193 friend bool operator>(const self& lhs, 194 const self& rhs) 195 { return lhs.index_>rhs.index_; } 210 friend bool operator>(const self& lhs, const self& rhs) 211 { 212 yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 213 "Iterator::operator>"); 214 return lhs.index_>rhs.index_; 215 } 196 216 197 217 /** … … 199 219 */ 200 220 friend bool operator>=(const self& lhs, const self& rhs) 201 { return lhs.index_>=rhs.index_; } 221 { 222 yat_assert<std::runtime_error>(lhs.container_==rhs.container_, 223 "Iterator::operator>="); 224 return lhs.index_>=rhs.index_; 225 } 202 226 203 227 private: 204 228 Container* container_; 205 size_t index_; 229 difference_type index_; 230 231 difference_type column(difference_type i) const 232 { return i % container_>columns(); } 233 difference_type row(difference_type i) const 234 { return static_cast<difference_type>(i/container_>columns()); } 235 difference_type size() const 236 { return container_>columns()*container_>rows(); } 206 237 207 238 // Using compiler generated copy
Note: See TracChangeset
for help on using the changeset viewer.