1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_LIBARTBASE_BASE_ARRAY_SLICE_H_
#define ART_LIBARTBASE_BASE_ARRAY_SLICE_H_
#include "bit_utils.h"
#include "casts.h"
#include "iteration_range.h"
#include "length_prefixed_array.h"
#include "stride_iterator.h"
namespace art {
// An ArraySlice is an abstraction over an array or a part of an array of a particular type. It does
// bounds checking and can be made from several common array-like structures in Art.
template <typename T>
class ArraySlice {
public:
using value_type = T;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
using iterator = StrideIterator<T>;
using const_iterator = StrideIterator<const T>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using difference_type = ptrdiff_t;
using size_type = size_t;
// Create an empty array slice.
ArraySlice() : array_(nullptr), size_(0), element_size_(0) {}
// Create an array slice of the first 'length' elements of the array, with each element being
// element_size bytes long.
ArraySlice(T* array,
size_t length,
size_t element_size = sizeof(T))
: array_(array),
size_(dchecked_integral_cast<uint32_t>(length)),
element_size_(element_size) {
DCHECK(array_ != nullptr || length == 0);
}
ArraySlice(LengthPrefixedArray<T>* lpa,
size_t element_size = sizeof(T),
size_t alignment = alignof(T))
: ArraySlice(
lpa != nullptr && lpa->size() != 0 ? &lpa->At(0, element_size, alignment) : nullptr,
lpa != nullptr ? lpa->size() : 0,
element_size) {}
// Iterators.
iterator begin() { return iterator(&AtUnchecked(0), element_size_); }
const_iterator begin() const { return const_iterator(&AtUnchecked(0), element_size_); }
const_iterator cbegin() const { return const_iterator(&AtUnchecked(0), element_size_); }
StrideIterator<T> end() { return StrideIterator<T>(&AtUnchecked(size_), element_size_); }
const_iterator end() const { return const_iterator(&AtUnchecked(size_), element_size_); }
const_iterator cend() const { return const_iterator(&AtUnchecked(size_), element_size_); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
// Size.
size_type size() const { return size_; }
bool empty() const { return size() == 0u; }
// Element access. NOTE: Not providing at() and data().
reference operator[](size_t index) {
DCHECK_LT(index, size_);
return AtUnchecked(index);
}
const_reference operator[](size_t index) const {
DCHECK_LT(index, size_);
return AtUnchecked(index);
}
reference front() {
DCHECK(!empty());
return (*this)[0];
}
const_reference front() const {
DCHECK(!empty());
return (*this)[0];
}
reference back() {
DCHECK(!empty());
return (*this)[size_ - 1u];
}
const_reference back() const {
DCHECK(!empty());
return (*this)[size_ - 1u];
}
ArraySlice<T> SubArray(size_type pos) {
return SubArray(pos, size() - pos);
}
ArraySlice<const T> SubArray(size_type pos) const {
return SubArray(pos, size() - pos);
}
ArraySlice<T> SubArray(size_type pos, size_type length) {
DCHECK_LE(pos, size());
DCHECK_LE(length, size() - pos);
return ArraySlice<T>(&AtUnchecked(pos), length, element_size_);
}
ArraySlice<const T> SubArray(size_type pos, size_type length) const {
DCHECK_LE(pos, size());
DCHECK_LE(length, size() - pos);
return ArraySlice<const T>(&AtUnchecked(pos), length, element_size_);
}
size_t ElementSize() const {
return element_size_;
}
bool Contains(const T* element) const {
return &AtUnchecked(0) <= element && element < &AtUnchecked(size_) &&
((reinterpret_cast<uintptr_t>(element) -
reinterpret_cast<uintptr_t>(&AtUnchecked(0))) % element_size_) == 0;
}
size_t OffsetOf(const T* element) const {
DCHECK(Contains(element));
// Since it's possible element_size_ != sizeof(T) we cannot just use pointer arithmatic
uintptr_t base_ptr = reinterpret_cast<uintptr_t>(&AtUnchecked(0));
uintptr_t obj_ptr = reinterpret_cast<uintptr_t>(element);
return (obj_ptr - base_ptr) / element_size_;
}
private:
T& AtUnchecked(size_t index) {
return *reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(array_) + index * element_size_);
}
const T& AtUnchecked(size_t index) const {
return *reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(array_) + index * element_size_);
}
T* array_;
size_t size_;
size_t element_size_;
};
} // namespace art
#endif // ART_LIBARTBASE_BASE_ARRAY_SLICE_H_
|