blob: 2489405ff786e6bf0fe3d2a5047749eb4dcd0656 [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{
43/** Implementation of a dataset created from a range of values. */
44template <typename T>
45class RangeDataset final : public NamedDataset
46{
47public:
48 /** Construct dataset with given name and values in the specified range.
49 *
50 * @param[in] name Description of the values.
51 * @param[in] first Iterator to the first value.
52 * @param[in] last Iterator behind the last value.
53 */
54 RangeDataset(std::string name, T &&first, T &&last)
55 : NamedDataset{ std::move(name) }, _first{ std::forward<T>(first) }, _last{ std::forward<T>(last) }
56 {
57 }
58
59 RangeDataset(RangeDataset &&) = default;
60
61 /** Type of the dataset. */
62 using type = std::tuple<typename std::iterator_traits<T>::value_type>;
63
64 /** Iterator for the dataset. */
65 struct iterator
66 {
67 iterator(std::string name, T iterator)
68 : _name{ name }, _iterator{ iterator }
69 {
70 }
71
72 std::string description() const
73 {
74 using support::cpp11::to_string;
75 return _name + "=" + to_string(*_iterator);
76 }
77
78 RangeDataset::type operator*() const
79 {
80 return std::make_tuple(*_iterator);
81 }
82
83 iterator &operator++()
84 {
85 ++_iterator;
86 return *this;
87 }
88
89 private:
90 std::string _name;
91 T _iterator;
92 };
93
94 /** Iterator pointing at the begin of the dataset.
95 *
96 * @return Iterator for the dataset.
97 */
98 iterator begin() const
99 {
100 return iterator(name(), _first);
101 }
102
103 /** Size of the dataset.
104 *
105 * @return Number of values in the dataset.
106 */
107 int size() const
108 {
109 return std::distance(_first, _last);
110 }
111
112private:
113 T _first;
114 T _last;
115};
116
117/** Helper function to create a @ref RangeDataset.
118 *
119 * @param[in] name Name of the dataset.
120 * @param[in] first Iterator to the first value.
121 * @param[in] last Iterator behind the last value.
122 *
123 * @return A range dataset.
124 */
125template <typename T>
126RangeDataset<T> make(std::string name, T &&first, T &&last)
127{
128 return RangeDataset<T>(std::move(name), std::forward<T>(first), std::forward<T>(last));
129}
130} // namespace dataset
131} // namespace framework
132} // namespace test
133} // namespace arm_compute
134#endif /* ARM_COMPUTE_TEST_DATASET_RANGE */