ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
StridedIterator.h
Go to the documentation of this file.
1// -*- c++ -*-
2/*
3 * Copyright (c) 2010-2012, Jim Bosch
4 * All rights reserved.
5 *
6 * ndarray is distributed under a simple BSD-like license;
7 * see the LICENSE file that should be present in the root
8 * of the source distribution, or alternately available at:
9 * https://github.com/ndarray/ndarray
10 */
11#ifndef NDARRAY_DETAIL_StridedIterator_h_INCLUDED
12#define NDARRAY_DETAIL_StridedIterator_h_INCLUDED
13
20#include "ndarray_fwd.h"
21#include <boost/iterator/iterator_facade.hpp>
22
23namespace ndarray {
24namespace detail {
25
31template <typename T>
32class StridedIterator : public boost::iterator_facade<
33 StridedIterator<T>,
34 T, boost::random_access_traversal_tag
35 >
36{
37public:
38 typedef T Value;
39 typedef T & Reference;
40
41 StridedIterator() : _data(0), _stride(0) {}
42
43 StridedIterator(T * data, Offset stride) : _data(data), _stride(stride) {}
44
45 StridedIterator(StridedIterator const & other) : _data(other._data), _stride(other._stride) {}
46
47 template <typename U>
48 StridedIterator(StridedIterator<U> const & other) : _data(other._data), _stride(other._stride) {
49 BOOST_STATIC_ASSERT((boost::is_convertible<U*,T*>::value));
50 }
51
52 StridedIterator & operator=(StridedIterator const & other) {
53 if (&other != this) {
54 _data = other._data;
55 _stride = other._stride;
56 }
57 return *this;
58 }
59
60 template <typename U>
61 StridedIterator & operator=(StridedIterator<U> const & other) {
62 BOOST_STATIC_ASSERT((boost::is_convertible<U*,T*>::value));
63 _data = other._data;
64 _stride = other._stride;
65 return *this;
66 }
67
68private:
69
70 friend class boost::iterator_core_access;
71
72 template <typename OtherT> friend class StridedIterator;
73
74 Reference dereference() const { return *_data; }
75
76 void increment() { _data += _stride; }
77 void decrement() { _data -= _stride; }
78 void advance(Offset n) { _data += _stride * n; }
79
80 template <typename U>
81 Offset distance_to(StridedIterator<U> const & other) const {
82 return std::distance(_data, other._data) / _stride;
83 }
84
85 template <typename U>
86 bool equal(StridedIterator<U> const & other) const {
87 return _data == other._data;
88 }
89
90 T * _data;
91 Offset _stride;
92
93};
94
95} // namespace detail
96} // namespace ndarray
97
98#endif // !NDARRAY_DETAIL_StridedIterator_h_INCLUDED
Definition StridedIterator.h:36
Forward declarations and default template parameters for ndarray.