20 #ifndef CS_CRYPTO_UTIL_SPAN_H
21 #define CS_CRYPTO_UTIL_SPAN_H
23 #include <util/tools/crypto_traits.h>
28 #include <type_traits>
30 namespace cs_crypto::util {
36 using element_type = T;
37 using value_type = std::remove_cv_t<T>;
38 using size_type = std::size_t;
39 using difference_type = std::ptrdiff_t;
40 using reference = element_type &;
41 using const_reference =
const element_type &;
42 using pointer = element_type *;
43 using const_pointer =
const element_type *;
44 using iterator = pointer;
45 using const_iterator = const_pointer;
47 constexpr span() =
default;
49 template <
typename Iter,
typename = std::enable_if_t<std::is_same_v<
typename std::iterator_traits<Iter>::iterator_category,
50 std::random_access_iterator_tag>>>
51 constexpr span(Iter iter, size_type size)
noexcept
52 : m_data(std::addressof(*iter)), m_size(size)
56 template <std::size_t N>
57 constexpr explicit span(
const cs_crypto::traits::identity_t<element_type> (&arr)[N])
noexcept
58 : m_data(std::data(arr)), m_size(N)
62 template <std::size_t N>
63 constexpr explicit span(std::array<T, N> &arr)
noexcept
64 : m_data(std::data(arr)), m_size(N)
68 template <std::size_t N>
69 constexpr explicit span(
const std::array<T, N> &arr)
noexcept
70 : m_data(std::data(arr)), m_size(N)
74 constexpr span(
const span &) =
default;
75 constexpr span &operator=(
const span &) & =
default;
77 constexpr span(span &&) =
default;
78 constexpr span &operator=(span &&) & =
default;
80 constexpr const_iterator begin()
const noexcept
85 constexpr const_iterator end()
const noexcept
87 return m_data + m_size;
90 constexpr const_pointer data()
const noexcept
95 constexpr size_type size()
const noexcept
100 constexpr bool empty()
const noexcept
106 const_pointer m_data =
nullptr;
107 const size_type m_size = 0;
111 span(It, std::size_t) -> span<std::remove_reference_t<
typename std::iterator_traits<It>::reference>>;