forked from GPUOpen-Drivers/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palSpan.h
225 lines (200 loc) · 8.77 KB
/
palSpan.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
***********************************************************************************************************************
*
* Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************************************/
/**
***********************************************************************************************************************
* @file palSpan.h
* @brief PAL utility collection Span class declaration.
***********************************************************************************************************************
*/
#pragma once
#include "palAssert.h"
#include "palUtil.h"
namespace Util
{
/**
***********************************************************************************************************************
* @brief Span container
*
* Span is an array with a length, where the data is not owned by the Span object. It is similar to C++20 std::span,
* but only the dynamic extent variant. It is similar to LLVM MutableArrayRef and ArrayRef. A Span is intended to
* be passed around by value.
*
***********************************************************************************************************************
*/
template<typename T>
class Span
{
public:
/// Constructor from nothing. This allows you to use {} to mean an empty Span.
constexpr Span() : m_pData(nullptr), m_numElements(0) {}
/// Constructor from pointer and length
///
/// @param [in] data Pointer to the start of the array
/// @param numElements Number of elements in the array
constexpr Span(T* pData, size_t numElements) : m_pData(pData), m_numElements(numElements) {}
/// Copy constructor
///
/// @param [in] src Other Span to copy from
constexpr Span(const Span<T>& src) : m_pData(src.m_pData), m_numElements(src.m_numElements) {}
/// Constructor from C++ array
///
/// @param [in] src C++ array
template<size_t NumElements> constexpr Span(T(& src)[NumElements]) : m_pData(&src[0]), m_numElements(NumElements) {}
/// Constructor from single element
///
/// @param [in] src Single element
constexpr Span(T& src) : m_pData(&src), m_numElements(1) {}
/// Implicitly convert a Span to its const-element equivalent.
///
/// @returns The same span, but with const element type
constexpr operator Span<const T>() const { return Span<const T>(m_pData, m_numElements); }
/// Returns the element at the location specified.
///
/// @param [in] index Integer location of the element needed.
///
/// @returns The element at location specified by index by reference
constexpr T& At(size_t index) const
{
PAL_CONSTEXPR_ASSERT(index < m_numElements);
return *(m_pData + index);
}
constexpr T& operator[](size_t index) const noexcept { return At(index); }
/// Returns the data at the front of the vector.
///
/// @returns The data at the front of the vector.
constexpr T& Front() const
{
PAL_CONSTEXPR_ASSERT(IsEmpty() == false);
return *m_pData;
}
/// Returns the data at the back of the vector.
///
/// @returns The data at the back of the vector.
constexpr T& Back() const
{
PAL_CONSTEXPR_ASSERT(IsEmpty() == false);
return *(m_pData + (m_numElements - 1));
}
/// Returns an iterator to the first element of the vector.
///
/// @returns An iterator to first element of the vector.
constexpr T* Begin() const { return m_pData; }
/// Returns an iterator beyond the last element of the vector. (NOT at the last element like Util::Vector::End()!)
///
/// @warning Accessing an element using an iterator of an empty vector will cause an access violation!
///
/// @returns VectorIterator An iterator to last element of the vector.
constexpr T* End() const { return m_pData + m_numElements; }
/// Returns pointer to the underlying buffer serving as data storage.
///
/// @returns Pointer to the underlying data storage.
/// For a non-empty span, the returned pointer contains address of the first element.
/// For an empty span, the returned pointer may or may not be a null pointer.
constexpr T* Data() const { return m_pData; }
/// Returns the extent of the span.
///
/// @returns An unsigned integer equal to the number of elements currently present in the span.
constexpr size_t NumElements() const { return m_numElements; }
/// Returns true if the number of elements present in the vector is equal to zero.
///
/// @returns True if the span is empty.
constexpr bool IsEmpty() const { return (m_numElements == 0); }
/// Returns a "subspan", a view over a subset range of the elements.
///
/// @warning Behavior is undefined if either
/// - offset is greater than NumElements(), or
/// - count is not size_t(-1) and is greater than NumElements()-offset.
///
/// Note that size_t(-1) is equivalent to C++20 std::dynamic_extent, which the C++20 std::span::subspan uses
/// in the same way to mean "take the remainder of the elements from offset".
///
/// @param offset Zero-based offset to start the subspan at
/// @param count Number of elements in the subspan, or size_t(-1) for the remainder of the elements from offset
///
/// @returns The subspan
constexpr Span Subspan(
size_t offset,
size_t count) const
{
PAL_CONSTEXPR_ASSERT((offset <= NumElements())
&& ((count == size_t(-1)) || (count <= NumElements() - offset)));
if (count == size_t(-1))
{
count = NumElements() - offset;
}
return Span(Data() + offset, count);
}
/// Returns a subspan dropping the specified number (default 1) of elements from the front.
/// Returns an empty Span if there were no more elements than that to start with.
///
/// @param count Number of elements to drop from the front
///
/// @returns The subspan
constexpr Span DropFront(
size_t count = 1) const
{
Span retVal;
if (count < NumElements())
{
retVal = Subspan(count, size_t(-1));
}
return retVal;
}
/// Returns a subspan dropping the specified number (default 1) of elements from the back.
/// Returns an empty Span if there were no more elements than that to start with.
///
/// @param count Number of elements to drop from the back
///
/// @returns The subspan
constexpr Span DropBack(
size_t count = 1) const
{
Span retVal;
if (count < NumElements())
{
retVal = Subspan(0, NumElements() - count);
}
return retVal;
}
///@{
/// @internal Satisfies concept `range_expression`, using T* as `iterator` and 32-bit size and difference types
///
/// @note - These are a convenience intended to be used by c++ language features such as `range for`.
/// These should not be called directly as they do not adhere to PAL coding standards.
using value_type = T;
using reference = T&;
using iterator = T*;
using difference_type = size_t;
using size_type = size_t;
constexpr iterator begin() const noexcept { return m_pData; }
constexpr iterator end() const noexcept { return (m_pData + m_numElements); }
constexpr bool empty() const noexcept { return IsEmpty(); }
constexpr size_type size() const noexcept { return m_numElements; }
///@}
private:
T* m_pData; // Pointer to the current data.
size_t m_numElements; // Number of elements present.
};
} // Util