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_LIST |
| 25 | #define ARM_COMPUTE_TEST_DATASET_LIST |
| 26 | |
| 27 | #include "Dataset.h" |
| 28 | #include "support/ToolchainSupport.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 | */ |
| 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 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 62 | /** Allow instances of this class to be move constructed */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 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 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 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 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 76 | iterator(std::string name, data_const_iterator iterator) |
| 77 | : _name{ name }, _iterator{ iterator } |
| 78 | { |
| 79 | } |
| 80 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 81 | /** Get a description of the current value. |
| 82 | * |
| 83 | * @return a description. |
| 84 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 85 | std::string description() const |
| 86 | { |
| 87 | using support::cpp11::to_string; |
| 88 | return _name + "=" + to_string(*_iterator); |
| 89 | } |
| 90 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 91 | /** Get the current value. |
| 92 | * |
| 93 | * @return the current value. |
| 94 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 95 | InitializerListDataset::type operator*() const |
| 96 | { |
| 97 | return std::make_tuple(*_iterator); |
| 98 | } |
| 99 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 100 | /** Increment the iterator. |
| 101 | * |
| 102 | * @return *this. |
| 103 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 104 | iterator &operator++() |
| 105 | { |
| 106 | ++_iterator; |
| 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | std::string _name; |
| 112 | data_const_iterator _iterator; |
| 113 | }; |
| 114 | |
| 115 | /** Iterator pointing at the begin of the dataset. |
| 116 | * |
| 117 | * @return Iterator for the dataset. |
| 118 | */ |
| 119 | iterator begin() const |
| 120 | { |
| 121 | return iterator(name(), _data.cbegin()); |
| 122 | } |
| 123 | |
| 124 | /** Size of the dataset. |
| 125 | * |
| 126 | * @return Number of values in the dataset. |
| 127 | */ |
| 128 | int size() const |
| 129 | { |
| 130 | return _data.size(); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | std::vector<T> _data; |
| 135 | }; |
| 136 | |
| 137 | /** Helper function to create an @ref InitializerListDataset. |
| 138 | * |
| 139 | * @param[in] name Name of the dataset. |
| 140 | * @param[in] list Initializer list. |
| 141 | * |
| 142 | * @return An initializer list dataset. |
| 143 | */ |
| 144 | template <typename T> |
| 145 | InitializerListDataset<T> make(std::string name, std::initializer_list<T> &&list) |
| 146 | { |
| 147 | return InitializerListDataset<T>(std::move(name), std::forward<std::initializer_list<T>>(list)); |
| 148 | } |
| 149 | } // namespace dataset |
| 150 | } // namespace framework |
| 151 | } // namespace test |
| 152 | } // namespace arm_compute |
| 153 | #endif /* ARM_COMPUTE_TEST_DATASET_LIST */ |