Home | History | Annotate | Download | only in ADT
      1 //===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef LLVM_ADT_ARRAYREF_H
     11 #define LLVM_ADT_ARRAYREF_H
     12 
     13 #include "llvm/ADT/None.h"
     14 #include "llvm/ADT/SmallVector.h"
     15 #include <vector>
     16 
     17 namespace llvm {
     18 
     19   /// ArrayRef - Represent a constant reference to an array (0 or more elements
     20   /// consecutively in memory), i.e. a start pointer and a length.  It allows
     21   /// various APIs to take consecutive elements easily and conveniently.
     22   ///
     23   /// This class does not own the underlying data, it is expected to be used in
     24   /// situations where the data resides in some other buffer, whose lifetime
     25   /// extends past that of the ArrayRef. For this reason, it is not in general
     26   /// safe to store an ArrayRef.
     27   ///
     28   /// This is intended to be trivially copyable, so it should be passed by
     29   /// value.
     30   template<typename T>
     31   class ArrayRef {
     32   public:
     33     typedef const T *iterator;
     34     typedef const T *const_iterator;
     35     typedef size_t size_type;
     36 
     37     typedef std::reverse_iterator<iterator> reverse_iterator;
     38 
     39   private:
     40     /// The start of the array, in an external buffer.
     41     const T *Data;
     42 
     43     /// The number of elements.
     44     size_type Length;
     45 
     46   public:
     47     /// @name Constructors
     48     /// @{
     49 
     50     /// Construct an empty ArrayRef.
     51     /*implicit*/ ArrayRef() : Data(0), Length(0) {}
     52 
     53     /// Construct an empty ArrayRef from None.
     54     /*implicit*/ ArrayRef(NoneType) : Data(0), Length(0) {}
     55 
     56     /// Construct an ArrayRef from a single element.
     57     /*implicit*/ ArrayRef(const T &OneElt)
     58       : Data(&OneElt), Length(1) {}
     59 
     60     /// Construct an ArrayRef from a pointer and length.
     61     /*implicit*/ ArrayRef(const T *data, size_t length)
     62       : Data(data), Length(length) {}
     63 
     64     /// Construct an ArrayRef from a range.
     65     ArrayRef(const T *begin, const T *end)
     66       : Data(begin), Length(end - begin) {}
     67 
     68     /// Construct an ArrayRef from a SmallVector. This is templated in order to
     69     /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
     70     /// copy-construct an ArrayRef.
     71     template<typename U>
     72     /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
     73       : Data(Vec.data()), Length(Vec.size()) {
     74     }
     75 
     76     /// Construct an ArrayRef from a std::vector.
     77     template<typename A>
     78     /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
     79       : Data(Vec.empty() ? (T*)0 : &Vec[0]), Length(Vec.size()) {}
     80 
     81     /// Construct an ArrayRef from a C array.
     82     template <size_t N>
     83     /*implicit*/ ArrayRef(const T (&Arr)[N])
     84       : Data(Arr), Length(N) {}
     85 
     86     /// @}
     87     /// @name Simple Operations
     88     /// @{
     89 
     90     iterator begin() const { return Data; }
     91     iterator end() const { return Data + Length; }
     92 
     93     reverse_iterator rbegin() const { return reverse_iterator(end()); }
     94     reverse_iterator rend() const { return reverse_iterator(begin()); }
     95 
     96     /// empty - Check if the array is empty.
     97     bool empty() const { return Length == 0; }
     98 
     99     const T *data() const { return Data; }
    100 
    101     /// size - Get the array size.
    102     size_t size() const { return Length; }
    103 
    104     /// front - Get the first element.
    105     const T &front() const {
    106       assert(!empty());
    107       return Data[0];
    108     }
    109 
    110     /// back - Get the last element.
    111     const T &back() const {
    112       assert(!empty());
    113       return Data[Length-1];
    114     }
    115 
    116     /// equals - Check for element-wise equality.
    117     bool equals(ArrayRef RHS) const {
    118       if (Length != RHS.Length)
    119         return false;
    120       for (size_type i = 0; i != Length; i++)
    121         if (Data[i] != RHS.Data[i])
    122           return false;
    123       return true;
    124     }
    125 
    126     /// slice(n) - Chop off the first N elements of the array.
    127     ArrayRef<T> slice(unsigned N) const {
    128       assert(N <= size() && "Invalid specifier");
    129       return ArrayRef<T>(data()+N, size()-N);
    130     }
    131 
    132     /// slice(n, m) - Chop off the first N elements of the array, and keep M
    133     /// elements in the array.
    134     ArrayRef<T> slice(unsigned N, unsigned M) const {
    135       assert(N+M <= size() && "Invalid specifier");
    136       return ArrayRef<T>(data()+N, M);
    137     }
    138 
    139     /// @}
    140     /// @name Operator Overloads
    141     /// @{
    142     const T &operator[](size_t Index) const {
    143       assert(Index < Length && "Invalid index!");
    144       return Data[Index];
    145     }
    146 
    147     /// @}
    148     /// @name Expensive Operations
    149     /// @{
    150     std::vector<T> vec() const {
    151       return std::vector<T>(Data, Data+Length);
    152     }
    153 
    154     /// @}
    155     /// @name Conversion operators
    156     /// @{
    157     operator std::vector<T>() const {
    158       return std::vector<T>(Data, Data+Length);
    159     }
    160 
    161     /// @}
    162   };
    163 
    164   /// MutableArrayRef - Represent a mutable reference to an array (0 or more
    165   /// elements consecutively in memory), i.e. a start pointer and a length.  It
    166   /// allows various APIs to take and modify consecutive elements easily and
    167   /// conveniently.
    168   ///
    169   /// This class does not own the underlying data, it is expected to be used in
    170   /// situations where the data resides in some other buffer, whose lifetime
    171   /// extends past that of the MutableArrayRef. For this reason, it is not in
    172   /// general safe to store a MutableArrayRef.
    173   ///
    174   /// This is intended to be trivially copyable, so it should be passed by
    175   /// value.
    176   template<typename T>
    177   class MutableArrayRef : public ArrayRef<T> {
    178   public:
    179     typedef T *iterator;
    180 
    181     /// Construct an empty MutableArrayRef.
    182     /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
    183 
    184     /// Construct an empty MutableArrayRef from None.
    185     /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
    186 
    187     /// Construct an MutableArrayRef from a single element.
    188     /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
    189 
    190     /// Construct an MutableArrayRef from a pointer and length.
    191     /*implicit*/ MutableArrayRef(T *data, size_t length)
    192       : ArrayRef<T>(data, length) {}
    193 
    194     /// Construct an MutableArrayRef from a range.
    195     MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
    196 
    197     /// Construct an MutableArrayRef from a SmallVector.
    198     /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
    199     : ArrayRef<T>(Vec) {}
    200 
    201     /// Construct a MutableArrayRef from a std::vector.
    202     /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
    203     : ArrayRef<T>(Vec) {}
    204 
    205     /// Construct an MutableArrayRef from a C array.
    206     template <size_t N>
    207     /*implicit*/ MutableArrayRef(T (&Arr)[N])
    208       : ArrayRef<T>(Arr) {}
    209 
    210     T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
    211 
    212     iterator begin() const { return data(); }
    213     iterator end() const { return data() + this->size(); }
    214 
    215     /// front - Get the first element.
    216     T &front() const {
    217       assert(!this->empty());
    218       return data()[0];
    219     }
    220 
    221     /// back - Get the last element.
    222     T &back() const {
    223       assert(!this->empty());
    224       return data()[this->size()-1];
    225     }
    226 
    227     /// slice(n) - Chop off the first N elements of the array.
    228     MutableArrayRef<T> slice(unsigned N) const {
    229       assert(N <= this->size() && "Invalid specifier");
    230       return MutableArrayRef<T>(data()+N, this->size()-N);
    231     }
    232 
    233     /// slice(n, m) - Chop off the first N elements of the array, and keep M
    234     /// elements in the array.
    235     MutableArrayRef<T> slice(unsigned N, unsigned M) const {
    236       assert(N+M <= this->size() && "Invalid specifier");
    237       return MutableArrayRef<T>(data()+N, M);
    238     }
    239 
    240     /// @}
    241     /// @name Operator Overloads
    242     /// @{
    243     T &operator[](size_t Index) const {
    244       assert(Index < this->size() && "Invalid index!");
    245       return data()[Index];
    246     }
    247   };
    248 
    249   /// @name ArrayRef Convenience constructors
    250   /// @{
    251 
    252   /// Construct an ArrayRef from a single element.
    253   template<typename T>
    254   ArrayRef<T> makeArrayRef(const T &OneElt) {
    255     return OneElt;
    256   }
    257 
    258   /// Construct an ArrayRef from a pointer and length.
    259   template<typename T>
    260   ArrayRef<T> makeArrayRef(const T *data, size_t length) {
    261     return ArrayRef<T>(data, length);
    262   }
    263 
    264   /// Construct an ArrayRef from a range.
    265   template<typename T>
    266   ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
    267     return ArrayRef<T>(begin, end);
    268   }
    269 
    270   /// Construct an ArrayRef from a SmallVector.
    271   template <typename T>
    272   ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
    273     return Vec;
    274   }
    275 
    276   /// Construct an ArrayRef from a SmallVector.
    277   template <typename T, unsigned N>
    278   ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
    279     return Vec;
    280   }
    281 
    282   /// Construct an ArrayRef from a std::vector.
    283   template<typename T>
    284   ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
    285     return Vec;
    286   }
    287 
    288   /// Construct an ArrayRef from a C array.
    289   template<typename T, size_t N>
    290   ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
    291     return ArrayRef<T>(Arr);
    292   }
    293 
    294   /// @}
    295   /// @name ArrayRef Comparison Operators
    296   /// @{
    297 
    298   template<typename T>
    299   inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
    300     return LHS.equals(RHS);
    301   }
    302 
    303   template<typename T>
    304   inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
    305     return !(LHS == RHS);
    306   }
    307 
    308   /// @}
    309 
    310   // ArrayRefs can be treated like a POD type.
    311   template <typename T> struct isPodLike;
    312   template <typename T> struct isPodLike<ArrayRef<T> > {
    313     static const bool value = true;
    314   };
    315 }
    316 
    317 #endif
    318