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_JOIN |
| 25 | #define ARM_COMPUTE_TEST_DATASET_JOIN |
| 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 the concatenation of the input datasets. |
| 42 | * |
| 43 | * For example, for the inputs {1, 2} and {3, 4} this dataset virtually |
| 44 | * represents the values {1, 2, 3, 4}. |
| 45 | */ |
| 46 | template <typename T, typename U> |
| 47 | class JoinDataset : public Dataset |
| 48 | { |
| 49 | private: |
| 50 | using iter1_type = typename T::iterator; |
| 51 | using iter2_type = typename U::iterator; |
| 52 | |
| 53 | public: |
| 54 | /** Construct dataset from the given datasets. |
| 55 | * |
| 56 | * @param[in] dataset1 First dataset. |
| 57 | * @param[in] dataset2 Second dataset. |
| 58 | */ |
| 59 | JoinDataset(T &&dataset1, U &&dataset2) |
| 60 | : _dataset1{ std::forward<T>(dataset1) }, |
| 61 | _dataset2{ std::forward<U>(dataset2) } |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | JoinDataset(JoinDataset &&) = default; |
| 66 | |
| 67 | /** Type of the dataset. */ |
| 68 | using type = typename T::type; |
| 69 | |
| 70 | /** Iterator for the dataset. */ |
| 71 | struct iterator |
| 72 | { |
| 73 | iterator(const T *dataset1, const U *dataset2) |
| 74 | : _iter1{ dataset1->begin() }, _iter2{ dataset2->begin() }, _first_size{ dataset1->size() } |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | std::string description() const |
| 79 | { |
| 80 | return _first_size > 0 ? _iter1.description() : _iter2.description(); |
| 81 | } |
| 82 | |
| 83 | JoinDataset::type operator*() const |
| 84 | { |
| 85 | return _first_size > 0 ? *_iter1 : *_iter2; |
| 86 | } |
| 87 | |
| 88 | iterator &operator++() |
| 89 | { |
| 90 | if(_first_size > 0) |
| 91 | { |
| 92 | --_first_size; |
| 93 | ++_iter1; |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | ++_iter2; |
| 98 | } |
| 99 | |
| 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | iter1_type _iter1; |
| 105 | iter2_type _iter2; |
| 106 | int _first_size; |
| 107 | }; |
| 108 | |
| 109 | /** Iterator pointing at the begin of the dataset. |
| 110 | * |
| 111 | * @return Iterator for the dataset. |
| 112 | */ |
| 113 | iterator begin() const |
| 114 | { |
| 115 | return iterator(&_dataset1, &_dataset2); |
| 116 | } |
| 117 | |
| 118 | /** Size of the dataset. |
| 119 | * |
| 120 | * @return Number of values in the dataset. |
| 121 | */ |
| 122 | int size() const |
| 123 | { |
| 124 | return _dataset1.size() + _dataset2.size(); |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | T _dataset1; |
| 129 | U _dataset2; |
| 130 | }; |
| 131 | |
| 132 | /** Helper function to create a @ref JoinDataset. |
| 133 | * |
| 134 | * @param[in] dataset1 First dataset. |
| 135 | * @param[in] dataset2 Second dataset. |
| 136 | * |
| 137 | * @return A join dataset. |
| 138 | */ |
| 139 | template <typename T, typename U> |
| 140 | JoinDataset<T, U> concat(T &&dataset1, U &&dataset2) |
| 141 | { |
| 142 | return JoinDataset<T, U>(std::forward<T>(dataset1), std::forward<U>(dataset2)); |
| 143 | } |
| 144 | } // namespace dataset |
| 145 | } // namespace framework |
| 146 | } // namespace test |
| 147 | } // namespace arm_compute |
| 148 | #endif /* ARM_COMPUTE_TEST_DATASET_JOIN */ |