Caffe2 - C++ API
A deep learning, cross platform ML framework
ArrayRef.h
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/SmallVector.h>
19 #include <c10/util/C++17.h>
20 #include <c10/util/Exception.h>
21 #include <c10/util/Deprecated.h>
22 
23 #include <array>
24 #include <iterator>
25 #include <vector>
26 
27 namespace c10 {
28 
40 template <typename T>
41 class ArrayRef final {
42  public:
43  using iterator = const T*;
44  using const_iterator = const T*;
45  using size_type = size_t;
46 
47  using reverse_iterator = std::reverse_iterator<iterator>;
48 
49  private:
51  const T* Data;
52 
54  size_type Length;
55 
56  public:
59 
61  /* implicit */ constexpr ArrayRef() : Data(nullptr), Length(0) {}
62 
64  // TODO Make this explicit
65  constexpr ArrayRef(const T& OneElt) : Data(&OneElt), Length(1) {}
66 
68  constexpr ArrayRef(const T* data, size_t length)
69  : Data(data), Length(length) {}
70 
72  constexpr ArrayRef(const T* begin, const T* end)
73  : Data(begin), Length(end - begin) {}
74 
78  template <typename U>
79  /* implicit */ ArrayRef(const SmallVectorTemplateCommon<T, U>& Vec)
80  : Data(Vec.data()), Length(Vec.size()) {}
81 
83  template <typename A>
84  /* implicit */ ArrayRef(const std::vector<T, A>& Vec)
85  : Data(Vec.data()), Length(Vec.size()) {}
86 
88  template <size_t N>
89  /* implicit */ constexpr ArrayRef(const std::array<T, N>& Arr)
90  : Data(Arr.data()), Length(N) {}
91 
93  template <size_t N>
94  /* implicit */ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {}
95 
97  /* implicit */ constexpr ArrayRef(const std::initializer_list<T>& Vec)
98  : Data(Vec.begin() == Vec.end() ? static_cast<T*>(nullptr) : Vec.begin()),
99  Length(Vec.size()) {}
100 
104 
105  constexpr iterator begin() const {
106  return Data;
107  }
108  constexpr iterator end() const {
109  return Data + Length;
110  }
111 
112  // These are actually the same as iterator, since ArrayRef only
113  // gives you const iterators.
114  constexpr const_iterator cbegin() const {
115  return Data;
116  }
117  constexpr const_iterator cend() const {
118  return Data + Length;
119  }
120 
121  constexpr reverse_iterator rbegin() const {
122  return reverse_iterator(end());
123  }
124  constexpr reverse_iterator rend() const {
125  return reverse_iterator(begin());
126  }
127 
129  constexpr bool empty() const {
130  return Length == 0;
131  }
132 
133  constexpr const T* data() const {
134  return Data;
135  }
136 
138  constexpr size_t size() const {
139  return Length;
140  }
141 
143  AT_CPP14_CONSTEXPR const T& front() const {
144  AT_CHECK(!empty(), "ArrayRef: attempted to access front() of empty list");
145  return Data[0];
146  }
147 
149  AT_CPP14_CONSTEXPR const T& back() const {
150  AT_CHECK(!empty(), "ArrayRef: attempted to access back() of empty list");
151  return Data[Length - 1];
152  }
153 
155  constexpr bool equals(ArrayRef RHS) const {
156  return Length == RHS.Length && std::equal(begin(), end(), RHS.begin());
157  }
158 
161  AT_CPP14_CONSTEXPR ArrayRef<T> slice(size_t N, size_t M) const {
162  AT_CHECK(
163  N + M <= size(),
164  "ArrayRef: invalid slice, N = ",
165  N,
166  "; M = ",
167  M,
168  "; size = ",
169  size());
170  return ArrayRef<T>(data() + N, M);
171  }
172 
174  constexpr ArrayRef<T> slice(size_t N) const {
175  return slice(N, size() - N);
176  }
177 
181  constexpr const T& operator[](size_t Index) const {
182  return Data[Index];
183  }
184 
186  AT_CPP14_CONSTEXPR const T& at(size_t Index) const {
187  AT_CHECK(
188  Index < Length,
189  "ArrayRef: invalid index Index = ",
190  Index,
191  "; Length = ",
192  Length);
193  return Data[Index];
194  }
195 
200  template <typename U>
201  typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type&
202  operator=(U&& Temporary) = delete;
203 
208  template <typename U>
209  typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type&
210  operator=(std::initializer_list<U>) = delete;
211 
215  std::vector<T> vec() const {
216  return std::vector<T>(Data, Data + Length);
217  }
218 
220 };
221 
222 template <typename T>
223 std::ostream& operator<<(std::ostream & out, ArrayRef<T> list) {
224  int i = 0;
225  out << "[";
226  for(auto e : list) {
227  if (i++ > 0)
228  out << ", ";
229  out << e;
230  }
231  out << "]";
232  return out;
233 }
234 
235 // WARNING: Template instantiation will NOT be willing to do an implicit
236 // conversions to get you to an c10::ArrayRef, which is why we need so
237 // many overloads.
238 
239 template <typename T>
240 bool operator==(c10::ArrayRef<T> a1, c10::ArrayRef<T> a2) {
241  return a1.equals(a2);
242 }
243 
244 template <typename T>
245 bool operator!=(c10::ArrayRef<T> a1, c10::ArrayRef<T> a2) {
246  return !a1.equals(a2);
247 }
248 
249 template <typename T>
250 bool operator==(std::vector<T> a1, c10::ArrayRef<T> a2) {
251  return c10::ArrayRef<T>(a1).equals(a2);
252 }
253 
254 template <typename T>
255 bool operator!=(std::vector<T> a1, c10::ArrayRef<T> a2) {
256  return !c10::ArrayRef<T>(a1).equals(a2);
257 }
258 
259 template <typename T>
260 bool operator==(c10::ArrayRef<T> a1, std::vector<T> a2) {
261  return a1.equals(c10::ArrayRef<T>(a2));
262 }
263 
264 template <typename T>
265 bool operator!=(c10::ArrayRef<T> a1, std::vector<T> a2) {
266  return !a1.equals(c10::ArrayRef<T>(a2));
267 }
268 
270 
271 // This alias is deprecated because it doesn't make ownership
272 // semantics obvious. Use IntArrayRef instead!
274 
275 } // namespace c10
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::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
Definition: ArrayRef.h:89
Definition: any.cpp:108
AT_CPP14_CONSTEXPR const T & front() const
front - Get the first element.
Definition: ArrayRef.h:143
constexpr ArrayRef(const std::initializer_list< T > &Vec)
Construct an ArrayRef from a std::initializer_list.
Definition: ArrayRef.h:97
constexpr ArrayRef(const T(&Arr)[N])
Construct an ArrayRef from a C array.
Definition: ArrayRef.h:94
ArrayRef(const std::vector< T, A > &Vec)
Construct an ArrayRef from a std::vector.
Definition: ArrayRef.h:84
This is the part of SmallVectorTemplateBase which does not depend on whether the type T is a POD...
Definition: SmallVector.h:88
AT_CPP14_CONSTEXPR const T & back() const
back - Get the last element.
Definition: ArrayRef.h:149
constexpr ArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:65
AT_CPP14_CONSTEXPR ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:161
constexpr bool equals(ArrayRef RHS) const
equals - Check for element-wise equality.
Definition: ArrayRef.h:155
constexpr size_t size() const
size - Get the array size.
Definition: ArrayRef.h:138
constexpr ArrayRef()
Construct an empty ArrayRef.
Definition: ArrayRef.h:61
ArrayRef(const SmallVectorTemplateCommon< T, U > &Vec)
Construct an ArrayRef from a SmallVector.
Definition: ArrayRef.h:79
constexpr ArrayRef(const T *begin, const T *end)
Construct an ArrayRef from a range.
Definition: ArrayRef.h:72
constexpr bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:129
To register your own kernel for an operator, do in one (!) cpp file: C10_REGISTER_KERNEL(OperatorHand...
Definition: alias_info.h:7
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
AT_CPP14_CONSTEXPR const T & at(size_t Index) const
Vector compatibility.
Definition: ArrayRef.h:186
constexpr ArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:174
constexpr ArrayRef(const T *data, size_t length)
Construct an ArrayRef from a pointer and length.
Definition: ArrayRef.h:68