1 //===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_ARRAYREF_H
11 #define LLVM_ADT_ARRAYREF_H
13 #include "llvm/ADT/None.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
20 /// ArrayRef - Represent a constant reference to an array (0 or more elements
21 /// consecutively in memory), i.e. a start pointer and a length. It allows
22 /// various APIs to take consecutive elements easily and conveniently.
24 /// This class does not own the underlying data, it is expected to be used in
25 /// situations where the data resides in some other buffer, whose lifetime
26 /// extends past that of the ArrayRef. For this reason, it is not in general
27 /// safe to store an ArrayRef.
29 /// This is intended to be trivially copyable, so it should be passed by
34 typedef const T *iterator;
35 typedef const T *const_iterator;
36 typedef size_t size_type;
38 typedef std::reverse_iterator<iterator> reverse_iterator;
41 /// The start of the array, in an external buffer.
44 /// The number of elements.
47 /// \brief A dummy "optional" type that is only created by implicit
48 /// conversion from a reference to T.
50 /// This type must *only* be used in a function argument or as a copy of
51 /// a function argument, as otherwise it will hold a pointer to a temporary
52 /// past that temporaries' lifetime.
53 struct TRefOrNothing {
56 TRefOrNothing() : TPtr(nullptr) {}
57 TRefOrNothing(const T &TRef) : TPtr(&TRef) {}
61 /// @name Constructors
64 /// Construct an empty ArrayRef.
65 /*implicit*/ ArrayRef() : Data(nullptr), Length(0) {}
67 /// Construct an empty ArrayRef from None.
68 /*implicit*/ ArrayRef(NoneType) : Data(nullptr), Length(0) {}
70 /// Construct an ArrayRef from a single element.
71 /*implicit*/ ArrayRef(const T &OneElt)
72 : Data(&OneElt), Length(1) {}
74 /// Construct an ArrayRef from a pointer and length.
75 /*implicit*/ ArrayRef(const T *data, size_t length)
76 : Data(data), Length(length) {}
78 /// Construct an ArrayRef from a range.
79 ArrayRef(const T *begin, const T *end)
80 : Data(begin), Length(end - begin) {}
82 /// Construct an ArrayRef from a SmallVector. This is templated in order to
83 /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
84 /// copy-construct an ArrayRef.
86 /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
87 : Data(Vec.data()), Length(Vec.size()) {
90 /// Construct an ArrayRef from a std::vector.
92 /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
93 : Data(Vec.data()), Length(Vec.size()) {}
95 /// Construct an ArrayRef from a C array.
97 /*implicit*/ LLVM_CONSTEXPR ArrayRef(const T (&Arr)[N])
98 : Data(Arr), Length(N) {}
100 #if LLVM_HAS_INITIALIZER_LISTS
101 /// Construct an ArrayRef from a std::initializer_list.
102 /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
103 : Data(Vec.begin() == Vec.end() ? (T*)0 : Vec.begin()),
104 Length(Vec.size()) {}
108 /// @name Simple Operations
111 iterator begin() const { return Data; }
112 iterator end() const { return Data + Length; }
114 reverse_iterator rbegin() const { return reverse_iterator(end()); }
115 reverse_iterator rend() const { return reverse_iterator(begin()); }
117 /// empty - Check if the array is empty.
118 bool empty() const { return Length == 0; }
120 const T *data() const { return Data; }
122 /// size - Get the array size.
123 size_t size() const { return Length; }
125 /// front - Get the first element.
126 const T &front() const {
131 /// back - Get the last element.
132 const T &back() const {
134 return Data[Length-1];
137 // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
138 template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
139 T *Buff = A.template Allocate<T>(Length);
140 std::copy(begin(), end(), Buff);
141 return ArrayRef<T>(Buff, Length);
144 /// equals - Check for element-wise equality.
145 bool equals(ArrayRef RHS) const {
146 if (Length != RHS.Length)
148 // Don't use std::equal(), since it asserts in MSVC on nullptr iterators.
149 for (auto L = begin(), LE = end(), R = RHS.begin(); L != LE; ++L, ++R)
150 // Match std::equal() in using == (instead of !=) to minimize API
151 // requirements of ArrayRef'ed types.
157 /// slice(n) - Chop off the first N elements of the array.
158 ArrayRef<T> slice(unsigned N) const {
159 assert(N <= size() && "Invalid specifier");
160 return ArrayRef<T>(data()+N, size()-N);
163 /// slice(n, m) - Chop off the first N elements of the array, and keep M
164 /// elements in the array.
165 ArrayRef<T> slice(unsigned N, unsigned M) const {
166 assert(N+M <= size() && "Invalid specifier");
167 return ArrayRef<T>(data()+N, M);
170 // \brief Drop the last \p N elements of the array.
171 ArrayRef<T> drop_back(unsigned N = 1) const {
172 assert(size() >= N && "Dropping more elements than exist");
173 return slice(0, size() - N);
177 /// @name Operator Overloads
179 const T &operator[](size_t Index) const {
180 assert(Index < Length && "Invalid index!");
185 /// @name Expensive Operations
187 std::vector<T> vec() const {
188 return std::vector<T>(Data, Data+Length);
192 /// @name Conversion operators
194 operator std::vector<T>() const {
195 return std::vector<T>(Data, Data+Length);
200 /// @name Convenience methods
202 /// @brief Predicate for testing that the array equals the exact sequence of
205 /// Will return false if the size is not equal to the exact number of
206 /// arguments given or if the array elements don't equal the argument
207 /// elements in order. Currently supports up to 16 arguments, but can
208 /// easily be extended.
209 bool equals(TRefOrNothing Arg0 = TRefOrNothing(),
210 TRefOrNothing Arg1 = TRefOrNothing(),
211 TRefOrNothing Arg2 = TRefOrNothing(),
212 TRefOrNothing Arg3 = TRefOrNothing(),
213 TRefOrNothing Arg4 = TRefOrNothing(),
214 TRefOrNothing Arg5 = TRefOrNothing(),
215 TRefOrNothing Arg6 = TRefOrNothing(),
216 TRefOrNothing Arg7 = TRefOrNothing(),
217 TRefOrNothing Arg8 = TRefOrNothing(),
218 TRefOrNothing Arg9 = TRefOrNothing(),
219 TRefOrNothing Arg10 = TRefOrNothing(),
220 TRefOrNothing Arg11 = TRefOrNothing(),
221 TRefOrNothing Arg12 = TRefOrNothing(),
222 TRefOrNothing Arg13 = TRefOrNothing(),
223 TRefOrNothing Arg14 = TRefOrNothing(),
224 TRefOrNothing Arg15 = TRefOrNothing()) {
225 TRefOrNothing Args[] = {Arg0, Arg1, Arg2, Arg3, Arg4, Arg5,
226 Arg6, Arg7, Arg8, Arg9, Arg10, Arg11,
227 Arg12, Arg13, Arg14, Arg15};
228 if (size() > array_lengthof(Args))
231 for (unsigned i = 0, e = size(); i != e; ++i)
232 if (Args[i].TPtr == nullptr || (*this)[i] != *Args[i].TPtr)
235 // Either the size is exactly as many args, or the next arg must be null.
236 return size() == array_lengthof(Args) || Args[size()].TPtr == nullptr;
242 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
243 /// elements consecutively in memory), i.e. a start pointer and a length. It
244 /// allows various APIs to take and modify consecutive elements easily and
247 /// This class does not own the underlying data, it is expected to be used in
248 /// situations where the data resides in some other buffer, whose lifetime
249 /// extends past that of the MutableArrayRef. For this reason, it is not in
250 /// general safe to store a MutableArrayRef.
252 /// This is intended to be trivially copyable, so it should be passed by
255 class MutableArrayRef : public ArrayRef<T> {
259 typedef std::reverse_iterator<iterator> reverse_iterator;
261 /// Construct an empty MutableArrayRef.
262 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
264 /// Construct an empty MutableArrayRef from None.
265 /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
267 /// Construct an MutableArrayRef from a single element.
268 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
270 /// Construct an MutableArrayRef from a pointer and length.
271 /*implicit*/ MutableArrayRef(T *data, size_t length)
272 : ArrayRef<T>(data, length) {}
274 /// Construct an MutableArrayRef from a range.
275 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
277 /// Construct an MutableArrayRef from a SmallVector.
278 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
279 : ArrayRef<T>(Vec) {}
281 /// Construct a MutableArrayRef from a std::vector.
282 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
283 : ArrayRef<T>(Vec) {}
285 /// Construct an MutableArrayRef from a C array.
287 /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
288 : ArrayRef<T>(Arr) {}
290 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
292 iterator begin() const { return data(); }
293 iterator end() const { return data() + this->size(); }
295 reverse_iterator rbegin() const { return reverse_iterator(end()); }
296 reverse_iterator rend() const { return reverse_iterator(begin()); }
298 /// front - Get the first element.
300 assert(!this->empty());
304 /// back - Get the last element.
306 assert(!this->empty());
307 return data()[this->size()-1];
310 /// slice(n) - Chop off the first N elements of the array.
311 MutableArrayRef<T> slice(unsigned N) const {
312 assert(N <= this->size() && "Invalid specifier");
313 return MutableArrayRef<T>(data()+N, this->size()-N);
316 /// slice(n, m) - Chop off the first N elements of the array, and keep M
317 /// elements in the array.
318 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
319 assert(N+M <= this->size() && "Invalid specifier");
320 return MutableArrayRef<T>(data()+N, M);
324 /// @name Operator Overloads
326 T &operator[](size_t Index) const {
327 assert(Index < this->size() && "Invalid index!");
328 return data()[Index];
332 /// @name ArrayRef Convenience constructors
335 /// Construct an ArrayRef from a single element.
337 ArrayRef<T> makeArrayRef(const T &OneElt) {
341 /// Construct an ArrayRef from a pointer and length.
343 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
344 return ArrayRef<T>(data, length);
347 /// Construct an ArrayRef from a range.
349 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
350 return ArrayRef<T>(begin, end);
353 /// Construct an ArrayRef from a SmallVector.
354 template <typename T>
355 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
359 /// Construct an ArrayRef from a SmallVector.
360 template <typename T, unsigned N>
361 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
365 /// Construct an ArrayRef from a std::vector.
367 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
371 /// Construct an ArrayRef from a C array.
372 template<typename T, size_t N>
373 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
374 return ArrayRef<T>(Arr);
378 /// @name ArrayRef Comparison Operators
382 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
383 return LHS.equals(RHS);
387 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
388 return !(LHS == RHS);
393 // ArrayRefs can be treated like a POD type.
394 template <typename T> struct isPodLike;
395 template <typename T> struct isPodLike<ArrayRef<T> > {
396 static const bool value = true;