Itoyori  v0.0.1
global_span.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ityr/common/util.hpp"
4 #include "ityr/ori/ori.hpp"
5 
6 namespace ityr {
7 
32 template <typename T>
33 class global_span {
34  using this_t = global_span<T>;
35 
36 public:
37  using element_type = T;
38  using value_type = std::remove_cv_t<element_type>;
39  using size_type = std::size_t;
42  using difference_type = typename std::iterator_traits<pointer>::difference_type;
43  using reference = typename std::iterator_traits<pointer>::reference;
44  using const_reference = typename std::iterator_traits<const_pointer>::reference;
45  using iterator = pointer;
47  using reverse_iterator = std::reverse_iterator<iterator>;
48  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
49 
50  constexpr global_span() noexcept {}
51 
52  template <typename ContiguousIterator>
53  constexpr global_span(ContiguousIterator first, size_type n)
54  : ptr_(&(*first)), n_(n) {}
55 
56  template <typename ContiguousIterator>
57  constexpr global_span(ContiguousIterator first, ContiguousIterator last)
58  : ptr_(&(*first)), n_(last - first) {}
59 
60  template <typename R>
61  constexpr global_span(R&& r)
62  : ptr_(r.data()), n_(r.size()) {
63  static_assert(ori::is_global_ptr_v<typename std::remove_reference_t<R>::iterator>);
64  }
65 
66  template <typename U>
67  constexpr global_span(const global_span<U>& s) noexcept
68  : ptr_(s.data()), n_(s.size()) { static_assert(sizeof(U) == sizeof(T)); }
69 
70  constexpr global_span(const this_t& other) noexcept = default;
71  constexpr this_t& operator=(const this_t& other) noexcept = default;
72 
73  constexpr pointer data() const noexcept { return ptr_; }
74  constexpr size_type size() const noexcept { return n_; }
75 
76  constexpr iterator begin() const noexcept { return ptr_; }
77  constexpr iterator end() const noexcept { return ptr_ + n_; }
78 
79  constexpr const_iterator cbegin() const noexcept { return ptr_; }
80  constexpr const_iterator cend() const noexcept { return ptr_ + n_; }
81 
82  constexpr reverse_iterator rbegin() const noexcept { return std::make_reverse_iterator(end()); }
83  constexpr reverse_iterator rend() const noexcept { return std::make_reverse_iterator(begin()); }
84 
85  constexpr const_reverse_iterator crbegin() const noexcept { return std::make_reverse_iterator(cend()); }
86  constexpr const_reverse_iterator crend() const noexcept { return std::make_reverse_iterator(begin()); }
87 
88  constexpr reference operator[](size_type i) const { assert(i <= n_); return ptr_[i]; }
89 
90  constexpr reference front() const { return *ptr_; }
91  constexpr reference back() const { return *(ptr_ + n_ - 1); }
92 
93  constexpr bool empty() const noexcept { return n_ == 0; }
94 
95  constexpr this_t subspan(size_type offset, size_type count) const {
96  assert(offset + count <= n_);
97  return this_t{ptr_ + offset, count};
98  }
99 
100 private:
101  pointer ptr_ = nullptr;
102  size_type n_ = 0;
103 };
104 
105 template <typename T>
106 inline constexpr auto data(const global_span<T>& s) noexcept {
107  return s.data();
108 }
109 
110 template <typename T>
111 inline constexpr auto size(const global_span<T>& s) noexcept {
112  return s.size();
113 }
114 
115 template <typename T>
116 inline constexpr auto begin(const global_span<T>& s) noexcept {
117  return s.begin();
118 }
119 
120 template <typename T>
121 inline constexpr auto end(const global_span<T>& s) noexcept {
122  return s.end();
123 }
124 
125 }
Global span to represent a view of a global memory range.
Definition: global_span.hpp:33
constexpr const_iterator cend() const noexcept
Definition: global_span.hpp:80
constexpr global_span(R &&r)
Definition: global_span.hpp:61
constexpr global_span() noexcept
Definition: global_span.hpp:50
constexpr iterator begin() const noexcept
Definition: global_span.hpp:76
constexpr const_iterator cbegin() const noexcept
Definition: global_span.hpp:79
ori::global_ptr< element_type > pointer
Definition: global_span.hpp:40
constexpr reverse_iterator rbegin() const noexcept
Definition: global_span.hpp:82
typename std::iterator_traits< pointer >::reference reference
Definition: global_span.hpp:43
constexpr const_reverse_iterator crend() const noexcept
Definition: global_span.hpp:86
std::size_t size_type
Definition: global_span.hpp:39
constexpr global_span(ContiguousIterator first, ContiguousIterator last)
Definition: global_span.hpp:57
T element_type
Definition: global_span.hpp:37
constexpr global_span(ContiguousIterator first, size_type n)
Definition: global_span.hpp:53
constexpr reference operator[](size_type i) const
Definition: global_span.hpp:88
constexpr const_reverse_iterator crbegin() const noexcept
Definition: global_span.hpp:85
constexpr size_type size() const noexcept
Definition: global_span.hpp:74
std::reverse_iterator< iterator > reverse_iterator
Definition: global_span.hpp:47
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: global_span.hpp:48
std::remove_cv_t< element_type > value_type
Definition: global_span.hpp:38
constexpr bool empty() const noexcept
Definition: global_span.hpp:93
typename std::iterator_traits< const_pointer >::reference const_reference
Definition: global_span.hpp:44
constexpr global_span(const global_span< U > &s) noexcept
Definition: global_span.hpp:67
constexpr this_t & operator=(const this_t &other) noexcept=default
constexpr iterator end() const noexcept
Definition: global_span.hpp:77
constexpr reference front() const
Definition: global_span.hpp:90
constexpr reference back() const
Definition: global_span.hpp:91
constexpr this_t subspan(size_type offset, size_type count) const
Definition: global_span.hpp:95
constexpr global_span(const this_t &other) noexcept=default
typename std::iterator_traits< pointer >::difference_type difference_type
Definition: global_span.hpp:42
constexpr pointer data() const noexcept
Definition: global_span.hpp:73
constexpr reverse_iterator rend() const noexcept
Definition: global_span.hpp:83
ori::global_ptr< std::add_const_t< element_type > > const_pointer
Definition: global_span.hpp:41
constexpr bool is_global_ptr_v
Definition: global_ptr.hpp:411
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
constexpr auto begin(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:183
constexpr auto data(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:173
constexpr auto size(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:178
constexpr auto end(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:188