ArrayRef: Remove the equals helper with many arguments.
[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)
139         return false;
140       // Don't use std::equal(), since it asserts in MSVC on nullptr iterators.
141       for (auto L = begin(), LE = end(), R = RHS.begin(); L != LE; ++L, ++R)
142         // Match std::equal() in using == (instead of !=) to minimize API
143         // requirements of ArrayRef'ed types.
144         if (!(*L == *R))
145           return false;
146       return true;
147     }
148
149     /// slice(n) - Chop off the first N elements of the array.
150     ArrayRef<T> slice(unsigned N) const {
151       assert(N <= size() && "Invalid specifier");
152       return ArrayRef<T>(data()+N, size()-N);
153     }
154
155     /// slice(n, m) - Chop off the first N elements of the array, and keep M
156     /// elements in the array.
157     ArrayRef<T> slice(unsigned N, unsigned M) const {
158       assert(N+M <= size() && "Invalid specifier");
159       return ArrayRef<T>(data()+N, M);
160     }
161
162     // \brief Drop the last \p N elements of the array.
163     ArrayRef<T> drop_back(unsigned N = 1) const {
164       assert(size() >= N && "Dropping more elements than exist");
165       return slice(0, size() - N);
166     }
167
168     /// @}
169     /// @name Operator Overloads
170     /// @{
171     const T &operator[](size_t Index) const {
172       assert(Index < Length && "Invalid index!");
173       return Data[Index];
174     }
175
176     /// @}
177     /// @name Expensive Operations
178     /// @{
179     std::vector<T> vec() const {
180       return std::vector<T>(Data, Data+Length);
181     }
182
183     /// @}
184     /// @name Conversion operators
185     /// @{
186     operator std::vector<T>() const {
187       return std::vector<T>(Data, Data+Length);
188     }
189
190     /// @}
191   };
192
193   /// MutableArrayRef - Represent a mutable reference to an array (0 or more
194   /// elements consecutively in memory), i.e. a start pointer and a length.  It
195   /// allows various APIs to take and modify consecutive elements easily and
196   /// conveniently.
197   ///
198   /// This class does not own the underlying data, it is expected to be used in
199   /// situations where the data resides in some other buffer, whose lifetime
200   /// extends past that of the MutableArrayRef. For this reason, it is not in
201   /// general safe to store a MutableArrayRef.
202   ///
203   /// This is intended to be trivially copyable, so it should be passed by
204   /// value.
205   template<typename T>
206   class MutableArrayRef : public ArrayRef<T> {
207   public:
208     typedef T *iterator;
209
210     typedef std::reverse_iterator<iterator> reverse_iterator;
211
212     /// Construct an empty MutableArrayRef.
213     /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
214
215     /// Construct an empty MutableArrayRef from None.
216     /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
217
218     /// Construct an MutableArrayRef from a single element.
219     /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
220
221     /// Construct an MutableArrayRef from a pointer and length.
222     /*implicit*/ MutableArrayRef(T *data, size_t length)
223       : ArrayRef<T>(data, length) {}
224
225     /// Construct an MutableArrayRef from a range.
226     MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
227
228     /// Construct an MutableArrayRef from a SmallVector.
229     /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
230     : ArrayRef<T>(Vec) {}
231
232     /// Construct a MutableArrayRef from a std::vector.
233     /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
234     : ArrayRef<T>(Vec) {}
235
236     /// Construct an MutableArrayRef from a C array.
237     template <size_t N>
238     /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
239       : ArrayRef<T>(Arr) {}
240
241     T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
242
243     iterator begin() const { return data(); }
244     iterator end() const { return data() + this->size(); }
245
246     reverse_iterator rbegin() const { return reverse_iterator(end()); }
247     reverse_iterator rend() const { return reverse_iterator(begin()); }
248
249     /// front - Get the first element.
250     T &front() const {
251       assert(!this->empty());
252       return data()[0];
253     }
254
255     /// back - Get the last element.
256     T &back() const {
257       assert(!this->empty());
258       return data()[this->size()-1];
259     }
260
261     /// slice(n) - Chop off the first N elements of the array.
262     MutableArrayRef<T> slice(unsigned N) const {
263       assert(N <= this->size() && "Invalid specifier");
264       return MutableArrayRef<T>(data()+N, this->size()-N);
265     }
266
267     /// slice(n, m) - Chop off the first N elements of the array, and keep M
268     /// elements in the array.
269     MutableArrayRef<T> slice(unsigned N, unsigned M) const {
270       assert(N+M <= this->size() && "Invalid specifier");
271       return MutableArrayRef<T>(data()+N, M);
272     }
273
274     /// @}
275     /// @name Operator Overloads
276     /// @{
277     T &operator[](size_t Index) const {
278       assert(Index < this->size() && "Invalid index!");
279       return data()[Index];
280     }
281   };
282
283   /// @name ArrayRef Convenience constructors
284   /// @{
285
286   /// Construct an ArrayRef from a single element.
287   template<typename T>
288   ArrayRef<T> makeArrayRef(const T &OneElt) {
289     return OneElt;
290   }
291
292   /// Construct an ArrayRef from a pointer and length.
293   template<typename T>
294   ArrayRef<T> makeArrayRef(const T *data, size_t length) {
295     return ArrayRef<T>(data, length);
296   }
297
298   /// Construct an ArrayRef from a range.
299   template<typename T>
300   ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
301     return ArrayRef<T>(begin, end);
302   }
303
304   /// Construct an ArrayRef from a SmallVector.
305   template <typename T>
306   ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
307     return Vec;
308   }
309
310   /// Construct an ArrayRef from a SmallVector.
311   template <typename T, unsigned N>
312   ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
313     return Vec;
314   }
315
316   /// Construct an ArrayRef from a std::vector.
317   template<typename T>
318   ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
319     return Vec;
320   }
321
322   /// Construct an ArrayRef from a C array.
323   template<typename T, size_t N>
324   ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
325     return ArrayRef<T>(Arr);
326   }
327
328   /// @}
329   /// @name ArrayRef Comparison Operators
330   /// @{
331
332   template<typename T>
333   inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
334     return LHS.equals(RHS);
335   }
336
337   template<typename T>
338   inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
339     return !(LHS == RHS);
340   }
341
342   /// @}
343
344   // ArrayRefs can be treated like a POD type.
345   template <typename T> struct isPodLike;
346   template <typename T> struct isPodLike<ArrayRef<T> > {
347     static const bool value = true;
348   };
349 }
350
351 #endif