blob: bd63fb8b921b0dc129e922a4fc02ed61a222114f [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2017-2020 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"
Anthony Barbier4dbc0a92018-08-27 13:39:47 +010029#include "tests/TypePrinter.h"
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010030
31#include <string>
32#include <tuple>
33#include <type_traits>
34#include <utility>
35#include <vector>
36
37namespace arm_compute
38{
39namespace test
40{
41namespace framework
42{
43namespace dataset
44{
45/** Base case. Nothing is a container. */
46template <typename T>
47struct is_container : public std::false_type
48{
49};
50
51/** Vector is considered a container. */
52template <typename V, typename A>
53struct is_container<std::vector<V, A>> : public std::true_type
54{
55};
56
57/** Implementation of a dataset created from a container. */
58template <typename T>
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010059class ContainerDataset : public NamedDataset
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010060{
61private:
62 using container_value_type = typename T::value_type;
63 using container_const_iterator = typename T::const_iterator;
64
65public:
66 /** Construct dataset with given name and values from the container.
67 *
68 * @param[in] name Description of the values.
69 * @param[in] container Values for the dataset.
70 */
71 ContainerDataset(std::string name, T &&container)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010072 : NamedDataset{ std::move(name) }, _container(std::forward<T>(container))
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010073 {
74 }
75
Alex Gildayc357c472018-03-21 13:54:09 +000076 /** Allow instances of this class to be copy constructed */
Diego Lopez Recas0021d752017-12-18 14:42:56 +000077 ContainerDataset(const ContainerDataset &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +000078 /** Allow instances of this class to be move constructed */
79 ContainerDataset(ContainerDataset &&) = default;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010080
81 /** Type of the dataset. */
82 using type = std::tuple<container_value_type>;
83
84 /** Iterator for the dataset. */
85 struct iterator
86 {
Alex Gildayc357c472018-03-21 13:54:09 +000087 /** Construct iterator
88 *
89 * @param[in] name Description of the values.
90 * @param[in] iterator Iterator for the values.
91 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010092 iterator(std::string name, container_const_iterator iterator)
93 : _name{ name }, _iterator{ iterator }
94 {
95 }
96
Alex Gildayc357c472018-03-21 13:54:09 +000097 /** Get a description of the current value.
98 *
99 * @return a description.
100 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100101 std::string description() const
102 {
103 using support::cpp11::to_string;
104 return _name + "=" + to_string(*_iterator);
105 }
106
Alex Gildayc357c472018-03-21 13:54:09 +0000107 /** Get the current value.
108 *
109 * @return the current value.
110 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100111 ContainerDataset::type operator*() const
112 {
113 return std::make_tuple(*_iterator);
114 }
115
Alex Gildayc357c472018-03-21 13:54:09 +0000116 /** Increment the iterator.
117 *
118 * @return this.
119 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100120 iterator &operator++()
121 {
122 ++_iterator;
123 return *this;
124 }
125
126 private:
127 std::string _name;
128 container_const_iterator _iterator;
129 };
130
131 /** Iterator pointing at the begin of the dataset.
132 *
133 * @return Iterator for the dataset.
134 */
135 iterator begin() const
136 {
137 return iterator(name(), _container.cbegin());
138 }
139
140 /** Size of the dataset.
141 *
142 * @return Number of values in the dataset.
143 */
144 int size() const
145 {
146 return _container.size();
147 }
148
149private:
150 T _container;
151};
152
153/** Helper function to create a @ref ContainerDataset.
154 *
155 * @param[in] name Name of the dataset.
156 * @param[in] values Container.
157 *
158 * @return A container dataset.
159 */
160template <typename T>
161typename std::enable_if<is_container<T>::value, ContainerDataset<T>>::type make(std::string name, T &&values)
162{
163 return ContainerDataset<T>(std::move(name), std::forward<T>(values));
164}
165} // namespace dataset
166} // namespace framework
167} // namespace test
168} // namespace arm_compute
169#endif /* ARM_COMPUTE_TEST_DATASET_CONTAINER */