1 /* 2 * Copyright (c) 2017-2021 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_CONTAINER 25 #define ARM_COMPUTE_TEST_DATASET_CONTAINER 26 27 #include "Dataset.h" 28 #include "support/StringSupport.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> 58 class ContainerDataset : public NamedDataset 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) 71 : NamedDataset{ std::move(name) }, _container(std::forward<T>(container)) 72 { 73 } 74 75 /** Allow instances of this class to be copy constructed */ 76 ContainerDataset(const ContainerDataset &) = default; 77 /** Allow instances of this class to be move constructed */ 78 ContainerDataset(ContainerDataset &&) = default; 79 80 /** Type of the dataset. */ 81 using type = std::tuple<container_value_type>; 82 83 /** Iterator for the dataset. */ 84 struct iterator 85 { 86 /** Construct iterator 87 * 88 * @param[in] name Description of the values. 89 * @param[in] iterator Iterator for the values. 90 */ 91 iterator(std::string name, container_const_iterator iterator) 92 : _name{ name }, _iterator{ iterator } 93 { 94 } 95 96 /** Get a description of the current value. 97 * 98 * @return a description. 99 */ 100 std::string description() const 101 { 102 using support::cpp11::to_string; 103 return _name + "=" + to_string(*_iterator); 104 } 105 106 /** Get the current value. 107 * 108 * @return the current value. 109 */ 110 ContainerDataset::type operator*() const 111 { 112 return std::make_tuple(*_iterator); 113 } 114 115 /** Increment the iterator. 116 * 117 * @return this. 118 */ 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 */ 169