blob: 3d93b925068b534cb41233acd9cb364f38a8b38f [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01003 *
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_ZIP
25#define ARM_COMPUTE_TEST_DATASET_ZIP
26
27#include "Dataset.h"
28
29#include <string>
30#include <tuple>
31#include <utility>
32
33namespace arm_compute
34{
35namespace test
36{
37namespace framework
38{
39namespace dataset
40{
41/** Implementation of a dataset representing pairs 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)}.
45 */
46template <typename T, typename U>
47class ZipDataset : public Dataset
48{
49private:
50 using iter1_type = typename T::iterator;
51 using iter2_type = typename U::iterator;
52
53public:
54 /** Construct dataset from the given datasets.
55 *
56 * @param[in] dataset1 First dataset.
57 * @param[in] dataset2 Second dataset.
58 */
59 ZipDataset(T &&dataset1, U &&dataset2)
60 : _dataset1{ std::forward<T>(dataset1) },
61 _dataset2{ std::forward<U>(dataset2) }
62 {
63 }
64
Alex Gildayc357c472018-03-21 13:54:09 +000065 /** Allow instances of this class to be move constructed */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010066 ZipDataset(ZipDataset &&) = default;
67
68 /** Type of the dataset. */
69 using type = decltype(std::tuple_cat(*std::declval<iter1_type>(), *std::declval<iter2_type>()));
70
71 /** Iterator for the dataset. */
72 struct iterator
73 {
Alex Gildayc357c472018-03-21 13:54:09 +000074 /** Construct an iterator.
75 *
76 * @param[in] iter1 Iterator 1.
77 * @param[in] iter2 Iterator 2.
78 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010079 iterator(iter1_type iter1, iter2_type iter2)
80 : _iter1{ std::move(iter1) }, _iter2{ std::move(iter2) }
81 {
82 }
83
Alex Gildayc357c472018-03-21 13:54:09 +000084 /** Get the description of the current value.
85 *
86 * @return description of the current value.
87 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010088 std::string description() const
89 {
90 return _iter1.description() + ":" + _iter2.description();
91 }
92
Alex Gildayc357c472018-03-21 13:54:09 +000093 /** Get the value of the iterator.
94 *
95 * @return the value of the iterator.
96 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010097 ZipDataset::type operator*() const
98 {
99 return std::tuple_cat(*_iter1, *_iter2);
100 }
101
Alex Gildayc357c472018-03-21 13:54:09 +0000102 /** Inrement the iterator.
103 *
104 * @return *this;
105 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100106 iterator &operator++()
107 {
108 ++_iter1;
109 ++_iter2;
110 return *this;
111 }
112
113 private:
114 iter1_type _iter1;
115 iter2_type _iter2;
116 };
117
118 /** Iterator pointing at the begin of the dataset.
119 *
120 * @return Iterator for the dataset.
121 */
122 iterator begin() const
123 {
124 return iterator(_dataset1.begin(), _dataset2.begin());
125 }
126
127 /** Size of the dataset.
128 *
129 * @return Number of values in the dataset.
130 */
131 int size() const
132 {
133 return std::min(_dataset1.size(), _dataset2.size());
134 }
135
136private:
137 T _dataset1;
138 U _dataset2;
139};
140
141/** Helper function to create a @ref ZipDataset.
142 *
143 * @param[in] dataset1 First dataset.
144 * @param[in] dataset2 Second dataset.
145 *
146 * @return A zip dataset.
147 */
148template <typename T, typename U>
149ZipDataset<T, U> zip(T &&dataset1, U &&dataset2)
150{
151 return ZipDataset<T, U>(std::forward<T>(dataset1), std::forward<U>(dataset2));
152}
153} // namespace dataset
154} // namespace framework
155} // namespace test
156} // namespace arm_compute
157#endif /* ARM_COMPUTE_TEST_DATASET_ZIP */