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/SmallVector.h"
19 /// ArrayRef - Represent a constant reference to an array (0 or more elements
20 /// consecutively in memory), i.e. a start pointer and a length. It allows
21 /// various APIs to take consecutive elements easily and conveniently.
23 /// This class does not own the underlying data, it is expected to be used in
24 /// situations where the data resides in some other buffer, whose lifetime
25 /// extends past that of the ArrayRef. For this reason, it is not in general
26 /// safe to store an ArrayRef.
28 /// This is intended to be trivially copyable, so it should be passed by
33 typedef const T *iterator;
34 typedef const T *const_iterator;
35 typedef size_t size_type;
37 typedef std::reverse_iterator<iterator> reverse_iterator;
40 /// The start of the array, in an external buffer.
43 /// The number of elements.
47 /// @name Constructors
50 /// Construct an empty ArrayRef.
51 /*implicit*/ ArrayRef() : Data(nullptr), Length(0) {}
53 /// Construct an empty ArrayRef from None.
54 /*implicit*/ ArrayRef(NoneType) : Data(nullptr), Length(0) {}
56 /// Construct an ArrayRef from a single element.
57 /*implicit*/ ArrayRef(const T &OneElt)
58 : Data(&OneElt), Length(1) {}
60 /// Construct an ArrayRef from a pointer and length.
61 /*implicit*/ ArrayRef(const T *data, size_t length)
62 : Data(data), Length(length) {}
64 /// Construct an ArrayRef from a range.
65 ArrayRef(const T *begin, const T *end)
66 : Data(begin), Length(end - begin) {}
68 /// Construct an ArrayRef from a SmallVector. This is templated in order to
69 /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
70 /// copy-construct an ArrayRef.
72 /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
73 : Data(Vec.data()), Length(Vec.size()) {
76 /// Construct an ArrayRef from a std::vector.
78 /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
79 : Data(Vec.data()), Length(Vec.size()) {}
81 /// Construct an ArrayRef from a C array.
83 /*implicit*/ LLVM_CONSTEXPR ArrayRef(const T (&Arr)[N])
84 : Data(Arr), Length(N) {}
86 /// Construct an ArrayRef from a std::initializer_list.
87 /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
88 : Data(Vec.begin() == Vec.end() ? (T*)0 : Vec.begin()),
91 /// Construct an ArrayRef<const T*> from ArrayRef<T*>. This uses SFINAE to
92 /// ensure that only ArrayRefs of pointers can be converted.
94 ArrayRef(const ArrayRef<U *> &A,
95 typename std::enable_if<
96 std::is_convertible<U *const *, T const *>::value>::type* = 0)
97 : Data(A.data()), Length(A.size()) {}
100 /// @name Simple Operations
103 iterator begin() const { return Data; }
104 iterator end() const { return Data + Length; }
106 reverse_iterator rbegin() const { return reverse_iterator(end()); }
107 reverse_iterator rend() const { return reverse_iterator(begin()); }
109 /// empty - Check if the array is empty.
110 bool empty() const { return Length == 0; }
112 const T *data() const { return Data; }
114 /// size - Get the array size.
115 size_t size() const { return Length; }
117 /// front - Get the first element.
118 const T &front() const {
123 /// back - Get the last element.
124 const T &back() const {
126 return Data[Length-1];
129 // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
130 template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
131 T *Buff = A.template Allocate<T>(Length);
132 std::copy(begin(), end(), Buff);
133 return ArrayRef<T>(Buff, Length);
136 /// equals - Check for element-wise equality.
137 bool equals(ArrayRef RHS) const {
138 if (Length != RHS.Length)
142 return std::equal(begin(), end(), RHS.begin());
145 /// slice(n) - Chop off the first N elements of the array.
146 ArrayRef<T> slice(unsigned N) const {
147 assert(N <= size() && "Invalid specifier");
148 return ArrayRef<T>(data()+N, size()-N);
151 /// slice(n, m) - Chop off the first N elements of the array, and keep M
152 /// elements in the array.
153 ArrayRef<T> slice(unsigned N, unsigned M) const {
154 assert(N+M <= size() && "Invalid specifier");
155 return ArrayRef<T>(data()+N, M);
158 // \brief Drop the last \p N elements of the array.
159 ArrayRef<T> drop_back(unsigned N = 1) const {
160 assert(size() >= N && "Dropping more elements than exist");
161 return slice(0, size() - N);
165 /// @name Operator Overloads
167 const T &operator[](size_t Index) const {
168 assert(Index < Length && "Invalid index!");
173 /// @name Expensive Operations
175 std::vector<T> vec() const {
176 return std::vector<T>(Data, Data+Length);
180 /// @name Conversion operators
182 operator std::vector<T>() const {
183 return std::vector<T>(Data, Data+Length);
189 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
190 /// elements consecutively in memory), i.e. a start pointer and a length. It
191 /// allows various APIs to take and modify consecutive elements easily and
194 /// This class does not own the underlying data, it is expected to be used in
195 /// situations where the data resides in some other buffer, whose lifetime
196 /// extends past that of the MutableArrayRef. For this reason, it is not in
197 /// general safe to store a MutableArrayRef.
199 /// This is intended to be trivially copyable, so it should be passed by
202 class MutableArrayRef : public ArrayRef<T> {
206 typedef std::reverse_iterator<iterator> reverse_iterator;
208 /// Construct an empty MutableArrayRef.
209 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
211 /// Construct an empty MutableArrayRef from None.
212 /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
214 /// Construct an MutableArrayRef from a single element.
215 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
217 /// Construct an MutableArrayRef from a pointer and length.
218 /*implicit*/ MutableArrayRef(T *data, size_t length)
219 : ArrayRef<T>(data, length) {}
221 /// Construct an MutableArrayRef from a range.
222 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
224 /// Construct an MutableArrayRef from a SmallVector.
225 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
226 : ArrayRef<T>(Vec) {}
228 /// Construct a MutableArrayRef from a std::vector.
229 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
230 : ArrayRef<T>(Vec) {}
232 /// Construct an MutableArrayRef from a C array.
234 /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
235 : ArrayRef<T>(Arr) {}
237 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
239 iterator begin() const { return data(); }
240 iterator end() const { return data() + this->size(); }
242 reverse_iterator rbegin() const { return reverse_iterator(end()); }
243 reverse_iterator rend() const { return reverse_iterator(begin()); }
245 /// front - Get the first element.
247 assert(!this->empty());
251 /// back - Get the last element.
253 assert(!this->empty());
254 return data()[this->size()-1];
257 /// slice(n) - Chop off the first N elements of the array.
258 MutableArrayRef<T> slice(unsigned N) const {
259 assert(N <= this->size() && "Invalid specifier");
260 return MutableArrayRef<T>(data()+N, this->size()-N);
263 /// slice(n, m) - Chop off the first N elements of the array, and keep M
264 /// elements in the array.
265 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
266 assert(N+M <= this->size() && "Invalid specifier");
267 return MutableArrayRef<T>(data()+N, M);
271 /// @name Operator Overloads
273 T &operator[](size_t Index) const {
274 assert(Index < this->size() && "Invalid index!");
275 return data()[Index];
279 /// @name ArrayRef Convenience constructors
282 /// Construct an ArrayRef from a single element.
284 ArrayRef<T> makeArrayRef(const T &OneElt) {
288 /// Construct an ArrayRef from a pointer and length.
290 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
291 return ArrayRef<T>(data, length);
294 /// Construct an ArrayRef from a range.
296 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
297 return ArrayRef<T>(begin, end);
300 /// Construct an ArrayRef from a SmallVector.
301 template <typename T>
302 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
306 /// Construct an ArrayRef from a SmallVector.
307 template <typename T, unsigned N>
308 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
312 /// Construct an ArrayRef from a std::vector.
314 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
318 /// Construct an ArrayRef from a C array.
319 template<typename T, size_t N>
320 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
321 return ArrayRef<T>(Arr);
325 /// @name ArrayRef Comparison Operators
329 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
330 return LHS.equals(RHS);
334 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
335 return !(LHS == RHS);
340 // ArrayRefs can be treated like a POD type.
341 template <typename T> struct isPodLike;
342 template <typename T> struct isPodLike<ArrayRef<T> > {
343 static const bool value = true;