[ADT] Fix a confusing interface spec and some annoying peculiarities
[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*)nullptr : 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     /// Construct an ArrayRef<const T*> from a SmallVector<T*>. This is
100     /// templated in order to avoid instantiating SmallVectorTemplateCommon<T>
101     /// whenever we copy-construct an ArrayRef.
102     template<typename U, typename DummyT>
103     /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<U*, DummyT> &Vec,
104                           typename std::enable_if<
105                               std::is_convertible<U *const *,
106                                                   T const *>::value>::type* = 0)
107       : Data(Vec.data()), Length(Vec.size()) {
108     }
109
110     /// Construct an ArrayRef<const T*> from std::vector<T*>. This uses SFINAE
111     /// to ensure that only vectors of pointers can be converted.
112     template<typename U, typename A>
113     ArrayRef(const std::vector<U *, A> &Vec,
114              typename std::enable_if<
115                  std::is_convertible<U *const *, T const *>::value>::type* = 0)
116       : Data(Vec.data()), Length(Vec.size()) {}
117
118     /// @}
119     /// @name Simple Operations
120     /// @{
121
122     iterator begin() const { return Data; }
123     iterator end() const { return Data + Length; }
124
125     reverse_iterator rbegin() const { return reverse_iterator(end()); }
126     reverse_iterator rend() const { return reverse_iterator(begin()); }
127
128     /// empty - Check if the array is empty.
129     bool empty() const { return Length == 0; }
130
131     const T *data() const { return Data; }
132
133     /// size - Get the array size.
134     size_t size() const { return Length; }
135
136     /// front - Get the first element.
137     const T &front() const {
138       assert(!empty());
139       return Data[0];
140     }
141
142     /// back - Get the last element.
143     const T &back() const {
144       assert(!empty());
145       return Data[Length-1];
146     }
147
148     // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
149     template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
150       T *Buff = A.template Allocate<T>(Length);
151       std::uninitialized_copy(begin(), end(), Buff);
152       return ArrayRef<T>(Buff, Length);
153     }
154
155     /// equals - Check for element-wise equality.
156     bool equals(ArrayRef RHS) const {
157       if (Length != RHS.Length)
158         return false;
159       return std::equal(begin(), end(), RHS.begin());
160     }
161
162     /// slice(n) - Chop off the first N elements of the array.
163     ArrayRef<T> slice(unsigned N) const {
164       assert(N <= size() && "Invalid specifier");
165       return ArrayRef<T>(data()+N, size()-N);
166     }
167
168     /// slice(n, m) - Chop off the first N elements of the array, and keep M
169     /// elements in the array.
170     ArrayRef<T> slice(unsigned N, unsigned M) const {
171       assert(N+M <= size() && "Invalid specifier");
172       return ArrayRef<T>(data()+N, M);
173     }
174
175     // \brief Drop the last \p N elements of the array.
176     ArrayRef<T> drop_back(unsigned N = 1) const {
177       assert(size() >= N && "Dropping more elements than exist");
178       return slice(0, size() - N);
179     }
180
181     /// @}
182     /// @name Operator Overloads
183     /// @{
184     const T &operator[](size_t Index) const {
185       assert(Index < Length && "Invalid index!");
186       return Data[Index];
187     }
188
189     /// @}
190     /// @name Expensive Operations
191     /// @{
192     std::vector<T> vec() const {
193       return std::vector<T>(Data, Data+Length);
194     }
195
196     /// @}
197     /// @name Conversion operators
198     /// @{
199     operator std::vector<T>() const {
200       return std::vector<T>(Data, Data+Length);
201     }
202
203     /// @}
204   };
205
206   /// MutableArrayRef - Represent a mutable reference to an array (0 or more
207   /// elements consecutively in memory), i.e. a start pointer and a length.  It
208   /// allows various APIs to take and modify consecutive elements easily and
209   /// conveniently.
210   ///
211   /// This class does not own the underlying data, it is expected to be used in
212   /// situations where the data resides in some other buffer, whose lifetime
213   /// extends past that of the MutableArrayRef. For this reason, it is not in
214   /// general safe to store a MutableArrayRef.
215   ///
216   /// This is intended to be trivially copyable, so it should be passed by
217   /// value.
218   template<typename T>
219   class MutableArrayRef : public ArrayRef<T> {
220   public:
221     typedef T *iterator;
222
223     typedef std::reverse_iterator<iterator> reverse_iterator;
224
225     /// Construct an empty MutableArrayRef.
226     /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
227
228     /// Construct an empty MutableArrayRef from None.
229     /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
230
231     /// Construct an MutableArrayRef from a single element.
232     /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
233
234     /// Construct an MutableArrayRef from a pointer and length.
235     /*implicit*/ MutableArrayRef(T *data, size_t length)
236       : ArrayRef<T>(data, length) {}
237
238     /// Construct an MutableArrayRef from a range.
239     MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
240
241     /// Construct an MutableArrayRef from a SmallVector.
242     /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
243     : ArrayRef<T>(Vec) {}
244
245     /// Construct a MutableArrayRef from a std::vector.
246     /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
247     : ArrayRef<T>(Vec) {}
248
249     /// Construct an MutableArrayRef from a C array.
250     template <size_t N>
251     /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
252       : ArrayRef<T>(Arr) {}
253
254     T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
255
256     iterator begin() const { return data(); }
257     iterator end() const { return data() + this->size(); }
258
259     reverse_iterator rbegin() const { return reverse_iterator(end()); }
260     reverse_iterator rend() const { return reverse_iterator(begin()); }
261
262     /// front - Get the first element.
263     T &front() const {
264       assert(!this->empty());
265       return data()[0];
266     }
267
268     /// back - Get the last element.
269     T &back() const {
270       assert(!this->empty());
271       return data()[this->size()-1];
272     }
273
274     /// slice(n) - Chop off the first N elements of the array.
275     MutableArrayRef<T> slice(unsigned N) const {
276       assert(N <= this->size() && "Invalid specifier");
277       return MutableArrayRef<T>(data()+N, this->size()-N);
278     }
279
280     /// slice(n, m) - Chop off the first N elements of the array, and keep M
281     /// elements in the array.
282     MutableArrayRef<T> slice(unsigned N, unsigned M) const {
283       assert(N+M <= this->size() && "Invalid specifier");
284       return MutableArrayRef<T>(data()+N, M);
285     }
286
287     MutableArrayRef<T> drop_back(unsigned N) const {
288       assert(this->size() >= N && "Dropping more elements than exist");
289       return slice(0, this->size() - N);
290     }
291
292     /// @}
293     /// @name Operator Overloads
294     /// @{
295     T &operator[](size_t Index) const {
296       assert(Index < this->size() && "Invalid index!");
297       return data()[Index];
298     }
299   };
300
301   /// @name ArrayRef Convenience constructors
302   /// @{
303
304   /// Construct an ArrayRef from a single element.
305   template<typename T>
306   ArrayRef<T> makeArrayRef(const T &OneElt) {
307     return OneElt;
308   }
309
310   /// Construct an ArrayRef from a pointer and length.
311   template<typename T>
312   ArrayRef<T> makeArrayRef(const T *data, size_t length) {
313     return ArrayRef<T>(data, length);
314   }
315
316   /// Construct an ArrayRef from a range.
317   template<typename T>
318   ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
319     return ArrayRef<T>(begin, end);
320   }
321
322   /// Construct an ArrayRef from a SmallVector.
323   template <typename T>
324   ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
325     return Vec;
326   }
327
328   /// Construct an ArrayRef from a SmallVector.
329   template <typename T, unsigned N>
330   ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
331     return Vec;
332   }
333
334   /// Construct an ArrayRef from a std::vector.
335   template<typename T>
336   ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
337     return Vec;
338   }
339
340   /// Construct an ArrayRef from an ArrayRef (no-op) (const)
341   template <typename T> ArrayRef<T> makeArrayRef(const ArrayRef<T> &Vec) {
342     return Vec;
343   }
344
345   /// Construct an ArrayRef from an ArrayRef (no-op)
346   template <typename T> ArrayRef<T> &makeArrayRef(ArrayRef<T> &Vec) {
347     return Vec;
348   }
349
350   /// Construct an ArrayRef from a C array.
351   template<typename T, size_t N>
352   ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
353     return ArrayRef<T>(Arr);
354   }
355
356   /// @}
357   /// @name ArrayRef Comparison Operators
358   /// @{
359
360   template<typename T>
361   inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
362     return LHS.equals(RHS);
363   }
364
365   template<typename T>
366   inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
367     return !(LHS == RHS);
368   }
369
370   /// @}
371
372   // ArrayRefs can be treated like a POD type.
373   template <typename T> struct isPodLike;
374   template <typename T> struct isPodLike<ArrayRef<T> > {
375     static const bool value = true;
376   };
377 }
378
379 #endif