LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
StridedIterator.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010, 2011 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #ifndef NDARRAY_DETAIL_StridedIterator_h_INCLUDED
24 #define NDARRAY_DETAIL_StridedIterator_h_INCLUDED
25 
32 #include "ndarray_fwd.h"
33 #include <boost/iterator/iterator_facade.hpp>
34 
35 namespace ndarray {
36 namespace detail {
37 
43 template <typename T>
44 class StridedIterator : public boost::iterator_facade<
45  StridedIterator<T>,
46  T, boost::random_access_traversal_tag
47  >
48 {
49 public:
50  typedef T Value;
51  typedef T & Reference;
52 
54 
55  StridedIterator(T * data, int stride) : _data(data), _stride(stride) {}
56 
57  StridedIterator(StridedIterator const & other) : _data(other._data), _stride(other._stride) {}
58 
59  template <typename U>
60  StridedIterator(StridedIterator<U> const & other) : _data(other._data), _stride(other._stride) {
61  BOOST_STATIC_ASSERT((boost::is_convertible<U*,T*>::value));
62  }
63 
65  if (&other != this) {
66  _data = other._data;
67  _stride = other._stride;
68  }
69  return *this;
70  }
71 
72  template <typename U>
74  BOOST_STATIC_ASSERT((boost::is_convertible<U*,T*>::value));
75  _data = other._data;
76  _stride = other._stride;
77  return *this;
78  }
79 
80 private:
81 
83 
84  template <typename OtherT> friend class StridedIterator;
85 
86  Reference dereference() const { return *_data; }
87 
88  void increment() { _data += _stride; }
89  void decrement() { _data -= _stride; }
90  void advance(int n) { _data += _stride * n; }
91 
92  template <typename U>
93  int distance_to(StridedIterator<U> const & other) const {
94  return std::distance(_data, other._data) / _stride;
95  }
96 
97  template <typename U>
98  bool equal(StridedIterator<U> const & other) const {
99  return _data == other._data;
100  }
101 
102  T * _data;
103  int _stride;
104 
105 };
106 
107 } // namespace detail
108 } // namespace ndarray
109 
110 #endif // !NDARRAY_DETAIL_StridedIterator_h_INCLUDED
StridedIterator(StridedIterator< U > const &other)
StridedIterator(T *data, int stride)
bool equal(StridedIterator< U > const &other) const
int distance_to(StridedIterator< U > const &other) const
Forward declarations and default template parameters for ndarray.
StridedIterator & operator=(StridedIterator const &other)
StridedIterator & operator=(StridedIterator< U > const &other)
StridedIterator(StridedIterator const &other)
friend class boost::iterator_core_access