blob: de77cb24d6d6130fcebe6309f7aa62b2c5248723 [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
Georgios Pinitasc6f95102021-03-30 10:03:01 +01002 * Copyright (c) 2017-2021 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_CONTAINER
25#define ARM_COMPUTE_TEST_DATASET_CONTAINER
26
27#include "Dataset.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000028#include "support/StringSupport.h"
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010029
30#include <string>
31#include <tuple>
32#include <type_traits>
33#include <utility>
34#include <vector>
35
36namespace arm_compute
37{
38namespace test
39{
40namespace framework
41{
42namespace dataset
43{
44/** Base case. Nothing is a container. */
45template <typename T>
46struct is_container : public std::false_type
47{
48};
49
50/** Vector is considered a container. */
51template <typename V, typename A>
52struct is_container<std::vector<V, A>> : public std::true_type
53{
54};
55
56/** Implementation of a dataset created from a container. */
57template <typename T>
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010058class ContainerDataset : public NamedDataset
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010059{
60private:
61 using container_value_type = typename T::value_type;
62 using container_const_iterator = typename T::const_iterator;
63
64public:
65 /** Construct dataset with given name and values from the container.
66 *
67 * @param[in] name Description of the values.
68 * @param[in] container Values for the dataset.
69 */
70 ContainerDataset(std::string name, T &&container)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010071 : NamedDataset{ std::move(name) }, _container(std::forward<T>(container))
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010072 {
73 }
74
Alex Gildayc357c472018-03-21 13:54:09 +000075 /** Allow instances of this class to be copy constructed */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000076 ContainerDataset(const ContainerDataset &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +000077 /** Allow instances of this class to be move constructed */
78 ContainerDataset(ContainerDataset &&) = default;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010079
80 /** Type of the dataset. */
81 using type = std::tuple<container_value_type>;
82
83 /** Iterator for the dataset. */
84 struct iterator
85 {
Alex Gildayc357c472018-03-21 13:54:09 +000086 /** Construct iterator
87 *
88 * @param[in] name Description of the values.
89 * @param[in] iterator Iterator for the values.
90 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010091 iterator(std::string name, container_const_iterator iterator)
92 : _name{ name }, _iterator{ iterator }
93 {
94 }
95
Alex Gildayc357c472018-03-21 13:54:09 +000096 /** Get a description of the current value.
97 *
98 * @return a description.
99 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100100 std::string description() const
101 {
102 using support::cpp11::to_string;
103 return _name + "=" + to_string(*_iterator);
104 }
105
Alex Gildayc357c472018-03-21 13:54:09 +0000106 /** Get the current value.
107 *
108 * @return the current value.
109 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100110 ContainerDataset::type operator*() const
111 {
112 return std::make_tuple(*_iterator);
113 }
114
Alex Gildayc357c472018-03-21 13:54:09 +0000115 /** Increment the iterator.
116 *
117 * @return this.
118 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100119 iterator &operator++()
120 {
121 ++_iterator;
122 return *this;
123 }
124
125 private:
126 std::string _name;
127 container_const_iterator _iterator;
128 };
129
130 /** Iterator pointing at the begin of the dataset.
131 *
132 * @return Iterator for the dataset.
133 */
134 iterator begin() const
135 {
136 return iterator(name(), _container.cbegin());
137 }
138
139 /** Size of the dataset.
140 *
141 * @return Number of values in the dataset.
142 */
143 int size() const
144 {
145 return _container.size();
146 }
147
148private:
149 T _container;
150};
151
152/** Helper function to create a @ref ContainerDataset.
153 *
154 * @param[in] name Name of the dataset.
155 * @param[in] values Container.
156 *
157 * @return A container dataset.
158 */
159template <typename T>
160typename std::enable_if<is_container<T>::value, ContainerDataset<T>>::type make(std::string name, T &&values)
161{
162 return ContainerDataset<T>(std::move(name), std::forward<T>(values));
163}
164} // namespace dataset
165} // namespace framework
166} // namespace test
167} // namespace arm_compute
168#endif /* ARM_COMPUTE_TEST_DATASET_CONTAINER */