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