yat  0.13.2pre
StrideIterator.h
1 #ifndef _theplu_yat_utility_stride_iterator_
2 #define _theplu_yat_utility_stride_iterator_
3 
4 // $Id: StrideIterator.h 3417 2015-05-25 01:35:59Z peter $
5 
6 /*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010, 2015 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 #include <boost/iterator/iterator_concepts.hpp>
29 
30 namespace theplu {
31 namespace yat {
32 namespace utility {
33 
49  template<typename RandomAccessIterator>
51  : public boost::iterator_adaptor<StrideIterator<RandomAccessIterator>
52  , RandomAccessIterator>
53  {
54  typedef boost::iterator_adaptor<StrideIterator<RandomAccessIterator>,
55  RandomAccessIterator>
56  super_t;
57 
58  public:
60  typedef RandomAccessIterator iterator_type;
61 
67  explicit StrideIterator(size_t stride=1)
68  : StrideIterator::iterator_adaptor_(), stride_(stride)
69  {
70  using boost_concepts::RandomAccessTraversal;
71  using boost_concepts::ReadableIterator;
72  BOOST_CONCEPT_ASSERT((ReadableIterator<RandomAccessIterator>));
73  BOOST_CONCEPT_ASSERT((RandomAccessTraversal<RandomAccessIterator>));
74  }
75 
79  explicit StrideIterator(RandomAccessIterator p, size_t stride=1)
80  : StrideIterator::iterator_adaptor_(p), stride_(stride) {}
81 
82 
87  : StrideIterator::iterator_adaptor_(other.base()), stride_(other.stride())
88  {}
89 
98  template<typename I2>
100  typename boost::enable_if_convertible<I2
101  , RandomAccessIterator>::type* = 0 )
102  : StrideIterator::iterator_adaptor_(other.base()),
103  stride_(other.stride()) {}
104 
109  {
110  stride_ = rhs.stride();
111  this->base_reference() = rhs.base();
112  return *this;
113  }
114 
118  inline size_t stride(void) const { return stride_; }
119 
120  private:
121  // to give base class access to private parts
122  friend class boost::iterator_core_access;
123 
124  size_t stride_;
125 
126  //typedef typename StrideIterator::iterator_adaptor_::difference_type
127  typedef typename StrideIterator::iterator_adaptor_::difference_type
128  difference_t;
129 
130  void advance(typename super_t::difference_type n)
131  { this->base_reference() += stride_*n; }
132 
133  void decrement(void) { this->base_reference()-=stride_; }
134 
135  template <class OtherIterator>
136  typename super_t::difference_type
137  distance_to(const StrideIterator<OtherIterator>& other) const
138  {
139  // casting to int to avoid loss of sign in numerator
140  return (other.base() - this->base() )/static_cast<int>(stride_);
141  }
142 
143  void increment(void) { this->base_reference()+=stride_; }
144  };
145 
146 }}} // of namespace utility, yat, and theplu
147 
148 #endif
StrideIterator & operator=(const StrideIterator &rhs)
Assignment operator.
Definition: StrideIterator.h:108
StrideIterator(size_t stride=1)
default constructor
Definition: StrideIterator.h:67
size_t stride(void) const
Definition: StrideIterator.h:118
StrideIterator(StrideIterator< I2 > other, typename boost::enable_if_convertible< I2, RandomAccessIterator >::type *=0)
Conversion constructor.
Definition: StrideIterator.h:99
StrideIterator(const StrideIterator &other)
Copy constructor.
Definition: StrideIterator.h:86
RandomAccessIterator iterator_type
type of underlying iterator
Definition: StrideIterator.h:60
StrideIterator(RandomAccessIterator p, size_t stride=1)
Constructor.
Definition: StrideIterator.h:79
Adaptor using a stride on underlying iterator.
Definition: StrideIterator.h:50

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5