Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 1 | /* |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 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 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace test |
| 38 | { |
| 39 | namespace framework |
| 40 | { |
| 41 | namespace dataset |
| 42 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 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 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 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. |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 54 | * @param[in] start Begin of the range. |
| 55 | * @param[in] end End of the range. |
| 56 | * @param[in] step Step size. |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 57 | */ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 58 | RangeDataset(std::string name, T start, T end, T step = 1) |
| 59 | : NamedDataset{ std::move(name) }, _start{ start }, _end{ end }, _step{ step } |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 63 | /** Allow instances of this class to be move constructed */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 64 | RangeDataset(RangeDataset &&) = default; |
| 65 | |
| 66 | /** Type of the dataset. */ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 67 | using type = std::tuple<T>; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 68 | |
| 69 | /** Iterator for the dataset. */ |
| 70 | struct iterator |
| 71 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 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 | */ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 78 | iterator(std::string name, T start, T step) |
| 79 | : _name{ name }, _value{ start }, _step{ step } |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 80 | { |
| 81 | } |
| 82 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 83 | /** Get the description of the current value. |
| 84 | * |
| 85 | * @return description of the current value. |
| 86 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 87 | std::string description() const |
| 88 | { |
| 89 | using support::cpp11::to_string; |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 90 | return _name + "=" + to_string(_value); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 93 | /** Get the value of the iterator. |
| 94 | * |
| 95 | * @return the value of the iterator. |
| 96 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 97 | RangeDataset::type operator*() const |
| 98 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 99 | return std::make_tuple(_value); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 102 | /** Inrement the iterator. |
| 103 | * |
| 104 | * @return *this; |
| 105 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 106 | iterator &operator++() |
| 107 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 108 | _value += _step; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 109 | return *this; |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | std::string _name; |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 114 | T _value; |
| 115 | T _step; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | /** Iterator pointing at the begin of the dataset. |
| 119 | * |
| 120 | * @return Iterator for the dataset. |
| 121 | */ |
| 122 | iterator begin() const |
| 123 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 124 | return iterator(name(), _start, _step); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** Size of the dataset. |
| 128 | * |
| 129 | * @return Number of values in the dataset. |
| 130 | */ |
| 131 | int size() const |
| 132 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 133 | return (_end - _start) / std::abs(_step); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private: |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 137 | T _start; |
| 138 | T _end; |
| 139 | T _step; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | /** Helper function to create a @ref RangeDataset. |
| 143 | * |
| 144 | * @param[in] name Name of the dataset. |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 145 | * @param[in] start Begin of the range. |
| 146 | * @param[in] end End of the range. |
| 147 | * @param[in] step Step size. |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 148 | * |
| 149 | * @return A range dataset. |
| 150 | */ |
| 151 | template <typename T> |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 152 | RangeDataset<T> make(std::string name, T start, T end, T step = 1) |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 153 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 154 | return RangeDataset<T>(std::move(name), start, end, step); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 155 | } |
| 156 | } // namespace dataset |
| 157 | } // namespace framework |
| 158 | } // namespace test |
| 159 | } // namespace arm_compute |
| 160 | #endif /* ARM_COMPUTE_TEST_DATASET_RANGE */ |