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     /// Construct an ArrayRef<const T*> from a SmallVector<T*>. This is
00100     /// templated in order to avoid instantiating SmallVectorTemplateCommon<T>
00101     /// whenever we copy-construct an ArrayRef.
00102     template<typename U, typename DummyT>
00103     /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<U*, DummyT> &Vec,
00104                           typename std::enable_if<
00105                               std::is_convertible<U *const *,
00106                                                   T const *>::value>::type* = 0)
00107       : Data(Vec.data()), Length(Vec.size()) {
00108     }
00109 
00110     /// Construct an ArrayRef<const T*> from std::vector<T*>. This uses SFINAE
00111     /// to ensure that only vectors of pointers can be converted.
00112     template<typename U, typename A>
00113     ArrayRef(const std::vector<U *, A> &Vec,
00114              typename std::enable_if<
00115                  std::is_convertible<U *const *, T const *>::value>::type* = 0)
00116       : Data(Vec.data()), Length(Vec.size()) {}
00117 
00118     /// @}
00119     /// @name Simple Operations
00120     /// @{
00121 
00122     iterator begin() const { return Data; }
00123     iterator end() const { return Data + Length; }
00124 
00125     reverse_iterator rbegin() const { return reverse_iterator(end()); }
00126     reverse_iterator rend() const { return reverse_iterator(begin()); }
00127 
00128     /// empty - Check if the array is empty.
00129     bool empty() const { return Length == 0; }
00130 
00131     const T *data() const { return Data; }
00132 
00133     /// size - Get the array size.
00134     size_t size() const { return Length; }
00135 
00136     /// front - Get the first element.
00137     const T &front() const {
00138       assert(!empty());
00139       return Data[0];
00140     }
00141 
00142     /// back - Get the last element.
00143     const T &back() const {
00144       assert(!empty());
00145       return Data[Length-1];
00146     }
00147 
00148     // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
00149     template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
00150       T *Buff = A.template Allocate<T>(Length);
00151       std::copy(begin(), end(), Buff);
00152       return ArrayRef<T>(Buff, Length);
00153     }
00154 
00155     /// equals - Check for element-wise equality.
00156     bool equals(ArrayRef RHS) const {
00157       if (Length != RHS.Length)
00158         return false;
00159       if (Length == 0)
00160         return true;
00161       return std::equal(begin(), end(), RHS.begin());
00162     }
00163 
00164     /// slice(n) - Chop off the first N elements of the array.
00165     ArrayRef<T> slice(unsigned N) const {
00166       assert(N <= size() && "Invalid specifier");
00167       return ArrayRef<T>(data()+N, size()-N);
00168     }
00169 
00170     /// slice(n, m) - Chop off the first N elements of the array, and keep M
00171     /// elements in the array.
00172     ArrayRef<T> slice(unsigned N, unsigned M) const {
00173       assert(N+M <= size() && "Invalid specifier");
00174       return ArrayRef<T>(data()+N, M);
00175     }
00176 
00177     // \brief Drop the last \p N elements of the array.
00178     ArrayRef<T> drop_back(unsigned N = 1) const {
00179       assert(size() >= N && "Dropping more elements than exist");
00180       return slice(0, size() - N);
00181     }
00182 
00183     /// @}
00184     /// @name Operator Overloads
00185     /// @{
00186     const T &operator[](size_t Index) const {
00187       assert(Index < Length && "Invalid index!");
00188       return Data[Index];
00189     }
00190 
00191     /// @}
00192     /// @name Expensive Operations
00193     /// @{
00194     std::vector<T> vec() const {
00195       return std::vector<T>(Data, Data+Length);
00196     }
00197 
00198     /// @}
00199     /// @name Conversion operators
00200     /// @{
00201     operator std::vector<T>() const {
00202       return std::vector<T>(Data, Data+Length);
00203     }
00204 
00205     /// @}
00206   };
00207 
00208   /// MutableArrayRef - Represent a mutable reference to an array (0 or more
00209   /// elements consecutively in memory), i.e. a start pointer and a length.  It
00210   /// allows various APIs to take and modify consecutive elements easily and
00211   /// conveniently.
00212   ///
00213   /// This class does not own the underlying data, it is expected to be used in
00214   /// situations where the data resides in some other buffer, whose lifetime
00215   /// extends past that of the MutableArrayRef. For this reason, it is not in
00216   /// general safe to store a MutableArrayRef.
00217   ///
00218   /// This is intended to be trivially copyable, so it should be passed by
00219   /// value.
00220   template<typename T>
00221   class MutableArrayRef : public ArrayRef<T> {
00222   public:
00223     typedef T *iterator;
00224 
00225     typedef std::reverse_iterator<iterator> reverse_iterator;
00226 
00227     /// Construct an empty MutableArrayRef.
00228     /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
00229 
00230     /// Construct an empty MutableArrayRef from None.
00231     /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
00232 
00233     /// Construct an MutableArrayRef from a single element.
00234     /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
00235 
00236     /// Construct an MutableArrayRef from a pointer and length.
00237     /*implicit*/ MutableArrayRef(T *data, size_t length)
00238       : ArrayRef<T>(data, length) {}
00239 
00240     /// Construct an MutableArrayRef from a range.
00241     MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
00242 
00243     /// Construct an MutableArrayRef from a SmallVector.
00244     /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
00245     : ArrayRef<T>(Vec) {}
00246 
00247     /// Construct a MutableArrayRef from a std::vector.
00248     /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
00249     : ArrayRef<T>(Vec) {}
00250 
00251     /// Construct an MutableArrayRef from a C array.
00252     template <size_t N>
00253     /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
00254       : ArrayRef<T>(Arr) {}
00255 
00256     T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
00257 
00258     iterator begin() const { return data(); }
00259     iterator end() const { return data() + this->size(); }
00260 
00261     reverse_iterator rbegin() const { return reverse_iterator(end()); }
00262     reverse_iterator rend() const { return reverse_iterator(begin()); }
00263 
00264     /// front - Get the first element.
00265     T &front() const {
00266       assert(!this->empty());
00267       return data()[0];
00268     }
00269 
00270     /// back - Get the last element.
00271     T &back() const {
00272       assert(!this->empty());
00273       return data()[this->size()-1];
00274     }
00275 
00276     /// slice(n) - Chop off the first N elements of the array.
00277     MutableArrayRef<T> slice(unsigned N) const {
00278       assert(N <= this->size() && "Invalid specifier");
00279       return MutableArrayRef<T>(data()+N, this->size()-N);
00280     }
00281 
00282     /// slice(n, m) - Chop off the first N elements of the array, and keep M
00283     /// elements in the array.
00284     MutableArrayRef<T> slice(unsigned N, unsigned M) const {
00285       assert(N+M <= this->size() && "Invalid specifier");
00286       return MutableArrayRef<T>(data()+N, M);
00287     }
00288 
00289     MutableArrayRef<T> drop_back(unsigned N) const {
00290       assert(this->size() >= N && "Dropping more elements than exist");
00291       return slice(0, this->size() - N);
00292     }
00293 
00294     /// @}
00295     /// @name Operator Overloads
00296     /// @{
00297     T &operator[](size_t Index) const {
00298       assert(Index < this->size() && "Invalid index!");
00299       return data()[Index];
00300     }
00301   };
00302 
00303   /// @name ArrayRef Convenience constructors
00304   /// @{
00305 
00306   /// Construct an ArrayRef from a single element.
00307   template<typename T>
00308   ArrayRef<T> makeArrayRef(const T &OneElt) {
00309     return OneElt;
00310   }
00311 
00312   /// Construct an ArrayRef from a pointer and length.
00313   template<typename T>
00314   ArrayRef<T> makeArrayRef(const T *data, size_t length) {
00315     return ArrayRef<T>(data, length);
00316   }
00317 
00318   /// Construct an ArrayRef from a range.
00319   template<typename T>
00320   ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
00321     return ArrayRef<T>(begin, end);
00322   }
00323 
00324   /// Construct an ArrayRef from a SmallVector.
00325   template <typename T>
00326   ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
00327     return Vec;
00328   }
00329 
00330   /// Construct an ArrayRef from a SmallVector.
00331   template <typename T, unsigned N>
00332   ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
00333     return Vec;
00334   }
00335 
00336   /// Construct an ArrayRef from a std::vector.
00337   template<typename T>
00338   ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
00339     return Vec;
00340   }
00341 
00342   /// Construct an ArrayRef from a C array.
00343   template<typename T, size_t N>
00344   ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
00345     return ArrayRef<T>(Arr);
00346   }
00347 
00348   /// @}
00349   /// @name ArrayRef Comparison Operators
00350   /// @{
00351 
00352   template<typename T>
00353   inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
00354     return LHS.equals(RHS);
00355   }
00356 
00357   template<typename T>
00358   inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
00359     return !(LHS == RHS);
00360   }
00361 
00362   /// @}
00363 
00364   // ArrayRefs can be treated like a POD type.
00365   template <typename T> struct isPodLike;
00366   template <typename T> struct isPodLike<ArrayRef<T> > {
00367     static const bool value = true;
00368   };
00369 }
00370 
00371 #endif