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