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