blob: 637abe0a83f96be1c68465902b2d140103271838 [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
2 * Copyright (c) 2017 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_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
63 RangeDataset(RangeDataset &&) = default;
64
65 /** Type of the dataset. */
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010066 using type = std::tuple<T>;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010067
68 /** Iterator for the dataset. */
69 struct iterator
70 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010071 iterator(std::string name, T start, T step)
72 : _name{ name }, _value{ start }, _step{ step }
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010073 {
74 }
75
76 std::string description() const
77 {
78 using support::cpp11::to_string;
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010079 return _name + "=" + to_string(_value);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010080 }
81
82 RangeDataset::type operator*() const
83 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010084 return std::make_tuple(_value);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010085 }
86
87 iterator &operator++()
88 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010089 _value += _step;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010090 return *this;
91 }
92
93 private:
94 std::string _name;
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010095 T _value;
96 T _step;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010097 };
98
99 /** Iterator pointing at the begin of the dataset.
100 *
101 * @return Iterator for the dataset.
102 */
103 iterator begin() const
104 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100105 return iterator(name(), _start, _step);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100106 }
107
108 /** Size of the dataset.
109 *
110 * @return Number of values in the dataset.
111 */
112 int size() const
113 {
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100114 return (_end - _start) / std::abs(_step);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100115 }
116
117private:
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100118 T _start;
119 T _end;
120 T _step;
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100121};
122
123/** Helper function to create a @ref RangeDataset.
124 *
125 * @param[in] name Name of the dataset.
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100126 * @param[in] start Begin of the range.
127 * @param[in] end End of the range.
128 * @param[in] step Step size.
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100129 *
130 * @return A range dataset.
131 */
132template <typename T>
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100133RangeDataset<T> make(std::string name, T start, T end, T step = 1)
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100134{
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +0100135 return RangeDataset<T>(std::move(name), start, end, step);
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100136}
137} // namespace dataset
138} // namespace framework
139} // namespace test
140} // namespace arm_compute
141#endif /* ARM_COMPUTE_TEST_DATASET_RANGE */