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