blob: a58ff4439dd967aeb86fe20ad3fd07a0d4cce23e [file] [log] [blame]
Alex Lighte64300b2015-12-15 15:02:47 -08001/*
2 * Copyright (C) 2015 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 Sehrc431b9d2018-03-02 12:01:51 -080017#ifndef ART_LIBARTBASE_BASE_ARRAY_SLICE_H_
18#define ART_LIBARTBASE_BASE_ARRAY_SLICE_H_
Alex Lighte64300b2015-12-15 15:02:47 -080019
Alex Light86fe9b82020-11-16 16:54:01 +000020#include <ostream>
David Sehr1979c642018-04-26 14:41:18 -070021#include "bit_utils.h"
22#include "casts.h"
23#include "iteration_range.h"
Alex Lightd6fa4f22019-09-11 14:45:31 -070024#include "length_prefixed_array.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "stride_iterator.h"
Alex Lighte64300b2015-12-15 15:02:47 -080026
27namespace art {
28
29// An ArraySlice is an abstraction over an array or a part of an array of a particular type. It does
30// bounds checking and can be made from several common array-like structures in Art.
Vladimir Marko9ac77492017-06-14 18:07:03 +010031template <typename T>
Alex Lighte64300b2015-12-15 15:02:47 -080032class ArraySlice {
33 public:
Vladimir Marko9ac77492017-06-14 18:07:03 +010034 using value_type = T;
35 using reference = T&;
36 using const_reference = const T&;
37 using pointer = T*;
38 using const_pointer = const T*;
39 using iterator = StrideIterator<T>;
40 using const_iterator = StrideIterator<const T>;
41 using reverse_iterator = std::reverse_iterator<iterator>;
42 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
43 using difference_type = ptrdiff_t;
44 using size_type = size_t;
45
Alex Lighte64300b2015-12-15 15:02:47 -080046 // Create an empty array slice.
47 ArraySlice() : array_(nullptr), size_(0), element_size_(0) {}
48
49 // Create an array slice of the first 'length' elements of the array, with each element being
50 // element_size bytes long.
51 ArraySlice(T* array,
52 size_t length,
53 size_t element_size = sizeof(T))
54 : array_(array),
55 size_(dchecked_integral_cast<uint32_t>(length)),
56 element_size_(element_size) {
57 DCHECK(array_ != nullptr || length == 0);
58 }
59
Alex Lightd6fa4f22019-09-11 14:45:31 -070060 ArraySlice(LengthPrefixedArray<T>* lpa,
61 size_t element_size = sizeof(T),
62 size_t alignment = alignof(T))
63 : ArraySlice(
64 lpa != nullptr && lpa->size() != 0 ? &lpa->At(0, element_size, alignment) : nullptr,
65 lpa != nullptr ? lpa->size() : 0,
66 element_size) {}
Alex Light86fe9b82020-11-16 16:54:01 +000067 ArraySlice(const ArraySlice<T>&) = default;
68 ArraySlice(ArraySlice<T>&&) = default;
69 ArraySlice<T>& operator=(const ArraySlice<T>&) = default;
70 ArraySlice<T>& operator=(ArraySlice<T>&&) = default;
Alex Lightd6fa4f22019-09-11 14:45:31 -070071
Vladimir Marko9ac77492017-06-14 18:07:03 +010072 // Iterators.
73 iterator begin() { return iterator(&AtUnchecked(0), element_size_); }
74 const_iterator begin() const { return const_iterator(&AtUnchecked(0), element_size_); }
75 const_iterator cbegin() const { return const_iterator(&AtUnchecked(0), element_size_); }
76 StrideIterator<T> end() { return StrideIterator<T>(&AtUnchecked(size_), element_size_); }
77 const_iterator end() const { return const_iterator(&AtUnchecked(size_), element_size_); }
78 const_iterator cend() const { return const_iterator(&AtUnchecked(size_), element_size_); }
79 reverse_iterator rbegin() { return reverse_iterator(end()); }
80 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
81 const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
82 reverse_iterator rend() { return reverse_iterator(begin()); }
83 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
84 const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
Alex Lighte64300b2015-12-15 15:02:47 -080085
Vladimir Marko9ac77492017-06-14 18:07:03 +010086 // Size.
87 size_type size() const { return size_; }
88 bool empty() const { return size() == 0u; }
Alex Lighte64300b2015-12-15 15:02:47 -080089
Vladimir Marko9ac77492017-06-14 18:07:03 +010090 // Element access. NOTE: Not providing at() and data().
91
92 reference operator[](size_t index) {
Alex Lighte64300b2015-12-15 15:02:47 -080093 DCHECK_LT(index, size_);
94 return AtUnchecked(index);
95 }
96
Vladimir Marko9ac77492017-06-14 18:07:03 +010097 const_reference operator[](size_t index) const {
Alex Lighte64300b2015-12-15 15:02:47 -080098 DCHECK_LT(index, size_);
99 return AtUnchecked(index);
100 }
101
Vladimir Marko9ac77492017-06-14 18:07:03 +0100102 reference front() {
103 DCHECK(!empty());
104 return (*this)[0];
Alex Lighte64300b2015-12-15 15:02:47 -0800105 }
106
Vladimir Marko9ac77492017-06-14 18:07:03 +0100107 const_reference front() const {
108 DCHECK(!empty());
109 return (*this)[0];
Alex Lighte64300b2015-12-15 15:02:47 -0800110 }
111
Vladimir Marko9ac77492017-06-14 18:07:03 +0100112 reference back() {
113 DCHECK(!empty());
114 return (*this)[size_ - 1u];
Alex Lighte64300b2015-12-15 15:02:47 -0800115 }
116
Vladimir Marko9ac77492017-06-14 18:07:03 +0100117 const_reference back() const {
118 DCHECK(!empty());
119 return (*this)[size_ - 1u];
Alex Lighte64300b2015-12-15 15:02:47 -0800120 }
121
Vladimir Marko9ac77492017-06-14 18:07:03 +0100122 ArraySlice<T> SubArray(size_type pos) {
123 return SubArray(pos, size() - pos);
Alex Lighte64300b2015-12-15 15:02:47 -0800124 }
125
Vladimir Marko9ac77492017-06-14 18:07:03 +0100126 ArraySlice<const T> SubArray(size_type pos) const {
127 return SubArray(pos, size() - pos);
Alex Lighte64300b2015-12-15 15:02:47 -0800128 }
129
Vladimir Marko9ac77492017-06-14 18:07:03 +0100130 ArraySlice<T> SubArray(size_type pos, size_type length) {
131 DCHECK_LE(pos, size());
132 DCHECK_LE(length, size() - pos);
133 return ArraySlice<T>(&AtUnchecked(pos), length, element_size_);
Alex Lighte64300b2015-12-15 15:02:47 -0800134 }
135
Vladimir Marko9ac77492017-06-14 18:07:03 +0100136 ArraySlice<const T> SubArray(size_type pos, size_type length) const {
137 DCHECK_LE(pos, size());
138 DCHECK_LE(length, size() - pos);
139 return ArraySlice<const T>(&AtUnchecked(pos), length, element_size_);
Alex Lighte64300b2015-12-15 15:02:47 -0800140 }
141
142 size_t ElementSize() const {
143 return element_size_;
144 }
145
Mathieu Chartier65975772016-08-05 10:46:36 -0700146 bool Contains(const T* element) const {
Alex Lightd6fa4f22019-09-11 14:45:31 -0700147 return &AtUnchecked(0) <= element && element < &AtUnchecked(size_) &&
148 ((reinterpret_cast<uintptr_t>(element) -
149 reinterpret_cast<uintptr_t>(&AtUnchecked(0))) % element_size_) == 0;
150 }
151
152 size_t OffsetOf(const T* element) const {
153 DCHECK(Contains(element));
154 // Since it's possible element_size_ != sizeof(T) we cannot just use pointer arithmatic
155 uintptr_t base_ptr = reinterpret_cast<uintptr_t>(&AtUnchecked(0));
156 uintptr_t obj_ptr = reinterpret_cast<uintptr_t>(element);
157 return (obj_ptr - base_ptr) / element_size_;
Mathieu Chartier65975772016-08-05 10:46:36 -0700158 }
159
Alex Lighte64300b2015-12-15 15:02:47 -0800160 private:
161 T& AtUnchecked(size_t index) {
162 return *reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(array_) + index * element_size_);
163 }
164
165 const T& AtUnchecked(size_t index) const {
166 return *reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(array_) + index * element_size_);
167 }
168
169 T* array_;
170 size_t size_;
171 size_t element_size_;
172};
173
Alex Light86fe9b82020-11-16 16:54:01 +0000174template<typename T>
175std::ostream& operator<<(std::ostream& os, const ArraySlice<T>& ts) {
176 bool first = true;
177 os << "[";
178 for (const T& t : ts) {
179 if (!first) { os << ", "; }
180 first = false;
181 os << t;
182 }
183 os << "]";
184 return os;
185}
186
Alex Lighte64300b2015-12-15 15:02:47 -0800187} // namespace art
188
David Sehrc431b9d2018-03-02 12:01:51 -0800189#endif // ART_LIBARTBASE_BASE_ARRAY_SLICE_H_