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