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