blob: a08756694aa9ffd05907741b57124c47d2317968 [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * 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_RANGE
25#define ARM_COMPUTE_TEST_DATASET_RANGE
26
27#include "Dataset.h"
28#include "support/ToolchainSupport.h"
29
30#include <string>
31#include <tuple>
32#include <type_traits>
33#include <utility>
34
35namespace arm_compute
36{
37namespace test
38{
39namespace framework
40{
41namespace dataset
42{
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010043/** Implementation of a dataset created from a range of values.
44 *
45 * The range is inclusive of the first value but exclusive of the last, i.e. [start, end).
46 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010047template <typename T>
48class RangeDataset final : public NamedDataset
49{
50public:
51 /** Construct dataset with given name and values in the specified range.
52 *
53 * @param[in] name Description of the values.
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010054 * @param[in] start Begin of the range.
55 * @param[in] end End of the range.
56 * @param[in] step Step size.
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010057 */
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010058 RangeDataset(std::string name, T start, T end, T step = 1)
59 : NamedDataset{ std::move(name) }, _start{ start }, _end{ end }, _step{ step }
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010060 {
61 }
62
Alex Gildayc357c472018-03-21 13:54:09 +000063 /** Allow instances of this class to be move constructed */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010064 RangeDataset(RangeDataset &&) = default;
65
66 /** Type of the dataset. */
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010067 using type = std::tuple<T>;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010068
69 /** Iterator for the dataset. */
70 struct iterator
71 {
Alex Gildayc357c472018-03-21 13:54:09 +000072 /** Construct an iterator.
73 *
74 * @param[in] name Dataset name.
75 * @param[in] start Dataset start value.
76 * @param[in] step Dataset step size.
77 */
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010078 iterator(std::string name, T start, T step)
79 : _name{ name }, _value{ start }, _step{ step }
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010080 {
81 }
82
Alex Gildayc357c472018-03-21 13:54:09 +000083 /** Get the description of the current value.
84 *
85 * @return description of the current value.
86 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010087 std::string description() const
88 {
89 using support::cpp11::to_string;
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010090 return _name + "=" + to_string(_value);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010091 }
92
Alex Gildayc357c472018-03-21 13:54:09 +000093 /** Get the value of the iterator.
94 *
95 * @return the value of the iterator.
96 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010097 RangeDataset::type operator*() const
98 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010099 return std::make_tuple(_value);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100100 }
101
Alex Gildayc357c472018-03-21 13:54:09 +0000102 /** Inrement the iterator.
103 *
104 * @return *this;
105 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100106 iterator &operator++()
107 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100108 _value += _step;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100109 return *this;
110 }
111
112 private:
113 std::string _name;
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100114 T _value;
115 T _step;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100116 };
117
118 /** Iterator pointing at the begin of the dataset.
119 *
120 * @return Iterator for the dataset.
121 */
122 iterator begin() const
123 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100124 return iterator(name(), _start, _step);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100125 }
126
127 /** Size of the dataset.
128 *
129 * @return Number of values in the dataset.
130 */
131 int size() const
132 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100133 return (_end - _start) / std::abs(_step);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100134 }
135
136private:
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100137 T _start;
138 T _end;
139 T _step;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100140};
141
142/** Helper function to create a @ref RangeDataset.
143 *
144 * @param[in] name Name of the dataset.
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100145 * @param[in] start Begin of the range.
146 * @param[in] end End of the range.
147 * @param[in] step Step size.
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100148 *
149 * @return A range dataset.
150 */
151template <typename T>
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100152RangeDataset<T> make(std::string name, T start, T end, T step = 1)
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100153{
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100154 return RangeDataset<T>(std::move(name), start, end, step);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100155}
156} // namespace dataset
157} // namespace framework
158} // namespace test
159} // namespace arm_compute
160#endif /* ARM_COMPUTE_TEST_DATASET_RANGE */