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 return std::equal(begin(), end(), RHS.begin());
151 /// slice(n) - Chop off the first N elements of the array.
152 ArrayRef<T> slice(unsigned N) const {
153 assert(N <= size() && "Invalid specifier");
154 return ArrayRef<T>(data()+N, size()-N);
157 /// slice(n, m) - Chop off the first N elements of the array, and keep M
158 /// elements in the array.
159 ArrayRef<T> slice(unsigned N, unsigned M) const {
160 assert(N+M <= size() && "Invalid specifier");
161 return ArrayRef<T>(data()+N, M);
164 // \brief Drop the last \p N elements of the array.
165 ArrayRef<T> drop_back(unsigned N = 1) const {
166 assert(size() >= N && "Dropping more elements than exist");
167 return slice(0, size() - N);
171 /// @name Operator Overloads
173 const T &operator[](size_t Index) const {
174 assert(Index < Length && "Invalid index!");
179 /// @name Expensive Operations
181 std::vector<T> vec() const {
182 return std::vector<T>(Data, Data+Length);
186 /// @name Conversion operators
188 operator std::vector<T>() const {
189 return std::vector<T>(Data, Data+Length);
194 /// @name Convenience methods
196 /// @brief Predicate for testing that the array equals the exact sequence of
199 /// Will return false if the size is not equal to the exact number of
200 /// arguments given or if the array elements don't equal the argument
201 /// elements in order. Currently supports up to 16 arguments, but can
202 /// easily be extended.
203 bool equals(TRefOrNothing Arg0 = TRefOrNothing(),
204 TRefOrNothing Arg1 = TRefOrNothing(),
205 TRefOrNothing Arg2 = TRefOrNothing(),
206 TRefOrNothing Arg3 = TRefOrNothing(),
207 TRefOrNothing Arg4 = TRefOrNothing(),
208 TRefOrNothing Arg5 = TRefOrNothing(),
209 TRefOrNothing Arg6 = TRefOrNothing(),
210 TRefOrNothing Arg7 = TRefOrNothing(),
211 TRefOrNothing Arg8 = TRefOrNothing(),
212 TRefOrNothing Arg9 = TRefOrNothing(),
213 TRefOrNothing Arg10 = TRefOrNothing(),
214 TRefOrNothing Arg11 = TRefOrNothing(),
215 TRefOrNothing Arg12 = TRefOrNothing(),
216 TRefOrNothing Arg13 = TRefOrNothing(),
217 TRefOrNothing Arg14 = TRefOrNothing(),
218 TRefOrNothing Arg15 = TRefOrNothing()) {
219 TRefOrNothing Args[] = {Arg0, Arg1, Arg2, Arg3, Arg4, Arg5,
220 Arg6, Arg7, Arg8, Arg9, Arg10, Arg11,
221 Arg12, Arg13, Arg14, Arg15};
222 if (size() > array_lengthof(Args))
225 for (unsigned i = 0, e = size(); i != e; ++i)
226 if (Args[i].TPtr == nullptr || (*this)[i] != *Args[i].TPtr)
229 // Either the size is exactly as many args, or the next arg must be null.
230 return size() == array_lengthof(Args) || Args[size()].TPtr == nullptr;
236 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
237 /// elements consecutively in memory), i.e. a start pointer and a length. It
238 /// allows various APIs to take and modify consecutive elements easily and
241 /// This class does not own the underlying data, it is expected to be used in
242 /// situations where the data resides in some other buffer, whose lifetime
243 /// extends past that of the MutableArrayRef. For this reason, it is not in
244 /// general safe to store a MutableArrayRef.
246 /// This is intended to be trivially copyable, so it should be passed by
249 class MutableArrayRef : public ArrayRef<T> {
253 typedef std::reverse_iterator<iterator> reverse_iterator;
255 /// Construct an empty MutableArrayRef.
256 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
258 /// Construct an empty MutableArrayRef from None.
259 /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
261 /// Construct an MutableArrayRef from a single element.
262 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
264 /// Construct an MutableArrayRef from a pointer and length.
265 /*implicit*/ MutableArrayRef(T *data, size_t length)
266 : ArrayRef<T>(data, length) {}
268 /// Construct an MutableArrayRef from a range.
269 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
271 /// Construct an MutableArrayRef from a SmallVector.
272 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
273 : ArrayRef<T>(Vec) {}
275 /// Construct a MutableArrayRef from a std::vector.
276 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
277 : ArrayRef<T>(Vec) {}
279 /// Construct an MutableArrayRef from a C array.
281 /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
282 : ArrayRef<T>(Arr) {}
284 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
286 iterator begin() const { return data(); }
287 iterator end() const { return data() + this->size(); }
289 reverse_iterator rbegin() const { return reverse_iterator(end()); }
290 reverse_iterator rend() const { return reverse_iterator(begin()); }
292 /// front - Get the first element.
294 assert(!this->empty());
298 /// back - Get the last element.
300 assert(!this->empty());
301 return data()[this->size()-1];
304 /// slice(n) - Chop off the first N elements of the array.
305 MutableArrayRef<T> slice(unsigned N) const {
306 assert(N <= this->size() && "Invalid specifier");
307 return MutableArrayRef<T>(data()+N, this->size()-N);
310 /// slice(n, m) - Chop off the first N elements of the array, and keep M
311 /// elements in the array.
312 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
313 assert(N+M <= this->size() && "Invalid specifier");
314 return MutableArrayRef<T>(data()+N, M);
318 /// @name Operator Overloads
320 T &operator[](size_t Index) const {
321 assert(Index < this->size() && "Invalid index!");
322 return data()[Index];
326 /// @name ArrayRef Convenience constructors
329 /// Construct an ArrayRef from a single element.
331 ArrayRef<T> makeArrayRef(const T &OneElt) {
335 /// Construct an ArrayRef from a pointer and length.
337 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
338 return ArrayRef<T>(data, length);
341 /// Construct an ArrayRef from a range.
343 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
344 return ArrayRef<T>(begin, end);
347 /// Construct an ArrayRef from a SmallVector.
348 template <typename T>
349 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
353 /// Construct an ArrayRef from a SmallVector.
354 template <typename T, unsigned N>
355 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
359 /// Construct an ArrayRef from a std::vector.
361 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
365 /// Construct an ArrayRef from a C array.
366 template<typename T, size_t N>
367 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
368 return ArrayRef<T>(Arr);
372 /// @name ArrayRef Comparison Operators
376 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
377 return LHS.equals(RHS);
381 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
382 return !(LHS == RHS);
387 // ArrayRefs can be treated like a POD type.
388 template <typename T> struct isPodLike;
389 template <typename T> struct isPodLike<ArrayRef<T> > {
390 static const bool value = true;