10 #ifndef WPIUTIL_WPI_ARRAYREF_H 11 #define WPIUTIL_WPI_ARRAYREF_H 13 #include "wpi/Hashing.h" 15 #include "wpi/SmallVector.h" 16 #include "wpi/STLExtras.h" 17 #include "wpi/Compiler.h" 22 #include <initializer_list> 25 #include <type_traits> 43 using iterator =
const T *;
44 using const_iterator =
const T *;
45 using size_type = size_t;
46 using reverse_iterator = std::reverse_iterator<iterator>;
51 const T *Data =
nullptr;
68 : Data(&OneElt), Length(1) {}
72 : Data(data), Length(length) {}
76 : Data(begin), Length(end - begin) {}
83 : Data(Vec.data()), Length(Vec.
size()) {
89 : Data(Vec.data()), Length(Vec.
size()) {}
93 constexpr
ArrayRef(
const std::array<T, N> &Arr)
94 : Data(Arr.data()), Length(N) {}
98 constexpr
ArrayRef(
const T (&Arr)[N]) : Data(Arr), Length(N) {}
102 : Data(Vec.begin() == Vec.end() ? (T*)nullptr : Vec.begin()),
103 Length(Vec.
size()) {}
107 template <
typename U>
110 typename std::enable_if<
111 std::is_convertible<U *const *, T const *>::value>::type * =
nullptr)
112 : Data(A.data()), Length(A.
size()) {}
117 template<
typename U,
typename DummyT>
120 typename std::enable_if<
121 std::is_convertible<U *const *, T const *>::value>::type * =
nullptr)
122 : Data(Vec.data()), Length(Vec.
size()) {
127 template<
typename U,
typename A>
129 typename std::enable_if<
130 std::is_convertible<U *const *, T const *>::value>::type* = 0)
131 : Data(Vec.data()), Length(Vec.
size()) {}
137 iterator begin()
const {
return Data; }
138 iterator end()
const {
return Data + Length; }
140 reverse_iterator rbegin()
const {
return reverse_iterator(end()); }
141 reverse_iterator rend()
const {
return reverse_iterator(begin()); }
144 bool empty()
const {
return Length == 0; }
146 const T *data()
const {
return Data; }
149 size_t size()
const {
return Length; }
160 return Data[Length-1];
164 template <
typename Allocator>
ArrayRef<T> copy(Allocator &A) {
165 T *Buff = A.template Allocate<T>(Length);
166 std::uninitialized_copy(begin(), end(), Buff);
172 if (Length != RHS.Length)
174 return std::equal(begin(), end(), RHS.begin());
180 assert(N+M <=
size() &&
"Invalid specifier");
189 assert(
size() >= N &&
"Dropping more elements than exist");
190 return slice(N,
size() - N);
195 assert(
size() >= N &&
"Dropping more elements than exist");
196 return slice(0,
size() - N);
202 return ArrayRef<T>(find_if_not(*
this, Pred), end());
215 return drop_back(
size() - N);
222 return drop_front(
size() - N);
228 return ArrayRef<T>(begin(), find_if_not(*
this, Pred));
240 const T &operator[](
size_t Index)
const {
241 assert(Index < Length &&
"Invalid index!");
249 template <
typename U>
250 typename std::enable_if<std::is_same<U, T>::value,
ArrayRef<T>>::type &
251 operator=(U &&Temporary) =
delete;
257 template <
typename U>
258 typename std::enable_if<std::is_same<U, T>::value,
ArrayRef<T>>::type &
259 operator=(std::initializer_list<U>) =
delete;
264 std::vector<T> vec()
const {
265 return std::vector<T>(Data, Data+Length);
271 operator std::vector<T>()
const {
272 return std::vector<T>(Data, Data+Length);
293 using iterator = T *;
294 using reverse_iterator = std::reverse_iterator<iterator>;
331 iterator begin()
const {
return data(); }
332 iterator end()
const {
return data() + this->
size(); }
334 reverse_iterator rbegin()
const {
return reverse_iterator(end()); }
335 reverse_iterator rend()
const {
return reverse_iterator(begin()); }
339 assert(!this->empty());
345 assert(!this->empty());
346 return data()[this->
size()-1];
352 assert(N + M <= this->
size() &&
"Invalid specifier");
358 return slice(N, this->
size() - N);
363 assert(this->
size() >= N &&
"Dropping more elements than exist");
364 return slice(N, this->
size() - N);
368 assert(this->
size() >= N &&
"Dropping more elements than exist");
369 return slice(0, this->
size() - N);
374 template <
class PredicateT>
381 template <
class PredicateT>
388 if (N >= this->
size())
390 return drop_back(this->
size() - N);
395 if (N >= this->
size())
397 return drop_front(this->
size() - N);
402 template <
class PredicateT>
409 template <
class PredicateT>
417 T &operator[](
size_t Index)
const {
418 assert(Index < this->
size() &&
"Invalid index!");
419 return data()[Index];
431 std::copy(Data.begin(), Data.end(), this->begin());
437 delete[] this->data();
468 template <
typename T>
474 template <
typename T,
unsigned N>
496 template<
typename T,
size_t N>
524 return !(LHS == RHS);
532 static const bool value =
true;
541 #endif // LLVM_ADT_ARRAYREF_H MutableArrayRef(T *data, size_t length)
Construct an MutableArrayRef from a pointer and length.
Definition: ArrayRef.h:306
constexpr MutableArrayRef(T(&Arr)[N])
Construct an MutableArrayRef from a C array.
Definition: ArrayRef.h:327
ArrayRef(const std::vector< T, A > &Vec)
Construct an ArrayRef from a std::vector.
Definition: ArrayRef.h:88
MutableArrayRef(SmallVectorImpl< T > &Vec)
Construct an MutableArrayRef from a SmallVector.
Definition: ArrayRef.h:313
MutableArrayRef(T &OneElt)
Construct an MutableArrayRef from a single element.
Definition: ArrayRef.h:303
MutableArrayRef(T *begin, T *end)
Construct an MutableArrayRef from a range.
Definition: ArrayRef.h:310
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:207
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:362
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:158
constexpr ArrayRef(const std::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
Definition: ArrayRef.h:93
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:868
MutableArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:357
ArrayRef< T > take_while(PredicateT Pred) const
Return the first N elements of this Array that satisfy the given predicate.
Definition: ArrayRef.h:227
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
Definition: ArrayRef.h:194
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:188
MutableArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:387
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:179
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:375
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
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:351
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:903
namespace to hold default to_json function
Definition: json_binary_writer.cpp:39
T & front() const
front - Get the first element.
Definition: ArrayRef.h:338
ArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:67
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:530
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
Definition: ArrayRef.h:503
ArrayRef(const SmallVectorTemplateCommon< T, U > &Vec)
Construct an ArrayRef from a SmallVector.
Definition: ArrayRef.h:82
ArrayRef(const std::initializer_list< T > &Vec)
Construct an ArrayRef from a std::initializer_list.
Definition: ArrayRef.h:101
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
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:410
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
ArrayRef(const T *data, size_t length)
Construct an ArrayRef from a pointer and length.
Definition: ArrayRef.h:71
MutableArrayRef(NoneType)
Construct an empty MutableArrayRef from None.
Definition: ArrayRef.h:300
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:128
MutableArrayRef(std::vector< T > &Vec)
Construct a MutableArrayRef from a std::vector.
Definition: ArrayRef.h:317
ArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:185
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:478
std::enable_if< std::is_same< U, T >::value, ArrayRef< T > >::type & operator=(U &&Temporary)=delete
Disallow accidental assignment from a temporary.
This is a MutableArrayRef that owns its array.
Definition: ArrayRef.h:424
MutableArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
Definition: ArrayRef.h:394
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:201
ArrayRef(NoneType)
Construct an empty ArrayRef from None.
Definition: ArrayRef.h:64
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: WindowsSupport.h:184
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:999
T & back() const
back - Get the last element.
Definition: ArrayRef.h:344
ArrayRef(const T *begin, const T *end)
Construct an ArrayRef from a range.
Definition: ArrayRef.h:75
This is the part of SmallVectorTemplateBase which does not depend on whether the type T is a POD...
Definition: SmallVector.h:76
NoneType
A simple null object to allow implicit construction of Optional<T> and similar types without having t...
Definition: None.h:23
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:108
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:212
constexpr ArrayRef(const T(&Arr)[N])
Construct an ArrayRef from a C array.
Definition: ArrayRef.h:98
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:118
MutableArrayRef< T > take_while(PredicateT Pred) const
Return the first N elements of this Array that satisfy the given predicate.
Definition: ArrayRef.h:403
bool operator==(json::const_reference lhs, json::const_reference rhs) noexcept
Definition: json.cpp:921
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:233
ArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
Definition: ArrayRef.h:219
constexpr MutableArrayRef(std::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
Definition: ArrayRef.h:322
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:382
bool equals(ArrayRef RHS) const
equals - Check for element-wise equality.
Definition: ArrayRef.h:171
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
An opaque object representing a hash code.
Definition: Hashing.h:70
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:152
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291