Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2020 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_CONTAINER |
| 25 | #define ARM_COMPUTE_TEST_DATASET_CONTAINER |
| 26 | |
| 27 | #include "Dataset.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame^] | 28 | #include "support/StringSupport.h" |
Anthony Barbier | 4dbc0a9 | 2018-08-27 13:39:47 +0100 | [diff] [blame] | 29 | #include "tests/TypePrinter.h" |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 30 | |
| 31 | #include <string> |
| 32 | #include <tuple> |
| 33 | #include <type_traits> |
| 34 | #include <utility> |
| 35 | #include <vector> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace framework |
| 42 | { |
| 43 | namespace dataset |
| 44 | { |
| 45 | /** Base case. Nothing is a container. */ |
| 46 | template <typename T> |
| 47 | struct is_container : public std::false_type |
| 48 | { |
| 49 | }; |
| 50 | |
| 51 | /** Vector is considered a container. */ |
| 52 | template <typename V, typename A> |
| 53 | struct is_container<std::vector<V, A>> : public std::true_type |
| 54 | { |
| 55 | }; |
| 56 | |
| 57 | /** Implementation of a dataset created from a container. */ |
| 58 | template <typename T> |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 59 | class ContainerDataset : public NamedDataset |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 60 | { |
| 61 | private: |
| 62 | using container_value_type = typename T::value_type; |
| 63 | using container_const_iterator = typename T::const_iterator; |
| 64 | |
| 65 | public: |
| 66 | /** Construct dataset with given name and values from the container. |
| 67 | * |
| 68 | * @param[in] name Description of the values. |
| 69 | * @param[in] container Values for the dataset. |
| 70 | */ |
| 71 | ContainerDataset(std::string name, T &&container) |
Moritz Pflanzer | b7c2a99 | 2017-07-18 14:37:35 +0100 | [diff] [blame] | 72 | : NamedDataset{ std::move(name) }, _container(std::forward<T>(container)) |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 73 | { |
| 74 | } |
| 75 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 76 | /** Allow instances of this class to be copy constructed */ |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 77 | ContainerDataset(const ContainerDataset &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 78 | /** Allow instances of this class to be move constructed */ |
| 79 | ContainerDataset(ContainerDataset &&) = default; |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 80 | |
| 81 | /** Type of the dataset. */ |
| 82 | using type = std::tuple<container_value_type>; |
| 83 | |
| 84 | /** Iterator for the dataset. */ |
| 85 | struct iterator |
| 86 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 87 | /** Construct iterator |
| 88 | * |
| 89 | * @param[in] name Description of the values. |
| 90 | * @param[in] iterator Iterator for the values. |
| 91 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 92 | iterator(std::string name, container_const_iterator iterator) |
| 93 | : _name{ name }, _iterator{ iterator } |
| 94 | { |
| 95 | } |
| 96 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 97 | /** Get a description of the current value. |
| 98 | * |
| 99 | * @return a description. |
| 100 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 101 | std::string description() const |
| 102 | { |
| 103 | using support::cpp11::to_string; |
| 104 | return _name + "=" + to_string(*_iterator); |
| 105 | } |
| 106 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 107 | /** Get the current value. |
| 108 | * |
| 109 | * @return the current value. |
| 110 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 111 | ContainerDataset::type operator*() const |
| 112 | { |
| 113 | return std::make_tuple(*_iterator); |
| 114 | } |
| 115 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 116 | /** Increment the iterator. |
| 117 | * |
| 118 | * @return this. |
| 119 | */ |
Moritz Pflanzer | 69e44dc | 2017-07-05 11:02:14 +0100 | [diff] [blame] | 120 | iterator &operator++() |
| 121 | { |
| 122 | ++_iterator; |
| 123 | return *this; |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | std::string _name; |
| 128 | container_const_iterator _iterator; |
| 129 | }; |
| 130 | |
| 131 | /** Iterator pointing at the begin of the dataset. |
| 132 | * |
| 133 | * @return Iterator for the dataset. |
| 134 | */ |
| 135 | iterator begin() const |
| 136 | { |
| 137 | return iterator(name(), _container.cbegin()); |
| 138 | } |
| 139 | |
| 140 | /** Size of the dataset. |
| 141 | * |
| 142 | * @return Number of values in the dataset. |
| 143 | */ |
| 144 | int size() const |
| 145 | { |
| 146 | return _container.size(); |
| 147 | } |
| 148 | |
| 149 | private: |
| 150 | T _container; |
| 151 | }; |
| 152 | |
| 153 | /** Helper function to create a @ref ContainerDataset. |
| 154 | * |
| 155 | * @param[in] name Name of the dataset. |
| 156 | * @param[in] values Container. |
| 157 | * |
| 158 | * @return A container dataset. |
| 159 | */ |
| 160 | template <typename T> |
| 161 | typename std::enable_if<is_container<T>::value, ContainerDataset<T>>::type make(std::string name, T &&values) |
| 162 | { |
| 163 | return ContainerDataset<T>(std::move(name), std::forward<T>(values)); |
| 164 | } |
| 165 | } // namespace dataset |
| 166 | } // namespace framework |
| 167 | } // namespace test |
| 168 | } // namespace arm_compute |
| 169 | #endif /* ARM_COMPUTE_TEST_DATASET_CONTAINER */ |