Itoyori  v0.0.1
global_iterator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ityr/common/util.hpp"
4 #include "ityr/ori/ori.hpp"
7 
8 namespace ityr {
9 
11 template <typename T, typename = void>
12 struct is_global_iterator : public std::false_type {};
13 
15 template <typename T>
16 struct is_global_iterator<T, std::void_t<typename T::checkout_iterator>> : public std::true_type {};
17 
22 template <typename T>
24 
25 namespace internal {
26 
27 template <typename GPtr, typename Mode>
28 using checkout_iterator_t =
29  std::conditional_t<std::is_same_v<Mode, checkout_mode::no_access_t>,
30  GPtr,
31  std::conditional_t<std::is_same_v<Mode, checkout_mode::read_t>,
32  const typename GPtr::value_type*,
33  typename GPtr::value_type*>>;
34 
35 template <typename T>
36 using src_checkout_mode_t = std::conditional_t<std::is_trivially_copyable_v<T>,
39 
40 template <typename T>
41 using dest_checkout_mode_t = std::conditional_t<std::is_trivially_copyable_v<T>,
44 
45 }
46 
51 template <typename T, typename Mode>
52 class global_iterator : public ori::global_ptr<T> {
54  using base_t = ori::global_ptr<T>;
55 
56 public:
57  using value_type = typename base_t::value_type;
59  using pointer = typename base_t::pointer;
60  using reference = typename base_t::reference;
62  using mode = Mode;
63  using checkout_iterator = internal::checkout_iterator_t<base_t, Mode>;
64 
65  global_iterator() : base_t(nullptr) {}
67 
68  constexpr this_t& operator+=(difference_type diff) noexcept {
69  base_t::operator+=(diff);
70  return *this;
71  }
72 
73  constexpr this_t& operator-=(difference_type diff) noexcept {
74  base_t::operator-=(diff);
75  return *this;
76  }
77 
78  constexpr this_t& operator++() noexcept { return (*this) += 1; }
79  constexpr this_t& operator--() noexcept { return (*this) -= 1; }
80 
81  constexpr this_t operator++(int) noexcept { this_t tmp(*this); ++(*this); return tmp; }
82  constexpr this_t operator--(int) noexcept { this_t tmp(*this); --(*this); return tmp; }
83 
84  constexpr this_t operator+(difference_type diff) const noexcept {
85  return this_t(base_t::operator+(diff));
86  }
87 
88  constexpr this_t operator-(difference_type diff) const noexcept {
89  return this_t(base_t::operator-(diff));
90  }
91 
92  auto checkout_nb(std::size_t count) const {
93  if constexpr(std::is_same_v<mode, checkout_mode::no_access_t>) {
94  return std::make_tuple(nullptr, base_t(*this));
95  } else {
97  cs.checkout_nb(base_t(*this), count, mode{});
98  return std::make_tuple(std::move(cs), cs.data());
99  }
100  }
101 };
102 
156 template <typename T, typename Mode>
157 inline global_iterator<T, Mode>
159  return global_iterator<T, Mode>(gptr);
160 }
161 
166 template <typename GlobalIterator>
167 class global_move_iterator : public GlobalIterator {
169  using base_t = GlobalIterator;
170 
171 public:
172  using value_type = typename base_t::value_type;
173  using difference_type = typename base_t::difference_type;
174  using pointer = typename base_t::pointer;
175  using reference = typename base_t::reference;
176  using iterator_category = typename base_t::iterator_category;
177  using mode = typename GlobalIterator::mode;
178  using checkout_iterator = std::move_iterator<typename GlobalIterator::checkout_iterator>;
179 
180  static_assert(std::is_same_v<mode, internal::src_checkout_mode_t<value_type>>);
181 
182  explicit global_move_iterator(GlobalIterator git)
183  : base_t(git) {}
184 
185  GlobalIterator base() const {
186  return static_cast<base_t>(*this);
187  }
188 
189  constexpr this_t& operator+=(difference_type diff) noexcept {
190  base_t::operator+=(diff);
191  return *this;
192  }
193 
194  constexpr this_t& operator-=(difference_type diff) noexcept {
195  base_t::operator-=(diff);
196  return *this;
197  }
198 
199  constexpr this_t& operator++() noexcept { return (*this) += 1; }
200  constexpr this_t& operator--() noexcept { return (*this) -= 1; }
201 
202  constexpr this_t operator++(int) noexcept { this_t tmp(*this); ++(*this); return tmp; }
203  constexpr this_t operator--(int) noexcept { this_t tmp(*this); --(*this); return tmp; }
204 
205  constexpr this_t operator+(difference_type diff) const noexcept {
206  return this_t(base_t::operator+(diff));
207  }
208 
209  constexpr this_t operator-(difference_type diff) const noexcept {
210  return this_t(base_t::operator-(diff));
211  }
212 
213  auto checkout_nb(std::size_t count) const {
214  auto&& [cs, it] = base_t::checkout_nb(count);
215  return std::make_tuple(std::move(cs), std::make_move_iterator(it));
216  }
217 };
218 
256 template <typename T>
257 inline global_move_iterator<global_iterator<T, internal::src_checkout_mode_t<T>>>
259  return global_move_iterator(make_global_iterator(gptr, internal::src_checkout_mode_t<T>{}));
260 }
261 
266 template <typename GlobalIterator>
267 class global_reverse_iterator : public std::reverse_iterator<GlobalIterator> {
269  using base_t = std::reverse_iterator<GlobalIterator>;
270 
271 public:
272  using value_type = typename base_t::value_type;
273  using difference_type = typename base_t::difference_type;
274  using pointer = typename base_t::pointer;
275  using reference = typename base_t::reference;
276  using iterator_category = typename base_t::iterator_category;
277  using mode = typename GlobalIterator::mode;
278  using checkout_iterator = std::reverse_iterator<typename GlobalIterator::checkout_iterator>;
279 
280  explicit global_reverse_iterator(GlobalIterator git)
281  : base_t(git) {}
282 
283  GlobalIterator base() const {
284  return static_cast<base_t>(*this).base();
285  }
286 
287  constexpr this_t& operator+=(difference_type diff) noexcept {
288  base_t::operator+=(diff);
289  return *this;
290  }
291 
292  constexpr this_t& operator-=(difference_type diff) noexcept {
293  base_t::operator-=(diff);
294  return *this;
295  }
296 
297  constexpr this_t& operator++() noexcept { return (*this) += 1; }
298  constexpr this_t& operator--() noexcept { return (*this) -= 1; }
299 
300  constexpr this_t operator++(int) noexcept { this_t tmp(*this); ++(*this); return tmp; }
301  constexpr this_t operator--(int) noexcept { this_t tmp(*this); --(*this); return tmp; }
302 
303  constexpr this_t operator+(difference_type diff) const noexcept {
304  return this_t(base_t::operator+(diff));
305  }
306 
307  constexpr this_t operator-(difference_type diff) const noexcept {
308  return this_t(base_t::operator-(diff));
309  }
310 
311  auto checkout_nb(std::size_t count) const {
312  GlobalIterator git = base();
313  auto&& [cs, it] = std::prev(git, count).checkout_nb(count);
314  return std::make_tuple(std::move(cs), std::make_reverse_iterator(std::next(it, count)));
315  }
316 };
317 
331 template <typename T, typename Mode>
332 inline global_reverse_iterator<global_iterator<T, Mode>>
335 }
336 
341 template <typename GlobalIterator>
342 class global_construct_iterator : public GlobalIterator {
344  using base_t = GlobalIterator;
345 
346  static_assert(std::is_same_v<typename GlobalIterator::mode, checkout_mode::write_t>);
347 
348 public:
349  using value_type = typename base_t::value_type;
350  using difference_type = typename base_t::difference_type;
351  using pointer = typename base_t::pointer;
352  using reference = typename base_t::reference;
353  using iterator_category = typename base_t::iterator_category;
354  using mode = typename GlobalIterator::mode;
356 
357  explicit global_construct_iterator(GlobalIterator git)
358  : base_t(git) {}
359 
360  GlobalIterator base() const {
361  return static_cast<base_t>(*this);
362  }
363 
364  constexpr this_t& operator+=(difference_type diff) noexcept {
365  base_t::operator+=(diff);
366  return *this;
367  }
368 
369  constexpr this_t& operator-=(difference_type diff) noexcept {
370  base_t::operator-=(diff);
371  return *this;
372  }
373 
374  constexpr this_t& operator++() noexcept { return (*this) += 1; }
375  constexpr this_t& operator--() noexcept { return (*this) -= 1; }
376 
377  constexpr this_t operator++(int) noexcept { this_t tmp(*this); ++(*this); return tmp; }
378  constexpr this_t operator--(int) noexcept { this_t tmp(*this); --(*this); return tmp; }
379 
380  constexpr this_t operator+(difference_type diff) const noexcept {
381  return this_t(base_t::operator+(diff));
382  }
383 
384  constexpr this_t operator-(difference_type diff) const noexcept {
385  return this_t(base_t::operator-(diff));
386  }
387 
388  auto checkout_nb(std::size_t count) const {
389  auto&& [cs, it] = base_t::checkout_nb(count);
390  return std::make_tuple(std::move(cs), make_count_iterator(it));
391  }
392 };
393 
404 template <typename GlobalIterator>
405 inline global_construct_iterator<GlobalIterator>
406 make_construct_iterator(GlobalIterator git) {
407  return global_construct_iterator(git);
408 }
409 
424 template <typename T>
425 inline global_construct_iterator<global_iterator<T, checkout_mode::write_t>>
428 }
429 
434 template <typename GlobalIterator>
435 class global_destruct_iterator : public GlobalIterator {
437  using base_t = GlobalIterator;
438 
439  static_assert(std::is_same_v<typename GlobalIterator::mode, checkout_mode::read_write_t>);
440 
441 public:
442  using value_type = typename base_t::value_type;
443  using difference_type = typename base_t::difference_type;
444  using pointer = typename base_t::pointer;
445  using reference = typename base_t::reference;
446  using iterator_category = typename base_t::iterator_category;
447  using mode = typename GlobalIterator::mode;
449 
450  explicit global_destruct_iterator(GlobalIterator git)
451  : base_t(git) {}
452 
453  GlobalIterator base() const {
454  return static_cast<base_t>(*this);
455  }
456 
457  constexpr this_t& operator+=(difference_type diff) noexcept {
458  base_t::operator+=(diff);
459  return *this;
460  }
461 
462  constexpr this_t& operator-=(difference_type diff) noexcept {
463  base_t::operator-=(diff);
464  return *this;
465  }
466 
467  constexpr this_t& operator++() noexcept { return (*this) += 1; }
468  constexpr this_t& operator--() noexcept { return (*this) -= 1; }
469 
470  constexpr this_t operator++(int) noexcept { this_t tmp(*this); ++(*this); return tmp; }
471  constexpr this_t operator--(int) noexcept { this_t tmp(*this); --(*this); return tmp; }
472 
473  constexpr this_t operator+(difference_type diff) const noexcept {
474  return this_t(base_t::operator+(diff));
475  }
476 
477  constexpr this_t operator-(difference_type diff) const noexcept {
478  return this_t(base_t::operator-(diff));
479  }
480 
481  auto checkout_nb(std::size_t count) const {
482  auto&& [cs, it] = base_t::checkout_nb(count);
483  return std::make_tuple(std::move(cs), make_count_iterator(it));
484  }
485 };
486 
497 template <typename GlobalIterator>
498 inline global_destruct_iterator<GlobalIterator>
499 make_destruct_iterator(GlobalIterator git) {
500  return global_destruct_iterator(git);
501 }
502 
517 template <typename T>
518 inline global_destruct_iterator<global_iterator<T, checkout_mode::read_write_t>>
521 }
522 
523 // Definitions for ADL
524 
535 template <typename T, typename Mode>
536 inline global_move_iterator<global_iterator<T, Mode>>
538  return global_move_iterator(git);
539 }
540 
551 template <typename GlobalIterator>
552 inline global_move_iterator<global_reverse_iterator<GlobalIterator>>
554  return global_move_iterator(git);
555 }
556 
567 template <typename T, typename Mode>
568 inline global_reverse_iterator<global_iterator<T, Mode>>
570  return global_reverse_iterator(git);
571 }
572 
583 template <typename GlobalIterator>
584 inline global_reverse_iterator<global_move_iterator<GlobalIterator>>
586  return global_reverse_iterator(git);
587 }
588 
589 static_assert(is_global_iterator_v<global_iterator<int, checkout_mode::read_write_t>>);
590 static_assert(is_global_iterator_v<global_move_iterator<global_iterator<int, checkout_mode::read_t>>>);
591 static_assert(is_global_iterator_v<global_reverse_iterator<global_iterator<int, checkout_mode::read_write_t>>>);
592 static_assert(is_global_iterator_v<global_move_iterator<global_reverse_iterator<global_iterator<int, checkout_mode::read_t>>>>);
593 static_assert(!is_global_iterator_v<int>);
594 static_assert(!is_global_iterator_v<ori::global_ptr<int>>);
595 
596 }
Checkout span to automatically manage the lifetime of checked-out memory.
Definition: checkout_span.hpp:66
constexpr pointer data() const noexcept
Definition: checkout_span.hpp:110
void checkout_nb(ori::global_ptr< T > gptr, std::size_t n, Mode)
Manually perform the nonblocking checkout operation by checking in the previous span.
Definition: checkout_span.hpp:144
Count iterator.
Definition: count_iterator.hpp:33
Global iterator for constructing objects.
Definition: global_iterator.hpp:342
typename base_t::iterator_category iterator_category
Definition: global_iterator.hpp:353
typename base_t::value_type value_type
Definition: global_iterator.hpp:349
typename GlobalIterator::mode mode
Definition: global_iterator.hpp:354
constexpr this_t & operator+=(difference_type diff) noexcept
Definition: global_iterator.hpp:364
constexpr this_t & operator-=(difference_type diff) noexcept
Definition: global_iterator.hpp:369
constexpr this_t & operator++() noexcept
Definition: global_iterator.hpp:374
typename base_t::reference reference
Definition: global_iterator.hpp:352
GlobalIterator base() const
Definition: global_iterator.hpp:360
constexpr this_t operator-(difference_type diff) const noexcept
Definition: global_iterator.hpp:384
constexpr this_t operator+(difference_type diff) const noexcept
Definition: global_iterator.hpp:380
typename base_t::difference_type difference_type
Definition: global_iterator.hpp:350
global_construct_iterator(GlobalIterator git)
Definition: global_iterator.hpp:357
constexpr this_t operator++(int) noexcept
Definition: global_iterator.hpp:377
auto checkout_nb(std::size_t count) const
Definition: global_iterator.hpp:388
constexpr this_t operator--(int) noexcept
Definition: global_iterator.hpp:378
constexpr this_t & operator--() noexcept
Definition: global_iterator.hpp:375
typename base_t::pointer pointer
Definition: global_iterator.hpp:351
Global iterator for destructing objects.
Definition: global_iterator.hpp:435
constexpr this_t operator-(difference_type diff) const noexcept
Definition: global_iterator.hpp:477
typename GlobalIterator::mode mode
Definition: global_iterator.hpp:447
constexpr this_t operator--(int) noexcept
Definition: global_iterator.hpp:471
global_destruct_iterator(GlobalIterator git)
Definition: global_iterator.hpp:450
typename base_t::value_type value_type
Definition: global_iterator.hpp:442
constexpr this_t & operator++() noexcept
Definition: global_iterator.hpp:467
typename base_t::difference_type difference_type
Definition: global_iterator.hpp:443
typename base_t::pointer pointer
Definition: global_iterator.hpp:444
constexpr this_t & operator--() noexcept
Definition: global_iterator.hpp:468
constexpr this_t & operator-=(difference_type diff) noexcept
Definition: global_iterator.hpp:462
auto checkout_nb(std::size_t count) const
Definition: global_iterator.hpp:481
typename base_t::iterator_category iterator_category
Definition: global_iterator.hpp:446
constexpr this_t & operator+=(difference_type diff) noexcept
Definition: global_iterator.hpp:457
constexpr this_t operator+(difference_type diff) const noexcept
Definition: global_iterator.hpp:473
GlobalIterator base() const
Definition: global_iterator.hpp:453
typename base_t::reference reference
Definition: global_iterator.hpp:445
constexpr this_t operator++(int) noexcept
Definition: global_iterator.hpp:470
Global iterator to enable/disable automatic checkout.
Definition: global_iterator.hpp:52
Mode mode
Definition: global_iterator.hpp:62
constexpr this_t operator++(int) noexcept
Definition: global_iterator.hpp:81
constexpr this_t operator--(int) noexcept
Definition: global_iterator.hpp:82
global_iterator(ori::global_ptr< T > gptr)
Definition: global_iterator.hpp:66
constexpr this_t & operator++() noexcept
Definition: global_iterator.hpp:78
constexpr this_t operator+(difference_type diff) const noexcept
Definition: global_iterator.hpp:84
constexpr this_t & operator+=(difference_type diff) noexcept
Definition: global_iterator.hpp:68
constexpr this_t operator-(difference_type diff) const noexcept
Definition: global_iterator.hpp:88
global_iterator()
Definition: global_iterator.hpp:65
auto checkout_nb(std::size_t count) const
Definition: global_iterator.hpp:92
internal::checkout_iterator_t< base_t, Mode > checkout_iterator
Definition: global_iterator.hpp:63
constexpr this_t & operator-=(difference_type diff) noexcept
Definition: global_iterator.hpp:73
constexpr this_t & operator--() noexcept
Definition: global_iterator.hpp:79
Global iterator for moving objects.
Definition: global_iterator.hpp:167
constexpr this_t operator--(int) noexcept
Definition: global_iterator.hpp:203
typename base_t::difference_type difference_type
Definition: global_iterator.hpp:173
typename base_t::iterator_category iterator_category
Definition: global_iterator.hpp:176
typename base_t::pointer pointer
Definition: global_iterator.hpp:174
typename base_t::value_type value_type
Definition: global_iterator.hpp:172
std::move_iterator< typename GlobalIterator::checkout_iterator > checkout_iterator
Definition: global_iterator.hpp:178
constexpr this_t & operator++() noexcept
Definition: global_iterator.hpp:199
constexpr this_t & operator-=(difference_type diff) noexcept
Definition: global_iterator.hpp:194
global_move_iterator(GlobalIterator git)
Definition: global_iterator.hpp:182
typename base_t::reference reference
Definition: global_iterator.hpp:175
constexpr this_t & operator+=(difference_type diff) noexcept
Definition: global_iterator.hpp:189
constexpr this_t operator-(difference_type diff) const noexcept
Definition: global_iterator.hpp:209
GlobalIterator base() const
Definition: global_iterator.hpp:185
auto checkout_nb(std::size_t count) const
Definition: global_iterator.hpp:213
constexpr this_t & operator--() noexcept
Definition: global_iterator.hpp:200
constexpr this_t operator+(difference_type diff) const noexcept
Definition: global_iterator.hpp:205
constexpr this_t operator++(int) noexcept
Definition: global_iterator.hpp:202
typename GlobalIterator::mode mode
Definition: global_iterator.hpp:177
Reverse iterator for global memory.
Definition: global_iterator.hpp:267
typename GlobalIterator::mode mode
Definition: global_iterator.hpp:277
constexpr this_t operator-(difference_type diff) const noexcept
Definition: global_iterator.hpp:307
constexpr this_t & operator--() noexcept
Definition: global_iterator.hpp:298
typename base_t::value_type value_type
Definition: global_iterator.hpp:272
constexpr this_t & operator-=(difference_type diff) noexcept
Definition: global_iterator.hpp:292
auto checkout_nb(std::size_t count) const
Definition: global_iterator.hpp:311
typename base_t::iterator_category iterator_category
Definition: global_iterator.hpp:276
constexpr this_t & operator+=(difference_type diff) noexcept
Definition: global_iterator.hpp:287
typename base_t::pointer pointer
Definition: global_iterator.hpp:274
typename base_t::reference reference
Definition: global_iterator.hpp:275
GlobalIterator base() const
Definition: global_iterator.hpp:283
constexpr this_t operator--(int) noexcept
Definition: global_iterator.hpp:301
constexpr this_t & operator++() noexcept
Definition: global_iterator.hpp:297
global_reverse_iterator(GlobalIterator git)
Definition: global_iterator.hpp:280
typename base_t::difference_type difference_type
Definition: global_iterator.hpp:273
std::reverse_iterator< typename GlobalIterator::checkout_iterator > checkout_iterator
Definition: global_iterator.hpp:278
constexpr this_t operator++(int) noexcept
Definition: global_iterator.hpp:300
constexpr this_t operator+(difference_type diff) const noexcept
Definition: global_iterator.hpp:303
Definition: global_ptr.hpp:19
std::ptrdiff_t difference_type
Definition: global_ptr.hpp:25
std::remove_cv_t< T > value_type
Definition: global_ptr.hpp:24
std::random_access_iterator_tag iterator_category
Definition: global_ptr.hpp:28
T * pointer
Definition: global_ptr.hpp:26
global_ref< T > reference
Definition: global_ptr.hpp:27
constexpr this_t & operator+=(difference_type diff) noexcept
Definition: global_ptr.hpp:51
constexpr this_t & operator-=(difference_type diff) noexcept
Definition: global_ptr.hpp:56
Definition: global_ptr.hpp:173
constexpr read_write_t read_write
Read+Write checkout mode.
Definition: checkout_span.hpp:39
ori::mode::read_t read_t
See ityr::checkout_mode::read.
Definition: checkout_span.hpp:12
ori::mode::write_t write_t
See ityr::checkout_mode::write.
Definition: checkout_span.hpp:22
constexpr write_t write
Write-only checkout mode.
Definition: checkout_span.hpp:29
ITYR_CONCAT(mode_, ITYR_PROFILER_MODE) mode
Definition: profiler.hpp:257
std::pair< T *, bool > checkout_nb(global_ptr< T > ptr, std::size_t count, mode::read_t)
Definition: ori.hpp:108
Definition: allocator.hpp:16
global_reverse_iterator< global_iterator< T, Mode > > make_reverse_iterator(ori::global_ptr< T > gptr, Mode mode)
Make a reverse iterator for global memory.
Definition: global_iterator.hpp:333
global_destruct_iterator< GlobalIterator > make_destruct_iterator(GlobalIterator git)
Make a global iterator for destructing objects.
Definition: global_iterator.hpp:499
global_construct_iterator< GlobalIterator > make_construct_iterator(GlobalIterator git)
Make a global iterator for constructing objects.
Definition: global_iterator.hpp:406
constexpr bool is_global_iterator_v
True if T is a global iterator (ityr::global_iterator).
Definition: global_iterator.hpp:23
global_move_iterator< global_reverse_iterator< GlobalIterator > > make_move_iterator(global_reverse_iterator< GlobalIterator > git)
Make a global iterator for moving objects.
Definition: global_iterator.hpp:553
global_iterator< T, Mode > make_global_iterator(ori::global_ptr< T > gptr, Mode)
Make a global iterator to enable/disable automatic checkout.
Definition: global_iterator.hpp:158
global_move_iterator< global_iterator< T, internal::src_checkout_mode_t< T > > > make_move_iterator(ori::global_ptr< T > gptr)
Make a global iterator for moving objects.
Definition: global_iterator.hpp:258
auto make_count_iterator(T x)
Definition: count_iterator.hpp:98
global_reverse_iterator< global_move_iterator< GlobalIterator > > make_reverse_iterator(global_move_iterator< GlobalIterator > git)
Make a reverse iterator for global memory.
Definition: global_iterator.hpp:585
ForwardIteratorD move(const ExecutionPolicy &policy, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIteratorD first_d)
Move a range to another.
Definition: parallel_loop.hpp:934
See ityr::is_global_iterator_v.
Definition: global_iterator.hpp:12
Definition: util.hpp:14