blob: 2753c81bd53afee89b9ce00b7e2860e588ca4944 [file] [log] [blame]
Vladimir Marko089142c2014-06-05 10:57:05 +01001/*
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 Brazdild9c90372016-09-14 16:53:55 +010017#ifndef ART_RUNTIME_BASE_ARRAY_REF_H_
18#define ART_RUNTIME_BASE_ARRAY_REF_H_
Vladimir Marko089142c2014-06-05 10:57:05 +010019
20#include <type_traits>
21#include <vector>
22
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
Vladimir Marko089142c2014-06-05 10:57:05 +010024
25namespace art {
26
Vladimir Marko089142c2014-06-05 10:57:05 +010027/**
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 */
40template <typename T>
41class ArrayRef {
42 public:
Vladimir Markoe1993c72017-06-14 17:01:38 +010043 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 Marko089142c2014-06-05 10:57:05 +010054
55 // Constructors.
56
57 constexpr ArrayRef()
58 : array_(nullptr), size_(0u) {
59 }
60
61 template <size_t size>
Vladimir Marko345f93e2015-07-14 18:58:59 +010062 explicit constexpr ArrayRef(T (&array)[size])
Vladimir Marko372f10e2016-05-17 16:30:10 +010063 : array_(array), size_(size) {
Vladimir Marko089142c2014-06-05 10:57:05 +010064 }
65
Vladimir Marko372f10e2016-05-17 16:30:10 +010066 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) {
Vladimir Marko089142c2014-06-05 10:57:05 +010071 }
72
Andreas Gampe277ccbd2014-11-03 21:36:10 -080073 constexpr ArrayRef(T* array_in, size_t size_in)
74 : array_(array_in), size_(size_in) {
Vladimir Marko089142c2014-06-05 10:57:05 +010075 }
76
Vladimir Markoec7802a2015-10-01 20:57:57 +010077 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)
Vladimir Marko089142c2014-06-05 10:57:05 +010081 : array_(v.data()), size_(v.size()) {
82 }
83
Vladimir Markoec7802a2015-10-01 20:57:57 +010084 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)
Vladimir Marko089142c2014-06-05 10:57:05 +010090 : array_(v.data()), size_(v.size()) {
91 }
92
Andreas Gampe758a8012015-04-03 21:28:42 -070093 ArrayRef(const ArrayRef&) = default;
94
Vladimir Marko089142c2014-06-05 10:57:05 +010095 // 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>
Vladimir Marko01106192014-06-05 16:35:31 +0100104 typename std::enable_if<std::is_same<T, const U>::value, ArrayRef>::type&
Vladimir Marko089142c2014-06-05 10:57:05 +0100105 operator=(const ArrayRef<U>& other) {
106 return *this = ArrayRef(other);
107 }
108
Mathieu Chartier51681732018-01-30 10:12:00 -0800109 template <typename U>
110 static ArrayRef Cast(const ArrayRef<U>& src) {
111 return ArrayRef(reinterpret_cast<const T*>(src.data()),
112 src.size() * sizeof(T) / sizeof(U));
113 }
114
Vladimir Marko089142c2014-06-05 10:57:05 +0100115 // Destructor.
116 ~ArrayRef() = default;
117
118 // Iterators.
119 iterator begin() { return array_; }
120 const_iterator begin() const { return array_; }
121 const_iterator cbegin() const { return array_; }
122 iterator end() { return array_ + size_; }
123 const_iterator end() const { return array_ + size_; }
124 const_iterator cend() const { return array_ + size_; }
125 reverse_iterator rbegin() { return reverse_iterator(end()); }
126 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
127 const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
128 reverse_iterator rend() { return reverse_iterator(begin()); }
129 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
130 const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
131
132 // Size.
133 size_type size() const { return size_; }
134 bool empty() const { return size() == 0u; }
135
136 // Element access. NOTE: Not providing at().
137
138 reference operator[](size_type n) {
139 DCHECK_LT(n, size_);
140 return array_[n];
141 }
142
143 const_reference operator[](size_type n) const {
144 DCHECK_LT(n, size_);
145 return array_[n];
146 }
147
148 reference front() {
Vladimir Markoe1993c72017-06-14 17:01:38 +0100149 DCHECK(!empty());
Vladimir Marko089142c2014-06-05 10:57:05 +0100150 return array_[0];
151 }
152
153 const_reference front() const {
Vladimir Markoe1993c72017-06-14 17:01:38 +0100154 DCHECK(!empty());
Vladimir Marko089142c2014-06-05 10:57:05 +0100155 return array_[0];
156 }
157
158 reference back() {
Vladimir Markoe1993c72017-06-14 17:01:38 +0100159 DCHECK(!empty());
Vladimir Marko089142c2014-06-05 10:57:05 +0100160 return array_[size_ - 1u];
161 }
162
163 const_reference back() const {
Vladimir Markoe1993c72017-06-14 17:01:38 +0100164 DCHECK(!empty());
Vladimir Marko089142c2014-06-05 10:57:05 +0100165 return array_[size_ - 1u];
166 }
167
168 value_type* data() { return array_; }
169 const value_type* data() const { return array_; }
170
Vladimir Marko372f10e2016-05-17 16:30:10 +0100171 ArrayRef SubArray(size_type pos) {
172 return SubArray(pos, size() - pos);
Vladimir Marko211c2112015-09-24 16:52:33 +0100173 }
Vladimir Markoe1993c72017-06-14 17:01:38 +0100174
Vladimir Marko372f10e2016-05-17 16:30:10 +0100175 ArrayRef<const T> SubArray(size_type pos) const {
176 return SubArray(pos, size() - pos);
177 }
Vladimir Markoe1993c72017-06-14 17:01:38 +0100178
Vladimir Marko372f10e2016-05-17 16:30:10 +0100179 ArrayRef SubArray(size_type pos, size_type length) {
Vladimir Marko211c2112015-09-24 16:52:33 +0100180 DCHECK_LE(pos, size());
181 DCHECK_LE(length, size() - pos);
Vladimir Marko372f10e2016-05-17 16:30:10 +0100182 return ArrayRef(data() + pos, length);
183 }
Vladimir Markoe1993c72017-06-14 17:01:38 +0100184
Vladimir Marko372f10e2016-05-17 16:30:10 +0100185 ArrayRef<const T> SubArray(size_type pos, size_type length) const {
186 DCHECK_LE(pos, size());
187 DCHECK_LE(length, size() - pos);
188 return ArrayRef<const T>(data() + pos, length);
Vladimir Marko211c2112015-09-24 16:52:33 +0100189 }
190
Vladimir Marko089142c2014-06-05 10:57:05 +0100191 private:
Vladimir Marko089142c2014-06-05 10:57:05 +0100192 T* array_;
193 size_t size_;
194};
195
Vladimir Markof4da6752014-08-01 19:04:18 +0100196template <typename T>
197bool operator==(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) {
198 return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
199}
200
201template <typename T>
202bool operator!=(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) {
203 return !(lhs == rhs);
204}
205
Vladimir Marko089142c2014-06-05 10:57:05 +0100206} // namespace art
207
208
David Brazdild9c90372016-09-14 16:53:55 +0100209#endif // ART_RUNTIME_BASE_ARRAY_REF_H_