Compute Library
 21.02
ZipDataset.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2018 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 
33 namespace arm_compute
34 {
35 namespace test
36 {
37 namespace framework
38 {
39 namespace 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  */
46 template <typename T, typename U>
47 class ZipDataset : 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  ZipDataset(T &&dataset1, U &&dataset2)
60  : _dataset1{ std::forward<T>(dataset1) },
61  _dataset2{ std::forward<U>(dataset2) }
62  {
63  }
64 
65  /** Allow instances of this class to be move constructed */
66  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  {
74  /** Construct an iterator.
75  *
76  * @param[in] iter1 Iterator 1.
77  * @param[in] iter2 Iterator 2.
78  */
79  iterator(iter1_type iter1, iter2_type iter2)
80  : _iter1{ std::move(iter1) }, _iter2{ std::move(iter2) }
81  {
82  }
83 
84  /** Get the description of the current value.
85  *
86  * @return description of the current value.
87  */
88  std::string description() const
89  {
90  return _iter1.description() + ":" + _iter2.description();
91  }
92 
93  /** Get the value of the iterator.
94  *
95  * @return the value of the iterator.
96  */
98  {
99  return std::tuple_cat(*_iter1, *_iter2);
100  }
101 
102  /** Inrement the iterator.
103  *
104  * @return *this;
105  */
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 
136 private:
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  */
148 template <typename T, typename U>
149 ZipDataset<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 */
iterator & operator++()
Inrement the iterator.
Definition: ZipDataset.h:106
Copyright (c) 2017-2021 Arm Limited.
Implementation of a dataset representing pairs of values of the input datasets.
Definition: ZipDataset.h:47
Abstract dataset base class.
Definition: Dataset.h:39
int size() const
Size of the dataset.
Definition: ZipDataset.h:131
iterator(iter1_type iter1, iter2_type iter2)
Construct an iterator.
Definition: ZipDataset.h:79
ZipDataset::type operator*() const
Get the value of the iterator.
Definition: ZipDataset.h:97
decltype(std::tuple_cat(*std::declval< iter1_type >(), *std::declval< iter2_type >())) type
Type of the dataset.
Definition: ZipDataset.h:69
ZipDataset(T &&dataset1, U &&dataset2)
Construct dataset from the given datasets.
Definition: ZipDataset.h:59
iterator begin() const
Iterator pointing at the begin of the dataset.
Definition: ZipDataset.h:122
ZipDataset< T, U > zip(T &&dataset1, U &&dataset2)
Helper function to create a ZipDataset.
Definition: ZipDataset.h:149
std::string description() const
Get the description of the current value.
Definition: ZipDataset.h:88