blob: d651ac51291a7436821407bf43301bc8a7f991e6 [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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_JOIN
25#define ARM_COMPUTE_TEST_DATASET_JOIN
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 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 */
46template <typename T, typename U>
47class JoinDataset : public Dataset
48{
49private:
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +000050 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 Pflanzer69e44dc2017-07-05 11:02:14 +010054
55public:
56 /** Construct dataset from the given datasets.
57 *
58 * @param[in] dataset1 First dataset.
59 * @param[in] dataset2 Second dataset.
60 */
61 JoinDataset(T &&dataset1, U &&dataset2)
62 : _dataset1{ std::forward<T>(dataset1) },
63 _dataset2{ std::forward<U>(dataset2) }
64 {
65 }
66
Alex Gildayc357c472018-03-21 13:54:09 +000067 /** Allow instances of this class to be move constructed */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010068 JoinDataset(JoinDataset &&) = default;
69
70 /** Type of the dataset. */
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +000071 using type = typename T_noref::type;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010072
73 /** Iterator for the dataset. */
74 struct iterator
75 {
Alex Gildayc357c472018-03-21 13:54:09 +000076 /** Construct an iterator.
77 *
78 * @param[in] dataset1 Dataset 1.
79 * @param[in] dataset2 Dataset 2.
80 */
Diego Lopez Recas61ef5bf2017-12-11 12:36:55 +000081 iterator(const T_noref *dataset1, const U_noref *dataset2)
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010082 : _iter1{ dataset1->begin() }, _iter2{ dataset2->begin() }, _first_size{ dataset1->size() }
83 {
84 }
85
Alex Gildayc357c472018-03-21 13:54:09 +000086 /** Get the description of the current value.
87 *
88 * @return description of the current value.
89 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010090 std::string description() const
91 {
92 return _first_size > 0 ? _iter1.description() : _iter2.description();
93 }
94
Alex Gildayc357c472018-03-21 13:54:09 +000095 /** Get the value of the iterator.
96 *
97 * @return the value of the iterator.
98 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010099 JoinDataset::type operator*() const
100 {
101 return _first_size > 0 ? *_iter1 : *_iter2;
102 }
103
Alex Gildayc357c472018-03-21 13:54:09 +0000104 /** Inrement the iterator.
105 *
106 * @return *this;
107 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100108 iterator &operator++()
109 {
110 if(_first_size > 0)
111 {
112 --_first_size;
113 ++_iter1;
114 }
115 else
116 {
117 ++_iter2;
118 }
119
120 return *this;
121 }
122
123 private:
124 iter1_type _iter1;
125 iter2_type _iter2;
126 int _first_size;
127 };
128
129 /** Iterator pointing at the begin of the dataset.
130 *
131 * @return Iterator for the dataset.
132 */
133 iterator begin() const
134 {
135 return iterator(&_dataset1, &_dataset2);
136 }
137
138 /** Size of the dataset.
139 *
140 * @return Number of values in the dataset.
141 */
142 int size() const
143 {
144 return _dataset1.size() + _dataset2.size();
145 }
146
147private:
148 T _dataset1;
149 U _dataset2;
150};
151
152/** Helper function to create a @ref JoinDataset.
153 *
154 * @param[in] dataset1 First dataset.
155 * @param[in] dataset2 Second dataset.
156 *
157 * @return A join dataset.
158 */
159template <typename T, typename U>
160JoinDataset<T, U> concat(T &&dataset1, U &&dataset2)
161{
162 return JoinDataset<T, U>(std::forward<T>(dataset1), std::forward<U>(dataset2));
163}
164} // namespace dataset
165} // namespace framework
166} // namespace test
167} // namespace arm_compute
168#endif /* ARM_COMPUTE_TEST_DATASET_JOIN */