blob: 87aae5f30836b24e94ff286dabc72b1b52e2f19b [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_LIST
25#define ARM_COMPUTE_TEST_DATASET_LIST
26
27#include "Dataset.h"
Anthony Barbier671a11e2018-07-06 15:11:36 +010028#include "utils/TypePrinter.h"
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010029
30#include <initializer_list>
31#include <string>
32#include <tuple>
33#include <utility>
34#include <vector>
35
36namespace arm_compute
37{
38namespace test
39{
40namespace framework
41{
42namespace dataset
43{
44/** Implementation of a dataset created from an initializer list. */
45template <typename T>
46class InitializerListDataset final : public NamedDataset
47{
48private:
49 using data_const_iterator = typename std::vector<T>::const_iterator;
50
51public:
52 /** Construct dataset with given name and values from the container.
53 *
54 * @param[in] name Description of the values.
55 * @param[in] list Values for the dataset.
56 */
57 InitializerListDataset(std::string name, std::initializer_list<T> &&list)
58 : NamedDataset{ std::move(name) }, _data(std::forward<std::initializer_list<T>>(list))
59 {
60 }
61
Alex Gildayc357c472018-03-21 13:54:09 +000062 /** Allow instances of this class to be move constructed */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010063 InitializerListDataset(InitializerListDataset &&) = default;
64
65 /** Type of the dataset. */
66 using type = std::tuple<T>;
67
68 /** Iterator for the dataset. */
69 struct iterator
70 {
Alex Gildayc357c472018-03-21 13:54:09 +000071 /** Construct an iterator for the dataset
72 *
73 * @param[in] name Name of the dataset.
74 * @param[in] iterator Iterator of the dataset values.
75 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010076 iterator(std::string name, data_const_iterator iterator)
77 : _name{ name }, _iterator{ iterator }
78 {
79 }
80
Alex Gildayc357c472018-03-21 13:54:09 +000081 /** Get a description of the current value.
82 *
83 * @return a description.
84 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010085 std::string description() const
86 {
Anthony Barbier671a11e2018-07-06 15:11:36 +010087 return _name + "=" + arm_compute::to_string(*_iterator);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010088 }
89
Alex Gildayc357c472018-03-21 13:54:09 +000090 /** Get the current value.
91 *
92 * @return the current value.
93 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010094 InitializerListDataset::type operator*() const
95 {
96 return std::make_tuple(*_iterator);
97 }
98
Alex Gildayc357c472018-03-21 13:54:09 +000099 /** Increment the iterator.
100 *
101 * @return *this.
102 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100103 iterator &operator++()
104 {
105 ++_iterator;
106 return *this;
107 }
108
109 private:
110 std::string _name;
111 data_const_iterator _iterator;
112 };
113
114 /** Iterator pointing at the begin of the dataset.
115 *
116 * @return Iterator for the dataset.
117 */
118 iterator begin() const
119 {
120 return iterator(name(), _data.cbegin());
121 }
122
123 /** Size of the dataset.
124 *
125 * @return Number of values in the dataset.
126 */
127 int size() const
128 {
129 return _data.size();
130 }
131
132private:
133 std::vector<T> _data;
134};
135
136/** Helper function to create an @ref InitializerListDataset.
137 *
138 * @param[in] name Name of the dataset.
139 * @param[in] list Initializer list.
140 *
141 * @return An initializer list dataset.
142 */
143template <typename T>
144InitializerListDataset<T> make(std::string name, std::initializer_list<T> &&list)
145{
146 return InitializerListDataset<T>(std::move(name), std::forward<std::initializer_list<T>>(list));
147}
148} // namespace dataset
149} // namespace framework
150} // namespace test
151} // namespace arm_compute
152#endif /* ARM_COMPUTE_TEST_DATASET_LIST */