xref: /aosp_15_r20/art/libartbase/base/transform_array_ref.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTBASE_BASE_TRANSFORM_ARRAY_REF_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_TRANSFORM_ARRAY_REF_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <type_traits>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "array_ref.h"
23*795d594fSAndroid Build Coastguard Worker #include "transform_iterator.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker /**
28*795d594fSAndroid Build Coastguard Worker  * @brief An ArrayRef<> wrapper that uses a transformation function for element access.
29*795d594fSAndroid Build Coastguard Worker  */
30*795d594fSAndroid Build Coastguard Worker template <typename BaseType, typename Function>
31*795d594fSAndroid Build Coastguard Worker class TransformArrayRef {
32*795d594fSAndroid Build Coastguard Worker  private:
33*795d594fSAndroid Build Coastguard Worker   using Iter = TransformIterator<typename ArrayRef<BaseType>::iterator, Function>;
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker   // The Function may take a non-const reference, so const_iterator may not exist.
36*795d594fSAndroid Build Coastguard Worker   struct FallbackConstIter {
37*795d594fSAndroid Build Coastguard Worker     using pointer = void;
38*795d594fSAndroid Build Coastguard Worker     using reference = void;
39*795d594fSAndroid Build Coastguard Worker   };
40*795d594fSAndroid Build Coastguard Worker   using PreferredConstIter =
41*795d594fSAndroid Build Coastguard Worker       TransformIterator<typename ArrayRef<BaseType>::const_iterator, Function>;
42*795d594fSAndroid Build Coastguard Worker   static constexpr bool kHasConstIter = std::is_invocable_v<Function, const BaseType&>;
43*795d594fSAndroid Build Coastguard Worker   using ConstIter = std::conditional_t<kHasConstIter, PreferredConstIter, FallbackConstIter>;
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker  public:
46*795d594fSAndroid Build Coastguard Worker   using value_type = typename Iter::value_type;
47*795d594fSAndroid Build Coastguard Worker   using reference = typename Iter::reference;
48*795d594fSAndroid Build Coastguard Worker   using const_reference = typename ConstIter::reference;
49*795d594fSAndroid Build Coastguard Worker   using pointer = typename Iter::pointer;
50*795d594fSAndroid Build Coastguard Worker   using const_pointer = typename ConstIter::pointer;
51*795d594fSAndroid Build Coastguard Worker   using iterator = Iter;
52*795d594fSAndroid Build Coastguard Worker   using const_iterator = std::conditional_t<kHasConstIter, ConstIter, void>;
53*795d594fSAndroid Build Coastguard Worker   using reverse_iterator = std::reverse_iterator<Iter>;
54*795d594fSAndroid Build Coastguard Worker   using const_reverse_iterator =
55*795d594fSAndroid Build Coastguard Worker       std::conditional_t<kHasConstIter, std::reverse_iterator<ConstIter>, void>;
56*795d594fSAndroid Build Coastguard Worker   using difference_type = typename ArrayRef<BaseType>::difference_type;
57*795d594fSAndroid Build Coastguard Worker   using size_type = typename ArrayRef<BaseType>::size_type;
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker   // Constructors.
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   TransformArrayRef(const TransformArrayRef& other) = default;
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker   template <typename OtherBT>
TransformArrayRef(const ArrayRef<OtherBT> & base,Function fn)64*795d594fSAndroid Build Coastguard Worker   TransformArrayRef(const ArrayRef<OtherBT>& base, Function fn)
65*795d594fSAndroid Build Coastguard Worker       : data_(base, fn) { }
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker   template <typename OtherBT,
68*795d594fSAndroid Build Coastguard Worker             typename = std::enable_if_t<std::is_same_v<BaseType, const OtherBT>>>
TransformArrayRef(const TransformArrayRef<OtherBT,Function> & other)69*795d594fSAndroid Build Coastguard Worker   TransformArrayRef(const TransformArrayRef<OtherBT, Function>& other)
70*795d594fSAndroid Build Coastguard Worker       : TransformArrayRef(other.base(), other.GetFunction()) { }
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker   // Assignment operators.
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker   TransformArrayRef& operator=(const TransformArrayRef& other) = default;
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   template <typename OtherBT,
77*795d594fSAndroid Build Coastguard Worker             typename = std::enable_if_t<std::is_same_v<BaseType, const OtherBT>>>
78*795d594fSAndroid Build Coastguard Worker   TransformArrayRef& operator=(const TransformArrayRef<OtherBT, Function>& other) {
79*795d594fSAndroid Build Coastguard Worker     return *this = TransformArrayRef(other.base(), other.GetFunction());
80*795d594fSAndroid Build Coastguard Worker   }
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker   // Destructor.
83*795d594fSAndroid Build Coastguard Worker   ~TransformArrayRef() = default;
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker   // Iterators.
begin()86*795d594fSAndroid Build Coastguard Worker   iterator begin() { return MakeIterator(base().begin()); }
begin()87*795d594fSAndroid Build Coastguard Worker   const_iterator begin() const { return MakeIterator(base().cbegin()); }
cbegin()88*795d594fSAndroid Build Coastguard Worker   const_iterator cbegin() const { return MakeIterator(base().cbegin()); }
end()89*795d594fSAndroid Build Coastguard Worker   iterator end() { return MakeIterator(base().end()); }
end()90*795d594fSAndroid Build Coastguard Worker   const_iterator end() const { return MakeIterator(base().cend()); }
cend()91*795d594fSAndroid Build Coastguard Worker   const_iterator cend() const { return MakeIterator(base().cend()); }
rbegin()92*795d594fSAndroid Build Coastguard Worker   reverse_iterator rbegin() { return reverse_iterator(end()); }
rbegin()93*795d594fSAndroid Build Coastguard Worker   const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
crbegin()94*795d594fSAndroid Build Coastguard Worker   const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
rend()95*795d594fSAndroid Build Coastguard Worker   reverse_iterator rend() { return reverse_iterator(begin()); }
rend()96*795d594fSAndroid Build Coastguard Worker   const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
crend()97*795d594fSAndroid Build Coastguard Worker   const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker   // Size.
size()100*795d594fSAndroid Build Coastguard Worker   size_type size() const { return base().size(); }
empty()101*795d594fSAndroid Build Coastguard Worker   bool empty() const { return base().empty(); }
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker   // Element access. NOTE: Not providing data().
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker   reference operator[](size_type n) { return GetFunction()(base()[n]); }
106*795d594fSAndroid Build Coastguard Worker   const_reference operator[](size_type n) const { return GetFunction()(base()[n]); }
107*795d594fSAndroid Build Coastguard Worker 
front()108*795d594fSAndroid Build Coastguard Worker   reference front() { return GetFunction()(base().front()); }
front()109*795d594fSAndroid Build Coastguard Worker   const_reference front() const { return GetFunction()(base().front()); }
110*795d594fSAndroid Build Coastguard Worker 
back()111*795d594fSAndroid Build Coastguard Worker   reference back() { return GetFunction()(base().back()); }
back()112*795d594fSAndroid Build Coastguard Worker   const_reference back() const { return GetFunction()(base().back()); }
113*795d594fSAndroid Build Coastguard Worker 
SubArray(size_type pos)114*795d594fSAndroid Build Coastguard Worker   TransformArrayRef SubArray(size_type pos) {
115*795d594fSAndroid Build Coastguard Worker     return TransformArrayRef(base().subarray(pos), GetFunction());
116*795d594fSAndroid Build Coastguard Worker   }
SubArray(size_type pos)117*795d594fSAndroid Build Coastguard Worker   TransformArrayRef SubArray(size_type pos) const {
118*795d594fSAndroid Build Coastguard Worker     return TransformArrayRef(base().subarray(pos), GetFunction());
119*795d594fSAndroid Build Coastguard Worker   }
SubArray(size_type pos,size_type length)120*795d594fSAndroid Build Coastguard Worker   TransformArrayRef SubArray(size_type pos, size_type length) const {
121*795d594fSAndroid Build Coastguard Worker     return TransformArrayRef(base().subarray(pos, length), GetFunction());
122*795d594fSAndroid Build Coastguard Worker   }
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker   // Retrieve the base ArrayRef<>.
base()125*795d594fSAndroid Build Coastguard Worker   ArrayRef<BaseType> base() {
126*795d594fSAndroid Build Coastguard Worker     return data_.base_;
127*795d594fSAndroid Build Coastguard Worker   }
base()128*795d594fSAndroid Build Coastguard Worker   ArrayRef<const BaseType> base() const {
129*795d594fSAndroid Build Coastguard Worker     return ArrayRef<const BaseType>(data_.base_);
130*795d594fSAndroid Build Coastguard Worker   }
131*795d594fSAndroid Build Coastguard Worker 
132*795d594fSAndroid Build Coastguard Worker  private:
133*795d594fSAndroid Build Coastguard Worker   // Allow EBO for state-less Function.
134*795d594fSAndroid Build Coastguard Worker   struct Data : Function {
135*795d594fSAndroid Build Coastguard Worker    public:
DataData136*795d594fSAndroid Build Coastguard Worker     Data(ArrayRef<BaseType> base, Function fn) : Function(fn), base_(base) { }
137*795d594fSAndroid Build Coastguard Worker 
138*795d594fSAndroid Build Coastguard Worker     ArrayRef<BaseType> base_;
139*795d594fSAndroid Build Coastguard Worker   };
140*795d594fSAndroid Build Coastguard Worker 
GetFunction()141*795d594fSAndroid Build Coastguard Worker   const Function& GetFunction() const {
142*795d594fSAndroid Build Coastguard Worker     return static_cast<const Function&>(data_);
143*795d594fSAndroid Build Coastguard Worker   }
144*795d594fSAndroid Build Coastguard Worker 
145*795d594fSAndroid Build Coastguard Worker   template <typename BaseIterator>
MakeIterator(BaseIterator base)146*795d594fSAndroid Build Coastguard Worker   auto MakeIterator(BaseIterator base) const {
147*795d594fSAndroid Build Coastguard Worker     return MakeTransformIterator(base, GetFunction());
148*795d594fSAndroid Build Coastguard Worker   }
149*795d594fSAndroid Build Coastguard Worker 
150*795d594fSAndroid Build Coastguard Worker   Data data_;
151*795d594fSAndroid Build Coastguard Worker 
152*795d594fSAndroid Build Coastguard Worker   template <typename OtherBT, typename OtherFunction>
153*795d594fSAndroid Build Coastguard Worker   friend class TransformArrayRef;
154*795d594fSAndroid Build Coastguard Worker };
155*795d594fSAndroid Build Coastguard Worker 
156*795d594fSAndroid Build Coastguard Worker template <typename BaseType, typename Function>
157*795d594fSAndroid Build Coastguard Worker bool operator==(const TransformArrayRef<BaseType, Function>& lhs,
158*795d594fSAndroid Build Coastguard Worker                 const TransformArrayRef<BaseType, Function>& rhs) {
159*795d594fSAndroid Build Coastguard Worker   return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
160*795d594fSAndroid Build Coastguard Worker }
161*795d594fSAndroid Build Coastguard Worker 
162*795d594fSAndroid Build Coastguard Worker template <typename BaseType, typename Function>
163*795d594fSAndroid Build Coastguard Worker bool operator!=(const TransformArrayRef<BaseType, Function>& lhs,
164*795d594fSAndroid Build Coastguard Worker                 const TransformArrayRef<BaseType, Function>& rhs) {
165*795d594fSAndroid Build Coastguard Worker   return !(lhs == rhs);
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker 
168*795d594fSAndroid Build Coastguard Worker template <typename ValueType, typename Function>
MakeTransformArrayRef(ArrayRef<ValueType> container,Function f)169*795d594fSAndroid Build Coastguard Worker TransformArrayRef<ValueType, Function> MakeTransformArrayRef(
170*795d594fSAndroid Build Coastguard Worker     ArrayRef<ValueType> container, Function f) {
171*795d594fSAndroid Build Coastguard Worker   return TransformArrayRef<ValueType, Function>(container, f);
172*795d594fSAndroid Build Coastguard Worker }
173*795d594fSAndroid Build Coastguard Worker 
174*795d594fSAndroid Build Coastguard Worker template <typename Container, typename Function>
MakeTransformArrayRef(Container & container,Function f)175*795d594fSAndroid Build Coastguard Worker TransformArrayRef<typename Container::value_type, Function> MakeTransformArrayRef(
176*795d594fSAndroid Build Coastguard Worker     Container& container, Function f) {
177*795d594fSAndroid Build Coastguard Worker   return TransformArrayRef<typename Container::value_type, Function>(
178*795d594fSAndroid Build Coastguard Worker       ArrayRef<typename Container::value_type>(container.data(), container.size()), f);
179*795d594fSAndroid Build Coastguard Worker }
180*795d594fSAndroid Build Coastguard Worker 
181*795d594fSAndroid Build Coastguard Worker template <typename Container, typename Function>
MakeTransformArrayRef(const Container & container,Function f)182*795d594fSAndroid Build Coastguard Worker TransformArrayRef<const typename Container::value_type, Function> MakeTransformArrayRef(
183*795d594fSAndroid Build Coastguard Worker     const Container& container, Function f) {
184*795d594fSAndroid Build Coastguard Worker   return TransformArrayRef<const typename Container::value_type, Function>(
185*795d594fSAndroid Build Coastguard Worker       ArrayRef<const typename Container::value_type>(container.data(), container.size()), f);
186*795d594fSAndroid Build Coastguard Worker }
187*795d594fSAndroid Build Coastguard Worker 
188*795d594fSAndroid Build Coastguard Worker }  // namespace art
189*795d594fSAndroid Build Coastguard Worker 
190*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTBASE_BASE_TRANSFORM_ARRAY_REF_H_
191