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/SmallVector.h"
18 /// ArrayRef - Represent a constant reference to an array (0 or more elements
19 /// consecutively in memory), i.e. a start pointer and a length. It allows
20 /// various APIs to take consecutive elements easily and conveniently.
22 /// This class does not own the underlying data, it is expected to be used in
23 /// situations where the data resides in some other buffer, whose lifetime
24 /// extends past that of the ArrayRef. For this reason, it is not in general
25 /// safe to store an ArrayRef.
27 /// This is intended to be trivially copyable, so it should be passed by
32 typedef const T *iterator;
33 typedef const T *const_iterator;
34 typedef size_t size_type;
36 typedef std::reverse_iterator<iterator> reverse_iterator;
39 /// The start of the array, in an external buffer.
42 /// The number of elements.
46 /// @name Constructors
49 /// Construct an empty ArrayRef.
50 /*implicit*/ ArrayRef() : Data(0), Length(0) {}
52 /// Construct an ArrayRef from a single element.
53 /*implicit*/ ArrayRef(const T &OneElt)
54 : Data(&OneElt), Length(1) {}
56 /// Construct an ArrayRef from a pointer and length.
57 /*implicit*/ ArrayRef(const T *data, size_t length)
58 : Data(data), Length(length) {}
60 /// Construct an ArrayRef from a range.
61 ArrayRef(const T *begin, const T *end)
62 : Data(begin), Length(end - begin) {}
64 /// Construct an ArrayRef from a SmallVector. This is templated in order to
65 /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
66 /// copy-construct an ArrayRef.
68 /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
69 : Data(Vec.data()), Length(Vec.size()) {
72 /// Construct an ArrayRef from a std::vector.
74 /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
75 : Data(Vec.empty() ? (T*)0 : &Vec[0]), Length(Vec.size()) {}
77 /// Construct an ArrayRef from a C array.
79 /*implicit*/ ArrayRef(const T (&Arr)[N])
80 : Data(Arr), Length(N) {}
83 /// @name Simple Operations
86 iterator begin() const { return Data; }
87 iterator end() const { return Data + Length; }
89 reverse_iterator rbegin() const { return reverse_iterator(end()); }
90 reverse_iterator rend() const { return reverse_iterator(begin()); }
92 /// empty - Check if the array is empty.
93 bool empty() const { return Length == 0; }
95 const T *data() const { return Data; }
97 /// size - Get the array size.
98 size_t size() const { return Length; }
100 /// front - Get the first element.
101 const T &front() const {
106 /// back - Get the last element.
107 const T &back() const {
109 return Data[Length-1];
112 /// equals - Check for element-wise equality.
113 bool equals(ArrayRef RHS) const {
114 if (Length != RHS.Length)
116 for (size_type i = 0; i != Length; i++)
117 if (Data[i] != RHS.Data[i])
122 /// slice(n) - Chop off the first N elements of the array.
123 ArrayRef<T> slice(unsigned N) const {
124 assert(N <= size() && "Invalid specifier");
125 return ArrayRef<T>(data()+N, size()-N);
128 /// slice(n, m) - Chop off the first N elements of the array, and keep M
129 /// elements in the array.
130 ArrayRef<T> slice(unsigned N, unsigned M) const {
131 assert(N+M <= size() && "Invalid specifier");
132 return ArrayRef<T>(data()+N, M);
136 /// @name Operator Overloads
138 const T &operator[](size_t Index) const {
139 assert(Index < Length && "Invalid index!");
144 /// @name Expensive Operations
146 std::vector<T> vec() const {
147 return std::vector<T>(Data, Data+Length);
151 /// @name Conversion operators
153 operator std::vector<T>() const {
154 return std::vector<T>(Data, Data+Length);
160 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
161 /// elements consecutively in memory), i.e. a start pointer and a length. It
162 /// allows various APIs to take and modify consecutive elements easily and
165 /// This class does not own the underlying data, it is expected to be used in
166 /// situations where the data resides in some other buffer, whose lifetime
167 /// extends past that of the MutableArrayRef. For this reason, it is not in
168 /// general safe to store a MutableArrayRef.
170 /// This is intended to be trivially copyable, so it should be passed by
173 class MutableArrayRef : public ArrayRef<T> {
177 /// Construct an empty ArrayRef.
178 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
180 /// Construct an MutableArrayRef from a single element.
181 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
183 /// Construct an MutableArrayRef from a pointer and length.
184 /*implicit*/ MutableArrayRef(T *data, size_t length)
185 : ArrayRef<T>(data, length) {}
187 /// Construct an MutableArrayRef from a range.
188 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
190 /// Construct an MutableArrayRef from a SmallVector.
191 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
192 : ArrayRef<T>(Vec) {}
194 /// Construct a MutableArrayRef from a std::vector.
195 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
196 : ArrayRef<T>(Vec) {}
198 /// Construct an MutableArrayRef from a C array.
200 /*implicit*/ MutableArrayRef(T (&Arr)[N])
201 : ArrayRef<T>(Arr) {}
203 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
205 iterator begin() const { return data(); }
206 iterator end() const { return data() + this->size(); }
208 /// front - Get the first element.
210 assert(!this->empty());
214 /// back - Get the last element.
216 assert(!this->empty());
217 return data()[this->size()-1];
220 /// slice(n) - Chop off the first N elements of the array.
221 MutableArrayRef<T> slice(unsigned N) const {
222 assert(N <= this->size() && "Invalid specifier");
223 return MutableArrayRef<T>(data()+N, this->size()-N);
226 /// slice(n, m) - Chop off the first N elements of the array, and keep M
227 /// elements in the array.
228 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
229 assert(N+M <= this->size() && "Invalid specifier");
230 return MutableArrayRef<T>(data()+N, M);
234 /// @name Operator Overloads
236 T &operator[](size_t Index) const {
237 assert(Index < this->size() && "Invalid index!");
238 return data()[Index];
242 /// @name ArrayRef Convenience constructors
245 /// Construct an ArrayRef from a single element.
247 ArrayRef<T> makeArrayRef(const T &OneElt) {
251 /// Construct an ArrayRef from a pointer and length.
253 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
254 return ArrayRef<T>(data, length);
257 /// Construct an ArrayRef from a range.
259 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
260 return ArrayRef<T>(begin, end);
263 /// Construct an ArrayRef from a SmallVector.
264 template <typename T>
265 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
269 /// Construct an ArrayRef from a SmallVector.
270 template <typename T, unsigned N>
271 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
275 /// Construct an ArrayRef from a std::vector.
277 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
281 /// Construct an ArrayRef from a C array.
282 template<typename T, size_t N>
283 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
284 return ArrayRef<T>(Arr);
288 /// @name ArrayRef Comparison Operators
292 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
293 return LHS.equals(RHS);
297 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
298 return !(LHS == RHS);
303 // ArrayRefs can be treated like a POD type.
304 template <typename T> struct isPodLike;
305 template <typename T> struct isPodLike<ArrayRef<T> > {
306 static const bool value = true;