WPILibC++  2020.3.2-60-g3011ebe
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 WPIUTIL_WPI_ARRAYREF_H
11 #define WPIUTIL_WPI_ARRAYREF_H
12 
13 #include "wpi/Hashing.h"
14 #include "wpi/SmallVector.h"
15 #include "wpi/STLExtras.h"
16 #include "wpi/Compiler.h"
17 #include <algorithm>
18 #include <array>
19 #include <cassert>
20 #include <cstddef>
21 #include <initializer_list>
22 #include <iterator>
23 #include <memory>
24 #include <optional>
25 #include <type_traits>
26 #include <vector>
27 
28 namespace wpi {
29 
41  template<typename T>
42  class LLVM_NODISCARD ArrayRef {
43  public:
44  using iterator = const T *;
45  using const_iterator = const T *;
46  using size_type = size_t;
47  using reverse_iterator = std::reverse_iterator<iterator>;
48  using value_type = T;
49 
50  private:
52  const T *Data = nullptr;
53 
55  size_type Length = 0;
56 
57  public:
60 
62  /*implicit*/ ArrayRef() = default;
63 
65  /*implicit*/ ArrayRef(std::nullopt_t) {}
66 
68  /*implicit*/ ArrayRef(const T &OneElt)
69  : Data(&OneElt), Length(1) {}
70 
72  /*implicit*/ ArrayRef(const T *data, size_t length)
73  : Data(data), Length(length) {}
74 
76  ArrayRef(const T *begin, const T *end)
77  : Data(begin), Length(end - begin) {}
78 
82  template<typename U>
83  /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
84  : Data(Vec.data()), Length(Vec.size()) {
85  }
86 
88  template<typename A>
89  /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
90  : Data(Vec.data()), Length(Vec.size()) {}
91 
93  template <size_t N>
94  /*implicit*/ constexpr ArrayRef(const std::array<T, N> &Arr)
95  : Data(Arr.data()), Length(N) {}
96 
98  template <size_t N>
99  /*implicit*/ constexpr ArrayRef(const T (&Arr)[N]) : Data(Arr), Length(N) {}
100 
103  template <typename U>
105  const ArrayRef<U *> &A,
106  typename std::enable_if<
107  std::is_convertible<U *const *, T const *>::value>::type * = nullptr)
108  : Data(A.data()), Length(A.size()) {}
109 
113  template<typename U, typename DummyT>
114  /*implicit*/ ArrayRef(
116  typename std::enable_if<
117  std::is_convertible<U *const *, T const *>::value>::type * = nullptr)
118  : Data(Vec.data()), Length(Vec.size()) {
119  }
120 
123  template<typename U, typename A>
124  ArrayRef(const std::vector<U *, A> &Vec,
125  typename std::enable_if<
126  std::is_convertible<U *const *, T const *>::value>::type* = 0)
127  : Data(Vec.data()), Length(Vec.size()) {}
128 
132 
133  iterator begin() const { return Data; }
134  iterator end() const { return Data + Length; }
135 
136  reverse_iterator rbegin() const { return reverse_iterator(end()); }
137  reverse_iterator rend() const { return reverse_iterator(begin()); }
138 
140  bool empty() const { return Length == 0; }
141 
142  const T *data() const { return Data; }
143 
145  size_t size() const { return Length; }
146 
148  const T &front() const {
149  assert(!empty());
150  return Data[0];
151  }
152 
154  const T &back() const {
155  assert(!empty());
156  return Data[Length-1];
157  }
158 
159  // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
160  template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
161  T *Buff = A.template Allocate<T>(Length);
162  std::uninitialized_copy(begin(), end(), Buff);
163  return ArrayRef<T>(Buff, Length);
164  }
165 
167  bool equals(ArrayRef RHS) const {
168  if (Length != RHS.Length)
169  return false;
170  return std::equal(begin(), end(), RHS.begin());
171  }
172 
175  ArrayRef<T> slice(size_t N, size_t M) const {
176  assert(N+M <= size() && "Invalid specifier");
177  return ArrayRef<T>(data()+N, M);
178  }
179 
181  ArrayRef<T> slice(size_t N) const { return slice(N, size() - N); }
182 
184  ArrayRef<T> drop_front(size_t N = 1) const {
185  assert(size() >= N && "Dropping more elements than exist");
186  return slice(N, size() - N);
187  }
188 
190  ArrayRef<T> drop_back(size_t N = 1) const {
191  assert(size() >= N && "Dropping more elements than exist");
192  return slice(0, size() - N);
193  }
194 
197  template <class PredicateT> ArrayRef<T> drop_while(PredicateT Pred) const {
198  return ArrayRef<T>(find_if_not(*this, Pred), end());
199  }
200 
203  template <class PredicateT> ArrayRef<T> drop_until(PredicateT Pred) const {
204  return ArrayRef<T>(find_if(*this, Pred), end());
205  }
206 
208  ArrayRef<T> take_front(size_t N = 1) const {
209  if (N >= size())
210  return *this;
211  return drop_back(size() - N);
212  }
213 
215  ArrayRef<T> take_back(size_t N = 1) const {
216  if (N >= size())
217  return *this;
218  return drop_front(size() - N);
219  }
220 
223  template <class PredicateT> ArrayRef<T> take_while(PredicateT Pred) const {
224  return ArrayRef<T>(begin(), find_if_not(*this, Pred));
225  }
226 
229  template <class PredicateT> ArrayRef<T> take_until(PredicateT Pred) const {
230  return ArrayRef<T>(begin(), find_if(*this, Pred));
231  }
232 
236  const T &operator[](size_t Index) const {
237  assert(Index < Length && "Invalid index!");
238  return Data[Index];
239  }
240 
245  template <typename U>
246  typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type &
247  operator=(U &&Temporary) = delete;
248 
253  template <typename U>
254  typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type &
255  operator=(std::initializer_list<U>) = delete;
256 
260  std::vector<T> vec() const {
261  return std::vector<T>(Data, Data+Length);
262  }
263 
267  operator std::vector<T>() const {
268  return std::vector<T>(Data, Data+Length);
269  }
270 
272  };
273 
286  template<typename T>
287  class LLVM_NODISCARD MutableArrayRef : public ArrayRef<T> {
288  public:
289  using iterator = T *;
290  using reverse_iterator = std::reverse_iterator<iterator>;
291 
293  /*implicit*/ MutableArrayRef() = default;
294 
296  /*implicit*/ MutableArrayRef(std::nullopt_t) : ArrayRef<T>() {}
297 
299  /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
300 
302  /*implicit*/ MutableArrayRef(T *data, size_t length)
303  : ArrayRef<T>(data, length) {}
304 
306  MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
307 
310  : ArrayRef<T>(Vec) {}
311 
313  /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
314  : ArrayRef<T>(Vec) {}
315 
317  template <size_t N>
318  /*implicit*/ constexpr MutableArrayRef(std::array<T, N> &Arr)
319  : ArrayRef<T>(Arr) {}
320 
322  template <size_t N>
323  /*implicit*/ constexpr MutableArrayRef(T (&Arr)[N]) : ArrayRef<T>(Arr) {}
324 
325  T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
326 
327  iterator begin() const { return data(); }
328  iterator end() const { return data() + this->size(); }
329 
330  reverse_iterator rbegin() const { return reverse_iterator(end()); }
331  reverse_iterator rend() const { return reverse_iterator(begin()); }
332 
334  T &front() const {
335  assert(!this->empty());
336  return data()[0];
337  }
338 
340  T &back() const {
341  assert(!this->empty());
342  return data()[this->size()-1];
343  }
344 
347  MutableArrayRef<T> slice(size_t N, size_t M) const {
348  assert(N + M <= this->size() && "Invalid specifier");
349  return MutableArrayRef<T>(this->data() + N, M);
350  }
351 
353  MutableArrayRef<T> slice(size_t N) const {
354  return slice(N, this->size() - N);
355  }
356 
358  MutableArrayRef<T> drop_front(size_t N = 1) const {
359  assert(this->size() >= N && "Dropping more elements than exist");
360  return slice(N, this->size() - N);
361  }
362 
363  MutableArrayRef<T> drop_back(size_t N = 1) const {
364  assert(this->size() >= N && "Dropping more elements than exist");
365  return slice(0, this->size() - N);
366  }
367 
370  template <class PredicateT>
371  MutableArrayRef<T> drop_while(PredicateT Pred) const {
372  return MutableArrayRef<T>(find_if_not(*this, Pred), end());
373  }
374 
377  template <class PredicateT>
378  MutableArrayRef<T> drop_until(PredicateT Pred) const {
379  return MutableArrayRef<T>(find_if(*this, Pred), end());
380  }
381 
383  MutableArrayRef<T> take_front(size_t N = 1) const {
384  if (N >= this->size())
385  return *this;
386  return drop_back(this->size() - N);
387  }
388 
390  MutableArrayRef<T> take_back(size_t N = 1) const {
391  if (N >= this->size())
392  return *this;
393  return drop_front(this->size() - N);
394  }
395 
398  template <class PredicateT>
399  MutableArrayRef<T> take_while(PredicateT Pred) const {
400  return MutableArrayRef<T>(begin(), find_if_not(*this, Pred));
401  }
402 
405  template <class PredicateT>
406  MutableArrayRef<T> take_until(PredicateT Pred) const {
407  return MutableArrayRef<T>(begin(), find_if(*this, Pred));
408  }
409 
413  T &operator[](size_t Index) const {
414  assert(Index < this->size() && "Invalid index!");
415  return data()[Index];
416  }
417  };
418 
420  template <typename T> class OwningArrayRef : public MutableArrayRef<T> {
421  public:
422  OwningArrayRef() = default;
423  OwningArrayRef(size_t Size) : MutableArrayRef<T>(new T[Size], Size) {}
424 
426  : MutableArrayRef<T>(new T[Data.size()], Data.size()) {
427  std::copy(Data.begin(), Data.end(), this->begin());
428  }
429 
430  OwningArrayRef(OwningArrayRef &&Other) { *this = Other; }
431 
432  OwningArrayRef &operator=(OwningArrayRef &&Other) {
433  delete[] this->data();
434  this->MutableArrayRef<T>::operator=(Other);
435  Other.MutableArrayRef<T>::operator=(MutableArrayRef<T>());
436  return *this;
437  }
438 
439  ~OwningArrayRef() { delete[] this->data(); }
440  };
441 
444 
446  template<typename T>
447  ArrayRef<T> makeArrayRef(const T &OneElt) {
448  return OneElt;
449  }
450 
452  template<typename T>
453  ArrayRef<T> makeArrayRef(const T *data, size_t length) {
454  return ArrayRef<T>(data, length);
455  }
456 
458  template<typename T>
459  ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
460  return ArrayRef<T>(begin, end);
461  }
462 
464  template <typename T>
466  return Vec;
467  }
468 
470  template <typename T, unsigned N>
472  return Vec;
473  }
474 
476  template<typename T>
477  ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
478  return Vec;
479  }
480 
482  template <typename T> ArrayRef<T> makeArrayRef(const ArrayRef<T> &Vec) {
483  return Vec;
484  }
485 
487  template <typename T> ArrayRef<T> &makeArrayRef(ArrayRef<T> &Vec) {
488  return Vec;
489  }
490 
492  template<typename T, size_t N>
493  ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
494  return ArrayRef<T>(Arr);
495  }
496 
498  template<typename T>
500  return OneElt;
501  }
502 
504  template<typename T>
505  MutableArrayRef<T> makeMutableArrayRef(T *data, size_t length) {
506  return MutableArrayRef<T>(data, length);
507  }
508 
512 
513  template<typename T>
514  inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
515  return LHS.equals(RHS);
516  }
517 
518  template<typename T>
519  inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
520  return !(LHS == RHS);
521  }
522 
524 
525  // ArrayRefs can be treated like a POD type.
526  template <typename T> struct isPodLike;
527  template <typename T> struct isPodLike<ArrayRef<T>> {
528  static const bool value = true;
529  };
530 
531  template <typename T> hash_code hash_value(ArrayRef<T> S) {
532  return hash_combine_range(S.begin(), S.end());
533  }
534 
535 } // end namespace wpi
536 
537 #endif // LLVM_ADT_ARRAYREF_H
wpi::ArrayRef::back
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:154
wpi::isPodLike
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: StringRef.h:942
wpi::ArrayRef::ArrayRef
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:124
wpi::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:140
wpi::operator!=
bool operator!=(const DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT > &LHS, const DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT > &RHS)
Inequality comparison for DenseMap.
Definition: DenseMap.h:659
wpi::ArrayRef::take_while
ArrayRef< T > take_while(PredicateT Pred) const
Return the first N elements of this Array that satisfy the given predicate.
Definition: ArrayRef.h:223
wpi::ArrayRef::ArrayRef
ArrayRef(const std::vector< T, A > &Vec)
Construct an ArrayRef from a std::vector.
Definition: ArrayRef.h:89
wpi::find_if
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1054
wpi::ArrayRef::drop_back
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
Definition: ArrayRef.h:190
wpi::MutableArrayRef::MutableArrayRef
constexpr MutableArrayRef(std::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
Definition: ArrayRef.h:318
wpi::empty
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:196
wpi::ArrayRef::drop_front
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:184
wpi::MutableArrayRef::MutableArrayRef
MutableArrayRef(std::vector< T > &Vec)
Construct a MutableArrayRef from a std::vector.
Definition: ArrayRef.h:313
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
wpi::SmallVectorTemplateCommon
This is the part of SmallVectorTemplateBase which does not depend on whether the type T is a POD.
Definition: SmallVector.h:92
wpi::MutableArrayRef::slice
MutableArrayRef< 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:347
wpi::MutableArrayRef::slice
MutableArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:353
wpi::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: StringExtras.h:32
wpi::ArrayRef::take_until
ArrayRef< T > take_until(PredicateT Pred) const
Return the first N elements of this Array that don't satisfy the given predicate.
Definition: ArrayRef.h:229
wpi::MutableArrayRef::take_front
MutableArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:383
wpi::MutableArrayRef::MutableArrayRef
MutableArrayRef(T &OneElt)
Construct an MutableArrayRef from a single element.
Definition: ArrayRef.h:299
wpi::ArrayRef::ArrayRef
ArrayRef(std::nullopt_t)
Construct an empty ArrayRef from nullopt.
Definition: ArrayRef.h:65
wpi::MutableArrayRef::MutableArrayRef
MutableArrayRef(T *begin, T *end)
Construct an MutableArrayRef from a range.
Definition: ArrayRef.h:306
wpi::ArrayRef::ArrayRef
ArrayRef(const ArrayRef< U * > &A, typename std::enable_if< std::is_convertible< U *const *, T const * >::value >::type *=nullptr)
Construct an ArrayRef<const T*> from ArrayRef<T*>.
Definition: ArrayRef.h:104
wpi::ArrayRef::slice
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:175
wpi::ArrayRef::ArrayRef
constexpr ArrayRef(const T(&Arr)[N])
Construct an ArrayRef from a C array.
Definition: ArrayRef.h:99
wpi::ArrayRef::ArrayRef
ArrayRef(const SmallVectorTemplateCommon< T, U > &Vec)
Construct an ArrayRef from a SmallVector.
Definition: ArrayRef.h:83
wpi::ArrayRef::drop_until
ArrayRef< T > drop_until(PredicateT Pred) const
Return a copy of *this with the first N elements not satisfying the given predicate removed.
Definition: ArrayRef.h:203
wpi::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:447
wpi::MutableArrayRef::MutableArrayRef
MutableArrayRef(T *data, size_t length)
Construct an MutableArrayRef from a pointer and length.
Definition: ArrayRef.h:302
wpi::MutableArrayRef::MutableArrayRef
constexpr MutableArrayRef(T(&Arr)[N])
Construct an MutableArrayRef from a C array.
Definition: ArrayRef.h:323
wpi::ArrayRef::equals
bool equals(ArrayRef RHS) const
equals - Check for element-wise equality.
Definition: ArrayRef.h:167
wpi::ArrayRef::ArrayRef
constexpr ArrayRef(const std::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
Definition: ArrayRef.h:94
wpi::MutableArrayRef::MutableArrayRef
MutableArrayRef(std::nullopt_t)
Construct an empty MutableArrayRef from nullopt.
Definition: ArrayRef.h:296
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: Endian.h:31
wpi::MutableArrayRef::take_while
MutableArrayRef< T > take_while(PredicateT Pred) const
Return the first N elements of this Array that satisfy the given predicate.
Definition: ArrayRef.h:399
wpi::makeMutableArrayRef
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
Definition: ArrayRef.h:499
wpi::ArrayRef::ArrayRef
ArrayRef(const T *data, size_t length)
Construct an ArrayRef from a pointer and length.
Definition: ArrayRef.h:72
wpi::MutableArrayRef::back
T & back() const
back - Get the last element.
Definition: ArrayRef.h:340
wpi::MutableArrayRef::take_until
MutableArrayRef< T > take_until(PredicateT Pred) const
Return the first N elements of this Array that don't satisfy the given predicate.
Definition: ArrayRef.h:406
wpi::MutableArrayRef::drop_front
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:358
wpi::MutableArrayRef::drop_until
MutableArrayRef< T > drop_until(PredicateT Pred) const
Return a copy of *this with the first N elements not satisfying the given predicate removed.
Definition: ArrayRef.h:378
wpi::ArrayRef::front
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:148
wpi::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:145
wpi::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:287
wpi::ArrayRef::drop_while
ArrayRef< T > drop_while(PredicateT Pred) const
Return a copy of *this with the first N elements satisfying the given predicate removed.
Definition: ArrayRef.h:197
wpi::ArrayRef::ArrayRef
ArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:68
wpi::ArrayRef::take_back
ArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
Definition: ArrayRef.h:215
wpi::MutableArrayRef::MutableArrayRef
MutableArrayRef(SmallVectorImpl< T > &Vec)
Construct an MutableArrayRef from a SmallVector.
Definition: ArrayRef.h:309
wpi::ArrayRef::ArrayRef
ArrayRef(const SmallVectorTemplateCommon< U *, DummyT > &Vec, typename std::enable_if< std::is_convertible< U *const *, T const * >::value >::type *=nullptr)
Construct an ArrayRef<const T*> from a SmallVector<T*>.
Definition: ArrayRef.h:114
wpi::hash_combine_range
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:484
wpi::OwningArrayRef
This is a MutableArrayRef that owns its array.
Definition: ArrayRef.h:420
wpi::size
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1007
wpi::hash_value
LLVM_NODISCARD hash_code hash_value(StringRef S)
Compute a hash_code for a StringRef.
wpi::ArrayRef::take_front
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:208
wpi::MutableArrayRef::front
T & front() const
front - Get the first element.
Definition: ArrayRef.h:334
wpi::ArrayRef::ArrayRef
ArrayRef(const T *begin, const T *end)
Construct an ArrayRef from a range.
Definition: ArrayRef.h:76
wpi::hash_code
An opaque object representing a hash code.
Definition: Hashing.h:77
wpi::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:856
wpi::MutableArrayRef::drop_while
MutableArrayRef< T > drop_while(PredicateT Pred) const
Return a copy of *this with the first N elements satisfying the given predicate removed.
Definition: ArrayRef.h:371
wpi::ArrayRef::slice
ArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:181
wpi::ArrayRef::operator=
std::enable_if< std::is_same< U, T >::value, ArrayRef< T > >::type & operator=(U &&Temporary)=delete
Disallow accidental assignment from a temporary.
wpi::MutableArrayRef::take_back
MutableArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
Definition: ArrayRef.h:390