DASH  0.3.0
UnorderedMapLocalIter.h
1 #ifndef DASH__MAP__UNORDERED_MAP_LOCAL_ITER_H__INCLUDED
2 #define DASH__MAP__UNORDERED_MAP_LOCAL_ITER_H__INCLUDED
3 
4 #include <dash/dart/if/dart.h>
5 
6 #include <dash/Types.h>
7 #include <dash/GlobPtr.h>
8 #include <dash/Allocator.h>
9 #include <dash/Team.h>
10 #include <dash/Onesided.h>
11 
12 #include <dash/map/UnorderedMapLocalIter.h>
13 
14 #include <dash/internal/Logging.h>
15 
16 #include <type_traits>
17 #include <list>
18 #include <vector>
19 #include <iterator>
20 #include <sstream>
21 #include <iostream>
22 
23 namespace dash {
24 
25 // Forward-declaration
26 template<
27  typename Key,
28  typename Mapped,
29  typename Hash,
30  typename Pred,
31  typename LMemSpace >
32 class UnorderedMap;
33 
34 template<
35  typename Key,
36  typename Mapped,
37  typename Hash,
38  typename Pred,
39  typename LMemSpace >
41 : public std::iterator<
42  std::random_access_iterator_tag,
43  std::pair<const Key, Mapped>,
44  dash::default_index_t,
45  std::pair<const Key, Mapped> *,
46  std::pair<const Key, Mapped> &>
47 {
48  template<typename K_, typename M_, typename H_, typename P_, typename LM_>
49  friend std::ostream & dash::operator<<(
50  std::ostream & os,
52 
53 private:
55  self_t;
56 
58  map_t;
59 
60 public:
61  typedef typename map_t::value_type value_type;
62 #if 0
63  typedef typename map_t::index_type index_type;
64  typedef typename map_t::size_type size_type;
65 #else
66  typedef dash::default_index_t index_type;
67  typedef dash::default_size_t size_type;
68 #endif
69 
70  typedef value_type * pointer;
71  typedef const value_type * const_pointer;
72  typedef value_type & reference;
73  typedef const value_type & const_reference;
74 
75  typedef struct {
76  team_unit_t unit;
77  index_type index{};
78  } local_index;
79 
80 public:
85  : UnorderedMapLocalIter(nullptr)
86  { }
87 
92  map_t * map,
93  index_type local_position)
94  : _map(map),
95  _idx(local_position),
96  _myid(dash::Team::GlobalUnitID())
97  {
98  DASH_LOG_TRACE("UnorderedMapLocalIter(map,lpos)()");
99  DASH_LOG_TRACE_VAR("UnorderedMapLocalIter(map,lpos)", _idx);
100  DASH_LOG_TRACE("UnorderedMapLocalIter(map,lpos) >");
101  }
102 
107  const self_t & other) = default;
108 
112  self_t & operator=(
113  const self_t & other) = default;
114 
118  UnorderedMapLocalIter(std::nullptr_t)
119  : _map(nullptr)
120  , _myid(DART_UNDEFINED_UNIT_ID)
121  , _is_nullptr(true)
122  {
123  DASH_LOG_TRACE("UnorderedMapLocalIter(nullptr)");
124  }
125 
129  self_t & operator=(std::nullptr_t) noexcept
130  {
131  _is_nullptr = true;
132  return *this;
133  }
134 
135  inline bool operator==(std::nullptr_t) const noexcept
136  {
137  return _is_nullptr;
138  }
139 
140  inline bool operator!=(std::nullptr_t) const noexcept
141  {
142  return !_is_nullptr;
143  }
144 
148  reference operator[](index_type offset)
149  {
150  auto res = *this;
151  res += offset;
152  return *this;
153  }
154 
160  explicit operator pointer() const
161  {
162  typedef typename map_t::local_node_iterator local_iter_t;
163  if (_is_nullptr) {
164  return nullptr;
165  }
166  // TODO: Must be extended for correctness: _idx refers to local iteration
167  // space, not local memory space. Undefined behaviour if local
168  // memory space has gaps, e.g. after erasing elements.
169  local_iter_t l_it = _map->globmem().lbegin();
170  return pointer(l_it + static_cast<index_type>(_idx));
171  }
172 
178  reference operator*() const
179  {
180  typedef typename map_t::local_node_iterator local_iter_t;
181  DASH_ASSERT(!_is_nullptr);
182  // TODO: Must be extended for correctness: _idx refers to local iteration
183  // space, not local memory space. Undefined behaviour if local
184  // memory space has gaps, e.g. after erasing elements.
185  local_iter_t l_it = _map->globmem().lbegin();
186  return *pointer(l_it + static_cast<index_type>(_idx));
187  }
188 
196  {
197  DASH_LOG_TRACE_VAR("UnorderedMapLocalIter.dart_gptr()", _idx);
199  if (!_is_nullptr) {
200  dart_gptr = _map->globmem().at(_myid, _idx).dart_gptr();
201  }
202  DASH_LOG_TRACE_VAR("UnorderedMapLocalIter.dart_gptr >", dart_gptr);
203  return dart_gptr;
204  }
205 
210  constexpr bool is_local() const noexcept
211  {
212  return true;
213  }
214 
218  inline local_index lpos() const noexcept
219  {
220  local_index local_pos;
221  local_pos.unit = _myid;
222  local_pos.index = _idx;
223  return local_pos;
224  }
225 
229  inline index_type pos() const noexcept
230  {
231  return _idx;
232  }
233 
237  inline self_t & operator++()
238  {
239  increment(1);
240  return *this;
241  }
242 
246  inline self_t & operator--()
247  {
248  decrement(1);
249  return *this;
250  }
251 
255  inline self_t operator++(int)
256  {
257  auto result = *this;
258  increment(1);
259  return result;
260  }
261 
265  inline self_t operator--(int)
266  {
267  auto result = *this;
268  decrement(1);
269  return result;
270  }
271 
272  template<typename K_, typename M_, typename H_, typename P_, typename A_>
273  inline bool operator==(
274  const UnorderedMapLocalIter<K_, M_, H_, P_, A_> & other) const
275  {
276  return (this == std::addressof(other) || _idx == other._idx);
277  }
278 
279  template<typename K_, typename M_, typename H_, typename P_, typename A_>
280  inline bool operator!=(
281  const UnorderedMapLocalIter<K_, M_, H_, P_, A_> & other) const
282  {
283  return !(*this == other);
284  }
285 
286  self_t & operator+=(index_type offset)
287  {
288  increment(offset);
289  return *this;
290  }
291 
292  self_t & operator-=(index_type offset)
293  {
294  decrement(offset);
295  return *this;
296  }
297 
298  self_t operator+(index_type offset) const
299  {
300  auto res = *this;
301  res += offset;
302  return res;
303  }
304 
305  self_t operator-(index_type offset) const
306  {
307  auto res = *this;
308  res -= offset;
309  return res;
310  }
311 
312  inline index_type operator+(
313  const self_t & other) const
314  {
315  return _idx + other._idx;
316  }
317 
318  inline index_type operator-(
319  const self_t & other) const
320  {
321  return _idx - other._idx;
322  }
323 
324  template<typename K_, typename M_, typename H_, typename P_, typename A_>
325  inline bool operator<(
326  const UnorderedMapLocalIter<K_, M_, H_, P_, A_> & other) const
327  {
328  return (_idx < other._idx);
329  }
330 
331  template<typename K_, typename M_, typename H_, typename P_, typename A_>
332  inline bool operator<=(
333  const UnorderedMapLocalIter<K_, M_, H_, P_, A_> & other) const
334  {
335  return (_idx <= other._idx);
336  }
337 
338  template<typename K_, typename M_, typename H_, typename P_, typename A_>
339  inline bool operator>(
340  const UnorderedMapLocalIter<K_, M_, H_, P_, A_> & other) const
341  {
342  return (_idx > other._idx);
343  }
344 
345  template<typename K_, typename M_, typename H_, typename P_, typename A_>
346  inline bool operator>=(
347  const UnorderedMapLocalIter<K_, M_, H_, P_, A_> & other) const
348  {
349  return (_idx >= other._idx);
350  }
351 
352 private:
356  void increment(index_type offset)
357  {
358  DASH_LOG_TRACE("UnorderedMapLocalIter.increment()",
359  "unit:", _myid,
360  "lidx:", _idx,
361  "offset:", offset);
362  _idx += offset;
363  DASH_LOG_TRACE("UnorderedMapLocalIter.increment >");
364  }
365 
369  void decrement(index_type offset)
370  {
371  DASH_LOG_TRACE("UnorderedMapLocalIter.decrement()",
372  "unit:", _myid,
373  "lidx:", _idx,
374  "offset:", -offset);
375  _idx -= offset;
376  DASH_LOG_TRACE("UnorderedMapLocalIter.decrement >");
377  }
378 
379 private:
381  map_t * _map = nullptr;
383  index_type _idx = -1;
387  bool _is_nullptr = false;
388 
389 }; // class UnorderedMapLocalIter
390 
391 template<
392  typename Key,
393  typename Mapped,
394  typename Hash,
395  typename Pred,
396  typename LMemSpace >
397 std::ostream & operator<<(
398  std::ostream & os,
400  Key, Mapped, Hash, Pred, LMemSpace> & it)
401 {
402  std::ostringstream ss;
403  ss << "dash::UnorderedMapLocalIter<"
404  << typeid(Key).name() << ","
405  << typeid(Mapped).name() << ">"
406  << "("
407  << "unit:" << it._myid << ", "
408  << "lidx:" << it._idx
409  << ")";
410  return operator<<(os, ss.str());
411 }
412 
413 } // namespace dash
414 
415 #endif // DASH__MAP__UNORDERED_MAP_LOCAL_ITER_H__INCLUDED
self_t operator++(int)
Postfix increment operator.
internal::default_unsigned_index default_size_t
Unsigned integer type used as default for size values.
Definition: Types.h:69
constexpr std::enable_if< std::is_integral< IndexType >::value, IndexType >::type index(IndexType idx)
Definition: Iterator.h:60
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
self_t & operator=(const self_t &other)=default
Assignment operator.
self_t & operator=(std::nullptr_t) noexcept
Null-pointer assignment operator.
const glob_mem_type & globmem() const
Reference to instance of DashGlobalMemoryConcept used for underlying memory management of this contai...
self_t & operator--()
Prefix decrement operator.
#define DART_UNDEFINED_TEAM_UNIT_ID
A dart_team_unit_t representing an undefined team-relative unit.
Definition: dart_types.h:237
UnorderedMapLocalIter(map_t *map, index_type local_position)
Constructor, creates iterator at specified global position.
self_t & operator++()
Prefix increment operator.
reference operator*() const
Dereference operator.
constexpr bool is_local() const noexcept
Checks whether the element referenced by this global iterator is in the calling unit&#39;s local memory...
dart_gptr_t dart_gptr() const
Explicit conversion to dart_gptr_t.
Definition: GlobHeapPtr.h:236
internal::default_signed_index default_index_t
Signed integer type used as default for index values.
Definition: Types.h:59
#define DART_GPTR_NULL
A NULL global pointer.
Definition: dart_globmem.h:105
A Team instance specifies a subset of all available units.
Definition: Team.h:41
index_type pos() const noexcept
Position of the iterator in global index space.
pointer at(team_unit_t unit, IndexT local_index)
Resolve the global iterator referencing an element position in a unit&#39;s local memory.
Definition: GlobHeapMem.h:913
DART Global pointer type.
Definition: dart_globmem.h:77
dart_gptr_t dart_gptr() const
Explicit conversion to dart_gptr_t.
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
Definition: Types.h:319
#define DART_UNDEFINED_UNIT_ID
Undefined unit ID.
Definition: dart_types.h:160
UnorderedMapLocalIter()
Default constructor.
UnorderedMapLocalIter(std::nullptr_t)
Null-pointer constructor.
reference operator[](index_type offset)
Random access operator.
local_pointer & lbegin() noexcept
Native pointer of the initial address of the local memory of the unit that initialized this GlobHeapM...
Definition: GlobHeapMem.h:831
self_t operator--(int)
Postfix decrement operator.
local_index lpos() const noexcept
Unit and local offset at the iterator&#39;s position.