xref: /aosp_15_r20/external/ComputeLibrary/tests/framework/datasets/InitializerListDataset.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2018 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_LIST
25 #define ARM_COMPUTE_TEST_DATASET_LIST
26 
27 #include "Dataset.h"
28 #include "utils/TypePrinter.h"
29 
30 #include <initializer_list>
31 #include <string>
32 #include <tuple>
33 #include <utility>
34 #include <vector>
35 
36 namespace arm_compute
37 {
38 namespace test
39 {
40 namespace framework
41 {
42 namespace dataset
43 {
44 /** Implementation of a dataset created from an initializer list. */
45 template <typename T>
46 class InitializerListDataset final : public NamedDataset
47 {
48 private:
49     using data_const_iterator = typename std::vector<T>::const_iterator;
50 
51 public:
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      */
InitializerListDataset(std::string name,std::initializer_list<T> && list)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 
62     /** Allow instances of this class to be move constructed */
63     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     {
71         /** 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          */
iteratoriterator76         iterator(std::string name, data_const_iterator iterator)
77             : _name{ name }, _iterator{ iterator }
78         {
79         }
80 
81         /** Get a description of the current value.
82          *
83          * @return a description.
84          */
descriptioniterator85         std::string description() const
86         {
87             return _name + "=" + arm_compute::to_string(*_iterator);
88         }
89 
90         /** Get the current value.
91          *
92          * @return the current value.
93          */
94         InitializerListDataset::type operator*() const
95         {
96             return std::make_tuple(*_iterator);
97         }
98 
99         /** Increment the iterator.
100          *
101          * @return *this.
102          */
103         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      */
begin()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      */
size()127     int size() const
128     {
129         return _data.size();
130     }
131 
132 private:
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  */
143 template <typename T>
make(std::string name,std::initializer_list<T> && list)144 InitializerListDataset<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 */
153