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