WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
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 #ifndef LLVM_ADT_ARRAYREF_H
11 #define LLVM_ADT_ARRAYREF_H
12 
13 #include "llvm/None.h"
14 #include "llvm/SmallVector.h"
15 #include <vector>
16 
17 #ifndef LLVM_CONSTEXPR
18 # ifdef _MSC_VER
19 # if _MSC_VER >= 1900
20 # define LLVM_CONSTEXPR constexpr
21 # else
22 # define LLVM_CONSTEXPR
23 # endif
24 # elif defined(__has_feature)
25 # if __has_feature(cxx_constexpr)
26 # define LLVM_CONSTEXPR constexpr
27 # else
28 # define LLVM_CONSTEXPR
29 # endif
30 # elif defined(__GXX_EXPERIMENTAL_CXX0X__)
31 # define LLVM_CONSTEXPR constexpr
32 # elif defined(__has_constexpr)
33 # define LLVM_CONSTEXPR constexpr
34 # else
35 # define LLVM_CONSTEXPR
36 # endif
37 # define DEFINED_LLVM_CONSTEXPR
38 #endif
39 
40 namespace llvm {
41 
53  template<typename T>
54  class ArrayRef {
55  public:
56  typedef const T *iterator;
57  typedef const T *const_iterator;
58  typedef size_t size_type;
59 
60  typedef std::reverse_iterator<iterator> reverse_iterator;
61 
62  private:
64  const T *Data;
65 
67  size_type Length;
68 
69  public:
72 
74  /*implicit*/ ArrayRef() : Data(nullptr), Length(0) {}
75 
77  /*implicit*/ ArrayRef(NoneType) : Data(nullptr), Length(0) {}
78 
80  /*implicit*/ ArrayRef(const T &OneElt)
81  : Data(&OneElt), Length(1) {}
82 
84  /*implicit*/ ArrayRef(const T *data, size_t length)
85  : Data(data), Length(length) {}
86 
88  ArrayRef(const T *begin, const T *end)
89  : Data(begin), Length(end - begin) {}
90 
94  template<typename U>
95  /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
96  : Data(Vec.data()), Length(Vec.size()) {
97  }
98 
100  template<typename A>
101  /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
102  : Data(Vec.data()), Length(Vec.size()) {}
103 
105  template <size_t N>
106  /*implicit*/ LLVM_CONSTEXPR ArrayRef(const T (&Arr)[N])
107  : Data(Arr), Length(N) {}
108 
110  /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
111  : Data(Vec.begin() == Vec.end() ? (T*)0 : Vec.begin()),
112  Length(Vec.size()) {}
113 
116  template <typename U>
118  typename std::enable_if<
119  std::is_convertible<U *const *, T const *>::value>::type* = 0)
120  : Data(A.data()), Length(A.size()) {}
121 
125  template<typename U, typename DummyT>
127  typename std::enable_if<
128  std::is_convertible<U *const *,
129  T const *>::value>::type* = 0)
130  : Data(Vec.data()), Length(Vec.size()) {
131  }
132 
135  template<typename U, typename A>
136  ArrayRef(const std::vector<U *, A> &Vec,
137  typename std::enable_if<
138  std::is_convertible<U *const *, T const *>::value>::type* = 0)
139  : Data(Vec.data()), Length(Vec.size()) {}
140 
144 
145  iterator begin() const { return Data; }
146  iterator end() const { return Data + Length; }
147 
148  reverse_iterator rbegin() const { return reverse_iterator(end()); }
149  reverse_iterator rend() const { return reverse_iterator(begin()); }
150 
152  bool empty() const { return Length == 0; }
153 
154  const T *data() const { return Data; }
155 
157  size_t size() const { return Length; }
158 
160  const T &front() const {
161  assert(!empty());
162  return Data[0];
163  }
164 
166  const T &back() const {
167  assert(!empty());
168  return Data[Length-1];
169  }
170 
171  // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
172  template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
173  T *Buff = A.template Allocate<T>(Length);
174  std::copy(begin(), end(), Buff);
175  return ArrayRef<T>(Buff, Length);
176  }
177 
179  bool equals(ArrayRef RHS) const {
180  if (Length != RHS.Length)
181  return false;
182  if (Length == 0)
183  return true;
184  return std::equal(begin(), end(), RHS.begin());
185  }
186 
188  ArrayRef<T> slice(unsigned N) const {
189  assert(N <= size() && "Invalid specifier");
190  return ArrayRef<T>(data()+N, size()-N);
191  }
192 
195  ArrayRef<T> slice(unsigned N, unsigned M) const {
196  assert(N+M <= size() && "Invalid specifier");
197  return ArrayRef<T>(data()+N, M);
198  }
199 
200  // \brief Drop the last \p N elements of the array.
201  ArrayRef<T> drop_back(unsigned N = 1) const {
202  assert(size() >= N && "Dropping more elements than exist");
203  return slice(0, size() - N);
204  }
205 
209  const T &operator[](size_t Index) const {
210  assert(Index < Length && "Invalid index!");
211  return Data[Index];
212  }
213 
217  std::vector<T> vec() const {
218  return std::vector<T>(Data, Data+Length);
219  }
220 
224  operator std::vector<T>() const {
225  return std::vector<T>(Data, Data+Length);
226  }
227 
229  };
230 
243  template<typename T>
244  class MutableArrayRef : public ArrayRef<T> {
245  public:
246  typedef T *iterator;
247 
248  typedef std::reverse_iterator<iterator> reverse_iterator;
249 
251  /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
252 
254  /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
255 
257  /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
258 
260  /*implicit*/ MutableArrayRef(T *data, size_t length)
261  : ArrayRef<T>(data, length) {}
262 
264  MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
265 
268  : ArrayRef<T>(Vec) {}
269 
271  /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
272  : ArrayRef<T>(Vec) {}
273 
275  template <size_t N>
276  /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
277  : ArrayRef<T>(Arr) {}
278 
279  T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
280 
281  iterator begin() const { return data(); }
282  iterator end() const { return data() + this->size(); }
283 
284  reverse_iterator rbegin() const { return reverse_iterator(end()); }
285  reverse_iterator rend() const { return reverse_iterator(begin()); }
286 
288  T &front() const {
289  assert(!this->empty());
290  return data()[0];
291  }
292 
294  T &back() const {
295  assert(!this->empty());
296  return data()[this->size()-1];
297  }
298 
300  MutableArrayRef<T> slice(unsigned N) const {
301  assert(N <= this->size() && "Invalid specifier");
302  return MutableArrayRef<T>(data()+N, this->size()-N);
303  }
304 
307  MutableArrayRef<T> slice(unsigned N, unsigned M) const {
308  assert(N+M <= this->size() && "Invalid specifier");
309  return MutableArrayRef<T>(data()+N, M);
310  }
311 
312  MutableArrayRef<T> drop_back(unsigned N) const {
313  assert(this->size() >= N && "Dropping more elements than exist");
314  return slice(0, this->size() - N);
315  }
316 
320  T &operator[](size_t Index) const {
321  assert(Index < this->size() && "Invalid index!");
322  return data()[Index];
323  }
324  };
325 
328 
330  template<typename T>
331  ArrayRef<T> makeArrayRef(const T &OneElt) {
332  return OneElt;
333  }
334 
336  template<typename T>
337  ArrayRef<T> makeArrayRef(const T *data, size_t length) {
338  return ArrayRef<T>(data, length);
339  }
340 
342  template<typename T>
343  ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
344  return ArrayRef<T>(begin, end);
345  }
346 
348  template <typename T>
349  ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
350  return Vec;
351  }
352 
354  template <typename T, unsigned N>
355  ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
356  return Vec;
357  }
358 
360  template<typename T>
361  ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
362  return Vec;
363  }
364 
366  template<typename T, size_t N>
367  ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
368  return ArrayRef<T>(Arr);
369  }
370 
374 
375  template<typename T>
376  inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
377  return LHS.equals(RHS);
378  }
379 
380  template<typename T>
381  inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
382  return !(LHS == RHS);
383  }
384 
386 
387  // ArrayRefs can be treated like a POD type.
388  template <typename T> struct isPodLike;
389  template <typename T> struct isPodLike<ArrayRef<T> > {
390  static const bool value = true;
391  };
392 } // namespace llvm
393 
394 #ifdef DEFINED_LLVM_CONSTEXPR
395 # undef DEFINED_LLVM_CONSTEXPR
396 # undef LLVM_CONSTEXPR
397 #endif
398 
399 #endif
ArrayRef(const ArrayRef< U * > &A, typename std::enable_if< std::is_convertible< U *const *, T const * >::value >::type *=0)
Construct an ArrayRef<const T*> from ArrayRef<T*>.
Definition: ArrayRef.h:117
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:160
ArrayRef(NoneType)
Construct an empty ArrayRef from None.
Definition: ArrayRef.h:77
MutableArrayRef(std::vector< T > &Vec)
Construct a MutableArrayRef from a std::vector.
Definition: ArrayRef.h:271
ArrayRef< T > slice(unsigned N, unsigned M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:195
LLVM_CONSTEXPR MutableArrayRef(T(&Arr)[N])
Construct an MutableArrayRef from a C array.
Definition: ArrayRef.h:276
LLVM_CONSTEXPR ArrayRef(const T(&Arr)[N])
Construct an ArrayRef from a C array.
Definition: ArrayRef.h:106
ArrayRef(const std::vector< T, A > &Vec)
Construct an ArrayRef from a std::vector.
Definition: ArrayRef.h:101
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: StringRef.h:23
ArrayRef(const std::initializer_list< T > &Vec)
Construct an ArrayRef from a std::initializer_list.
Definition: ArrayRef.h:110
ArrayRef< T > slice(unsigned N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:188
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:54
MutableArrayRef< T > slice(unsigned N, unsigned M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:307
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:157
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:244
ArrayRef(const SmallVectorTemplateCommon< T, U > &Vec)
Construct an ArrayRef from a SmallVector.
Definition: ArrayRef.h:95
MutableArrayRef(T *begin, T *end)
Construct an MutableArrayRef from a range.
Definition: ArrayRef.h:264
ArrayRef()
Construct an empty ArrayRef.
Definition: ArrayRef.h:74
bool equals(ArrayRef RHS) const
equals - Check for element-wise equality.
Definition: ArrayRef.h:179
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:152
MutableArrayRef()
Construct an empty MutableArrayRef.
Definition: ArrayRef.h:251
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:166
ArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:80
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: StringRef.h:535
MutableArrayRef(T *data, size_t length)
Construct an MutableArrayRef from a pointer and length.
Definition: ArrayRef.h:260
ArrayRef(const SmallVectorTemplateCommon< U *, DummyT > &Vec, typename std::enable_if< std::is_convertible< U *const *, T const * >::value >::type *=0)
Construct an ArrayRef<const T*> from a SmallVector<T*>.
Definition: ArrayRef.h:126
MutableArrayRef(T &OneElt)
Construct an MutableArrayRef from a single element.
Definition: ArrayRef.h:257
ArrayRef(const T *data, size_t length)
Construct an ArrayRef from a pointer and length.
Definition: ArrayRef.h:84
MutableArrayRef< T > slice(unsigned N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:300
This is the part of SmallVectorTemplateBase which does not depend on whether the type T is a POD...
Definition: SmallVector.h:66
T & front() const
front - Get the first element.
Definition: ArrayRef.h:288
ArrayRef(const T *begin, const T *end)
Construct an ArrayRef from a range.
Definition: ArrayRef.h:88
MutableArrayRef(NoneType)
Construct an empty MutableArrayRef from None.
Definition: ArrayRef.h:254
MutableArrayRef(SmallVectorImpl< T > &Vec)
Construct an MutableArrayRef from a SmallVector.
Definition: ArrayRef.h:267
T & back() const
back - Get the last element.
Definition: ArrayRef.h:294
ArrayRef(const std::vector< U *, A > &Vec, typename std::enable_if< std::is_convertible< U *const *, T const * >::value >::type *=0)
Construct an ArrayRef<const T*> from std::vector<T*>.
Definition: ArrayRef.h:136