Changeset 1038
- Timestamp:
- Feb 5, 2008, 9:46:37 PM (16 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/iterator_test.cc
r1028 r1038 53 53 utility::vector::iterator begin=vec.begin(); 54 54 // test iterator to const_iterator conversion 55 ///////////////////////////////////////////////////////56 /* const conversion doesn't work for new vector structure57 see ticket 30358 55 utility::vector::const_iterator ci = vec.begin(); 59 56 ci = begin; 60 57 if (begin!=ci) 61 58 ok = false; 62 */ 63 /////////////////////////////////////////////////////// 59 64 60 utility::vector::iterator end=vec.end(); 65 61 std::sort(begin, end); -
trunk/yat/utility/Makefile.am
r1027 r1038 40 40 Option.h OptionArg.h OptionFile.h OptionInFile.h OptionOutFile.h \ 41 41 OptionHelp.h OptionSwitch.h \ 42 PCA.h stl_utility.h S VD.h TypeInfo.h utility.h vector.h \42 PCA.h stl_utility.h StrideIterator.h SVD.h TypeInfo.h utility.h vector.h \ 43 43 VectorBase.h VectorConstView.h VectorMutable.h VectorView.h \ 44 44 WeNNI.h yat_assert.h -
trunk/yat/utility/StrideIterator.h
r1037 r1038 1 #ifndef _theplu_yat_utility_ iterator_2 #define _theplu_yat_utility_ iterator_1 #ifndef _theplu_yat_utility_stride_iterator_ 2 #define _theplu_yat_utility_stride_iterator_ 3 3 4 4 // $Id$ 5 5 6 6 /* 7 Copyright (C) 200 7Peter Johansson7 Copyright (C) 2008 Peter Johansson 8 8 9 9 This file is part of the yat library, http://trac.thep.lu.se/yat … … 28 28 29 29 #include <iterator> 30 #include <stddef.h>31 #include <stdexcept>32 #include <utility>33 30 34 31 namespace theplu { … … 36 33 namespace utility { 37 34 38 class VectorBase;39 40 35 /** 41 @brief Iterator 36 @brief Stride Iterator 37 38 Works as a pointer except that all arithmetic is using the stride value 42 39 */ 43 template<typename T, typename Container> 44 class Iterator 40 template<typename T> 41 class StrideIterator 42 : public std::iterator<std::random_access_iterator_tag, T> 45 43 { 46 44 public: 47 typedef std::random_access_iterator_tag iterator_category; 48 typedef double value_type; 49 typedef size_t difference_type; 50 typedef double* pointer; 51 typedef T reference; 52 53 private: 54 typedef Iterator<reference, Container> self; 55 56 public: 57 /** 58 \brief Default Constructor 45 typedef typename std::iterator_traits<T*>::difference_type difference_type; 46 typedef typename std::iterator_traits<T*>::reference reference; 47 typedef typename std::iterator_traits<T*>::pointer pointer; 48 49 /** 50 \brief Constructor 59 51 */ 60 Iterator(void) {}; 61 62 /** 63 \brief Constructor 64 65 \param container iterator points to 66 \param index telling which element iterator points to 67 */ 68 Iterator(Container& container, difference_type index) 69 : container_(&container), index_(index) {} 70 71 operator Iterator<const reference, const Container>() 72 { return Iterator<const reference, const Container>(*container_, index_); } 52 explicit StrideIterator(pointer p, difference_type stride=1) 53 : pointer_(p), stride_(stride) {} 54 55 operator StrideIterator<const T>() 56 { return StrideIterator<const T>(pointer_, stride_); } 73 57 74 58 /** … … 76 60 */ 77 61 reference operator*(void) const 78 { 79 yat_assert<std::out_of_range>(index_<container_->size(), 80 "Iterator::operator*");81 return container_->operator()(index_);82 }62 { return *pointer_; } 63 64 /** 65 */ 66 pointer operator->() const { return &(operator*()); } 83 67 84 68 /** … … 87 71 reference operator[](difference_type n) const 88 72 { 89 yat_assert<std::out_of_range>(index_+n < container_->size(), 90 "Iterator::operator[]"); 91 return container_->operator()(index_+n); 73 return pointer_[stride_*n]; 92 74 } 93 75 … … 97 79 \return reference to *this 98 80 */ 99 Iterator& operator++(void) { ++index_; return *this; }81 StrideIterator& operator++(void) { pointer_+=stride_; return *this; } 100 82 101 83 /** … … 104 86 \return copy of iterator prior increment 105 87 */ 106 Iterator operator++(int)107 { self tmp(*this); ++index_; return tmp;}88 StrideIterator operator++(int) 89 { StrideIterator tmp(*this); pointer_+=stride_; return tmp;} 108 90 109 91 /** … … 112 94 \return reference to resulting iterator 113 95 */ 114 Iterator& operator+=(int n) { index_+=n; return *this; }96 StrideIterator& operator+=(int n) { pointer_+=stride_*n; return *this; } 115 97 116 98 /** … … 119 101 \return copy of iterator prior decrement 120 102 */ 121 Iterator operator--(int)122 { self tmp(*this); --index_; return tmp;}103 StrideIterator operator--(int) 104 { StrideIterator tmp(*this); pointer_-=stride_; return tmp;} 123 105 124 106 /** … … 127 109 \return reference to resulting iterator 128 110 */ 129 Iterator& operator--(void) { --index_; return *this; }111 StrideIterator& operator--(void) { pointer_-=stride_; return *this; } 130 112 131 113 /** … … 134 116 \return reference to resulting iterator 135 117 */ 136 Iterator& operator-=(int n) { index_-=n; return *this; }118 StrideIterator& operator-=(int n) { pointer_-=n*stride_; return *this; } 137 119 138 120 /** … … 141 123 \return copy of resulting iterator 142 124 */ 143 friend Iterator operator+(const Iterator& lhs, size_tn)144 { return self(*lhs.container_, lhs.index_+n); }125 friend StrideIterator operator+(const StrideIterator& lhs,difference_type n) 126 { StrideIterator tmp(lhs); tmp+=n; return tmp; } 145 127 146 128 /** … … 149 131 \return copy of resulting iterator 150 132 */ 151 friend Iterator operator-(const Iterator& lhs, size_t n) 152 { return self(*lhs.container_, lhs.index_-n); } 133 friend StrideIterator operator-(const StrideIterator& lhs, 134 difference_type n) 135 { StrideIterator tmp(lhs); tmp-=n; return tmp; } 153 136 154 137 /** … … 157 140 \return distance between \a lhs and \a rhs 158 141 */ 159 friend difference_type operator-(const Iterator& lhs, const Iterator& rhs) 160 { return lhs.index_-rhs.index_; } 142 friend difference_type operator-(const StrideIterator& lhs, 143 const StrideIterator& rhs) 144 { 145 yat_assert<std::runtime_error>(lhs.stride_==rhs.stride_, 146 "StrideIterator::operator- different stride in lhs and rhs."); 147 return static_cast<difference_type>( (lhs.pointer_-rhs.pointer_)/ 148 lhs.stride_); 149 } 161 150 162 151 … … 166 155 \return True if \a lhs and \a rhs are pointing to same element 167 156 */ 168 friend bool operator==(const self& lhs, const self& rhs)169 { return lhs. container_==rhs.container_ && lhs.index_==rhs.index_; }157 friend bool operator==(const StrideIterator& lhs, const StrideIterator& rhs) 158 { return lhs.pointer_==rhs.pointer_; } 170 159 171 160 /** … … 174 163 \return False if \a lhs and \a rhs are pointing to same element 175 164 */ 176 friend bool operator!=(const Iterator& lhs, 177 const Iterator& rhs) 178 { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); } 165 friend bool operator!=(const StrideIterator& lhs, const StrideIterator& rhs) 166 { return !(lhs==rhs); } 179 167 180 168 /** 181 169 \brief Less operator 182 170 */ 183 friend bool operator<(const self& lhs, const self& rhs)184 { return lhs. index_<rhs.index_; }171 friend bool operator<(const StrideIterator& lhs, const StrideIterator& rhs) 172 { return lhs.pointer_<rhs.pointer_; } 185 173 186 174 /** 187 175 \brief Less equal operator 188 176 */ 189 friend bool operator<=(const self& lhs, const self& rhs)190 { return lhs.index_<=rhs.index_; }177 friend bool operator<=(const StrideIterator& lhs, const StrideIterator& rhs) 178 { return !(rhs<lhs); } 191 179 192 180 /** 193 181 \brief Larger operator 194 182 */ 195 friend bool operator>(const self& lhs, 196 const self& rhs) 197 { return lhs.index_>rhs.index_; } 183 friend bool operator>(const StrideIterator& lhs, const StrideIterator& rhs) 184 { return rhs<lhs; } 198 185 199 186 /** 200 187 \brief Larger equal operator 201 188 */ 202 friend bool operator>=(const self& lhs, const self& rhs)203 { return lhs.index_>=rhs.index_; }189 friend bool operator>=(const StrideIterator& lhs, const StrideIterator& rhs) 190 { return !(lhs<rhs); } 204 191 205 192 private: 206 Container* container_;207 size_t index_;193 T* pointer_; 194 size_t stride_; 208 195 209 196 // Using compiler generated copy 210 // Iterator(constIterator&);211 // Iterator& operator=(constIterator&);197 //StrideIterator(const StrideIterator&); 198 //StrideIterator& operator=(const StrideIterator&); 212 199 }; 213 200 }}} // of namespace utility, yat, and theplu -
trunk/yat/utility/VectorBase.cc
r1027 r1038 56 56 VectorBase::const_iterator VectorBase::begin(void) const 57 57 { 58 return const_iterator(*this, 0); 58 if (const_vec_) 59 return const_iterator(&(this->operator()(0)), const_vec_->stride); 60 return const_iterator(NULL, 1); 59 61 } 60 62 … … 62 64 VectorBase::const_iterator VectorBase::end(void) const 63 65 { 64 return const_iterator(*this, size()); 66 if (const_vec_) 67 return const_iterator(&(this->operator()(0))+const_vec_->stride*size(), 68 const_vec_->stride); 69 return const_iterator(NULL, 1); 65 70 } 66 71 -
trunk/yat/utility/VectorBase.h
r1027 r1038 31 31 32 32 #include "Exception.h" 33 #include " Iterator.h"33 #include "StrideIterator.h" 34 34 35 35 #include <iostream> … … 90 90 public: 91 91 /// \brief VectorBase::const_iterator 92 typedef Iterator<const double&, const VectorBase> const_iterator;92 typedef StrideIterator<const double> const_iterator; 93 93 94 94 /** -
trunk/yat/utility/VectorMutable.cc
r1027 r1038 75 75 VectorMutable::iterator VectorMutable::begin(void) 76 76 { 77 return iterator(*this, 0); 77 if (vec_) 78 return iterator(&(this->operator()(0)), vec_->stride); 79 return iterator(NULL, 1); 78 80 } 79 81 … … 90 92 VectorMutable::iterator VectorMutable::end(void) 91 93 { 92 return iterator(*this, size()); 94 if (vec_) 95 return iterator(&(this->operator()(0))+vec_->stride*size(), 96 const_vec_->stride); 97 return iterator(NULL, 1); 93 98 } 94 99 -
trunk/yat/utility/VectorMutable.h
r1027 r1038 32 32 #include "VectorBase.h" 33 33 #include "Exception.h" 34 #include " Iterator.h"34 #include "StrideIterator.h" 35 35 36 36 #include <iostream> … … 93 93 \brief mutable iterator 94 94 */ 95 typedef Iterator<double&, VectorMutable> iterator;95 typedef StrideIterator<double> iterator; 96 96 97 97 /**
Note: See TracChangeset
for help on using the changeset viewer.