Elffuss BraidHub

kikocis/kleidiai public

main · 7.1 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6
7#pragma once
8
9#include <array>
10#include <cstddef>
11#include <initializer_list>
12#include <iterator>
13#include <type_traits>
14#include <vector>
15
16#include "test/common/assert.hpp"
17
18namespace kai::test {
19
20/// Reference to a contiguous sequence of objects in the memory.
21///
22/// This class is similar to @ref std::span from C++20.
23template <typename T>
24class Span {
25 template <typename U>
26 friend class Span;
27
28public:
29 class Iterator {
30 public:
31 using iterator_category = std::random_access_iterator_tag;
32 using value_type = std::remove_cv_t<T>;
33 using difference_type = ptrdiff_t;
34 using pointer = T*;
35 using reference = T&;
36
37 constexpr Iterator(T* ptr) : m_ptr(ptr) {
38 }
39
40 constexpr T& operator*() const noexcept {
41 return *m_ptr;
42 }
43
44 constexpr T* operator->() const noexcept {
45 return m_ptr;
46 }
47
48 constexpr Iterator& operator++() noexcept {
49 ++m_ptr;
50 return *this;
51 }
52
53 constexpr Iterator operator++(int) noexcept {
54 Iterator it = *this;
55 ++m_ptr;
56 return it;
57 }
58
59 constexpr Iterator& operator--() noexcept {
60 --m_ptr;
61 return *this;
62 }
63
64 constexpr Iterator operator--(int) noexcept {
65 Iterator it = *this;
66 --m_ptr;
67 return it;
68 }
69
70 constexpr Iterator& operator+=(size_t len) noexcept {
71 m_ptr += len;
72 return *this;
73 }
74
75 constexpr Iterator& operator-=(size_t len) noexcept {
76 m_ptr -= len;
77 return *this;
78 }
79
80 [[nodiscard]] constexpr Iterator operator+(size_t len) const noexcept {
81 return {m_ptr + len};
82 }
83
84 [[nodiscard]] friend constexpr Iterator operator+(size_t len, Iterator it) noexcept {
85 return {it.m_ptr + len};
86 }
87
88 [[nodiscard]] constexpr Iterator operator-(size_t len) const noexcept {
89 return {m_ptr - len};
90 }
91
92 [[nodiscard]] constexpr ptrdiff_t operator-(Iterator other) const noexcept {
93 return m_ptr - other.m_ptr;
94 }
95
96 [[nodiscard]] constexpr T& operator[](size_t idx) const noexcept {
97 return m_ptr[idx];
98 }
99
100 [[nodiscard]] constexpr bool operator==(Iterator other) const noexcept {
101 return m_ptr == other.m_ptr;
102 }
103
104 [[nodiscard]] constexpr bool operator!=(Iterator other) const noexcept {
105 return m_ptr != other.m_ptr;
106 }
107
108 [[nodiscard]] constexpr bool operator>(Iterator other) const noexcept {
109 return m_ptr > other.m_ptr;
110 }
111
112 [[nodiscard]] constexpr bool operator<(Iterator other) const noexcept {
113 return m_ptr < other.m_ptr;
114 }
115
116 [[nodiscard]] constexpr bool operator>=(Iterator other) const noexcept {
117 return m_ptr >= other.m_ptr;
118 }
119
120 [[nodiscard]] constexpr bool operator<=(Iterator other) const noexcept {
121 return m_ptr <= other.m_ptr;
122 }
123
124 private:
125 T* m_ptr;
126 };
127
128 /// Creates an empty span.
129 constexpr Span() noexcept : m_ptr(nullptr), m_size(0) {
130 }
131
132 /// Creates a span refering to the specified sequence.
133 ///
134 /// @param[in] first The iterator to the first element of the sequence.
135 /// @param[in] count The number of elements.
136 template <typename It>
137 constexpr Span(It first, size_t count) noexcept : m_ptr(first), m_size(count) {
138 }
139
140 /// Creates a span refering to the specified array.
141 ///
142 /// @param[in] arr The array to be refered.
143 template <typename U, size_t N>
144 constexpr Span(const std::array<U, N>& arr) noexcept : m_ptr(arr.data()), m_size(arr.size()) {
145 }
146
147 /// Creates a span refering to the specified array.
148 ///
149 /// @param[in] arr The array to be refered.
150 template <typename U, size_t N>
151 constexpr Span(std::array<U, N>& arr) noexcept : m_ptr(arr.data()), m_size(arr.size()) {
152 }
153
154 /// Creates a span refering to the specified vector.
155 ///
156 /// @param[in] vec The vector to be refered.
157 template <typename U>
158 constexpr Span(const std::vector<U>& vec) noexcept : m_ptr(vec.data()), m_size(vec.size()) {
159 }
160
161 /// Creates a span refering to the specified vector.
162 ///
163 /// @param[in] vec The vector to be refered.
164 template <typename U>
165 constexpr Span(std::vector<U>& vec) noexcept : m_ptr(vec.data()), m_size(vec.size()) {
166 }
167
168 /// Creates a span refering to the specified initializer list.
169 ///
170 /// @param[in] il The initializer list to be refered.
171 constexpr Span(std::initializer_list<T> il) noexcept : m_ptr(std::data(il)), m_size(il.size()) {
172 }
173
174 /// Creates a spen refering to the specified span.
175 ///
176 /// @param[in] other The span to be refered.
177 template <typename U>
178 constexpr Span(const Span<U>& other) noexcept : m_ptr(other.m_ptr), m_size(other.m_size) {
179 }
180
181 /// Destructor.
182 ~Span() = default;
183
184 /// Copy constructor.
185 constexpr Span(const Span&) noexcept = default;
186
187 /// Copy assignment.
188 constexpr Span& operator=(const Span&) noexcept = default;
189
190 /// Move constructor.
191 constexpr Span(Span&&) noexcept = default;
192
193 /// Move assignment.
194 constexpr Span& operator=(Span&&) noexcept = default;
195
196 /// Gets a forward iterator to the beginning of the span.
197 [[nodiscard]] constexpr Iterator begin() const noexcept {
198 return {m_ptr};
199 }
200
201 /// Gets a forward iterator to the end of the span.
202 [[nodiscard]] constexpr Iterator end() const noexcept {
203 return {m_ptr + m_size};
204 }
205
206 /// Gets the first element.
207 [[nodiscard]] constexpr T& front() const {
208 return m_ptr[0];
209 }
210
211 /// Gets the last element.
212 [[nodiscard]] constexpr T& back() const {
213 return m_ptr[m_size - 1];
214 }
215
216 /// Gets the element at the specified index with bounds checking.
217 [[nodiscard]] constexpr T& at(size_t idx) const {
218 KAI_TEST_ASSERT(idx < m_size);
219 return m_ptr[idx];
220 }
221
222 /// Gets the element at the specified index.
223 [[nodiscard]] constexpr T& operator[](size_t idx) const {
224 return m_ptr[idx];
225 }
226
227 /// Gets the pointer to the data.
228 [[nodiscard]] constexpr T* data() const noexcept {
229 return m_ptr;
230 }
231
232 /// Gets the number of elements.
233 [[nodiscard]] constexpr size_t size() const noexcept {
234 return m_size;
235 }
236
237 /// Gets a value indicating whether the span is empty.
238 [[nodiscard]] constexpr bool empty() const noexcept {
239 return m_size == 0;
240 }
241
242 /// Gets a sub-view of the span.
243 [[nodiscard]] constexpr Span subspan(size_t offset) const {
244 KAI_TEST_ASSERT(offset <= m_size);
245 return {m_ptr + offset, m_size - offset};
246 }
247
248 /// Gets a sub-view of the span.
249 [[nodiscard]] constexpr Span subspan(size_t offset, size_t count) const {
250 KAI_TEST_ASSERT(offset + count <= m_size);
251 return {m_ptr + offset, count};
252 }
253
254private:
255 T* m_ptr;
256 size_t m_size;
257};
258
259} // namespace kai::test