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(0), Length(0) {}
53 /// Construct an empty ArrayRef from None.
54 /*implicit*/ ArrayRef(NoneType) : Data(0), 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.empty() ? (T*)0 : &Vec[0]), 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 #if LLVM_HAS_INITIALIZER_LISTS
87 /// Construct an ArrayRef from a std::initializer_list.
88 /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
89 : Data(Vec.begin() == Vec.end() ? (T*)0 : Vec.begin()),
94 /// @name Simple Operations
97 iterator begin() const { return Data; }
98 iterator end() const { return Data + Length; }
100 reverse_iterator rbegin() const { return reverse_iterator(end()); }
101 reverse_iterator rend() const { return reverse_iterator(begin()); }
103 /// empty - Check if the array is empty.
104 bool empty() const { return Length == 0; }
106 const T *data() const { return Data; }
108 /// size - Get the array size.
109 size_t size() const { return Length; }
111 /// front - Get the first element.
112 const T &front() const {
117 /// back - Get the last element.
118 const T &back() const {
120 return Data[Length-1];
123 /// equals - Check for element-wise equality.
124 bool equals(ArrayRef RHS) const {
125 if (Length != RHS.Length)
127 for (size_type i = 0; i != Length; i++)
128 if (Data[i] != RHS.Data[i])
133 /// slice(n) - Chop off the first N elements of the array.
134 ArrayRef<T> slice(unsigned N) const {
135 assert(N <= size() && "Invalid specifier");
136 return ArrayRef<T>(data()+N, size()-N);
139 /// slice(n, m) - Chop off the first N elements of the array, and keep M
140 /// elements in the array.
141 ArrayRef<T> slice(unsigned N, unsigned M) const {
142 assert(N+M <= size() && "Invalid specifier");
143 return ArrayRef<T>(data()+N, M);
147 /// @name Operator Overloads
149 const T &operator[](size_t Index) const {
150 assert(Index < Length && "Invalid index!");
155 /// @name Expensive Operations
157 std::vector<T> vec() const {
158 return std::vector<T>(Data, Data+Length);
162 /// @name Conversion operators
164 operator std::vector<T>() const {
165 return std::vector<T>(Data, Data+Length);
171 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
172 /// elements consecutively in memory), i.e. a start pointer and a length. It
173 /// allows various APIs to take and modify consecutive elements easily and
176 /// This class does not own the underlying data, it is expected to be used in
177 /// situations where the data resides in some other buffer, whose lifetime
178 /// extends past that of the MutableArrayRef. For this reason, it is not in
179 /// general safe to store a MutableArrayRef.
181 /// This is intended to be trivially copyable, so it should be passed by
184 class MutableArrayRef : public ArrayRef<T> {
188 typedef std::reverse_iterator<iterator> reverse_iterator;
190 /// Construct an empty MutableArrayRef.
191 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
193 /// Construct an empty MutableArrayRef from None.
194 /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
196 /// Construct an MutableArrayRef from a single element.
197 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
199 /// Construct an MutableArrayRef from a pointer and length.
200 /*implicit*/ MutableArrayRef(T *data, size_t length)
201 : ArrayRef<T>(data, length) {}
203 /// Construct an MutableArrayRef from a range.
204 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
206 /// Construct an MutableArrayRef from a SmallVector.
207 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
208 : ArrayRef<T>(Vec) {}
210 /// Construct a MutableArrayRef from a std::vector.
211 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
212 : ArrayRef<T>(Vec) {}
214 /// Construct an MutableArrayRef from a C array.
216 /*implicit*/ MutableArrayRef(T (&Arr)[N])
217 : ArrayRef<T>(Arr) {}
219 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
221 iterator begin() const { return data(); }
222 iterator end() const { return data() + this->size(); }
224 reverse_iterator rbegin() const { return reverse_iterator(end()); }
225 reverse_iterator rend() const { return reverse_iterator(begin()); }
227 /// front - Get the first element.
229 assert(!this->empty());
233 /// back - Get the last element.
235 assert(!this->empty());
236 return data()[this->size()-1];
239 /// slice(n) - Chop off the first N elements of the array.
240 MutableArrayRef<T> slice(unsigned N) const {
241 assert(N <= this->size() && "Invalid specifier");
242 return MutableArrayRef<T>(data()+N, this->size()-N);
245 /// slice(n, m) - Chop off the first N elements of the array, and keep M
246 /// elements in the array.
247 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
248 assert(N+M <= this->size() && "Invalid specifier");
249 return MutableArrayRef<T>(data()+N, M);
253 /// @name Operator Overloads
255 T &operator[](size_t Index) const {
256 assert(Index < this->size() && "Invalid index!");
257 return data()[Index];
261 /// @name ArrayRef Convenience constructors
264 /// Construct an ArrayRef from a single element.
266 ArrayRef<T> makeArrayRef(const T &OneElt) {
270 /// Construct an ArrayRef from a pointer and length.
272 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
273 return ArrayRef<T>(data, length);
276 /// Construct an ArrayRef from a range.
278 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
279 return ArrayRef<T>(begin, end);
282 /// Construct an ArrayRef from a SmallVector.
283 template <typename T>
284 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
288 /// Construct an ArrayRef from a SmallVector.
289 template <typename T, unsigned N>
290 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
294 /// Construct an ArrayRef from a std::vector.
296 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
300 /// Construct an ArrayRef from a C array.
301 template<typename T, size_t N>
302 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
303 return ArrayRef<T>(Arr);
307 /// @name ArrayRef Comparison Operators
311 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
312 return LHS.equals(RHS);
316 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
317 return !(LHS == RHS);
322 // ArrayRefs can be treated like a POD type.
323 template <typename T> struct isPodLike;
324 template <typename T> struct isPodLike<ArrayRef<T> > {
325 static const bool value = true;