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*/ ArrayRef(const T (&Arr)[N])
84 : Data(Arr), Length(N) {}
87 /// @name Simple Operations
90 iterator begin() const { return Data; }
91 iterator end() const { return Data + Length; }
93 reverse_iterator rbegin() const { return reverse_iterator(end()); }
94 reverse_iterator rend() const { return reverse_iterator(begin()); }
96 /// empty - Check if the array is empty.
97 bool empty() const { return Length == 0; }
99 const T *data() const { return Data; }
101 /// size - Get the array size.
102 size_t size() const { return Length; }
104 /// front - Get the first element.
105 const T &front() const {
110 /// back - Get the last element.
111 const T &back() const {
113 return Data[Length-1];
116 /// equals - Check for element-wise equality.
117 bool equals(ArrayRef RHS) const {
118 if (Length != RHS.Length)
120 for (size_type i = 0; i != Length; i++)
121 if (Data[i] != RHS.Data[i])
126 /// slice(n) - Chop off the first N elements of the array.
127 ArrayRef<T> slice(unsigned N) const {
128 assert(N <= size() && "Invalid specifier");
129 return ArrayRef<T>(data()+N, size()-N);
132 /// slice(n, m) - Chop off the first N elements of the array, and keep M
133 /// elements in the array.
134 ArrayRef<T> slice(unsigned N, unsigned M) const {
135 assert(N+M <= size() && "Invalid specifier");
136 return ArrayRef<T>(data()+N, M);
140 /// @name Operator Overloads
142 const T &operator[](size_t Index) const {
143 assert(Index < Length && "Invalid index!");
148 /// @name Expensive Operations
150 std::vector<T> vec() const {
151 return std::vector<T>(Data, Data+Length);
155 /// @name Conversion operators
157 operator std::vector<T>() const {
158 return std::vector<T>(Data, Data+Length);
164 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
165 /// elements consecutively in memory), i.e. a start pointer and a length. It
166 /// allows various APIs to take and modify consecutive elements easily and
169 /// This class does not own the underlying data, it is expected to be used in
170 /// situations where the data resides in some other buffer, whose lifetime
171 /// extends past that of the MutableArrayRef. For this reason, it is not in
172 /// general safe to store a MutableArrayRef.
174 /// This is intended to be trivially copyable, so it should be passed by
177 class MutableArrayRef : public ArrayRef<T> {
181 /// Construct an empty MutableArrayRef.
182 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
184 /// Construct an empty MutableArrayRef from None.
185 /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
187 /// Construct an MutableArrayRef from a single element.
188 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
190 /// Construct an MutableArrayRef from a pointer and length.
191 /*implicit*/ MutableArrayRef(T *data, size_t length)
192 : ArrayRef<T>(data, length) {}
194 /// Construct an MutableArrayRef from a range.
195 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
197 /// Construct an MutableArrayRef from a SmallVector.
198 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
199 : ArrayRef<T>(Vec) {}
201 /// Construct a MutableArrayRef from a std::vector.
202 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
203 : ArrayRef<T>(Vec) {}
205 /// Construct an MutableArrayRef from a C array.
207 /*implicit*/ MutableArrayRef(T (&Arr)[N])
208 : ArrayRef<T>(Arr) {}
210 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
212 iterator begin() const { return data(); }
213 iterator end() const { return data() + this->size(); }
215 /// front - Get the first element.
217 assert(!this->empty());
221 /// back - Get the last element.
223 assert(!this->empty());
224 return data()[this->size()-1];
227 /// slice(n) - Chop off the first N elements of the array.
228 MutableArrayRef<T> slice(unsigned N) const {
229 assert(N <= this->size() && "Invalid specifier");
230 return MutableArrayRef<T>(data()+N, this->size()-N);
233 /// slice(n, m) - Chop off the first N elements of the array, and keep M
234 /// elements in the array.
235 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
236 assert(N+M <= this->size() && "Invalid specifier");
237 return MutableArrayRef<T>(data()+N, M);
241 /// @name Operator Overloads
243 T &operator[](size_t Index) const {
244 assert(Index < this->size() && "Invalid index!");
245 return data()[Index];
249 /// @name ArrayRef Convenience constructors
252 /// Construct an ArrayRef from a single element.
254 ArrayRef<T> makeArrayRef(const T &OneElt) {
258 /// Construct an ArrayRef from a pointer and length.
260 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
261 return ArrayRef<T>(data, length);
264 /// Construct an ArrayRef from a range.
266 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
267 return ArrayRef<T>(begin, end);
270 /// Construct an ArrayRef from a SmallVector.
271 template <typename T>
272 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
276 /// Construct an ArrayRef from a SmallVector.
277 template <typename T, unsigned N>
278 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
282 /// Construct an ArrayRef from a std::vector.
284 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
288 /// Construct an ArrayRef from a C array.
289 template<typename T, size_t N>
290 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
291 return ArrayRef<T>(Arr);
295 /// @name ArrayRef Comparison Operators
299 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
300 return LHS.equals(RHS);
304 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
305 return !(LHS == RHS);
310 // ArrayRefs can be treated like a POD type.
311 template <typename T> struct isPodLike;
312 template <typename T> struct isPodLike<ArrayRef<T> > {
313 static const bool value = true;