xref: /aosp_15_r20/external/ComputeLibrary/tests/framework/datasets/RangeDataset.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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          */
iteratoriterator78         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          */
descriptioniterator87         std::string description() const
88         {
89             using support::cpp11::to_string;
90             return _name + "=" + to_string(_value);
91         }
92 
93         /** Get the value of the iterator.
94          *
95          * @return the value of the iterator.
96          */
97         RangeDataset::type operator*() const
98         {
99             return std::make_tuple(_value);
100         }
101 
102         /** Inrement the iterator.
103          *
104          * @return *this;
105          */
106         iterator &operator++()
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      */
begin()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      */
size()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 */
161