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