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::uninitialized_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)
159 return std::equal(begin(), end(), RHS.begin());
162 /// slice(n) - Chop off the first N elements of the array.
163 ArrayRef<T> slice(unsigned N) const {
164 assert(N <= size() && "Invalid specifier");
165 return ArrayRef<T>(data()+N, size()-N);
168 /// slice(n, m) - Chop off the first N elements of the array, and keep M
169 /// elements in the array.
170 ArrayRef<T> slice(unsigned N, unsigned M) const {
171 assert(N+M <= size() && "Invalid specifier");
172 return ArrayRef<T>(data()+N, M);
175 // \brief Drop the last \p N elements of the array.
176 ArrayRef<T> drop_back(unsigned N = 1) const {
177 assert(size() >= N && "Dropping more elements than exist");
178 return slice(0, size() - N);
182 /// @name Operator Overloads
184 const T &operator[](size_t Index) const {
185 assert(Index < Length && "Invalid index!");
190 /// @name Expensive Operations
192 std::vector<T> vec() const {
193 return std::vector<T>(Data, Data+Length);
197 /// @name Conversion operators
199 operator std::vector<T>() const {
200 return std::vector<T>(Data, Data+Length);
206 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
207 /// elements consecutively in memory), i.e. a start pointer and a length. It
208 /// allows various APIs to take and modify consecutive elements easily and
211 /// This class does not own the underlying data, it is expected to be used in
212 /// situations where the data resides in some other buffer, whose lifetime
213 /// extends past that of the MutableArrayRef. For this reason, it is not in
214 /// general safe to store a MutableArrayRef.
216 /// This is intended to be trivially copyable, so it should be passed by
219 class MutableArrayRef : public ArrayRef<T> {
223 typedef std::reverse_iterator<iterator> reverse_iterator;
225 /// Construct an empty MutableArrayRef.
226 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
228 /// Construct an empty MutableArrayRef from None.
229 /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
231 /// Construct an MutableArrayRef from a single element.
232 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
234 /// Construct an MutableArrayRef from a pointer and length.
235 /*implicit*/ MutableArrayRef(T *data, size_t length)
236 : ArrayRef<T>(data, length) {}
238 /// Construct an MutableArrayRef from a range.
239 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
241 /// Construct an MutableArrayRef from a SmallVector.
242 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
243 : ArrayRef<T>(Vec) {}
245 /// Construct a MutableArrayRef from a std::vector.
246 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
247 : ArrayRef<T>(Vec) {}
249 /// Construct an MutableArrayRef from a C array.
251 /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
252 : ArrayRef<T>(Arr) {}
254 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
256 iterator begin() const { return data(); }
257 iterator end() const { return data() + this->size(); }
259 reverse_iterator rbegin() const { return reverse_iterator(end()); }
260 reverse_iterator rend() const { return reverse_iterator(begin()); }
262 /// front - Get the first element.
264 assert(!this->empty());
268 /// back - Get the last element.
270 assert(!this->empty());
271 return data()[this->size()-1];
274 /// slice(n) - Chop off the first N elements of the array.
275 MutableArrayRef<T> slice(unsigned N) const {
276 assert(N <= this->size() && "Invalid specifier");
277 return MutableArrayRef<T>(data()+N, this->size()-N);
280 /// slice(n, m) - Chop off the first N elements of the array, and keep M
281 /// elements in the array.
282 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
283 assert(N+M <= this->size() && "Invalid specifier");
284 return MutableArrayRef<T>(data()+N, M);
287 MutableArrayRef<T> drop_back(unsigned N) const {
288 assert(this->size() >= N && "Dropping more elements than exist");
289 return slice(0, this->size() - N);
293 /// @name Operator Overloads
295 T &operator[](size_t Index) const {
296 assert(Index < this->size() && "Invalid index!");
297 return data()[Index];
301 /// @name ArrayRef Convenience constructors
304 /// Construct an ArrayRef from a single element.
306 ArrayRef<T> makeArrayRef(const T &OneElt) {
310 /// Construct an ArrayRef from a pointer and length.
312 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
313 return ArrayRef<T>(data, length);
316 /// Construct an ArrayRef from a range.
318 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
319 return ArrayRef<T>(begin, end);
322 /// Construct an ArrayRef from a SmallVector.
323 template <typename T>
324 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
328 /// Construct an ArrayRef from a SmallVector.
329 template <typename T, unsigned N>
330 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
334 /// Construct an ArrayRef from a std::vector.
336 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
340 /// Construct an ArrayRef from an ArrayRef (no-op) (const)
341 template <typename T> ArrayRef<T> makeArrayRef(const ArrayRef<T> &Vec) {
345 /// Construct an ArrayRef from an ArrayRef (no-op)
346 template <typename T> ArrayRef<T> &makeArrayRef(ArrayRef<T> &Vec) {
350 /// Construct an ArrayRef from a C array.
351 template<typename T, size_t N>
352 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
353 return ArrayRef<T>(Arr);
357 /// @name ArrayRef Comparison Operators
361 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
362 return LHS.equals(RHS);
366 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
367 return !(LHS == RHS);
372 // ArrayRefs can be treated like a POD type.
373 template <typename T> struct isPodLike;
374 template <typename T> struct isPodLike<ArrayRef<T> > {
375 static const bool value = true;