18#include <c10/util/C++17.h>
19#include <c10/util/Deprecated.h>
21#include <c10/util/SmallVector.h>
56 void debugCheckNullptrInvariant() {
58 Data !=
nullptr || Length == 0,
59 "created ArrayRef with nullptr and non-zero length! c10::optional relies on this being illegal");
67 constexpr ArrayRef() : Data(nullptr), Length(0) {}
71 constexpr ArrayRef(
const T& OneElt) : Data(&OneElt), Length(1) {}
74 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA
ArrayRef(
const T*
data,
size_t length)
75 : Data(
data), Length(length) {
76 debugCheckNullptrInvariant();
82 debugCheckNullptrInvariant();
89 ArrayRef(
const SmallVectorTemplateCommon<T, U>& Vec)
90 : Data(Vec.
data()), Length(Vec.
size()) {
91 debugCheckNullptrInvariant();
96 typename = std::enable_if_t<std::is_same<
97 std::remove_const_t<decltype(std::declval<Container>().data())>,
100 : Data(container.data()), Length(container.size()) {
101 debugCheckNullptrInvariant();
108 template <
typename A>
110 : Data(Vec.
data()), Length(Vec.
size()) {
112 !std::is_same<T, bool>::value,
113 "ArrayRef<bool> cannot be constructed from a std::vector<bool> bitfield.");
119 : Data(Arr.
data()), Length(N) {}
123 constexpr ArrayRef(
const T (&Arr)[N]) : Data(Arr), Length(N) {}
126 constexpr ArrayRef(
const std::initializer_list<T>& Vec)
130 Length(Vec.
size()) {}
140 return Data + Length;
149 return Data + Length;
164 constexpr const T*
data()
const {
169 constexpr size_t size()
const {
174 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA
const T&
front()
const {
176 !
empty(),
"ArrayRef: attempted to access front() of empty list");
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];
188 return Length == RHS.Length && std::equal(
begin(),
end(), RHS.
begin());
196 "ArrayRef: invalid slice, N = ",
218 C10_HOST_CONSTEXPR_EXCEPT_WIN_CUDA
const T&
at(
size_t Index)
const {
221 "ArrayRef: invalid index Index = ",
232 template <
typename U>
233 typename std::enable_if<std::is_same<U, T>::value,
ArrayRef<T>>::type&
240 template <
typename U>
241 typename std::enable_if<std::is_same<U, T>::value,
ArrayRef<T>>::type&
247 std::vector<T>
vec()
const {
248 return std::vector<T>(Data, Data + Length);
258 for (
const auto& e : list) {
295template <
typename T,
unsigned N>
307template <
typename T, std::
size_t N>
325template <
typename T,
size_t N>
#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
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