ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
NestedIterator.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_NestedIterator_h_INCLUDED
12#define NDARRAY_DETAIL_NestedIterator_h_INCLUDED
13
20#include <boost/iterator/iterator_facade.hpp>
21#include "ndarray_fwd.h"
22
23namespace ndarray {
24namespace detail {
25
39template <typename T, int N, int C>
40class NestedIterator : public boost::iterator_facade<
41 NestedIterator<T,N,C>,
42 typename ArrayTraits<T,N,C>::Value,
43 boost::random_access_traversal_tag,
44 typename ArrayTraits<T,N,C>::Reference
45 >
46{
47public:
48 typedef typename ArrayTraits<T,N,C>::Value Value;
50
51 Reference operator[](Size n) const {
52 Reference r(_ref);
53 r._data += n * _stride;
54 return r;
55 }
56
57 Reference const & operator*() const { return _ref; }
58
59 Reference const * operator->() { return &_ref; }
60
61 NestedIterator() : _ref(Value()), _stride(0) {}
62
63 NestedIterator(Reference const & ref, Offset stride) : _ref(ref), _stride(stride) {}
64
65 NestedIterator(NestedIterator const & other) : _ref(other._ref), _stride(other._stride) {}
66
67 template <typename T_, int C_>
68 NestedIterator(NestedIterator<T_,N,C_> const & other) : _ref(other._ref), _stride(other._stride) {}
69
70 NestedIterator & operator=(NestedIterator const & other) {
71 if (&other != this) {
72 _ref._data = other._ref._data;
73 _ref._core = other._ref._core;
74 _stride = other._stride;
75 }
76 return *this;
77 }
78
79 template <typename T_, int C_>
80 NestedIterator & operator=(NestedIterator<T_,N,C_> const & other) {
81 _ref._data = other._ref._data;
82 _ref._core = other._ref._core;
83 _stride = other._stride;
84 return *this;
85 }
86
87private:
88
89 friend class boost::iterator_core_access;
90
91 template <typename T_, int N_, int C_> friend class NestedIterator;
92
93 Reference const & dereference() const { return _ref; }
94
95 void increment() { _ref._data += _stride; }
96 void decrement() { _ref._data -= _stride; }
97 void advance(Offset n) { _ref._data += _stride * n; }
98
99 template <typename T_, int C_>
100 Offset distance_to(NestedIterator<T_,N,C_> const & other) const {
101 return std::distance(_ref._data, other._ref._data) / _stride;
102 }
103
104 template <typename T_, int C_>
105 bool equal(NestedIterator<T_,N,C_> const & other) const {
106 return _ref._data == other._ref._data;
107 }
108
109 Reference _ref;
110 Offset _stride;
111};
112
113} // namespace detail
114} // namespace ndarray
115
116#endif // !NDARRAY_DETAIL_NestedIterator_h_INCLUDED
A proxy class for Array with deep assignment operators.
Definition ArrayRef.h:34
A multidimensional strided array.
Definition Array.h:35
Definition NestedIterator.h:46
Forward declarations and default template parameters for ndarray.