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 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_ARRAY_REF_H_ |
| 18 | #define ART_LIBARTBASE_BASE_ARRAY_REF_H_ |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 19 | |
| 20 | #include <type_traits> |
| 21 | #include <vector> |
| 22 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 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 { |
| 42 | public: |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 43 | using value_type = T; |
| 44 | using reference = T&; |
| 45 | using const_reference = const T&; |
| 46 | using pointer = T*; |
| 47 | using const_pointer = const T*; |
| 48 | using iterator = T*; |
| 49 | using const_iterator = const T*; |
| 50 | using reverse_iterator = std::reverse_iterator<iterator>; |
| 51 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 52 | using difference_type = ptrdiff_t; |
| 53 | using size_type = size_t; |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 54 | |
| 55 | // Constructors. |
| 56 | |
| 57 | constexpr ArrayRef() |
| 58 | : array_(nullptr), size_(0u) { |
| 59 | } |
| 60 | |
| 61 | template <size_t size> |
Vladimir Marko | 345f93e | 2015-07-14 18:58:59 +0100 | [diff] [blame] | 62 | explicit constexpr ArrayRef(T (&array)[size]) |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 63 | : array_(array), size_(size) { |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 66 | template <typename U, |
| 67 | size_t size, |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 68 | typename = std::enable_if_t<std::is_same_v<T, const U>>> |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 69 | explicit constexpr ArrayRef(U (&array)[size]) |
| 70 | : array_(array), size_(size) { |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Vladimir Marko | b7bf843 | 2019-12-03 13:18:50 +0000 | [diff] [blame] | 73 | constexpr ArrayRef(T* array, size_t size) |
| 74 | : array_(array), size_(size) { |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 77 | template <typename Vector, |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 78 | typename = std::enable_if_t<std::is_same_v<typename Vector::value_type, value_type>>> |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 79 | explicit ArrayRef(Vector& v) |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 80 | : array_(v.data()), size_(v.size()) { |
| 81 | } |
| 82 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 83 | template <typename Vector, |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 84 | typename = std::enable_if_t< |
| 85 | std::is_same_v<std::add_const_t<typename Vector::value_type>, value_type>>> |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 86 | explicit ArrayRef(const Vector& v) |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 87 | : array_(v.data()), size_(v.size()) { |
| 88 | } |
| 89 | |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 90 | ArrayRef(const ArrayRef&) = default; |
| 91 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 92 | // Assignment operators. |
| 93 | |
| 94 | ArrayRef& operator=(const ArrayRef& other) { |
| 95 | array_ = other.array_; |
| 96 | size_ = other.size_; |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | template <typename U> |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 101 | std::enable_if_t<std::is_same_v<T, const U>, ArrayRef>& |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 102 | operator=(const ArrayRef<U>& other) { |
| 103 | return *this = ArrayRef(other); |
| 104 | } |
| 105 | |
Mathieu Chartier | 5168173 | 2018-01-30 10:12:00 -0800 | [diff] [blame] | 106 | template <typename U> |
| 107 | static ArrayRef Cast(const ArrayRef<U>& src) { |
| 108 | return ArrayRef(reinterpret_cast<const T*>(src.data()), |
| 109 | src.size() * sizeof(T) / sizeof(U)); |
| 110 | } |
| 111 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 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() { |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 146 | DCHECK(!empty()); |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 147 | return array_[0]; |
| 148 | } |
| 149 | |
| 150 | const_reference front() const { |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 151 | DCHECK(!empty()); |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 152 | return array_[0]; |
| 153 | } |
| 154 | |
| 155 | reference back() { |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 156 | DCHECK(!empty()); |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 157 | return array_[size_ - 1u]; |
| 158 | } |
| 159 | |
| 160 | const_reference back() const { |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 161 | DCHECK(!empty()); |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 162 | return array_[size_ - 1u]; |
| 163 | } |
| 164 | |
| 165 | value_type* data() { return array_; } |
| 166 | const value_type* data() const { return array_; } |
| 167 | |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 168 | ArrayRef SubArray(size_type pos) { |
| 169 | return SubArray(pos, size() - pos); |
Vladimir Marko | 211c211 | 2015-09-24 16:52:33 +0100 | [diff] [blame] | 170 | } |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 171 | |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 172 | ArrayRef<const T> SubArray(size_type pos) const { |
| 173 | return SubArray(pos, size() - pos); |
| 174 | } |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 175 | |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 176 | ArrayRef SubArray(size_type pos, size_type length) { |
Vladimir Marko | 211c211 | 2015-09-24 16:52:33 +0100 | [diff] [blame] | 177 | DCHECK_LE(pos, size()); |
| 178 | DCHECK_LE(length, size() - pos); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 179 | return ArrayRef(data() + pos, length); |
| 180 | } |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 181 | |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 182 | ArrayRef<const T> SubArray(size_type pos, size_type length) const { |
| 183 | DCHECK_LE(pos, size()); |
| 184 | DCHECK_LE(length, size() - pos); |
| 185 | return ArrayRef<const T>(data() + pos, length); |
Vladimir Marko | 211c211 | 2015-09-24 16:52:33 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 188 | private: |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 189 | T* array_; |
| 190 | size_t size_; |
| 191 | }; |
| 192 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 193 | template <typename T> |
| 194 | bool operator==(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) { |
| 195 | return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); |
| 196 | } |
| 197 | |
| 198 | template <typename T> |
| 199 | bool operator!=(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) { |
| 200 | return !(lhs == rhs); |
| 201 | } |
| 202 | |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 203 | template<typename T> |
| 204 | std::ostream& operator<<(std::ostream& os, const ArrayRef<T>& ts) { |
| 205 | bool first = true; |
| 206 | os << "["; |
| 207 | for (const T& t : ts) { |
| 208 | if (!first) { os << ", "; } |
| 209 | first = false; |
| 210 | os << t; |
| 211 | } |
| 212 | os << "]"; |
| 213 | return os; |
| 214 | } |
| 215 | |
Vladimir Marko | 089142c | 2014-06-05 10:57:05 +0100 | [diff] [blame] | 216 | } // namespace art |
| 217 | |
| 218 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 219 | #endif // ART_LIBARTBASE_BASE_ARRAY_REF_H_ |