Changeset 1490
- Timestamp:
- Sep 12, 2008, 12:10:26 AM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/iterator_test.cc
r1487 r1490 32 32 #include "yat/utility/stl_utility.h" 33 33 #include "yat/utility/Vector.h" 34 #include "yat/utility/VectorView.h" 35 #include "yat/utility/VectorConstView.h" 34 36 #include "yat/utility/WeightIterator.h" 35 37 … … 72 74 void test_random_access_iterator(RandomAccessIterator, test::Suite&); 73 75 76 void test_stride_iterator(test::Suite& suite); 77 74 78 int main(int argc, char* argv[]) 75 79 { … … 77 81 suite.err() << "testing iterator" << std::endl; 78 82 83 test_stride_iterator(suite); 79 84 old_main(suite); 80 85 test_boost_util(suite); … … 111 116 suite.add(false); 112 117 118 suite.err() << "sorting..."; 113 119 utility::Vector::iterator end=vec.end(); 114 120 std::sort(begin, end); 121 suite.err() << " sorting done\n"; 115 122 116 123 suite.err() << "testing classifier::DataLookup1D::const_iterator" << std::endl; … … 371 378 if (ok_cached && !suite.ok()) 372 379 suite.err() << "failed" << std::endl; 373 374 375 } 380 } 381 382 void test_stride_iterator(test::Suite& suite) 383 { 384 suite.err() << "testing StrideIterator" << std::endl; 385 using utility::Vector; 386 Vector a(10); 387 // stride 2 388 utility::VectorConstView b(a, 0, 5, 2); 389 if (!suite.add(b.end()-b.begin()==5)) 390 suite.err() << "ERROR: StrideIterator::operator- returned: " 391 << b.end()-b.begin() 392 << " expected 5\n"; 393 utility::VectorView c(a, 0, 3, 2); 394 Vector::iterator begin = c.begin(); 395 Vector::iterator end = c.end(); 396 Vector::const_iterator cbegin = begin; 397 Vector::const_iterator cend = end; 398 399 if (!suite.add(c.size()==3)) 400 suite.err() << "c.size() incorrect" << std::endl; 401 if (!suite.add(cend-cbegin == 3)) 402 suite.err() << "cend-cbegin failed\n"; 403 } -
trunk/yat/utility/StrideIterator.h
r1487 r1490 23 23 along with yat. If not, see <http://www.gnu.org/licenses/>. 24 24 */ 25 26 // debug 27 #include <iostream> 25 28 26 29 #include "iterator_traits.h" … … 88 91 : public boost::iterator_adaptor<StrideIterator<Iter>, Iter> 89 92 { 93 typedef boost::iterator_adaptor<StrideIterator<Iter>,Iter> super_t; 94 90 95 public: 91 96 /// type of underlying iterator 92 97 typedef Iter iterator_type; 98 99 /* 93 100 /// value type 94 101 typedef typename std::iterator_traits<Iter>::value_type value_type; … … 101 108 /// weighted_iterator_tag if iterator is weighted 102 109 typedef typename yat::utility::weighted_iterator_traits<Iter>::type w_type; 110 */ 103 111 104 112 /** … … 107 115 Using default constructor of BASE iterator. 108 116 */ 109 StrideIterator(size_t stride=1)117 explicit StrideIterator(size_t stride=1) 110 118 : StrideIterator::iterator_adaptor_(), stride_(stride) {} 111 119 … … 117 125 118 126 127 /** 128 \brief Copy constructor 129 */ 130 StrideIterator(const StrideIterator& other) 131 : StrideIterator::iterator_adaptor_(other.base()), stride_(other.stride()) 132 {} 133 119 134 /** 120 135 \brief Conversion constructor. … … 130 145 stride_(other.stride()) {} 131 146 147 /** 148 \brief Assignment operator 149 */ 150 StrideIterator& operator=(const StrideIterator& rhs) 151 { 152 stride_ = rhs.stride(); 153 this->base_reference() = rhs.base(); 154 return *this; 155 } 156 132 157 /** 133 158 \return stride … … 141 166 size_t stride_; 142 167 168 //typedef typename StrideIterator::iterator_adaptor_::difference_type 143 169 typedef typename StrideIterator::iterator_adaptor_::difference_type 144 170 difference_t; 145 171 146 void advance(difference_t n){ this->base_reference() += stride_*n; } 172 void advance(typename super_t::difference_type n) 173 { this->base_reference() += stride_*n; } 174 147 175 void decrement(void) { this->base_reference()-=stride_; } 148 176 149 177 template <class OtherIterator> 150 difference_t distance_type(const StrideIterator<OtherIterator>& other) const 151 { return (other.base() - this->base())/stride_; } 178 typename super_t::difference_type 179 distance_to(const StrideIterator<OtherIterator>& other) const 180 { 181 // casting to int to avoid loss of sign in numerator 182 return (other.base() - this->base() )/static_cast<int>(stride_); 183 } 152 184 153 185 void increment(void) { this->base_reference()+=stride_; } 154 186 155 // Using compiler generated copy156 //StrideIterator(const StrideIterator&);157 //StrideIterator& operator=(const StrideIterator&);158 187 }; 159 188
Note: See TracChangeset
for help on using the changeset viewer.