span.hpp Source File

span.hpp Source File#

Composable Kernel: span.hpp Source File
utility/span.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
6#include <cstddef>
7#include <array>
8#include <type_traits>
9
10namespace ck {
11
12template <typename T>
13class span
14{
15 public:
16 using element_type = T;
17 using value_type = std::remove_cv_t<element_type>;
18 using size_type = std::size_t;
19 using difference_type = std::ptrdiff_t;
26
27 constexpr span() : span(nullptr, size_type{0}) {}
28
29 constexpr span(pointer first, size_type count) : ptr_(first), size_(count) {}
30
31 constexpr span(pointer first, pointer last) : span(first, last - first) {}
32
33 template <std::size_t N>
34 constexpr span(element_type (&arr)[N]) noexcept : span(arr, N)
35 {
36 }
37
38 template <std::size_t N>
39 constexpr span(std::array<value_type, N>& arr) noexcept : span(arr.data(), N)
40 {
41 }
42
43 template <typename Container>
44 constexpr span(const Container& container) : span(container.data(), container.size())
45 {
46 }
47
48 constexpr iterator begin() const noexcept { return ptr_; }
49 constexpr const_iterator cbegin() const noexcept { return begin(); }
50
51 constexpr iterator end() const noexcept { return begin() + size(); }
52 constexpr const_iterator cend() const noexcept { return end(); }
53
54 constexpr reference front() const { return *begin(); }
55 constexpr reference back() const { return *(--end()); }
56
57 constexpr reference operator[](size_type idx) const { return *(begin() + idx); }
58 constexpr pointer data() const noexcept { return ptr_; }
59
60 constexpr size_type size() const noexcept { return size_; }
61
62 private:
63 pointer ptr_;
64 size_type size_;
65};
66
67} // namespace ck
T element_type
Definition utility/span.hpp:16
constexpr size_type size() const noexcept
Definition utility/span.hpp:60
element_type * pointer
Definition utility/span.hpp:20
constexpr span(const Container &container)
Definition utility/span.hpp:44
constexpr pointer data() const noexcept
Definition utility/span.hpp:58
constexpr iterator begin() const noexcept
Definition utility/span.hpp:48
constexpr span(element_type(&arr)[N]) noexcept
Definition utility/span.hpp:34
constexpr span()
Definition utility/span.hpp:27
const element_type * const_pointer
Definition utility/span.hpp:21
element_type & reference
Definition utility/span.hpp:22
constexpr const_iterator cbegin() const noexcept
Definition utility/span.hpp:49
const element_type & const_reference
Definition utility/span.hpp:23
std::remove_cv_t< element_type > value_type
Definition utility/span.hpp:17
constexpr const_iterator cend() const noexcept
Definition utility/span.hpp:52
pointer iterator
Definition utility/span.hpp:24
constexpr iterator end() const noexcept
Definition utility/span.hpp:51
pointer const_iterator
Definition utility/span.hpp:25
constexpr span(pointer first, size_type count)
Definition utility/span.hpp:29
std::ptrdiff_t difference_type
Definition utility/span.hpp:19
std::size_t size_type
Definition utility/span.hpp:18
constexpr span(std::array< value_type, N > &arr) noexcept
Definition utility/span.hpp:39
constexpr reference back() const
Definition utility/span.hpp:55
constexpr span(pointer first, pointer last)
Definition utility/span.hpp:31
constexpr reference operator[](size_type idx) const
Definition utility/span.hpp:57
constexpr reference front() const
Definition utility/span.hpp:54
Definition ck.hpp:268
const GenericPointer< typename T::ValueType > & pointer
Definition pointer.h:1514