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*)nullptr : 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()) {}
99 /// Construct an ArrayRef<const T*> from a SmallVector<T*>. This is
100 /// templated in order to avoid instantiating SmallVectorTemplateCommon<T>
101 /// whenever we copy-construct an ArrayRef.
102 template<typename U, typename DummyT>
103 /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<U*, DummyT> &Vec,
104 typename std::enable_if<
105 std::is_convertible<U *const *,
106 T const *>::value>::type* = 0)
107 : Data(Vec.data()), Length(Vec.size()) {
110 /// Construct an ArrayRef<const T*> from std::vector<T*>. This uses SFINAE
111 /// to ensure that only vectors of pointers can be converted.
112 template<typename U, typename A>
113 ArrayRef(const std::vector<U *, A> &Vec,
114 typename std::enable_if<
115 std::is_convertible<U *const *, T const *>::value>::type* = 0)
116 : Data(Vec.data()), Length(Vec.size()) {}
119 /// @name Simple Operations
122 iterator begin() const { return Data; }
123 iterator end() const { return Data + Length; }
125 reverse_iterator rbegin() const { return reverse_iterator(end()); }
126 reverse_iterator rend() const { return reverse_iterator(begin()); }
128 /// empty - Check if the array is empty.
129 bool empty() const { return Length == 0; }
131 const T *data() const { return Data; }
133 /// size - Get the array size.
134 size_t size() const { return Length; }
136 /// front - Get the first element.
137 const T &front() const {
142 /// back - Get the last element.
143 const T &back() const {
145 return Data[Length-1];
148 // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
149 template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
150 T *Buff = A.template Allocate<T>(Length);
151 std::copy(begin(), end(), Buff);
152 return ArrayRef<T>(Buff, Length);
155 /// equals - Check for element-wise equality.
156 bool equals(ArrayRef RHS) const {
157 if (Length != RHS.Length)
161 return std::equal(begin(), end(), RHS.begin());
164 /// slice(n) - Chop off the first N elements of the array.
165 ArrayRef<T> slice(unsigned N) const {
166 assert(N <= size() && "Invalid specifier");
167 return ArrayRef<T>(data()+N, size()-N);
170 /// slice(n, m) - Chop off the first N elements of the array, and keep M
171 /// elements in the array.
172 ArrayRef<T> slice(unsigned N, unsigned M) const {
173 assert(N+M <= size() && "Invalid specifier");
174 return ArrayRef<T>(data()+N, M);
177 // \brief Drop the last \p N elements of the array.
178 ArrayRef<T> drop_back(unsigned N = 1) const {
179 assert(size() >= N && "Dropping more elements than exist");
180 return slice(0, size() - N);
184 /// @name Operator Overloads
186 const T &operator[](size_t Index) const {
187 assert(Index < Length && "Invalid index!");
192 /// @name Expensive Operations
194 std::vector<T> vec() const {
195 return std::vector<T>(Data, Data+Length);
199 /// @name Conversion operators
201 operator std::vector<T>() const {
202 return std::vector<T>(Data, Data+Length);
208 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
209 /// elements consecutively in memory), i.e. a start pointer and a length. It
210 /// allows various APIs to take and modify consecutive elements easily and
213 /// This class does not own the underlying data, it is expected to be used in
214 /// situations where the data resides in some other buffer, whose lifetime
215 /// extends past that of the MutableArrayRef. For this reason, it is not in
216 /// general safe to store a MutableArrayRef.
218 /// This is intended to be trivially copyable, so it should be passed by
221 class MutableArrayRef : public ArrayRef<T> {
225 typedef std::reverse_iterator<iterator> reverse_iterator;
227 /// Construct an empty MutableArrayRef.
228 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
230 /// Construct an empty MutableArrayRef from None.
231 /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
233 /// Construct an MutableArrayRef from a single element.
234 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
236 /// Construct an MutableArrayRef from a pointer and length.
237 /*implicit*/ MutableArrayRef(T *data, size_t length)
238 : ArrayRef<T>(data, length) {}
240 /// Construct an MutableArrayRef from a range.
241 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
243 /// Construct an MutableArrayRef from a SmallVector.
244 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
245 : ArrayRef<T>(Vec) {}
247 /// Construct a MutableArrayRef from a std::vector.
248 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
249 : ArrayRef<T>(Vec) {}
251 /// Construct an MutableArrayRef from a C array.
253 /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
254 : ArrayRef<T>(Arr) {}
256 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
258 iterator begin() const { return data(); }
259 iterator end() const { return data() + this->size(); }
261 reverse_iterator rbegin() const { return reverse_iterator(end()); }
262 reverse_iterator rend() const { return reverse_iterator(begin()); }
264 /// front - Get the first element.
266 assert(!this->empty());
270 /// back - Get the last element.
272 assert(!this->empty());
273 return data()[this->size()-1];
276 /// slice(n) - Chop off the first N elements of the array.
277 MutableArrayRef<T> slice(unsigned N) const {
278 assert(N <= this->size() && "Invalid specifier");
279 return MutableArrayRef<T>(data()+N, this->size()-N);
282 /// slice(n, m) - Chop off the first N elements of the array, and keep M
283 /// elements in the array.
284 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
285 assert(N+M <= this->size() && "Invalid specifier");
286 return MutableArrayRef<T>(data()+N, M);
289 MutableArrayRef<T> drop_back(unsigned N) const {
290 assert(this->size() >= N && "Dropping more elements than exist");
291 return slice(0, this->size() - N);
295 /// @name Operator Overloads
297 T &operator[](size_t Index) const {
298 assert(Index < this->size() && "Invalid index!");
299 return data()[Index];
303 /// @name ArrayRef Convenience constructors
306 /// Construct an ArrayRef from a single element.
308 ArrayRef<T> makeArrayRef(const T &OneElt) {
312 /// Construct an ArrayRef from a pointer and length.
314 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
315 return ArrayRef<T>(data, length);
318 /// Construct an ArrayRef from a range.
320 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
321 return ArrayRef<T>(begin, end);
324 /// Construct an ArrayRef from a SmallVector.
325 template <typename T>
326 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
330 /// Construct an ArrayRef from a SmallVector.
331 template <typename T, unsigned N>
332 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
336 /// Construct an ArrayRef from a std::vector.
338 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
342 /// Construct an ArrayRef from a C array.
343 template<typename T, size_t N>
344 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
345 return ArrayRef<T>(Arr);
349 /// @name ArrayRef Comparison Operators
353 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
354 return LHS.equals(RHS);
358 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
359 return !(LHS == RHS);
364 // ArrayRefs can be treated like a POD type.
365 template <typename T> struct isPodLike;
366 template <typename T> struct isPodLike<ArrayRef<T> > {
367 static const bool value = true;