1//===- ArrayRef.h - Array Reference Wrapper ---------------------*- C++ -*-===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 9#ifndef LLVM_ADT_ARRAYREF_H 10#define LLVM_ADT_ARRAYREF_H 20#include <initializer_list> 29 /// ArrayRef - Represent a constant reference to an array (0 or more elements 30 /// consecutively in memory), i.e. a start pointer and a length. It allows 31 /// various APIs to take consecutive elements easily and conveniently. 33 /// This class does not own the underlying data, it is expected to be used in 34 /// situations where the data resides in some other buffer, whose lifetime 35 /// extends past that of the ArrayRef. For this reason, it is not in general 36 /// safe to store an ArrayRef. 38 /// This is intended to be trivially copyable, so it should be passed by 56 /// The start of the array, in an external buffer. 59 /// The number of elements. 63 /// @name Constructors 66 /// Construct an empty ArrayRef. 69 /// Construct an empty ArrayRef from std::nullopt. 72 /// Construct an ArrayRef from a single element. 74 : Data(&OneElt),
Length(1) {}
76 /// Construct an ArrayRef from a pointer and length. 81 /// Construct an ArrayRef from a range. 83 : Data(begin),
Length(end - begin) {
87 /// Construct an ArrayRef from a SmallVector. This is templated in order to 88 /// avoid instantiating SmallVectorTemplateCommon<T> whenever we 89 /// copy-construct an ArrayRef. 95 /// Construct an ArrayRef from a std::vector. 97/*implicit*/ArrayRef(
const std::vector<T, A> &Vec)
100 /// Construct an ArrayRef from a std::array 102/*implicit*/constexprArrayRef(
const std::array<T, N> &Arr)
105 /// Construct an ArrayRef from a C array. 110 /// Construct an ArrayRef from a std::initializer_list. 111#if LLVM_GNUC_PREREQ(9, 0, 0) 112// Disable gcc's warning in this constructor as it generates an enormous amount 113// of messages. Anyone using ArrayRef should already be aware of the fact that 114// it does not do lifetime extension. 115#pragma GCC diagnostic push 116#pragma GCC diagnostic ignored "-Winit-list-lifetime" 120 : Data(Vec.begin() == Vec.end() ? (
T *)nullptr : Vec.begin()),
122#if LLVM_GNUC_PREREQ(9, 0, 0) 123#pragma GCC diagnostic pop 126 /// Construct an ArrayRef<const T*> from ArrayRef<T*>. This uses SFINAE to 127 /// ensure that only ArrayRefs of pointers can be converted. 130 std::enable_if_t<std::is_convertible<U *const *, T const *>::value>
134 /// Construct an ArrayRef<const T*> from a SmallVector<T*>. This is 135 /// templated in order to avoid instantiating SmallVectorTemplateCommon<T> 136 /// whenever we copy-construct an ArrayRef. 137template <
typename U,
typename DummyT>
140 std::enable_if_t<std::is_convertible<U *const *, T const *>::value> * =
144 /// Construct an ArrayRef<const T*> from std::vector<T*>. This uses SFINAE 145 /// to ensure that only vectors of pointers can be converted. 146template <
typename U,
typename A>
148 std::enable_if_t<std::is_convertible<U *const *, T const *>::value>
153 /// @name Simple Operations 162 /// empty - Check if the array is empty. 167 /// size - Get the array size. 170 /// front - Get the first element. 176 /// back - Get the last element. 182// copy - Allocate copy in Allocator and return ArrayRef<T> to it. 184T *Buff =
A.template Allocate<T>(
Length);
185 std::uninitialized_copy(begin(), end(), Buff);
189 /// equals - Check for element-wise equality. 193return std::equal(begin(), end(),
RHS.begin());
196 /// slice(n, m) - Chop off the first N elements of the array, and keep M 197 /// elements in the array. 203 /// slice(n) - Chop off the first N elements of the array. 206 /// Drop the first \p N elements of the array. 208assert(
size() >=
N &&
"Dropping more elements than exist");
212 /// Drop the last \p N elements of the array. 214assert(
size() >=
N &&
"Dropping more elements than exist");
215return slice(0,
size() -
N);
218 /// Return a copy of *this with the first N elements satisfying the 219 /// given predicate removed. 224 /// Return a copy of *this with the first N elements not satisfying 225 /// the given predicate removed. 230 /// Return a copy of *this with only the first \p N elements. 234return drop_back(
size() -
N);
237 /// Return a copy of *this with only the last \p N elements. 241return drop_front(
size() -
N);
244 /// Return the first N elements of this Array that satisfy the given 250 /// Return the first N elements of this Array that don't satisfy the 257 /// @name Operator Overloads 264 /// Disallow accidental assignment from a temporary. 266 /// The declaration here is extra complicated so that "arrayRef = {}" 267 /// continues to select the move assignment operator. 269 std::enable_if_t<std::is_same<U, T>::value,
ArrayRef<T>> &
272 /// Disallow accidental assignment from a temporary. 274 /// The declaration here is extra complicated so that "arrayRef = {}" 275 /// continues to select the move assignment operator. 277 std::enable_if_t<std::is_same<U, T>::value,
ArrayRef<T>> &
281 /// @name Expensive Operations 283 std::vector<T>
vec()
const{
284return std::vector<T>(Data, Data+
Length);
288 /// @name Conversion operators 290operator std::vector<T>()
const{
291return std::vector<T>(Data, Data+
Length);
297 /// MutableArrayRef - Represent a mutable reference to an array (0 or more 298 /// elements consecutively in memory), i.e. a start pointer and a length. It 299 /// allows various APIs to take and modify consecutive elements easily and 302 /// This class does not own the underlying data, it is expected to be used in 303 /// situations where the data resides in some other buffer, whose lifetime 304 /// extends past that of the MutableArrayRef. For this reason, it is not in 305 /// general safe to store a MutableArrayRef. 307 /// This is intended to be trivially copyable, so it should be passed by 324 /// Construct an empty MutableArrayRef. 327 /// Construct an empty MutableArrayRef from std::nullopt. 330 /// Construct a MutableArrayRef from a single element. 333 /// Construct a MutableArrayRef from a pointer and length. 337 /// Construct a MutableArrayRef from a range. 340 /// Construct a MutableArrayRef from a SmallVector. 344 /// Construct a MutableArrayRef from a std::vector. 348 /// Construct a MutableArrayRef from a std::array 353 /// Construct a MutableArrayRef from a C array. 365 /// front - Get the first element. 371 /// back - Get the last element. 377 /// slice(n, m) - Chop off the first N elements of the array, and keep M 378 /// elements in the array. 380assert(
N + M <= this->
size() &&
"Invalid specifier");
384 /// slice(n) - Chop off the first N elements of the array. 386return slice(
N, this->
size() -
N);
389 /// Drop the first \p N elements of the array. 391assert(this->
size() >=
N &&
"Dropping more elements than exist");
392return slice(
N, this->
size() -
N);
396assert(this->
size() >=
N &&
"Dropping more elements than exist");
397return slice(0, this->
size() -
N);
400 /// Return a copy of *this with the first N elements satisfying the 401 /// given predicate removed. 402template <
class PredicateT>
407 /// Return a copy of *this with the first N elements not satisfying 408 /// the given predicate removed. 409template <
class PredicateT>
414 /// Return a copy of *this with only the first \p N elements. 416if (
N >= this->
size())
418return drop_back(this->
size() -
N);
421 /// Return a copy of *this with only the last \p N elements. 423if (
N >= this->
size())
425return drop_front(this->
size() -
N);
428 /// Return the first N elements of this Array that satisfy the given 430template <
class PredicateT>
435 /// Return the first N elements of this Array that don't satisfy the 437template <
class PredicateT>
443 /// @name Operator Overloads 446assert(Index < this->
size() &&
"Invalid index!");
451 /// This is a MutableArrayRef that owns its array. 459 std::copy(Data.begin(), Data.end(), this->begin());
465delete[] this->
data();
474 /// @name ArrayRef Deduction guides 476 /// Deduction guide to construct an ArrayRef from a single element. 479 /// Deduction guide to construct an ArrayRef from a pointer and length 482 /// Deduction guide to construct an ArrayRef from a range 485 /// Deduction guide to construct an ArrayRef from a SmallVector 488 /// Deduction guide to construct an ArrayRef from a SmallVector 489template <
typename T,
unsigned N>
492 /// Deduction guide to construct an ArrayRef from a std::vector 495 /// Deduction guide to construct an ArrayRef from a std::array 496template <
typename T, std::
size_t N>
499 /// Deduction guide to construct an ArrayRef from an ArrayRef (const) 502 /// Deduction guide to construct an ArrayRef from an ArrayRef 505 /// Deduction guide to construct an ArrayRef from a C array. 510 /// @name MutableArrayRef Deduction guides 512 /// Deduction guide to construct a `MutableArrayRef` from a single element 515 /// Deduction guide to construct a `MutableArrayRef` from a pointer and 520 /// Deduction guide to construct a `MutableArrayRef` from a `SmallVector`. 524template <
class T,
unsigned N>
527 /// Deduction guide to construct a `MutableArrayRef` from a `std::vector`. 530 /// Deduction guide to construct a `MutableArrayRef` from a `std::array`. 531template <
class T, std::
size_t N>
534 /// Deduction guide to construct a `MutableArrayRef` from a C array. 535template <
typename T,
size_t N>
539 /// @name ArrayRef Comparison Operators 568// Provide DenseMapInfo for ArrayRefs. 572reinterpret_cast<constT *
>(~
static_cast<uintptr_t
>(0)),
size_t(0));
577reinterpret_cast<constT *
>(~
static_cast<uintptr_t
>(1)),
size_t(0));
582"Cannot hash the empty key!");
584"Cannot hash the tombstone key!");
589if (
RHS.data() == getEmptyKey().
data())
590returnLHS.data() == getEmptyKey().data();
591if (
RHS.data() == getTombstoneKey().
data())
592returnLHS.data() == getTombstoneKey().data();
597}
// end namespace llvm 599#endif// LLVM_ADT_ARRAYREF_H static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_LIFETIME_BOUND
#define LLVM_GSL_POINTER
LLVM_GSL_POINTER - Apply this to non-owning classes like StringRef to enable lifetime warnings.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
std::enable_if_t< std::is_same< U, T >::value, ArrayRef< T > > & operator=(std::initializer_list< U >)=delete
Disallow accidental assignment from a temporary.
std::vector< T > vec() const
bool equals(ArrayRef RHS) const
equals - Check for element-wise equality.
ArrayRef< T > drop_while(PredicateT Pred) const
Return a copy of *this with the first N elements satisfying the given predicate removed.
const T & back() const
back - Get the last element.
ArrayRef(const std::vector< U *, A > &Vec, std::enable_if_t< std::is_convertible< U *const *, T const * >::value > *=nullptr)
Construct an ArrayRef<const T*> from std::vector<T*>.
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
constexpr ArrayRef(std::initializer_list< T > Vec LLVM_LIFETIME_BOUND)
Construct an ArrayRef from a std::initializer_list.
MutableArrayRef< T > copy(Allocator &A)
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
ArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
reverse_iterator rend() const
const T & front() const
front - Get the first element.
ArrayRef< T > take_while(PredicateT Pred) const
Return the first N elements of this Array that satisfy the given predicate.
ArrayRef(const T &OneElt LLVM_LIFETIME_BOUND)
Construct an ArrayRef from a single element.
constexpr ArrayRef(const T *begin LLVM_LIFETIME_BOUND, const T *end)
Construct an ArrayRef from a range.
std::reverse_iterator< const_iterator > const_reverse_iterator
ArrayRef< T > take_until(PredicateT Pred) const
Return the first N elements of this Array that don't satisfy the given predicate.
ArrayRef< T > drop_until(PredicateT Pred) const
Return a copy of *this with the first N elements not satisfying the given predicate removed.
size_t size() const
size - Get the array size.
ArrayRef()=default
Construct an empty ArrayRef.
ArrayRef(std::nullopt_t)
Construct an empty ArrayRef from std::nullopt.
ArrayRef(const ArrayRef< U * > &A, std::enable_if_t< std::is_convertible< U *const *, T const * >::value > *=nullptr)
Construct an ArrayRef<const T*> from ArrayRef<T*>.
std::reverse_iterator< iterator > reverse_iterator
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
constexpr ArrayRef(const std::array< T, N > &Arr)
Construct an ArrayRef from a std::array.
ArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
bool empty() const
empty - Check if the array is empty.
constexpr ArrayRef(const T(&Arr LLVM_LIFETIME_BOUND)[N])
Construct an ArrayRef from a C array.
ArrayRef(const SmallVectorTemplateCommon< U *, DummyT > &Vec, std::enable_if_t< std::is_convertible< U *const *, T const * >::value > *=nullptr)
Construct an ArrayRef<const T*> from a SmallVector<T*>.
constexpr ArrayRef(const T *data LLVM_LIFETIME_BOUND, size_t length)
Construct an ArrayRef from a pointer and length.
const T & operator[](size_t Index) const
ArrayRef(const std::vector< T, A > &Vec)
Construct an ArrayRef from a std::vector.
std::enable_if_t< std::is_same< U, T >::value, ArrayRef< T > > & operator=(U &&Temporary)=delete
Disallow accidental assignment from a temporary.
reverse_iterator rbegin() const
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.
ArrayRef(const SmallVectorTemplateCommon< T, U > &Vec)
Construct an ArrayRef from a SmallVector.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
MutableArrayRef< T > take_until(PredicateT Pred) const
Return the first N elements of this Array that don't satisfy the given predicate.
MutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
MutableArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
reverse_iterator rbegin() const
MutableArrayRef(T *begin, T *end)
Construct a MutableArrayRef from a range.
MutableArrayRef< T > slice(size_t N) const
slice(n) - Chop off the first N elements of the array.
MutableArrayRef(T *data, size_t length)
Construct a MutableArrayRef from a pointer and length.
MutableArrayRef()=default
Construct an empty MutableArrayRef.
constexpr MutableArrayRef(std::array< T, N > &Arr)
Construct a MutableArrayRef from a std::array.
T & front() const
front - Get the first element.
std::reverse_iterator< const_iterator > const_reverse_iterator
T & operator[](size_t Index) const
T & back() const
back - Get the last element.
MutableArrayRef(std::vector< T > &Vec)
Construct a MutableArrayRef from a std::vector.
constexpr MutableArrayRef(T(&Arr)[N])
Construct a MutableArrayRef from a C array.
MutableArrayRef(SmallVectorImpl< T > &Vec)
Construct a MutableArrayRef from a SmallVector.
MutableArrayRef< T > drop_back(size_t N=1) const
MutableArrayRef< T > take_while(PredicateT Pred) const
Return the first N elements of this Array that satisfy the given predicate.
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.
MutableArrayRef< T > drop_while(PredicateT Pred) const
Return a copy of *this with the first N elements satisfying the given predicate removed.
reverse_iterator rend() const
MutableArrayRef< T > drop_until(PredicateT Pred) const
Return a copy of *this with the first N elements not satisfying the given predicate removed.
MutableArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
MutableArrayRef(std::nullopt_t)
Construct an empty MutableArrayRef from std::nullopt.
std::reverse_iterator< iterator > reverse_iterator
This is a MutableArrayRef that owns its array.
OwningArrayRef & operator=(OwningArrayRef &&Other)
OwningArrayRef(OwningArrayRef &&Other)
OwningArrayRef(ArrayRef< T > Data)
OwningArrayRef(size_t Size)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is the part of SmallVectorTemplateBase which does not depend on whether the type T is a POD.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An opaque object representing a hash code.
This is an optimization pass for GlobalISel generic memory operations.
hash_code hash_value(const FixedPointSemantics &Val)
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
bool operator!=(uint64_t V1, const APInt &V2)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
auto find_if_not(R &&Range, UnaryPredicate P)
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
static bool isEqual(ArrayRef< T > LHS, ArrayRef< T > RHS)
static ArrayRef< T > getTombstoneKey()
static unsigned getHashValue(ArrayRef< T > Val)
static ArrayRef< T > getEmptyKey()
An information struct used to provide DenseMap with the various necessary components for a given valu...