Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ART_RUNTIME_BASE_ARRAY_REF_H_
     18 #define ART_RUNTIME_BASE_ARRAY_REF_H_
     19 
     20 #include <type_traits>
     21 #include <vector>
     22 
     23 #include "base/logging.h"
     24 
     25 namespace art {
     26 
     27 /**
     28  * @brief A container that references an array.
     29  *
     30  * @details The template class ArrayRef provides a container that references
     31  * an external array. This external array must remain alive while the ArrayRef
     32  * object is in use. The external array may be a std::vector<>-backed storage
     33  * or any other contiguous chunk of memory but that memory must remain valid,
     34  * i.e. the std::vector<> must not be resized for example.
     35  *
     36  * Except for copy/assign and insert/erase/capacity functions, the interface
     37  * is essentially the same as std::vector<>. Since we don't want to throw
     38  * exceptions, at() is also excluded.
     39  */
     40 template <typename T>
     41 class ArrayRef {
     42  public:
     43   typedef T value_type;
     44   typedef T& reference;
     45   typedef const T& const_reference;
     46   typedef T* pointer;
     47   typedef const T* const_pointer;
     48   typedef T* iterator;
     49   typedef const T* const_iterator;
     50   typedef std::reverse_iterator<iterator> reverse_iterator;
     51   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
     52   typedef ptrdiff_t difference_type;
     53   typedef size_t size_type;
     54 
     55   // Constructors.
     56 
     57   constexpr ArrayRef()
     58       : array_(nullptr), size_(0u) {
     59   }
     60 
     61   template <size_t size>
     62   explicit constexpr ArrayRef(T (&array)[size])
     63       : array_(array), size_(size) {
     64   }
     65 
     66   template <typename U,
     67             size_t size,
     68             typename = typename std::enable_if<std::is_same<T, const U>::value>::type>
     69   explicit constexpr ArrayRef(U (&array)[size])
     70       : array_(array), size_(size) {
     71   }
     72 
     73   constexpr ArrayRef(T* array_in, size_t size_in)
     74       : array_(array_in), size_(size_in) {
     75   }
     76 
     77   template <typename Vector,
     78             typename = typename std::enable_if<
     79                 std::is_same<typename Vector::value_type, value_type>::value>::type>
     80   explicit ArrayRef(Vector& v)
     81       : array_(v.data()), size_(v.size()) {
     82   }
     83 
     84   template <typename Vector,
     85             typename = typename std::enable_if<
     86                 std::is_same<
     87                     typename std::add_const<typename Vector::value_type>::type,
     88                     value_type>::value>::type>
     89   explicit ArrayRef(const Vector& v)
     90       : array_(v.data()), size_(v.size()) {
     91   }
     92 
     93   ArrayRef(const ArrayRef&) = default;
     94 
     95   // Assignment operators.
     96 
     97   ArrayRef& operator=(const ArrayRef& other) {
     98     array_ = other.array_;
     99     size_ = other.size_;
    100     return *this;
    101   }
    102 
    103   template <typename U>
    104   typename std::enable_if<std::is_same<T, const U>::value, ArrayRef>::type&
    105   operator=(const ArrayRef<U>& other) {
    106     return *this = ArrayRef(other);
    107   }
    108 
    109   // Destructor.
    110   ~ArrayRef() = default;
    111 
    112   // Iterators.
    113   iterator begin() { return array_; }
    114   const_iterator begin() const { return array_; }
    115   const_iterator cbegin() const { return array_; }
    116   iterator end() { return array_ + size_; }
    117   const_iterator end() const { return array_ + size_; }
    118   const_iterator cend() const { return array_ + size_; }
    119   reverse_iterator rbegin() { return reverse_iterator(end()); }
    120   const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
    121   const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
    122   reverse_iterator rend() { return reverse_iterator(begin()); }
    123   const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
    124   const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
    125 
    126   // Size.
    127   size_type size() const { return size_; }
    128   bool empty() const { return size() == 0u; }
    129 
    130   // Element access. NOTE: Not providing at().
    131 
    132   reference operator[](size_type n) {
    133     DCHECK_LT(n, size_);
    134     return array_[n];
    135   }
    136 
    137   const_reference operator[](size_type n) const {
    138     DCHECK_LT(n, size_);
    139     return array_[n];
    140   }
    141 
    142   reference front() {
    143     DCHECK_NE(size_, 0u);
    144     return array_[0];
    145   }
    146 
    147   const_reference front() const {
    148     DCHECK_NE(size_, 0u);
    149     return array_[0];
    150   }
    151 
    152   reference back() {
    153     DCHECK_NE(size_, 0u);
    154     return array_[size_ - 1u];
    155   }
    156 
    157   const_reference back() const {
    158     DCHECK_NE(size_, 0u);
    159     return array_[size_ - 1u];
    160   }
    161 
    162   value_type* data() { return array_; }
    163   const value_type* data() const { return array_; }
    164 
    165   ArrayRef SubArray(size_type pos) {
    166     return SubArray(pos, size() - pos);
    167   }
    168   ArrayRef<const T> SubArray(size_type pos) const {
    169     return SubArray(pos, size() - pos);
    170   }
    171   ArrayRef SubArray(size_type pos, size_type length) {
    172     DCHECK_LE(pos, size());
    173     DCHECK_LE(length, size() - pos);
    174     return ArrayRef(data() + pos, length);
    175   }
    176   ArrayRef<const T> SubArray(size_type pos, size_type length) const {
    177     DCHECK_LE(pos, size());
    178     DCHECK_LE(length, size() - pos);
    179     return ArrayRef<const T>(data() + pos, length);
    180   }
    181 
    182  private:
    183   T* array_;
    184   size_t size_;
    185 };
    186 
    187 template <typename T>
    188 bool operator==(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) {
    189   return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
    190 }
    191 
    192 template <typename T>
    193 bool operator!=(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) {
    194   return !(lhs == rhs);
    195 }
    196 
    197 }  // namespace art
    198 
    199 
    200 #endif  // ART_RUNTIME_BASE_ARRAY_REF_H_
    201