LLVM API Documentation

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