Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame^] | 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_COMPILER_UTILS_TRANSFORM_ARRAY_REF_H_ |
| 18 | #define ART_COMPILER_UTILS_TRANSFORM_ARRAY_REF_H_ |
| 19 | |
| 20 | #include <type_traits> |
| 21 | |
| 22 | #include "utils/array_ref.h" |
| 23 | #include "utils/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 = typename std::result_of<F(const BaseType&)>::type> |
| 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 = typename std::conditional< |
| 54 | std::is_same<ConstIter, FallbackConstIter>::value, |
| 55 | void, |
| 56 | ConstIter>::type; |
| 57 | using reverse_iterator = std::reverse_iterator<Iter>; |
| 58 | using const_reverse_iterator = typename std::conditional< |
| 59 | std::is_same<ConstIter, FallbackConstIter>::value, |
| 60 | void, |
| 61 | std::reverse_iterator<ConstIter>>::type; |
| 62 | using difference_type = typename ArrayRef<BaseType>::difference_type; |
| 63 | using size_type = typename ArrayRef<BaseType>::size_type; |
| 64 | |
| 65 | // Constructors. |
| 66 | |
| 67 | TransformArrayRef(const TransformArrayRef& other) = default; |
| 68 | |
| 69 | template <typename OtherBT> |
| 70 | TransformArrayRef(const ArrayRef<OtherBT>& base, Function fn) |
| 71 | : data_(base, fn) { } |
| 72 | |
| 73 | // Assignment operators. |
| 74 | |
| 75 | TransformArrayRef& operator=(const TransformArrayRef& other) = default; |
| 76 | |
| 77 | template <typename OtherBT, |
| 78 | typename = typename std::enable_if<std::is_same<BaseType, const OtherBT>::value>::type> |
| 79 | TransformArrayRef& operator=(const TransformArrayRef<OtherBT, Function>& other) { |
| 80 | return *this = TransformArrayRef(other.base(), other.GetFunction()); |
| 81 | } |
| 82 | |
| 83 | // Destructor. |
| 84 | ~TransformArrayRef() = default; |
| 85 | |
| 86 | // Iterators. |
| 87 | iterator begin() { return MakeIterator(base().begin()); } |
| 88 | const_iterator begin() const { return MakeIterator(base().cbegin()); } |
| 89 | const_iterator cbegin() const { return MakeIterator(base().cbegin()); } |
| 90 | iterator end() { return MakeIterator(base().end()); } |
| 91 | const_iterator end() const { MakeIterator(base().cend()); } |
| 92 | const_iterator cend() const { return MakeIterator(base().cend()); } |
| 93 | reverse_iterator rbegin() { return reverse_iterator(end()); } |
| 94 | const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } |
| 95 | const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); } |
| 96 | reverse_iterator rend() { return reverse_iterator(begin()); } |
| 97 | const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } |
| 98 | const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); } |
| 99 | |
| 100 | // Size. |
| 101 | size_type size() const { return base().size(); } |
| 102 | bool empty() const { return base().empty(); } |
| 103 | |
| 104 | // Element access. NOTE: Not providing data(). |
| 105 | |
| 106 | reference operator[](size_type n) { return GetFunction()(base()[n]); } |
| 107 | const_reference operator[](size_type n) const { return GetFunction()(base()[n]); } |
| 108 | |
| 109 | reference front() { return GetFunction()(base().front()); } |
| 110 | const_reference front() const { return GetFunction()(base().front()); } |
| 111 | |
| 112 | reference back() { return GetFunction()(base().back()); } |
| 113 | const_reference back() const { return GetFunction()(base().back()); } |
| 114 | |
| 115 | TransformArrayRef SubArray(size_type pos) { |
| 116 | return TransformArrayRef(base().subarray(pos), GetFunction()); |
| 117 | } |
| 118 | TransformArrayRef SubArray(size_type pos) const { |
| 119 | return TransformArrayRef(base().subarray(pos), GetFunction()); |
| 120 | } |
| 121 | TransformArrayRef SubArray(size_type pos, size_type length) const { |
| 122 | return TransformArrayRef(base().subarray(pos, length), GetFunction()); |
| 123 | } |
| 124 | |
| 125 | // Retrieve the base ArrayRef<>. |
| 126 | ArrayRef<BaseType> base() { |
| 127 | return data_.base_; |
| 128 | } |
| 129 | ArrayRef<const BaseType> base() const { |
| 130 | return ArrayRef<const BaseType>(data_.base_); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | // Allow EBO for state-less Function. |
| 135 | struct Data : Function { |
| 136 | public: |
| 137 | Data(ArrayRef<BaseType> base, Function fn) : Function(fn), base_(base) { } |
| 138 | |
| 139 | ArrayRef<BaseType> base_; |
| 140 | }; |
| 141 | |
| 142 | const Function& GetFunction() const { |
| 143 | return static_cast<const Function&>(data_); |
| 144 | } |
| 145 | |
| 146 | template <typename BaseIterator> |
| 147 | auto MakeIterator(BaseIterator base) const { |
| 148 | return MakeTransformIterator(base, GetFunction()); |
| 149 | } |
| 150 | |
| 151 | Data data_; |
| 152 | }; |
| 153 | |
| 154 | template <typename BaseType, typename Function> |
| 155 | bool operator==(const TransformArrayRef<BaseType, Function>& lhs, |
| 156 | const TransformArrayRef<BaseType, Function>& rhs) { |
| 157 | return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); |
| 158 | } |
| 159 | |
| 160 | template <typename BaseType, typename Function> |
| 161 | bool operator!=(const TransformArrayRef<BaseType, Function>& lhs, |
| 162 | const TransformArrayRef<BaseType, Function>& rhs) { |
| 163 | return !(lhs == rhs); |
| 164 | } |
| 165 | |
| 166 | template <typename ValueType, typename Function> |
| 167 | TransformArrayRef<ValueType, Function> MakeTransformArrayRef( |
| 168 | ArrayRef<ValueType> container, Function f) { |
| 169 | return TransformArrayRef<ValueType, Function>(container, f); |
| 170 | } |
| 171 | |
| 172 | template <typename Container, typename Function> |
| 173 | TransformArrayRef<typename Container::value_type, Function> MakeTransformArrayRef( |
| 174 | Container& container, Function f) { |
| 175 | return TransformArrayRef<typename Container::value_type, Function>( |
| 176 | ArrayRef<typename Container::value_type>(container.data(), container.size()), f); |
| 177 | } |
| 178 | |
| 179 | template <typename Container, typename Function> |
| 180 | TransformArrayRef<const typename Container::value_type, Function> MakeTransformArrayRef( |
| 181 | const Container& container, Function f) { |
| 182 | return TransformArrayRef<const typename Container::value_type, Function>( |
| 183 | ArrayRef<const typename Container::value_type>(container.data(), container.size()), f); |
| 184 | } |
| 185 | |
| 186 | } // namespace art |
| 187 | |
| 188 | #endif // ART_COMPILER_UTILS_TRANSFORM_ARRAY_REF_H_ |