Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_DATASET_CARTESIAN_PRODUCT |
| 25 | #define ARM_COMPUTE_TEST_DATASET_CARTESIAN_PRODUCT |
| 26 | |
| 27 | #include "Dataset.h" |
| 28 | |
| 29 | #include <string> |
| 30 | #include <tuple> |
| 31 | #include <utility> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | namespace framework |
| 38 | { |
| 39 | namespace dataset |
| 40 | { |
| 41 | /** Implementation of a dataset representing all combinations of values of the input datasets. |
| 42 | * |
| 43 | * For example, for the inputs {1, 2} and {3, 4} this dataset virtually |
| 44 | * represents the values {(1, 3), (1, 4), (2, 3), (2, 4)}. |
| 45 | */ |
| 46 | template <typename T, typename U> |
| 47 | class CartesianProductDataset : public Dataset |
| 48 | { |
| 49 | private: |
Moritz Pflanzer | fce8795 | 2017-07-19 10:02:07 +0100 | [diff] [blame] | 50 | using T_noref = typename std::remove_reference<T>::type; |
| 51 | using U_noref = typename std::remove_reference<U>::type; |
| 52 | using iter1_type = typename T_noref::iterator; |
| 53 | using iter2_type = typename U_noref::iterator; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 54 | |
| 55 | public: |
| 56 | /** Construct dataset from the given datasets. |
| 57 | * |
| 58 | * @param[in] dataset1 First dataset. |
| 59 | * @param[in] dataset2 Second dataset. |
| 60 | */ |
| 61 | CartesianProductDataset(T &&dataset1, U &&dataset2) |
| 62 | : _dataset1{ std::forward<T>(dataset1) }, |
| 63 | _dataset2{ std::forward<U>(dataset2) } |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | CartesianProductDataset(CartesianProductDataset &&) = default; |
| 68 | |
| 69 | /** Type of the dataset. */ |
| 70 | using type = decltype(std::tuple_cat(*std::declval<iter1_type>(), *std::declval<iter2_type>())); |
| 71 | |
| 72 | /** Iterator for the dataset. */ |
| 73 | struct iterator |
| 74 | { |
Moritz Pflanzer | fce8795 | 2017-07-19 10:02:07 +0100 | [diff] [blame] | 75 | iterator(const T_noref *dataset1, const U_noref *dataset2) |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 76 | : _iter1{ dataset1->begin() }, |
| 77 | _dataset2{ dataset2 }, |
| 78 | _iter2{ dataset2->begin() } |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | iterator(const iterator &) = default; |
| 83 | iterator &operator=(const iterator &) = default; |
| 84 | iterator(iterator &&) = default; |
| 85 | iterator &operator=(iterator &&) = default; |
| 86 | |
| 87 | ~iterator() = default; |
| 88 | |
| 89 | std::string description() const |
| 90 | { |
| 91 | return _iter1.description() + ":" + _iter2.description(); |
| 92 | } |
| 93 | |
| 94 | CartesianProductDataset::type operator*() const |
| 95 | { |
| 96 | return std::tuple_cat(*_iter1, *_iter2); |
| 97 | } |
| 98 | |
| 99 | iterator &operator++() |
| 100 | { |
| 101 | ++_second_pos; |
| 102 | |
| 103 | if(_second_pos < _dataset2->size()) |
| 104 | { |
| 105 | ++_iter2; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | _second_pos = 0; |
| 110 | _iter2 = _dataset2->begin(); |
| 111 | |
| 112 | ++_iter1; |
| 113 | } |
| 114 | |
| 115 | return *this; |
| 116 | } |
| 117 | |
| 118 | private: |
Moritz Pflanzer | fce8795 | 2017-07-19 10:02:07 +0100 | [diff] [blame] | 119 | iter1_type _iter1; |
| 120 | const U_noref *_dataset2; |
| 121 | iter2_type _iter2; |
| 122 | int _first_pos{ 0 }; |
| 123 | int _second_pos{ 0 }; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | /** Iterator pointing at the begin of the dataset. |
| 127 | * |
| 128 | * @return Iterator for the dataset. |
| 129 | */ |
| 130 | iterator begin() const |
| 131 | { |
| 132 | return iterator(&_dataset1, &_dataset2); |
| 133 | } |
| 134 | |
| 135 | /** Size of the dataset. |
| 136 | * |
| 137 | * @return Number of values in the dataset. |
| 138 | */ |
| 139 | int size() const |
| 140 | { |
| 141 | return _dataset1.size() * _dataset2.size(); |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | T _dataset1; |
| 146 | U _dataset2; |
| 147 | }; |
| 148 | |
| 149 | /** Helper function to create a @ref CartesianProductDataset. |
| 150 | * |
| 151 | * @param[in] dataset1 First dataset. |
| 152 | * @param[in] dataset2 Second dataset. |
| 153 | * |
| 154 | * @return A grid dataset. |
| 155 | */ |
| 156 | template <typename T, typename U> |
| 157 | CartesianProductDataset<T, U> combine(T &&dataset1, U &&dataset2) |
| 158 | { |
| 159 | return CartesianProductDataset<T, U>(std::forward<T>(dataset1), std::forward<U>(dataset2)); |
| 160 | } |
| 161 | } // namespace dataset |
| 162 | } // namespace framework |
| 163 | } // namespace test |
| 164 | } // namespace arm_compute |
| 165 | #endif /* ARM_COMPUTE_TEST_DATASET_CARTESIAN_PRODUCT */ |