DASH  0.3.0
ViewIterator.h
1 #ifndef DASH__VIEW__VIEW_ITERATOR_H__INCLUDED
2 #define DASH__VIEW__VIEW_ITERATOR_H__INCLUDED
3 
4 #include <dash/GlobPtr.h>
5 #include <dash/iterator/internal/IteratorBase.h>
6 #include <dash/meta/TypeInfo.h>
7 
8 #include <iostream>
9 #include <sstream>
10 
11 
12 namespace dash {
13 
14 // --------------------------------------------------------------------
15 // ViewIterator
16 // --------------------------------------------------------------------
17 
18 template <
19  class DomainIterator,
20  class IndexSetType >
22  : public dash::internal::IndexIteratorBase<
23  ViewIterator<DomainIterator, IndexSetType>,
24  typename DomainIterator::value_type, // value type
25  typename DomainIterator::difference_type, // difference type
26  typename DomainIterator::pointer, // pointer
27  typename DomainIterator::reference // reference
28 > {
30  typedef dash::internal::IndexIteratorBase<
32  typename DomainIterator::value_type,
33  typename DomainIterator::difference_type,
34  typename DomainIterator::pointer,
35  typename DomainIterator::reference > base_t;
36 
37  template<class DomainT, class IndexSetT>
38  friend std::ostream & operator<<(
39  std::ostream & os,
40  const ViewIterator<DomainT, IndexSetT> & view_it);
41 public:
42  typedef typename base_t::reference reference;
43  typedef typename base_t::value_type value_type;
44  typedef typename IndexSetType::index_type index_type;
45 private:
46  DomainIterator _domain_it;
47  IndexSetType _index_set;
48 public:
49  constexpr ViewIterator() = delete;
50 
51  template <class DomainItType>
53  const DomainItType & domain_it,
54  const IndexSetType & index_set,
55  index_type position)
56  : base_t(position)
57  , _domain_it(domain_it)
58  , _index_set(index_set)
59  { }
60 
61  template <class DomainItType>
63  DomainItType && domain_it,
64  const IndexSetType & index_set,
65  index_type position)
66  : base_t(position)
67  , _domain_it(std::forward<DomainItType>(domain_it))
68  , _index_set(index_set)
69  { }
70 
72  const self_t & other,
73  index_type position)
74  : base_t(position)
75  , _domain_it(other._domain_it)
76  , _index_set(other._index_set)
77  { }
78 
79  constexpr reference dereference(index_type idx) const {
80  return *(_domain_it + (_index_set[idx]));
81  }
82 
83  constexpr index_type gpos() const {
84  return (_index_set)[this->pos()];
85  }
86 
87  constexpr const value_type * local() const {
88  return (_domain_it + (_index_set[this->pos()])).local();
89  }
90 
91  inline value_type * local() {
92  return (_domain_it + (_index_set[this->pos()])).local();
93  }
94 
95  constexpr dart_gptr_t dart_gptr() const {
96  return (_domain_it + _index_set[this->pos()]).dart_gptr();
97  }
98 
99  constexpr explicit operator dart_gptr_t() const {
100  return dart_gptr();
101  }
102 
103  constexpr explicit operator DomainIterator() const {
104  return (_domain_it + _index_set[this->pos()]);
105  }
106 };
107 
108 template <
109  class DomainIterator,
110  class IndexSetType >
111 class ViewIterator<DomainIterator *, IndexSetType>
112  : public dash::internal::IndexIteratorBase<
114  DomainIterator,
115  std::ptrdiff_t,
116  DomainIterator *,
117  DomainIterator & >
118 {
120  typedef dash::internal::IndexIteratorBase<
121  ViewIterator<DomainIterator *, IndexSetType>,
122  DomainIterator,
123  std::ptrdiff_t,
124  DomainIterator *,
125  DomainIterator & > base_t;
126 
127  template<class DomainT, class IndexSetT>
128  friend std::ostream & operator<<(
129  std::ostream & os,
130  const ViewIterator<DomainT, IndexSetT> & view_it);
131 public:
132  typedef DomainIterator & reference;
133  typedef DomainIterator value_type;
134  typedef std::ptrdiff_t index_type;
135 private:
136  DomainIterator * _domain_it;
137  IndexSetType _index_set;
138 public:
139  constexpr ViewIterator() = delete;
140 
141  template <class DomainItType>
142  ViewIterator(
143  DomainItType * domain_it,
144  const IndexSetType & index_set,
145  index_type position)
146  : base_t(position)
147  , _domain_it(domain_it)
148  , _index_set(index_set)
149  { }
150 
151  ViewIterator(
152  const self_t & other,
153  index_type position)
154  : base_t(position)
155  , _domain_it(other._domain_it)
156  , _index_set(other._index_set)
157  { }
158 
159  constexpr reference dereference(index_type idx) const {
160  return (_domain_it)[ (_index_set)[idx] ];
161  }
162 
163  constexpr index_type gpos() const {
164  return (_index_set)[this->pos()];
165  }
166 
167  constexpr const value_type * local() const {
168  return (_domain_it + (_index_set[this->pos()])).local();
169  }
170 
171  inline value_type * local() {
172  return (_domain_it + (_index_set[this->pos()])).local();
173  }
174 };
175 
176 template<class DomainT, class IndexSetT>
177 std::ostream & operator<<(
178  std::ostream & os,
179  const ViewIterator<DomainT, IndexSetT> & view_it)
180 {
181  std::ostringstream ss;
182  ss << dash::typestr(view_it) << " "
183  << "{ "
184  << "domain_it: " << view_it._domain_it << ", "
185  << "rpos: " << view_it.pos() << ", "
186  << "gpos: " << view_it.gpos()
187  << " }";
188  return operator<<(os, ss.str());
189 }
190 
191 } // namespace dash
192 
193 #endif // DASH__VIEW__VIEW_ITERATOR_H__INCLUDED
constexpr auto local(ViewType &v) -> typename std::enable_if<(std::is_pointer< typename ViewType::iterator >::value||(dash::view_traits< ViewValueT >::is_local::value)), ViewType &>::type
Definition: Local.h:28
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
DART Global pointer type.
Definition: dart_globmem.h:77
std::string typestr(const T &obj)
Returns string containing the type name of the given object.
Definition: TypeInfo.h:20