Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 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_COMPILER_UTILS_ARRAY_REF_H_ |
| 18 | #define ART_COMPILER_UTILS_ARRAY_REF_H_ |
| 19 | |
| 20 | #include <type_traits> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/logging.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 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 { |
Vladimir Marko | 0110619 | 2014-06-05 16:35:31 +0100 | [diff] [blame] | 42 | private: |
| 43 | struct tag { }; |
| 44 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 45 | public: |
| 46 | typedef T value_type; |
| 47 | typedef T& reference; |
| 48 | typedef const T& const_reference; |
| 49 | typedef T* pointer; |
| 50 | typedef const T* const_pointer; |
| 51 | typedef T* iterator; |
| 52 | typedef const T* const_iterator; |
| 53 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 54 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 55 | typedef ptrdiff_t difference_type; |
| 56 | typedef size_t size_type; |
| 57 | |
| 58 | // Constructors. |
| 59 | |
| 60 | constexpr ArrayRef() |
| 61 | : array_(nullptr), size_(0u) { |
| 62 | } |
| 63 | |
| 64 | template <size_t size> |
Vladimir Marko | 345f93e | 2015-07-14 18:58:59 +0100 | [diff] [blame] | 65 | explicit constexpr ArrayRef(T (&array)[size]) |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 66 | : array_(array), size_(size) { |
| 67 | } |
| 68 | |
| 69 | template <typename U, size_t size> |
Vladimir Marko | 345f93e | 2015-07-14 18:58:59 +0100 | [diff] [blame] | 70 | explicit constexpr ArrayRef(U (&array)[size], |
| 71 | typename std::enable_if<std::is_same<T, const U>::value, tag>::type |
| 72 | t ATTRIBUTE_UNUSED = tag()) |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 73 | : array_(array), size_(size) { |
| 74 | } |
| 75 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 76 | constexpr ArrayRef(T* array_in, size_t size_in) |
| 77 | : array_(array_in), size_(size_in) { |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 80 | template <typename Vector, |
| 81 | typename = typename std::enable_if< |
| 82 | std::is_same<typename Vector::value_type, value_type>::value>::type> |
| 83 | explicit ArrayRef(Vector& v) |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 84 | : array_(v.data()), size_(v.size()) { |
| 85 | } |
| 86 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 87 | template <typename Vector, |
| 88 | typename = typename std::enable_if< |
| 89 | std::is_same< |
| 90 | typename std::add_const<typename Vector::value_type>::type, |
| 91 | value_type>::value>::type> |
| 92 | explicit ArrayRef(const Vector& v) |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 93 | : array_(v.data()), size_(v.size()) { |
| 94 | } |
| 95 | |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 96 | ArrayRef(const ArrayRef&) = default; |
| 97 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 98 | // Assignment operators. |
| 99 | |
| 100 | ArrayRef& operator=(const ArrayRef& other) { |
| 101 | array_ = other.array_; |
| 102 | size_ = other.size_; |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | template <typename U> |
Vladimir Marko | 0110619 | 2014-06-05 16:35:31 +0100 | [diff] [blame] | 107 | typename std::enable_if<std::is_same<T, const U>::value, ArrayRef>::type& |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 108 | operator=(const ArrayRef<U>& other) { |
| 109 | return *this = ArrayRef(other); |
| 110 | } |
| 111 | |
| 112 | // Destructor. |
| 113 | ~ArrayRef() = default; |
| 114 | |
| 115 | // Iterators. |
| 116 | iterator begin() { return array_; } |
| 117 | const_iterator begin() const { return array_; } |
| 118 | const_iterator cbegin() const { return array_; } |
| 119 | iterator end() { return array_ + size_; } |
| 120 | const_iterator end() const { return array_ + size_; } |
| 121 | const_iterator cend() const { return array_ + size_; } |
| 122 | reverse_iterator rbegin() { return reverse_iterator(end()); } |
| 123 | const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } |
| 124 | const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); } |
| 125 | reverse_iterator rend() { return reverse_iterator(begin()); } |
| 126 | const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } |
| 127 | const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); } |
| 128 | |
| 129 | // Size. |
| 130 | size_type size() const { return size_; } |
| 131 | bool empty() const { return size() == 0u; } |
| 132 | |
| 133 | // Element access. NOTE: Not providing at(). |
| 134 | |
| 135 | reference operator[](size_type n) { |
| 136 | DCHECK_LT(n, size_); |
| 137 | return array_[n]; |
| 138 | } |
| 139 | |
| 140 | const_reference operator[](size_type n) const { |
| 141 | DCHECK_LT(n, size_); |
| 142 | return array_[n]; |
| 143 | } |
| 144 | |
| 145 | reference front() { |
| 146 | DCHECK_NE(size_, 0u); |
| 147 | return array_[0]; |
| 148 | } |
| 149 | |
| 150 | const_reference front() const { |
| 151 | DCHECK_NE(size_, 0u); |
| 152 | return array_[0]; |
| 153 | } |
| 154 | |
| 155 | reference back() { |
| 156 | DCHECK_NE(size_, 0u); |
| 157 | return array_[size_ - 1u]; |
| 158 | } |
| 159 | |
| 160 | const_reference back() const { |
| 161 | DCHECK_NE(size_, 0u); |
| 162 | return array_[size_ - 1u]; |
| 163 | } |
| 164 | |
| 165 | value_type* data() { return array_; } |
| 166 | const value_type* data() const { return array_; } |
| 167 | |
Vladimir Marko | 211c211 | 2015-09-24 16:52:33 +0100 | [diff] [blame] | 168 | ArrayRef SubArray(size_type pos) const { |
| 169 | return SubArray(pos, size_ - pos); |
| 170 | } |
| 171 | ArrayRef SubArray(size_type pos, size_type length) const { |
| 172 | DCHECK_LE(pos, size()); |
| 173 | DCHECK_LE(length, size() - pos); |
| 174 | return ArrayRef(array_ + pos, length); |
| 175 | } |
| 176 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 177 | private: |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 178 | T* array_; |
| 179 | size_t size_; |
| 180 | }; |
| 181 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 182 | template <typename T> |
| 183 | bool operator==(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) { |
| 184 | return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); |
| 185 | } |
| 186 | |
| 187 | template <typename T> |
| 188 | bool operator!=(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) { |
| 189 | return !(lhs == rhs); |
| 190 | } |
| 191 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 192 | } // namespace art |
| 193 | |
| 194 | |
| 195 | #endif // ART_COMPILER_UTILS_ARRAY_REF_H_ |