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;
37 /// The start of the array, in an external buffer.
40 /// The number of elements.
44 /// @name Constructors
47 /// Construct an empty ArrayRef.
48 /*implicit*/ ArrayRef() : Data(0), Length(0) {}
50 /// Construct an ArrayRef from a single element.
51 /*implicit*/ ArrayRef(const T &OneElt)
52 : Data(&OneElt), Length(1) {}
54 /// Construct an ArrayRef from a pointer and length.
55 /*implicit*/ ArrayRef(const T *data, size_t length)
56 : Data(data), Length(length) {}
58 /// Construct an ArrayRef from a range.
59 ArrayRef(const T *begin, const T *end)
60 : Data(begin), Length(end - begin) {}
62 /// Construct an ArrayRef from a SmallVector.
63 /*implicit*/ ArrayRef(const SmallVectorImpl<T> &Vec)
64 : Data(Vec.data()), Length(Vec.size()) {}
66 /// Construct an ArrayRef from a std::vector.
67 /*implicit*/ ArrayRef(const std::vector<T> &Vec)
68 : Data(Vec.empty() ? (T*)0 : &Vec[0]), Length(Vec.size()) {}
70 /// Construct an ArrayRef from a C array.
72 /*implicit*/ ArrayRef(const T (&Arr)[N])
73 : Data(Arr), Length(N) {}
76 /// @name Simple Operations
79 iterator begin() const { return Data; }
80 iterator end() const { return Data + Length; }
82 /// empty - Check if the array is empty.
83 bool empty() const { return Length == 0; }
85 const T *data() const { return Data; }
87 /// size - Get the array size.
88 size_t size() const { return Length; }
90 /// front - Get the first element.
91 const T &front() const {
96 /// back - Get the last element.
97 const T &back() const {
99 return Data[Length-1];
102 /// equals - Check for element-wise equality.
103 bool equals(ArrayRef RHS) const {
104 if (Length != RHS.Length)
106 for (size_type i = 0; i != Length; i++)
107 if (Data[i] != RHS.Data[i])
112 /// slice(n) - Chop off the first N elements of the array.
113 ArrayRef<T> slice(unsigned N) const {
114 assert(N <= size() && "Invalid specifier");
115 return ArrayRef<T>(data()+N, size()-N);
118 /// slice(n, m) - Chop off the first N elements of the array, and keep M
119 /// elements in the array.
120 ArrayRef<T> slice(unsigned N, unsigned M) const {
121 assert(N+M <= size() && "Invalid specifier");
122 return ArrayRef<T>(data()+N, M);
126 /// @name Operator Overloads
128 const T &operator[](size_t Index) const {
129 assert(Index < Length && "Invalid index!");
134 /// @name Expensive Operations
136 std::vector<T> vec() const {
137 return std::vector<T>(Data, Data+Length);
141 /// @name Conversion operators
143 operator std::vector<T>() const {
144 return std::vector<T>(Data, Data+Length);
150 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
151 /// elements consecutively in memory), i.e. a start pointer and a length. It
152 /// allows various APIs to take and modify consecutive elements easily and
155 /// This class does not own the underlying data, it is expected to be used in
156 /// situations where the data resides in some other buffer, whose lifetime
157 /// extends past that of the MutableArrayRef. For this reason, it is not in
158 /// general safe to store a MutableArrayRef.
160 /// This is intended to be trivially copyable, so it should be passed by
163 class MutableArrayRef : public ArrayRef<T> {
167 /// Construct an empty ArrayRef.
168 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
170 /// Construct an MutableArrayRef from a single element.
171 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
173 /// Construct an MutableArrayRef from a pointer and length.
174 /*implicit*/ MutableArrayRef(T *data, size_t length)
175 : ArrayRef<T>(data, length) {}
177 /// Construct an MutableArrayRef from a range.
178 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
180 /// Construct an MutableArrayRef from a SmallVector.
181 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
182 : ArrayRef<T>(Vec) {}
184 /// Construct a MutableArrayRef from a std::vector.
185 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
186 : ArrayRef<T>(Vec) {}
188 /// Construct an MutableArrayRef from a C array.
190 /*implicit*/ MutableArrayRef(T (&Arr)[N])
191 : ArrayRef<T>(Arr) {}
193 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
195 iterator begin() const { return data(); }
196 iterator end() const { return data() + this->size(); }
198 /// front - Get the first element.
200 assert(!this->empty());
204 /// back - Get the last element.
206 assert(!this->empty());
207 return data()[this->size()-1];
210 /// slice(n) - Chop off the first N elements of the array.
211 MutableArrayRef<T> slice(unsigned N) const {
212 assert(N <= this->size() && "Invalid specifier");
213 return MutableArrayRef<T>(data()+N, this->size()-N);
216 /// slice(n, m) - Chop off the first N elements of the array, and keep M
217 /// elements in the array.
218 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
219 assert(N+M <= this->size() && "Invalid specifier");
220 return MutableArrayRef<T>(data()+N, M);
224 /// @name Operator Overloads
226 T &operator[](size_t Index) const {
227 assert(Index < this->size() && "Invalid index!");
228 return data()[Index];
232 /// @name ArrayRef Convenience constructors
235 /// Construct an ArrayRef from a single element.
237 ArrayRef<T> makeArrayRef(const T &OneElt) {
241 /// Construct an ArrayRef from a pointer and length.
243 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
244 return ArrayRef<T>(data, length);
247 /// Construct an ArrayRef from a range.
249 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
250 return ArrayRef<T>(begin, end);
253 /// Construct an ArrayRef from a SmallVector.
254 template <typename T>
255 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
259 /// Construct an ArrayRef from a SmallVector.
260 template <typename T, unsigned N>
261 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
265 /// Construct an ArrayRef from a std::vector.
267 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
271 /// Construct an ArrayRef from a C array.
272 template<typename T, size_t N>
273 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
274 return ArrayRef<T>(Arr);
278 /// @name ArrayRef Comparison Operators
282 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
283 return LHS.equals(RHS);
287 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
288 return !(LHS == RHS);
293 // ArrayRefs can be treated like a POD type.
294 template <typename T> struct isPodLike;
295 template <typename T> struct isPodLike<ArrayRef<T> > {
296 static const bool value = true;