10 #ifndef WPIUTIL_WPI_ARRAYREF_H
11 #define WPIUTIL_WPI_ARRAYREF_H
13 #include "wpi/Hashing.h"
14 #include "wpi/SmallVector.h"
15 #include "wpi/STLExtras.h"
16 #include "wpi/Compiler.h"
21 #include <initializer_list>
25 #include <type_traits>
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>;
52 const T *Data =
nullptr;
69 : Data(&OneElt), Length(1) {}
73 : Data(data), Length(length) {}
77 : Data(begin), Length(end - begin) {}
84 : Data(Vec.data()), Length(Vec.
size()) {
90 : Data(Vec.data()), Length(Vec.
size()) {}
94 constexpr
ArrayRef(
const std::array<T, N> &Arr)
95 : Data(Arr.data()), Length(N) {}
99 constexpr
ArrayRef(
const T (&Arr)[N]) : Data(Arr), Length(N) {}
103 template <
typename U>
106 typename std::enable_if<
107 std::is_convertible<U *const *, T const *>::value>::type * =
nullptr)
108 : Data(A.data()), Length(A.
size()) {}
113 template<
typename U,
typename DummyT>
116 typename std::enable_if<
117 std::is_convertible<U *const *, T const *>::value>::type * =
nullptr)
118 : Data(Vec.data()), Length(Vec.
size()) {
123 template<
typename U,
typename A>
125 typename std::enable_if<
126 std::is_convertible<U *const *, T const *>::value>::type* = 0)
127 : Data(Vec.data()), Length(Vec.
size()) {}
133 iterator begin()
const {
return Data; }
134 iterator end()
const {
return Data + Length; }
136 reverse_iterator rbegin()
const {
return reverse_iterator(end()); }
137 reverse_iterator rend()
const {
return reverse_iterator(begin()); }
140 bool empty()
const {
return Length == 0; }
142 const T *data()
const {
return Data; }
145 size_t size()
const {
return Length; }
156 return Data[Length-1];
160 template <
typename Allocator>
ArrayRef<T> copy(Allocator &A) {
161 T *Buff = A.template Allocate<T>(Length);
162 std::uninitialized_copy(begin(), end(), Buff);
168 if (Length != RHS.Length)
170 return std::equal(begin(), end(), RHS.begin());
176 assert(N+M <=
size() &&
"Invalid specifier");
185 assert(
size() >= N &&
"Dropping more elements than exist");
186 return slice(N,
size() - N);
191 assert(
size() >= N &&
"Dropping more elements than exist");
192 return slice(0,
size() - N);
198 return ArrayRef<T>(find_if_not(*
this, Pred), end());
211 return drop_back(
size() - N);
218 return drop_front(
size() - N);
224 return ArrayRef<T>(begin(), find_if_not(*
this, Pred));
236 const T &operator[](
size_t Index)
const {
237 assert(Index < Length &&
"Invalid index!");
245 template <
typename U>
246 typename std::enable_if<std::is_same<U, T>::value, ArrayRef<T>>::type &
247 operator=(U &&Temporary) =
delete;
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;
260 std::vector<T> vec()
const {
261 return std::vector<T>(Data, Data+Length);
267 operator std::vector<T>()
const {
268 return std::vector<T>(Data, Data+Length);
289 using iterator = T *;
290 using reverse_iterator = std::reverse_iterator<iterator>;
327 iterator begin()
const {
return data(); }
328 iterator end()
const {
return data() + this->
size(); }
330 reverse_iterator rbegin()
const {
return reverse_iterator(end()); }
331 reverse_iterator rend()
const {
return reverse_iterator(begin()); }
335 assert(!this->
empty());
341 assert(!this->
empty());
342 return data()[this->
size()-1];
348 assert(N + M <= this->
size() &&
"Invalid specifier");
354 return slice(N, this->
size() - N);
359 assert(this->
size() >= N &&
"Dropping more elements than exist");
360 return slice(N, this->
size() - N);
364 assert(this->
size() >= N &&
"Dropping more elements than exist");
365 return slice(0, this->
size() - N);
370 template <
class PredicateT>
377 template <
class PredicateT>
384 if (N >= this->
size())
386 return drop_back(this->
size() - N);
391 if (N >= this->
size())
393 return drop_front(this->
size() - N);
398 template <
class PredicateT>
405 template <
class PredicateT>
413 T &operator[](
size_t Index)
const {
414 assert(Index < this->
size() &&
"Invalid index!");
415 return data()[Index];
427 std::copy(Data.begin(), Data.end(), this->begin());
433 delete[] this->data();
464 template <
typename T>
470 template <
typename T,
unsigned N>
492 template<
typename T,
size_t N>
514 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
515 return LHS.equals(RHS);
519 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
520 return !(LHS == RHS);
528 static const bool value =
true;
537 #endif // LLVM_ADT_ARRAYREF_H
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:154
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:526
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
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:140
ArrayRef< T > take_while(PredicateT Pred) const
Return the first N elements of this Array that satisfy the given predicate.
Definition: ArrayRef.h:223
ArrayRef(const std::vector< T, A > &Vec)
Construct an ArrayRef from a std::vector.
Definition: ArrayRef.h:89
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
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
Definition: ArrayRef.h:190
constexpr MutableArrayRef(std::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
Definition: ArrayRef.h:318
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:196
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:184
MutableArrayRef(std::vector< T > &Vec)
Construct a MutableArrayRef from a std::vector.
Definition: ArrayRef.h:313
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
This is the part of SmallVectorTemplateBase which does not depend on whether the type T is a POD.
Definition: SmallVector.h:92
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
MutableArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:353
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: sha1.h:31
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
MutableArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:383
MutableArrayRef(T &OneElt)
Construct an MutableArrayRef from a single element.
Definition: ArrayRef.h:299
ArrayRef(std::nullopt_t)
Construct an empty ArrayRef from nullopt.
Definition: ArrayRef.h:65
MutableArrayRef(T *begin, T *end)
Construct an MutableArrayRef from a range.
Definition: ArrayRef.h:306
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
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
constexpr ArrayRef(const T(&Arr)[N])
Construct an ArrayRef from a C array.
Definition: ArrayRef.h:99
ArrayRef(const SmallVectorTemplateCommon< T, U > &Vec)
Construct an ArrayRef from a SmallVector.
Definition: ArrayRef.h:83
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
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:447
MutableArrayRef(T *data, size_t length)
Construct an MutableArrayRef from a pointer and length.
Definition: ArrayRef.h:302
constexpr MutableArrayRef(T(&Arr)[N])
Construct an MutableArrayRef from a C array.
Definition: ArrayRef.h:323
bool equals(ArrayRef RHS) const
equals - Check for element-wise equality.
Definition: ArrayRef.h:167
constexpr ArrayRef(const std::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
Definition: ArrayRef.h:94
MutableArrayRef(std::nullopt_t)
Construct an empty MutableArrayRef from nullopt.
Definition: ArrayRef.h:296
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
MutableArrayRef< T > take_while(PredicateT Pred) const
Return the first N elements of this Array that satisfy the given predicate.
Definition: ArrayRef.h:399
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
Definition: ArrayRef.h:499
ArrayRef(const T *data, size_t length)
Construct an ArrayRef from a pointer and length.
Definition: ArrayRef.h:72
T & back() const
back - Get the last element.
Definition: ArrayRef.h:340
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
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:358
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
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:148
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:145
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:287
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
ArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:68
ArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
Definition: ArrayRef.h:215
MutableArrayRef(SmallVectorImpl< T > &Vec)
Construct an MutableArrayRef from a SmallVector.
Definition: ArrayRef.h:309
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
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:484
This is a MutableArrayRef that owns its array.
Definition: ArrayRef.h:420
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
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:208
T & front() const
front - Get the first element.
Definition: ArrayRef.h:334
ArrayRef(const T *begin, const T *end)
Construct an ArrayRef from a range.
Definition: ArrayRef.h:76
An opaque object representing a hash code.
Definition: Hashing.h:77
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:856
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
ArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:181
std::enable_if< std::is_same< U, T >::value, ArrayRef< T > >::type & operator=(U &&Temporary)=delete
Disallow accidental assignment from a temporary.
MutableArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
Definition: ArrayRef.h:390