LLVM  mainline
ArrayRef.h
Go to the documentation of this file.
00001 //===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_ADT_ARRAYREF_H
00011 #define LLVM_ADT_ARRAYREF_H
00012 
00013 #include "llvm/ADT/None.h"
00014 #include "llvm/ADT/SmallVector.h"
00015 #include <vector>
00016 
00017 namespace llvm {
00018 
00019   /// ArrayRef - Represent a constant reference to an array (0 or more elements
00020   /// consecutively in memory), i.e. a start pointer and a length.  It allows
00021   /// various APIs to take consecutive elements easily and conveniently.
00022   ///
00023   /// This class does not own the underlying data, it is expected to be used in
00024   /// situations where the data resides in some other buffer, whose lifetime
00025   /// extends past that of the ArrayRef. For this reason, it is not in general
00026   /// safe to store an ArrayRef.
00027   ///
00028   /// This is intended to be trivially copyable, so it should be passed by
00029   /// value.
00030   template<typename T>
00031   class ArrayRef {
00032   public:
00033     typedef const T *iterator;
00034     typedef const T *const_iterator;
00035     typedef size_t size_type;
00036 
00037     typedef std::reverse_iterator<iterator> reverse_iterator;
00038 
00039   private:
00040     /// The start of the array, in an external buffer.
00041     const T *Data;
00042 
00043     /// The number of elements.
00044     size_type Length;
00045 
00046   public:
00047     /// @name Constructors
00048     /// @{
00049 
00050     /// Construct an empty ArrayRef.
00051     /*implicit*/ ArrayRef() : Data(nullptr), Length(0) {}
00052 
00053     /// Construct an empty ArrayRef from None.
00054     /*implicit*/ ArrayRef(NoneType) : Data(nullptr), Length(0) {}
00055 
00056     /// Construct an ArrayRef from a single element.
00057     /*implicit*/ ArrayRef(const T &OneElt)
00058       : Data(&OneElt), Length(1) {}
00059 
00060     /// Construct an ArrayRef from a pointer and length.
00061     /*implicit*/ ArrayRef(const T *data, size_t length)
00062       : Data(data), Length(length) {}
00063 
00064     /// Construct an ArrayRef from a range.
00065     ArrayRef(const T *begin, const T *end)
00066       : Data(begin), Length(end - begin) {}
00067 
00068     /// Construct an ArrayRef from a SmallVector. This is templated in order to
00069     /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
00070     /// copy-construct an ArrayRef.
00071     template<typename U>
00072     /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
00073       : Data(Vec.data()), Length(Vec.size()) {
00074     }
00075 
00076     /// Construct an ArrayRef from a std::vector.
00077     template<typename A>
00078     /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
00079       : Data(Vec.data()), Length(Vec.size()) {}
00080 
00081     /// Construct an ArrayRef from a C array.
00082     template <size_t N>
00083     /*implicit*/ LLVM_CONSTEXPR ArrayRef(const T (&Arr)[N])
00084       : Data(Arr), Length(N) {}
00085 
00086     /// Construct an ArrayRef from a std::initializer_list.
00087     /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
00088     : Data(Vec.begin() == Vec.end() ? (T*)0 : Vec.begin()),
00089       Length(Vec.size()) {}
00090 
00091     /// Construct an ArrayRef<const T*> from ArrayRef<T*>. This uses SFINAE to
00092     /// ensure that only ArrayRefs of pointers can be converted.
00093     template <typename U>
00094     ArrayRef(const ArrayRef<U *> &A,
00095              typename std::enable_if<
00096                  std::is_convertible<U *const *, T const *>::value>::type* = 0)
00097       : Data(A.data()), Length(A.size()) {}
00098 
00099     /// @}
00100     /// @name Simple Operations
00101     /// @{
00102 
00103     iterator begin() const { return Data; }
00104     iterator end() const { return Data + Length; }
00105 
00106     reverse_iterator rbegin() const { return reverse_iterator(end()); }
00107     reverse_iterator rend() const { return reverse_iterator(begin()); }
00108 
00109     /// empty - Check if the array is empty.
00110     bool empty() const { return Length == 0; }
00111 
00112     const T *data() const { return Data; }
00113 
00114     /// size - Get the array size.
00115     size_t size() const { return Length; }
00116 
00117     /// front - Get the first element.
00118     const T &front() const {
00119       assert(!empty());
00120       return Data[0];
00121     }
00122 
00123     /// back - Get the last element.
00124     const T &back() const {
00125       assert(!empty());
00126       return Data[Length-1];
00127     }
00128 
00129     // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
00130     template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
00131       T *Buff = A.template Allocate<T>(Length);
00132       std::copy(begin(), end(), Buff);
00133       return ArrayRef<T>(Buff, Length);
00134     }
00135 
00136     /// equals - Check for element-wise equality.
00137     bool equals(ArrayRef RHS) const {
00138       if (Length != RHS.Length)
00139         return false;
00140       if (Length == 0)
00141         return true;
00142       return std::equal(begin(), end(), RHS.begin());
00143     }
00144 
00145     /// slice(n) - Chop off the first N elements of the array.
00146     ArrayRef<T> slice(unsigned N) const {
00147       assert(N <= size() && "Invalid specifier");
00148       return ArrayRef<T>(data()+N, size()-N);
00149     }
00150 
00151     /// slice(n, m) - Chop off the first N elements of the array, and keep M
00152     /// elements in the array.
00153     ArrayRef<T> slice(unsigned N, unsigned M) const {
00154       assert(N+M <= size() && "Invalid specifier");
00155       return ArrayRef<T>(data()+N, M);
00156     }
00157 
00158     // \brief Drop the last \p N elements of the array.
00159     ArrayRef<T> drop_back(unsigned N = 1) const {
00160       assert(size() >= N && "Dropping more elements than exist");
00161       return slice(0, size() - N);
00162     }
00163 
00164     /// @}
00165     /// @name Operator Overloads
00166     /// @{
00167     const T &operator[](size_t Index) const {
00168       assert(Index < Length && "Invalid index!");
00169       return Data[Index];
00170     }
00171 
00172     /// @}
00173     /// @name Expensive Operations
00174     /// @{
00175     std::vector<T> vec() const {
00176       return std::vector<T>(Data, Data+Length);
00177     }
00178 
00179     /// @}
00180     /// @name Conversion operators
00181     /// @{
00182     operator std::vector<T>() const {
00183       return std::vector<T>(Data, Data+Length);
00184     }
00185 
00186     /// @}
00187   };
00188 
00189   /// MutableArrayRef - Represent a mutable reference to an array (0 or more
00190   /// elements consecutively in memory), i.e. a start pointer and a length.  It
00191   /// allows various APIs to take and modify consecutive elements easily and
00192   /// conveniently.
00193   ///
00194   /// This class does not own the underlying data, it is expected to be used in
00195   /// situations where the data resides in some other buffer, whose lifetime
00196   /// extends past that of the MutableArrayRef. For this reason, it is not in
00197   /// general safe to store a MutableArrayRef.
00198   ///
00199   /// This is intended to be trivially copyable, so it should be passed by
00200   /// value.
00201   template<typename T>
00202   class MutableArrayRef : public ArrayRef<T> {
00203   public:
00204     typedef T *iterator;
00205 
00206     typedef std::reverse_iterator<iterator> reverse_iterator;
00207 
00208     /// Construct an empty MutableArrayRef.
00209     /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
00210 
00211     /// Construct an empty MutableArrayRef from None.
00212     /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
00213 
00214     /// Construct an MutableArrayRef from a single element.
00215     /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
00216 
00217     /// Construct an MutableArrayRef from a pointer and length.
00218     /*implicit*/ MutableArrayRef(T *data, size_t length)
00219       : ArrayRef<T>(data, length) {}
00220 
00221     /// Construct an MutableArrayRef from a range.
00222     MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
00223 
00224     /// Construct an MutableArrayRef from a SmallVector.
00225     /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
00226     : ArrayRef<T>(Vec) {}
00227 
00228     /// Construct a MutableArrayRef from a std::vector.
00229     /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
00230     : ArrayRef<T>(Vec) {}
00231 
00232     /// Construct an MutableArrayRef from a C array.
00233     template <size_t N>
00234     /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
00235       : ArrayRef<T>(Arr) {}
00236 
00237     T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
00238 
00239     iterator begin() const { return data(); }
00240     iterator end() const { return data() + this->size(); }
00241 
00242     reverse_iterator rbegin() const { return reverse_iterator(end()); }
00243     reverse_iterator rend() const { return reverse_iterator(begin()); }
00244 
00245     /// front - Get the first element.
00246     T &front() const {
00247       assert(!this->empty());
00248       return data()[0];
00249     }
00250 
00251     /// back - Get the last element.
00252     T &back() const {
00253       assert(!this->empty());
00254       return data()[this->size()-1];
00255     }
00256 
00257     /// slice(n) - Chop off the first N elements of the array.
00258     MutableArrayRef<T> slice(unsigned N) const {
00259       assert(N <= this->size() && "Invalid specifier");
00260       return MutableArrayRef<T>(data()+N, this->size()-N);
00261     }
00262 
00263     /// slice(n, m) - Chop off the first N elements of the array, and keep M
00264     /// elements in the array.
00265     MutableArrayRef<T> slice(unsigned N, unsigned M) const {
00266       assert(N+M <= this->size() && "Invalid specifier");
00267       return MutableArrayRef<T>(data()+N, M);
00268     }
00269 
00270     /// @}
00271     /// @name Operator Overloads
00272     /// @{
00273     T &operator[](size_t Index) const {
00274       assert(Index < this->size() && "Invalid index!");
00275       return data()[Index];
00276     }
00277   };
00278 
00279   /// @name ArrayRef Convenience constructors
00280   /// @{
00281 
00282   /// Construct an ArrayRef from a single element.
00283   template<typename T>
00284   ArrayRef<T> makeArrayRef(const T &OneElt) {
00285     return OneElt;
00286   }
00287 
00288   /// Construct an ArrayRef from a pointer and length.
00289   template<typename T>
00290   ArrayRef<T> makeArrayRef(const T *data, size_t length) {
00291     return ArrayRef<T>(data, length);
00292   }
00293 
00294   /// Construct an ArrayRef from a range.
00295   template<typename T>
00296   ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
00297     return ArrayRef<T>(begin, end);
00298   }
00299 
00300   /// Construct an ArrayRef from a SmallVector.
00301   template <typename T>
00302   ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
00303     return Vec;
00304   }
00305 
00306   /// Construct an ArrayRef from a SmallVector.
00307   template <typename T, unsigned N>
00308   ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
00309     return Vec;
00310   }
00311 
00312   /// Construct an ArrayRef from a std::vector.
00313   template<typename T>
00314   ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
00315     return Vec;
00316   }
00317 
00318   /// Construct an ArrayRef from a C array.
00319   template<typename T, size_t N>
00320   ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
00321     return ArrayRef<T>(Arr);
00322   }
00323 
00324   /// @}
00325   /// @name ArrayRef Comparison Operators
00326   /// @{
00327 
00328   template<typename T>
00329   inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
00330     return LHS.equals(RHS);
00331   }
00332 
00333   template<typename T>
00334   inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
00335     return !(LHS == RHS);
00336   }
00337 
00338   /// @}
00339 
00340   // ArrayRefs can be treated like a POD type.
00341   template <typename T> struct isPodLike;
00342   template <typename T> struct isPodLike<ArrayRef<T> > {
00343     static const bool value = true;
00344   };
00345 }
00346 
00347 #endif