PyTorch
Loading...
Searching...
No Matches
ArrayRef.h
Go to the documentation of this file.
1//===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// ATen: modified from llvm::ArrayRef.
11// removed llvm-specific functionality
12// removed some implicit const -> non-const conversions that rely on
13// complicated std::enable_if meta-programming
14// removed a bunch of slice variants for simplicity...
15
16#pragma once
17
18#include <c10/util/C++17.h>
19#include <c10/util/Deprecated.h>
20#include <c10/util/Exception.h>
21#include <c10/util/SmallVector.h>
22
23#include <array>
24#include <iterator>
25#include <vector>
26
27namespace c10 {
39template <typename T>
40class ArrayRef final {
41 public:
42 using iterator = const T*;
43 using const_iterator = const T*;
44 using size_type = size_t;
45 using value_type = T;
46
47 using reverse_iterator = std::reverse_iterator<iterator>;
48
49 private:
51 const T* Data;
52
54 size_type Length;
55
56 void debugCheckNullptrInvariant() {
58 Data != nullptr || Length == 0,
59 "created ArrayRef with nullptr and non-zero length! c10::optional relies on this being illegal");
60 }
61
62 public:
65
67 /* implicit */ constexpr ArrayRef() : Data(nullptr), Length(0) {}
68
70 // TODO Make this explicit
71 constexpr ArrayRef(const T& OneElt) : Data(&OneElt), Length(1) {}
72
74 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA ArrayRef(const T* data, size_t length)
75 : Data(data), Length(length) {
76 debugCheckNullptrInvariant();
77 }
78
80 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA ArrayRef(const T* begin, const T* end)
81 : Data(begin), Length(end - begin) {
82 debugCheckNullptrInvariant();
83 }
84
88 template <typename U>
89 /* implicit */ ArrayRef(const SmallVectorTemplateCommon<T, U>& Vec)
90 : Data(Vec.data()), Length(Vec.size()) {
91 debugCheckNullptrInvariant();
92 }
93
94 template <
95 typename Container,
96 typename = std::enable_if_t<std::is_same<
97 std::remove_const_t<decltype(std::declval<Container>().data())>,
98 T*>::value>>
99 /* implicit */ ArrayRef(const Container& container)
100 : Data(container.data()), Length(container.size()) {
101 debugCheckNullptrInvariant();
102 }
103
105 // The enable_if stuff here makes sure that this isn't used for
106 // std::vector<bool>, because ArrayRef can't work on a std::vector<bool>
107 // bitfield.
108 template <typename A>
109 /* implicit */ ArrayRef(const std::vector<T, A>& Vec)
110 : Data(Vec.data()), Length(Vec.size()) {
111 static_assert(
112 !std::is_same<T, bool>::value,
113 "ArrayRef<bool> cannot be constructed from a std::vector<bool> bitfield.");
114 }
115
117 template <size_t N>
118 /* implicit */ constexpr ArrayRef(const std::array<T, N>& Arr)
119 : Data(Arr.data()), Length(N) {}
120
122 template <size_t N>
123 /* implicit */ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {}
124
126 /* implicit */ constexpr ArrayRef(const std::initializer_list<T>& Vec)
127 : Data(
128 std::begin(Vec) == std::end(Vec) ? static_cast<T*>(nullptr)
129 : std::begin(Vec)),
130 Length(Vec.size()) {}
131
135
136 constexpr iterator begin() const {
137 return Data;
138 }
139 constexpr iterator end() const {
140 return Data + Length;
141 }
142
143 // These are actually the same as iterator, since ArrayRef only
144 // gives you const iterators.
145 constexpr const_iterator cbegin() const {
146 return Data;
147 }
148 constexpr const_iterator cend() const {
149 return Data + Length;
150 }
151
152 constexpr reverse_iterator rbegin() const {
153 return reverse_iterator(end());
154 }
155 constexpr reverse_iterator rend() const {
156 return reverse_iterator(begin());
157 }
158
160 constexpr bool empty() const {
161 return Length == 0;
162 }
163
164 constexpr const T* data() const {
165 return Data;
166 }
167
169 constexpr size_t size() const {
170 return Length;
171 }
172
174 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA const T& front() const {
176 !empty(), "ArrayRef: attempted to access front() of empty list");
177 return Data[0];
178 }
179
181 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA const T& back() const {
182 TORCH_CHECK(!empty(), "ArrayRef: attempted to access back() of empty list");
183 return Data[Length - 1];
184 }
185
187 constexpr bool equals(ArrayRef RHS) const {
188 return Length == RHS.Length && std::equal(begin(), end(), RHS.begin());
189 }
190
192 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA ArrayRef<T> slice(size_t N, size_t M)
193 const {
195 N + M <= size(),
196 "ArrayRef: invalid slice, N = ",
197 N,
198 "; M = ",
199 M,
200 "; size = ",
201 size());
202 return ArrayRef<T>(data() + N, M);
203 }
204
206 constexpr ArrayRef<T> slice(size_t N) const {
207 return slice(N, size() - N);
208 }
209
213 constexpr const T& operator[](size_t Index) const {
214 return Data[Index];
215 }
216
218 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA const T& at(size_t Index) const {
220 Index < Length,
221 "ArrayRef: invalid index Index = ",
222 Index,
223 "; Length = ",
224 Length);
225 return Data[Index];
226 }
227
232 template <typename U>
233 typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type&
234 operator=(U&& Temporary) = delete;
235
240 template <typename U>
241 typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type&
242 operator=(std::initializer_list<U>) = delete;
243
247 std::vector<T> vec() const {
248 return std::vector<T>(Data, Data + Length);
249 }
250
252};
253
254template <typename T>
255std::ostream& operator<<(std::ostream& out, ArrayRef<T> list) {
256 int i = 0;
257 out << "[";
258 for (const auto& e : list) {
259 if (i++ > 0)
260 out << ", ";
261 out << e;
262 }
263 out << "]";
264 return out;
265}
266
269
271template <typename T>
272ArrayRef<T> makeArrayRef(const T& OneElt) {
273 return OneElt;
274}
275
277template <typename T>
278ArrayRef<T> makeArrayRef(const T* data, size_t length) {
279 return ArrayRef<T>(data, length);
280}
281
283template <typename T>
284ArrayRef<T> makeArrayRef(const T* begin, const T* end) {
285 return ArrayRef<T>(begin, end);
286}
287
289template <typename T>
290ArrayRef<T> makeArrayRef(const SmallVectorImpl<T>& Vec) {
291 return Vec;
292}
293
295template <typename T, unsigned N>
296ArrayRef<T> makeArrayRef(const SmallVector<T, N>& Vec) {
297 return Vec;
298}
299
301template <typename T>
302ArrayRef<T> makeArrayRef(const std::vector<T>& Vec) {
303 return Vec;
304}
305
307template <typename T, std::size_t N>
308ArrayRef<T> makeArrayRef(const std::array<T, N>& Arr) {
309 return Arr;
310}
311
313template <typename T>
315 return Vec;
316}
317
319template <typename T>
321 return Vec;
322}
323
325template <typename T, size_t N>
326ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
327 return ArrayRef<T>(Arr);
328}
329
330// WARNING: Template instantiation will NOT be willing to do an implicit
331// conversions to get you to an c10::ArrayRef, which is why we need so
332// many overloads.
333
334template <typename T>
336 return a1.equals(a2);
337}
338
339template <typename T>
341 return !a1.equals(a2);
342}
343
344template <typename T>
345bool operator==(const std::vector<T>& a1, c10::ArrayRef<T> a2) {
346 return c10::ArrayRef<T>(a1).equals(a2);
347}
348
349template <typename T>
350bool operator!=(const std::vector<T>& a1, c10::ArrayRef<T> a2) {
351 return !c10::ArrayRef<T>(a1).equals(a2);
352}
353
354template <typename T>
355bool operator==(c10::ArrayRef<T> a1, const std::vector<T>& a2) {
356 return a1.equals(c10::ArrayRef<T>(a2));
357}
358
359template <typename T>
360bool operator!=(c10::ArrayRef<T> a1, const std::vector<T>& a2) {
361 return !a1.equals(c10::ArrayRef<T>(a2));
362}
363
365
366// This alias is deprecated because it doesn't make ownership
367// semantics obvious. Use IntArrayRef instead!
368C10_DEFINE_DEPRECATED_USING(IntList, ArrayRef<int64_t>)
369
370} // namespace c10
#define TORCH_INTERNAL_ASSERT_DEBUG_ONLY(...)
Definition: Exception.h:534
#define TORCH_CHECK(cond,...)
Definition: Exception.h:505
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:40
ArrayRef(const SmallVectorTemplateCommon< T, U > &Vec)
Construct an ArrayRef from a SmallVector.
Definition: ArrayRef.h:89
constexpr size_t size() const
size - Get the array size.
Definition: ArrayRef.h:169
constexpr const T * data() const
Definition: ArrayRef.h:164
constexpr bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
constexpr const_iterator cend() const
Definition: ArrayRef.h:148
std::enable_if< std::is_same< U, T >::value, ArrayRef< T > >::type & operator=(std::initializer_list< U >)=delete
Disallow accidental assignment from a temporary.
constexpr ArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:71
constexpr reverse_iterator rend() const
Definition: ArrayRef.h:155
const T * const_iterator
Definition: ArrayRef.h:43
ArrayRef(const std::vector< T, A > &Vec)
Construct an ArrayRef from a std::vector.
Definition: ArrayRef.h:109
constexpr ArrayRef(const T *data, size_t length)
Construct an ArrayRef from a pointer and length.
Definition: ArrayRef.h:74
constexpr ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Take M elements of the array starting at element N
Definition: ArrayRef.h:192
constexpr ArrayRef(const T(&Arr)[N])
Construct an ArrayRef from a C array.
Definition: ArrayRef.h:123
constexpr const T & at(size_t Index) const
Vector compatibility.
Definition: ArrayRef.h:218
std::reverse_iterator< iterator > reverse_iterator
Definition: ArrayRef.h:47
constexpr const T & back() const
back - Get the last element.
Definition: ArrayRef.h:181
constexpr bool equals(ArrayRef RHS) const
equals - Check for element-wise equality.
Definition: ArrayRef.h:187
constexpr ArrayRef()
Construct an empty ArrayRef.
Definition: ArrayRef.h:67
constexpr ArrayRef(const T *begin, const T *end)
Construct an ArrayRef from a range.
Definition: ArrayRef.h:80
size_t size_type
Definition: ArrayRef.h:44
const T * iterator
Definition: ArrayRef.h:42
constexpr iterator end() const
Definition: ArrayRef.h:139
T value_type
Definition: ArrayRef.h:45
std::enable_if< std::is_same< U, T >::value, ArrayRef< T > >::type & operator=(U &&Temporary)=delete
Disallow accidental assignment from a temporary.
constexpr ArrayRef(const std::initializer_list< T > &Vec)
Construct an ArrayRef from a std::initializer_list.
Definition: ArrayRef.h:126
constexpr const T & operator[](size_t Index) const
Definition: ArrayRef.h:213
ArrayRef(const Container &container)
Definition: ArrayRef.h:99
constexpr const_iterator cbegin() const
Definition: ArrayRef.h:145
constexpr ArrayRef(const std::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
Definition: ArrayRef.h:118
constexpr iterator begin() const
Definition: ArrayRef.h:136
constexpr ArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:206
std::vector< T > vec() const
Definition: ArrayRef.h:247
constexpr const T & front() const
front - Get the first element.
Definition: ArrayRef.h:174
constexpr reverse_iterator rbegin() const
Definition: ArrayRef.h:152
Definition: ivalue.h:27
bool operator!=(c10::ArrayRef< T > a1, c10::ArrayRef< T > a2)
Definition: ArrayRef.h:340
std::ostream & operator<<(std::ostream &stream, const Device &device)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:272
IntList
Definition: ArrayRef.h:368
bool operator==(c10::ArrayRef< T > a1, c10::ArrayRef< T > a2)
Definition: ArrayRef.h:335
Definition: Device.h:181