OpenFPM  5.2.0
Project that contain the implementation of distributed structures
multi_array_iterator_openfpm.hpp
1 /*
2  * multi_array_iterator_openfpm.hpp
3  *
4  * Created on: Jul 7, 2018
5  * Author: i-bird
6  */
7 
8 #ifndef MULTI_ARRAY_ITERATOR_OPENFPM_HPP_
9 #define MULTI_ARRAY_ITERATOR_OPENFPM_HPP_
10 
11 
12 
13 //
14 // iterator.hpp - implementation of iterators for the
15 // multi-dimensional array class
16 //
17 
18 #include "boost/iterator/iterator_facade.hpp"
19 #include <algorithm>
20 #include <cstddef>
21 #include <iterator>
22 //#include "util/boost/boost_multi_array_base_openfpm.hpp"
23 #include "util/cuda_util.hpp"
24 
25 namespace openfpm {
26 namespace detail {
27 namespace multi_array {
28 
29 template <class T>
31 {
32  operator_arrow_proxy_openfpm(T const& px) : value_(px) {}
33  T* operator->() const { return &value_; }
34  // This function is needed for MWCW and BCC, which won't call operator->
35  // again automatically per 13.3.1.2 para 8
36  operator T*() const { return &value_; }
37  mutable T value_;
38 };
39 
41 // iterator components
43 
44 template <typename T, typename TPtr, typename NumDims, typename vector, typename Reference,
45  typename IteratorCategory>
47 
48 template <typename T, typename TPtr, typename NumDims, typename vector, typename Reference,
49  typename IteratorCategory>
51  : public
52  boost::iterator_facade<
53  array_iterator_openfpm<T,TPtr,NumDims,vector,Reference,IteratorCategory>
54  , typename associated_types_openfpm<T,NumDims,vector>::value_type
55  , IteratorCategory
56  , Reference
57  >
58  , private
59  value_accessor_generator_openfpm<T,NumDims,vector>::type
60 {
61  friend class iterator_core_access;
63 
64  typedef boost::iterator_facade<
67  , boost::random_access_traversal_tag
68  , Reference
69  > facade_type;
70 
71  typedef typename access_t::index index;
72  typedef typename access_t::size_type size_type;
73 
74 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
75  template <typename, typename, typename, typename, typename, typename>
76  friend class array_iterator_openfpm;
77 #else
78  public:
79 #endif
80 
81  index idx_;
82  TPtr base_;
83  const size_type extent;
84  const index* strides_;
85 
86 public:
87  // Typedefs to circumvent ambiguities between parent classes
88  typedef typename facade_type::reference reference;
89  typedef typename facade_type::value_type value_type;
90  typedef typename facade_type::difference_type difference_type;
91  typedef typename std::random_access_iterator_tag iterator_category;
92 
93  __device__ __host__ array_iterator_openfpm() {}
94 
95  __device__ __host__ array_iterator_openfpm(index idx,TPtr base, const size_type extent, const index* strides)
96  :idx_(idx), base_(base), extent(extent),strides_(strides)
97  {}
98 
99  template <typename OPtr, typename ORef, typename Cat>
102  , typename boost::enable_if_convertible<OPtr,TPtr>::type* = 0
103  )
104  : idx_(rhs.idx_), base_(rhs.base_), extent(rhs.extent),strides_(rhs.strides_)
105  { }
106 
107 
108  // RG - we make our own operator->
110  operator->() const
111  {
112  return operator_arrow_proxy_openfpm<reference>(this->dereference());
113  }
114 
115 
116  reference dereference() const
117  {
118  typedef typename value_accessor_generator_openfpm<T,NumDims,vector>::type accessor;
119  return accessor::access(boost::type<reference>(),
120  idx_,
121  strides_,
122  base_);
123  }
124 
125  void increment() { ++idx_; }
126  void decrement() { --idx_; }
127 
128  template <class IteratorAdaptor>
129  bool equal(IteratorAdaptor& rhs) const {
130  const std::size_t N = NumDims::value;
131  return (idx_ == rhs.idx_) &&
132  (base_ == rhs.base_) &&
133  (extent == rhs.extent) &&
134  ( (strides_ == rhs.strides_) ||
135  std::equal(strides_,strides_+N,rhs.strides_) );
136  }
137 
138  template <class DifferenceType>
139  void advance(DifferenceType n) {
140  idx_ += n;
141  }
142 
143  template <class IteratorAdaptor>
144  typename facade_type::difference_type
145  distance_to(IteratorAdaptor& rhs) const {
146  return rhs.idx_ - idx_;
147  }
148 
149 
150 };
151 
152 } // namespace multi_array
153 } // namespace detail
154 } // namespace boost
155 
156 
157 
158 
159 #endif /* MULTI_ARRAY_ITERATOR_OPENFPM_HPP_ */
This class is a trick to indicate the compiler a specific specialization pattern.
Definition: memory_c.hpp:231
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:204
convert a type into constant type
Definition: aggregate.hpp:302