Itoyori  v0.0.1
span.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 
5 #include "ityr/common/util.hpp"
6 
7 namespace ityr::common {
8 
9 // TODO: remove it and use std::span in C++20
10 template <typename T>
11 class span {
12  using this_t = span<T>;
13 
14 public:
15  using element_type = T;
16  using value_type = std::remove_cv_t<T>;
17  using size_type = std::size_t;
18  using pointer = T*;
19  using iterator = pointer;
20  using reference = T&;
21 
22  span() {}
23  template <typename ContiguousIterator>
24  span(ContiguousIterator first, size_type n)
25  : ptr_(&(*first)), n_(n) {}
26  template <typename ContiguousIterator>
27  span(ContiguousIterator first, ContiguousIterator last)
28  : ptr_(&(*first)), n_(last - first) {}
29  template <typename U>
30  span(span<U> s) : ptr_(s.data()), n_(s.size() * sizeof(U) / sizeof(T)) {}
31 
32  constexpr pointer data() const noexcept { return ptr_; }
33  constexpr size_type size() const noexcept { return n_; }
34 
35  constexpr iterator begin() const noexcept { return ptr_; }
36  constexpr iterator end() const noexcept { return ptr_ + n_; }
37 
38  constexpr reference operator[](size_type i) const { assert(i <= n_); return ptr_[i]; }
39 
40  constexpr reference front() const { return *ptr_; }
41  constexpr reference back() const { return *(ptr_ + n_ - 1); }
42 
43  constexpr bool empty() const noexcept { return n_ == 0; }
44 
45  constexpr this_t subspan(size_type offset, size_type count) const {
46  assert(offset + count <= n_);
47  return {ptr_ + offset, count};
48  }
49 
50 private:
51  pointer ptr_ = nullptr;
52  size_type n_ = 0;
53 };
54 
55 template <typename T>
56 inline constexpr auto data(const span<T>& s) noexcept {
57  return s.data();
58 }
59 
60 template <typename T>
61 inline constexpr auto size(const span<T>& s) noexcept {
62  return s.size();
63 }
64 
65 template <typename T>
66 inline constexpr auto begin(const span<T>& s) noexcept {
67  return s.begin();
68 }
69 
70 template <typename T>
71 inline constexpr auto end(const span<T>& s) noexcept {
72  return s.end();
73 }
74 
75 }
Definition: span.hpp:11
span(ContiguousIterator first, size_type n)
Definition: span.hpp:24
T element_type
Definition: span.hpp:15
span(ContiguousIterator first, ContiguousIterator last)
Definition: span.hpp:27
std::remove_cv_t< T > value_type
Definition: span.hpp:16
T & reference
Definition: span.hpp:20
constexpr this_t subspan(size_type offset, size_type count) const
Definition: span.hpp:45
constexpr reference operator[](size_type i) const
Definition: span.hpp:38
span()
Definition: span.hpp:22
std::size_t size_type
Definition: span.hpp:17
constexpr pointer data() const noexcept
Definition: span.hpp:32
constexpr iterator end() const noexcept
Definition: span.hpp:36
constexpr reference back() const
Definition: span.hpp:41
constexpr bool empty() const noexcept
Definition: span.hpp:43
constexpr iterator begin() const noexcept
Definition: span.hpp:35
constexpr reference front() const
Definition: span.hpp:40
T * pointer
Definition: span.hpp:18
constexpr size_type size() const noexcept
Definition: span.hpp:33
pointer iterator
Definition: span.hpp:19
span(span< U > s)
Definition: span.hpp:30
Definition: allocator.hpp:16
constexpr auto end(const span< T > &s) noexcept
Definition: span.hpp:71
constexpr auto data(const span< T > &s) noexcept
Definition: span.hpp:56
constexpr auto size(const span< T > &s) noexcept
Definition: span.hpp:61
constexpr auto begin(const span< T > &s) noexcept
Definition: span.hpp:66