blob: e7203ae461138e158ac9169780024932b451520b [file] [log] [blame]
Brian Silvermanf7bd1c22015-12-24 16:07:11 -08001//===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_ADT_ARRAYREF_H
11#define LLVM_ADT_ARRAYREF_H
12
13#include "llvm/None.h"
14#include "llvm/SmallVector.h"
15#include <vector>
16
17#ifndef LLVM_CONSTEXPR
18# ifdef _MSC_VER
19# if _MSC_VER >= 1900
20# define LLVM_CONSTEXPR constexpr
21# else
22# define LLVM_CONSTEXPR
23# endif
24# elif defined(__has_feature)
25# if __has_feature(cxx_constexpr)
26# define LLVM_CONSTEXPR constexpr
27# else
28# define LLVM_CONSTEXPR
29# endif
30# elif defined(__GXX_EXPERIMENTAL_CXX0X__)
31# define LLVM_CONSTEXPR constexpr
32# elif defined(__has_constexpr)
33# define LLVM_CONSTEXPR constexpr
34# else
35# define LLVM_CONSTEXPR
36# endif
37# define DEFINED_LLVM_CONSTEXPR
38#endif
39
40namespace llvm {
41
42 /// ArrayRef - Represent a constant reference to an array (0 or more elements
43 /// consecutively in memory), i.e. a start pointer and a length. It allows
44 /// various APIs to take consecutive elements easily and conveniently.
45 ///
46 /// This class does not own the underlying data, it is expected to be used in
47 /// situations where the data resides in some other buffer, whose lifetime
48 /// extends past that of the ArrayRef. For this reason, it is not in general
49 /// safe to store an ArrayRef.
50 ///
51 /// This is intended to be trivially copyable, so it should be passed by
52 /// value.
53 template<typename T>
54 class ArrayRef {
55 public:
56 typedef const T *iterator;
57 typedef const T *const_iterator;
58 typedef size_t size_type;
59
60 typedef std::reverse_iterator<iterator> reverse_iterator;
61
62 private:
63 /// The start of the array, in an external buffer.
64 const T *Data;
65
66 /// The number of elements.
67 size_type Length;
68
69 public:
70 /// @name Constructors
71 /// @{
72
73 /// Construct an empty ArrayRef.
74 /*implicit*/ ArrayRef() : Data(nullptr), Length(0) {}
75
76 /// Construct an empty ArrayRef from None.
77 /*implicit*/ ArrayRef(NoneType) : Data(nullptr), Length(0) {}
78
79 /// Construct an ArrayRef from a single element.
80 /*implicit*/ ArrayRef(const T &OneElt)
81 : Data(&OneElt), Length(1) {}
82
83 /// Construct an ArrayRef from a pointer and length.
84 /*implicit*/ ArrayRef(const T *data, size_t length)
85 : Data(data), Length(length) {}
86
87 /// Construct an ArrayRef from a range.
88 ArrayRef(const T *begin, const T *end)
89 : Data(begin), Length(end - begin) {}
90
91 /// Construct an ArrayRef from a SmallVector. This is templated in order to
92 /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
93 /// copy-construct an ArrayRef.
94 template<typename U>
95 /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
96 : Data(Vec.data()), Length(Vec.size()) {
97 }
98
99 /// Construct an ArrayRef from a std::vector.
100 template<typename A>
101 /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
102 : Data(Vec.data()), Length(Vec.size()) {}
103
104 /// Construct an ArrayRef from a C array.
105 template <size_t N>
106 /*implicit*/ LLVM_CONSTEXPR ArrayRef(const T (&Arr)[N])
107 : Data(Arr), Length(N) {}
108
109 /// Construct an ArrayRef from a std::initializer_list.
110 /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
111 : Data(Vec.begin() == Vec.end() ? (T*)0 : Vec.begin()),
112 Length(Vec.size()) {}
113
114 /// Construct an ArrayRef<const T*> from ArrayRef<T*>. This uses SFINAE to
115 /// ensure that only ArrayRefs of pointers can be converted.
116 template <typename U>
117 ArrayRef(const ArrayRef<U *> &A,
118 typename std::enable_if<
119 std::is_convertible<U *const *, T const *>::value>::type* = 0)
120 : Data(A.data()), Length(A.size()) {}
121
122 /// Construct an ArrayRef<const T*> from a SmallVector<T*>. This is
123 /// templated in order to avoid instantiating SmallVectorTemplateCommon<T>
124 /// whenever we copy-construct an ArrayRef.
125 template<typename U, typename DummyT>
126 /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<U*, DummyT> &Vec,
127 typename std::enable_if<
128 std::is_convertible<U *const *,
129 T const *>::value>::type* = 0)
130 : Data(Vec.data()), Length(Vec.size()) {
131 }
132
133 /// Construct an ArrayRef<const T*> from std::vector<T*>. This uses SFINAE
134 /// to ensure that only vectors of pointers can be converted.
135 template<typename U, typename A>
136 ArrayRef(const std::vector<U *, A> &Vec,
137 typename std::enable_if<
138 std::is_convertible<U *const *, T const *>::value>::type* = 0)
139 : Data(Vec.data()), Length(Vec.size()) {}
140
141 /// @}
142 /// @name Simple Operations
143 /// @{
144
145 iterator begin() const { return Data; }
146 iterator end() const { return Data + Length; }
147
148 reverse_iterator rbegin() const { return reverse_iterator(end()); }
149 reverse_iterator rend() const { return reverse_iterator(begin()); }
150
151 /// empty - Check if the array is empty.
152 bool empty() const { return Length == 0; }
153
154 const T *data() const { return Data; }
155
156 /// size - Get the array size.
157 size_t size() const { return Length; }
158
159 /// front - Get the first element.
160 const T &front() const {
161 assert(!empty());
162 return Data[0];
163 }
164
165 /// back - Get the last element.
166 const T &back() const {
167 assert(!empty());
168 return Data[Length-1];
169 }
170
171 // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
172 template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
173 T *Buff = A.template Allocate<T>(Length);
174 std::copy(begin(), end(), Buff);
175 return ArrayRef<T>(Buff, Length);
176 }
177
178 /// equals - Check for element-wise equality.
179 bool equals(ArrayRef RHS) const {
180 if (Length != RHS.Length)
181 return false;
182 if (Length == 0)
183 return true;
184 return std::equal(begin(), end(), RHS.begin());
185 }
186
187 /// slice(n) - Chop off the first N elements of the array.
188 ArrayRef<T> slice(unsigned N) const {
189 assert(N <= size() && "Invalid specifier");
190 return ArrayRef<T>(data()+N, size()-N);
191 }
192
193 /// slice(n, m) - Chop off the first N elements of the array, and keep M
194 /// elements in the array.
195 ArrayRef<T> slice(unsigned N, unsigned M) const {
196 assert(N+M <= size() && "Invalid specifier");
197 return ArrayRef<T>(data()+N, M);
198 }
199
200 // \brief Drop the last \p N elements of the array.
201 ArrayRef<T> drop_back(unsigned N = 1) const {
202 assert(size() >= N && "Dropping more elements than exist");
203 return slice(0, size() - N);
204 }
205
206 /// @}
207 /// @name Operator Overloads
208 /// @{
209 const T &operator[](size_t Index) const {
210 assert(Index < Length && "Invalid index!");
211 return Data[Index];
212 }
213
214 /// @}
215 /// @name Expensive Operations
216 /// @{
217 std::vector<T> vec() const {
218 return std::vector<T>(Data, Data+Length);
219 }
220
221 /// @}
222 /// @name Conversion operators
223 /// @{
224 operator std::vector<T>() const {
225 return std::vector<T>(Data, Data+Length);
226 }
227
228 /// @}
229 };
230
231 /// MutableArrayRef - Represent a mutable reference to an array (0 or more
232 /// elements consecutively in memory), i.e. a start pointer and a length. It
233 /// allows various APIs to take and modify consecutive elements easily and
234 /// conveniently.
235 ///
236 /// This class does not own the underlying data, it is expected to be used in
237 /// situations where the data resides in some other buffer, whose lifetime
238 /// extends past that of the MutableArrayRef. For this reason, it is not in
239 /// general safe to store a MutableArrayRef.
240 ///
241 /// This is intended to be trivially copyable, so it should be passed by
242 /// value.
243 template<typename T>
244 class MutableArrayRef : public ArrayRef<T> {
245 public:
246 typedef T *iterator;
247
248 typedef std::reverse_iterator<iterator> reverse_iterator;
249
250 /// Construct an empty MutableArrayRef.
251 /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
252
253 /// Construct an empty MutableArrayRef from None.
254 /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
255
256 /// Construct an MutableArrayRef from a single element.
257 /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
258
259 /// Construct an MutableArrayRef from a pointer and length.
260 /*implicit*/ MutableArrayRef(T *data, size_t length)
261 : ArrayRef<T>(data, length) {}
262
263 /// Construct an MutableArrayRef from a range.
264 MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
265
266 /// Construct an MutableArrayRef from a SmallVector.
267 /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
268 : ArrayRef<T>(Vec) {}
269
270 /// Construct a MutableArrayRef from a std::vector.
271 /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
272 : ArrayRef<T>(Vec) {}
273
274 /// Construct an MutableArrayRef from a C array.
275 template <size_t N>
276 /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
277 : ArrayRef<T>(Arr) {}
278
279 T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
280
281 iterator begin() const { return data(); }
282 iterator end() const { return data() + this->size(); }
283
284 reverse_iterator rbegin() const { return reverse_iterator(end()); }
285 reverse_iterator rend() const { return reverse_iterator(begin()); }
286
287 /// front - Get the first element.
288 T &front() const {
289 assert(!this->empty());
290 return data()[0];
291 }
292
293 /// back - Get the last element.
294 T &back() const {
295 assert(!this->empty());
296 return data()[this->size()-1];
297 }
298
299 /// slice(n) - Chop off the first N elements of the array.
300 MutableArrayRef<T> slice(unsigned N) const {
301 assert(N <= this->size() && "Invalid specifier");
302 return MutableArrayRef<T>(data()+N, this->size()-N);
303 }
304
305 /// slice(n, m) - Chop off the first N elements of the array, and keep M
306 /// elements in the array.
307 MutableArrayRef<T> slice(unsigned N, unsigned M) const {
308 assert(N+M <= this->size() && "Invalid specifier");
309 return MutableArrayRef<T>(data()+N, M);
310 }
311
312 MutableArrayRef<T> drop_back(unsigned N) const {
313 assert(this->size() >= N && "Dropping more elements than exist");
314 return slice(0, this->size() - N);
315 }
316
317 /// @}
318 /// @name Operator Overloads
319 /// @{
320 T &operator[](size_t Index) const {
321 assert(Index < this->size() && "Invalid index!");
322 return data()[Index];
323 }
324 };
325
326 /// @name ArrayRef Convenience constructors
327 /// @{
328
329 /// Construct an ArrayRef from a single element.
330 template<typename T>
331 ArrayRef<T> makeArrayRef(const T &OneElt) {
332 return OneElt;
333 }
334
335 /// Construct an ArrayRef from a pointer and length.
336 template<typename T>
337 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
338 return ArrayRef<T>(data, length);
339 }
340
341 /// Construct an ArrayRef from a range.
342 template<typename T>
343 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
344 return ArrayRef<T>(begin, end);
345 }
346
347 /// Construct an ArrayRef from a SmallVector.
348 template <typename T>
349 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
350 return Vec;
351 }
352
353 /// Construct an ArrayRef from a SmallVector.
354 template <typename T, unsigned N>
355 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
356 return Vec;
357 }
358
359 /// Construct an ArrayRef from a std::vector.
360 template<typename T>
361 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
362 return Vec;
363 }
364
365 /// Construct an ArrayRef from a C array.
366 template<typename T, size_t N>
367 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
368 return ArrayRef<T>(Arr);
369 }
370
371 /// @}
372 /// @name ArrayRef Comparison Operators
373 /// @{
374
375 template<typename T>
376 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
377 return LHS.equals(RHS);
378 }
379
380 template<typename T>
381 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
382 return !(LHS == RHS);
383 }
384
385 /// @}
386
387 // ArrayRefs can be treated like a POD type.
388 template <typename T> struct isPodLike;
389 template <typename T> struct isPodLike<ArrayRef<T> > {
390 static const bool value = true;
391 };
392} // namespace llvm
393
394#ifdef DEFINED_LLVM_CONSTEXPR
395# undef DEFINED_LLVM_CONSTEXPR
396# undef LLVM_CONSTEXPR
397#endif
398
399#endif