Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 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 | |
| 63 | RangeDataset(RangeDataset &&) = default; |
| 64 | |
| 65 | /** Type of the dataset. */ |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 66 | using type = std::tuple<T>; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 67 | |
| 68 | /** Iterator for the dataset. */ |
| 69 | struct iterator |
| 70 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 71 | iterator(std::string name, T start, T step) |
| 72 | : _name{ name }, _value{ start }, _step{ step } |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 73 | { |
| 74 | } |
| 75 | |
| 76 | std::string description() const |
| 77 | { |
| 78 | using support::cpp11::to_string; |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 79 | return _name + "=" + to_string(_value); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | RangeDataset::type operator*() const |
| 83 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 84 | return std::make_tuple(_value); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | iterator &operator++() |
| 88 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 89 | _value += _step; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | std::string _name; |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 95 | T _value; |
| 96 | T _step; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /** Iterator pointing at the begin of the dataset. |
| 100 | * |
| 101 | * @return Iterator for the dataset. |
| 102 | */ |
| 103 | iterator begin() const |
| 104 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 105 | return iterator(name(), _start, _step); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** Size of the dataset. |
| 109 | * |
| 110 | * @return Number of values in the dataset. |
| 111 | */ |
| 112 | int size() const |
| 113 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 114 | return (_end - _start) / std::abs(_step); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | private: |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 118 | T _start; |
| 119 | T _end; |
| 120 | T _step; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /** Helper function to create a @ref RangeDataset. |
| 124 | * |
| 125 | * @param[in] name Name of the dataset. |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 126 | * @param[in] start Begin of the range. |
| 127 | * @param[in] end End of the range. |
| 128 | * @param[in] step Step size. |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 129 | * |
| 130 | * @return A range dataset. |
| 131 | */ |
| 132 | template <typename T> |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 133 | 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] | 134 | { |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 135 | return RangeDataset<T>(std::move(name), start, end, step); |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 136 | } |
| 137 | } // namespace dataset |
| 138 | } // namespace framework |
| 139 | } // namespace test |
| 140 | } // namespace arm_compute |
| 141 | #endif /* ARM_COMPUTE_TEST_DATASET_RANGE */ |