yat  0.10.4pre
StrideIterator.h
1 #ifndef _theplu_yat_utility_stride_iterator_
2 #define _theplu_yat_utility_stride_iterator_
3 
4 // $Id: StrideIterator.h 2365 2010-12-06 00:40:13Z peter $
5 
6 /*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010 Peter Johansson
9 
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11 
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16 
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #include <boost/concept_check.hpp>
27 #include <boost/iterator/iterator_adaptor.hpp>
28 
29 namespace theplu {
30 namespace yat {
31 namespace utility {
32 
44  template<typename RandomAccessIterator>
46  : public boost::iterator_adaptor<StrideIterator<RandomAccessIterator>
47  , RandomAccessIterator
48  , boost::use_default
49  , typename std::iterator_traits<RandomAccessIterator>::iterator_category>
50  {
51  typedef boost::iterator_adaptor<StrideIterator<RandomAccessIterator>,
52  RandomAccessIterator,
53  boost::use_default,
54  typename std::iterator_traits<RandomAccessIterator>::iterator_category>
55  super_t;
56 
57  public:
59  typedef RandomAccessIterator iterator_type;
60 
66  explicit StrideIterator(size_t stride=1)
67  : StrideIterator::iterator_adaptor_(), stride_(stride)
68  {
69  BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<RandomAccessIterator>));
70  }
71 
75  explicit StrideIterator(RandomAccessIterator p, size_t stride=1)
76  : StrideIterator::iterator_adaptor_(p), stride_(stride) {}
77 
78 
83  : StrideIterator::iterator_adaptor_(other.base()), stride_(other.stride())
84  {}
85 
94  template<typename I2>
96  typename boost::enable_if_convertible<I2
97  , RandomAccessIterator>::type* = 0 )
98  : StrideIterator::iterator_adaptor_(other.base()),
99  stride_(other.stride()) {}
100 
105  {
106  stride_ = rhs.stride();
107  this->base_reference() = rhs.base();
108  return *this;
109  }
110 
114  inline size_t stride(void) const { return stride_; }
115 
116  private:
117  // to give base class access to private parts
118  friend class boost::iterator_core_access;
119 
120  size_t stride_;
121 
122  //typedef typename StrideIterator::iterator_adaptor_::difference_type
123  typedef typename StrideIterator::iterator_adaptor_::difference_type
124  difference_t;
125 
126  void advance(typename super_t::difference_type n)
127  { this->base_reference() += stride_*n; }
128 
129  void decrement(void) { this->base_reference()-=stride_; }
130 
131  template <class OtherIterator>
132  typename super_t::difference_type
133  distance_to(const StrideIterator<OtherIterator>& other) const
134  {
135  // casting to int to avoid loss of sign in numerator
136  return (other.base() - this->base() )/static_cast<int>(stride_);
137  }
138 
139  void increment(void) { this->base_reference()+=stride_; }
140  };
141 
142 }}} // of namespace utility, yat, and theplu
143 
144 #endif

Generated on Mon Nov 11 2013 09:41:44 for yat by  doxygen 1.8.1