1 /*
2 * Copyright (C) 2016 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_LIBARTBASE_BASE_TRANSFORM_ARRAY_REF_H_
18 #define ART_LIBARTBASE_BASE_TRANSFORM_ARRAY_REF_H_
19
20 #include <type_traits>
21
22 #include "array_ref.h"
23 #include "transform_iterator.h"
24
25 namespace art {
26
27 /**
28 * @brief An ArrayRef<> wrapper that uses a transformation function for element access.
29 */
30 template <typename BaseType, typename Function>
31 class TransformArrayRef {
32 private:
33 using Iter = TransformIterator<typename ArrayRef<BaseType>::iterator, Function>;
34
35 // The Function may take a non-const reference, so const_iterator may not exist.
36 using FallbackConstIter = std::iterator<std::random_access_iterator_tag, void, void, void, void>;
37 using PreferredConstIter =
38 TransformIterator<typename ArrayRef<BaseType>::const_iterator, Function>;
39 template <typename F, typename = std::result_of_t<F(const BaseType&)>>
40 static PreferredConstIter ConstIterHelper(int&);
41 template <typename F>
42 static FallbackConstIter ConstIterHelper(const int&);
43
44 using ConstIter = decltype(ConstIterHelper<Function>(*reinterpret_cast<int*>(0)));
45
46 public:
47 using value_type = typename Iter::value_type;
48 using reference = typename Iter::reference;
49 using const_reference = typename ConstIter::reference;
50 using pointer = typename Iter::pointer;
51 using const_pointer = typename ConstIter::pointer;
52 using iterator = Iter;
53 using const_iterator =
54 std::conditional_t<std::is_same_v<ConstIter, FallbackConstIter>, void, ConstIter>;
55 using reverse_iterator = std::reverse_iterator<Iter>;
56 using const_reverse_iterator = std::conditional_t<std::is_same_v<ConstIter, FallbackConstIter>,
57 void,
58 std::reverse_iterator<ConstIter>>;
59 using difference_type = typename ArrayRef<BaseType>::difference_type;
60 using size_type = typename ArrayRef<BaseType>::size_type;
61
62 // Constructors.
63
64 TransformArrayRef(const TransformArrayRef& other) = default;
65
66 template <typename OtherBT>
TransformArrayRef(const ArrayRef<OtherBT> & base,Function fn)67 TransformArrayRef(const ArrayRef<OtherBT>& base, Function fn)
68 : data_(base, fn) { }
69
70 template <typename OtherBT,
71 typename = std::enable_if_t<std::is_same_v<BaseType, const OtherBT>>>
TransformArrayRef(const TransformArrayRef<OtherBT,Function> & other)72 TransformArrayRef(const TransformArrayRef<OtherBT, Function>& other)
73 : TransformArrayRef(other.base(), other.GetFunction()) { }
74
75 // Assignment operators.
76
77 TransformArrayRef& operator=(const TransformArrayRef& other) = default;
78
79 template <typename OtherBT,
80 typename = std::enable_if_t<std::is_same_v<BaseType, const OtherBT>>>
81 TransformArrayRef& operator=(const TransformArrayRef<OtherBT, Function>& other) {
82 return *this = TransformArrayRef(other.base(), other.GetFunction());
83 }
84
85 // Destructor.
86 ~TransformArrayRef() = default;
87
88 // Iterators.
begin()89 iterator begin() { return MakeIterator(base().begin()); }
begin()90 const_iterator begin() const { return MakeIterator(base().cbegin()); }
cbegin()91 const_iterator cbegin() const { return MakeIterator(base().cbegin()); }
end()92 iterator end() { return MakeIterator(base().end()); }
end()93 const_iterator end() const { return MakeIterator(base().cend()); }
cend()94 const_iterator cend() const { return MakeIterator(base().cend()); }
rbegin()95 reverse_iterator rbegin() { return reverse_iterator(end()); }
rbegin()96 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
crbegin()97 const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
rend()98 reverse_iterator rend() { return reverse_iterator(begin()); }
rend()99 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
crend()100 const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
101
102 // Size.
size()103 size_type size() const { return base().size(); }
empty()104 bool empty() const { return base().empty(); }
105
106 // Element access. NOTE: Not providing data().
107
108 reference operator[](size_type n) { return GetFunction()(base()[n]); }
109 const_reference operator[](size_type n) const { return GetFunction()(base()[n]); }
110
front()111 reference front() { return GetFunction()(base().front()); }
front()112 const_reference front() const { return GetFunction()(base().front()); }
113
back()114 reference back() { return GetFunction()(base().back()); }
back()115 const_reference back() const { return GetFunction()(base().back()); }
116
SubArray(size_type pos)117 TransformArrayRef SubArray(size_type pos) {
118 return TransformArrayRef(base().subarray(pos), GetFunction());
119 }
SubArray(size_type pos)120 TransformArrayRef SubArray(size_type pos) const {
121 return TransformArrayRef(base().subarray(pos), GetFunction());
122 }
SubArray(size_type pos,size_type length)123 TransformArrayRef SubArray(size_type pos, size_type length) const {
124 return TransformArrayRef(base().subarray(pos, length), GetFunction());
125 }
126
127 // Retrieve the base ArrayRef<>.
base()128 ArrayRef<BaseType> base() {
129 return data_.base_;
130 }
base()131 ArrayRef<const BaseType> base() const {
132 return ArrayRef<const BaseType>(data_.base_);
133 }
134
135 private:
136 // Allow EBO for state-less Function.
137 struct Data : Function {
138 public:
DataData139 Data(ArrayRef<BaseType> base, Function fn) : Function(fn), base_(base) { }
140
141 ArrayRef<BaseType> base_;
142 };
143
GetFunction()144 const Function& GetFunction() const {
145 return static_cast<const Function&>(data_);
146 }
147
148 template <typename BaseIterator>
MakeIterator(BaseIterator base)149 auto MakeIterator(BaseIterator base) const {
150 return MakeTransformIterator(base, GetFunction());
151 }
152
153 Data data_;
154
155 template <typename OtherBT, typename OtherFunction>
156 friend class TransformArrayRef;
157 };
158
159 template <typename BaseType, typename Function>
160 bool operator==(const TransformArrayRef<BaseType, Function>& lhs,
161 const TransformArrayRef<BaseType, Function>& rhs) {
162 return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
163 }
164
165 template <typename BaseType, typename Function>
166 bool operator!=(const TransformArrayRef<BaseType, Function>& lhs,
167 const TransformArrayRef<BaseType, Function>& rhs) {
168 return !(lhs == rhs);
169 }
170
171 template <typename ValueType, typename Function>
MakeTransformArrayRef(ArrayRef<ValueType> container,Function f)172 TransformArrayRef<ValueType, Function> MakeTransformArrayRef(
173 ArrayRef<ValueType> container, Function f) {
174 return TransformArrayRef<ValueType, Function>(container, f);
175 }
176
177 template <typename Container, typename Function>
MakeTransformArrayRef(Container & container,Function f)178 TransformArrayRef<typename Container::value_type, Function> MakeTransformArrayRef(
179 Container& container, Function f) {
180 return TransformArrayRef<typename Container::value_type, Function>(
181 ArrayRef<typename Container::value_type>(container.data(), container.size()), f);
182 }
183
184 template <typename Container, typename Function>
MakeTransformArrayRef(const Container & container,Function f)185 TransformArrayRef<const typename Container::value_type, Function> MakeTransformArrayRef(
186 const Container& container, Function f) {
187 return TransformArrayRef<const typename Container::value_type, Function>(
188 ArrayRef<const typename Container::value_type>(container.data(), container.size()), f);
189 }
190
191 } // namespace art
192
193 #endif // ART_LIBARTBASE_BASE_TRANSFORM_ARRAY_REF_H_
194