Compute Library
 22.11
RangeDataset.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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/StringSupport.h"
29 
30 #include <string>
31 #include <tuple>
32 #include <type_traits>
33 #include <utility>
34 
35 namespace arm_compute
36 {
37 namespace test
38 {
39 namespace framework
40 {
41 namespace dataset
42 {
43 /** 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  */
47 template <typename T>
48 class RangeDataset final : public NamedDataset
49 {
50 public:
51  /** Construct dataset with given name and values in the specified range.
52  *
53  * @param[in] name Description of the values.
54  * @param[in] start Begin of the range.
55  * @param[in] end End of the range.
56  * @param[in] step Step size.
57  */
58  RangeDataset(std::string name, T start, T end, T step = 1)
59  : NamedDataset{ std::move(name) }, _start{ start }, _end{ end }, _step{ step }
60  {
61  }
62 
63  /** Allow instances of this class to be move constructed */
64  RangeDataset(RangeDataset &&) = default;
65 
66  /** Type of the dataset. */
67  using type = std::tuple<T>;
68 
69  /** Iterator for the dataset. */
70  struct iterator
71  {
72  /** 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  */
78  iterator(std::string name, T start, T step)
79  : _name{ name }, _value{ start }, _step{ step }
80  {
81  }
82 
83  /** Get the description of the current value.
84  *
85  * @return description of the current value.
86  */
87  std::string description() const
88  {
90  return _name + "=" + to_string(_value);
91  }
92 
93  /** Get the value of the iterator.
94  *
95  * @return the value of the iterator.
96  */
98  {
99  return std::make_tuple(_value);
100  }
101 
102  /** Inrement the iterator.
103  *
104  * @return *this;
105  */
107  {
108  _value += _step;
109  return *this;
110  }
111 
112  private:
113  std::string _name;
114  T _value;
115  T _step;
116  };
117 
118  /** Iterator pointing at the begin of the dataset.
119  *
120  * @return Iterator for the dataset.
121  */
122  iterator begin() const
123  {
124  return iterator(name(), _start, _step);
125  }
126 
127  /** Size of the dataset.
128  *
129  * @return Number of values in the dataset.
130  */
131  int size() const
132  {
133  return (_end - _start) / std::abs(_step);
134  }
135 
136 private:
137  T _start;
138  T _end;
139  T _step;
140 };
141 
142 /** Helper function to create a @ref RangeDataset.
143  *
144  * @param[in] name Name of the dataset.
145  * @param[in] start Begin of the range.
146  * @param[in] end End of the range.
147  * @param[in] step Step size.
148  *
149  * @return A range dataset.
150  */
151 template <typename T>
152 RangeDataset<T> make(std::string name, T start, T end, T step = 1)
153 {
154  return RangeDataset<T>(std::move(name), start, end, step);
155 }
156 } // namespace dataset
157 } // namespace framework
158 } // namespace test
159 } // namespace arm_compute
160 #endif /* ARM_COMPUTE_TEST_DATASET_RANGE */
Abstract implementation of a named dataset.
Definition: Dataset.h:56
std::string to_string(DatasetMode mode)
Definition: DatasetModes.h:100
std::tuple< T > type
Type of the dataset.
Definition: RangeDataset.h:67
std::string to_string(T &&value)
Convert integer and float values to string.
iterator(std::string name, T start, T step)
Construct an iterator.
Definition: RangeDataset.h:78
RangeDataset(std::string name, T start, T end, T step=1)
Construct dataset with given name and values in the specified range.
Definition: RangeDataset.h:58
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
Copyright (c) 2017-2022 Arm Limited.
iterator begin() const
Iterator pointing at the begin of the dataset.
Definition: RangeDataset.h:122
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
constexpr int step
Definition: fp32.cpp:35
Implementation of a dataset created from a range of values.
Definition: RangeDataset.h:48
std::string description() const
Get the description of the current value.
Definition: RangeDataset.h:87
std::string name() const
Return name of the dataset.
Definition: Dataset.h:79
RangeDataset::type operator*() const
Get the value of the iterator.
Definition: RangeDataset.h:97