blob: b95b7209a719ed65cabd8566549b2c0dd110513b [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
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_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
65 ZipDataset(ZipDataset &&) = default;
66
67 /** Type of the dataset. */
68 using type = decltype(std::tuple_cat(*std::declval<iter1_type>(), *std::declval<iter2_type>()));
69
70 /** Iterator for the dataset. */
71 struct iterator
72 {
73 iterator(iter1_type iter1, iter2_type iter2)
74 : _iter1{ std::move(iter1) }, _iter2{ std::move(iter2) }
75 {
76 }
77
78 std::string description() const
79 {
80 return _iter1.description() + ":" + _iter2.description();
81 }
82
83 ZipDataset::type operator*() const
84 {
85 return std::tuple_cat(*_iter1, *_iter2);
86 }
87
88 iterator &operator++()
89 {
90 ++_iter1;
91 ++_iter2;
92 return *this;
93 }
94
95 private:
96 iter1_type _iter1;
97 iter2_type _iter2;
98 };
99
100 /** Iterator pointing at the begin of the dataset.
101 *
102 * @return Iterator for the dataset.
103 */
104 iterator begin() const
105 {
106 return iterator(_dataset1.begin(), _dataset2.begin());
107 }
108
109 /** Size of the dataset.
110 *
111 * @return Number of values in the dataset.
112 */
113 int size() const
114 {
115 return std::min(_dataset1.size(), _dataset2.size());
116 }
117
118private:
119 T _dataset1;
120 U _dataset2;
121};
122
123/** Helper function to create a @ref ZipDataset.
124 *
125 * @param[in] dataset1 First dataset.
126 * @param[in] dataset2 Second dataset.
127 *
128 * @return A zip dataset.
129 */
130template <typename T, typename U>
131ZipDataset<T, U> zip(T &&dataset1, U &&dataset2)
132{
133 return ZipDataset<T, U>(std::forward<T>(dataset1), std::forward<U>(dataset2));
134}
135} // namespace dataset
136} // namespace framework
137} // namespace test
138} // namespace arm_compute
139#endif /* ARM_COMPUTE_TEST_DATASET_ZIP */