]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright Gennadiy Rozental 2001. |
2 | // Distributed under the Boost Software License, Version 1.0. | |
3 | // (See accompanying file LICENSE_1_0.txt or copy at | |
4 | // http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | // See http://www.boost.org/libs/test for the library home page. | |
7 | // | |
8 | ///@file | |
9 | /// Defines monomorphic dataset n+m dimentional *. Samples in this | |
10 | /// dataset is grid of elements in DataSet1 and DataSet2. There will be total | |
11 | /// |DataSet1| * |DataSet2| samples | |
12 | // *************************************************************************** | |
13 | ||
14 | #ifndef BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER | |
15 | #define BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER | |
16 | ||
17 | // Boost.Test | |
18 | #include <boost/test/data/config.hpp> | |
19 | ||
20 | #if !defined(BOOST_TEST_NO_GRID_COMPOSITION_AVAILABLE) || defined(BOOST_TEST_DOXYGEN_DOC__) | |
21 | ||
22 | #include <boost/test/data/monomorphic/fwd.hpp> | |
23 | #include <boost/test/data/monomorphic/sample_merge.hpp> | |
24 | ||
25 | #include <boost/test/detail/suppress_warnings.hpp> | |
26 | ||
27 | //____________________________________________________________________________// | |
28 | ||
29 | namespace boost { | |
30 | namespace unit_test { | |
31 | namespace data { | |
32 | namespace monomorphic { | |
33 | ||
34 | // ************************************************************************** // | |
35 | // ************** grid ************** // | |
36 | // ************************************************************************** // | |
37 | ||
38 | ||
39 | //! Implements the dataset resulting from a cartesian product/grid operation on datasets. | |
40 | //! | |
41 | //! The arity of the resulting dataset is the sum of the arity of its operands. | |
42 | template<typename DataSet1, typename DataSet2> | |
43 | class grid { | |
44 | typedef typename boost::decay<DataSet1>::type dataset1_decay; | |
45 | typedef typename boost::decay<DataSet2>::type dataset2_decay; | |
46 | ||
47 | typedef typename dataset1_decay::iterator dataset1_iter; | |
48 | typedef typename dataset2_decay::iterator dataset2_iter; | |
49 | ||
50 | public: | |
51 | ||
52 | struct iterator { | |
53 | // Constructor | |
54 | explicit iterator( dataset1_iter iter1, DataSet2 const& ds2 ) | |
55 | : m_iter1( std::move( iter1 ) ) | |
56 | , m_iter2( std::move( ds2.begin() ) ) | |
57 | , m_ds2( &ds2 ) | |
58 | , m_ds2_pos( 0 ) | |
59 | {} | |
60 | ||
61 | using iterator_sample = decltype( | |
62 | sample_merge( *std::declval<dataset1_iter>(), | |
63 | *std::declval<dataset2_iter>()) ); | |
64 | ||
65 | // forward iterator interface | |
66 | auto operator*() const -> iterator_sample { | |
67 | return sample_merge( *m_iter1, *m_iter2 ); | |
68 | } | |
69 | void operator++() | |
70 | { | |
71 | ++m_ds2_pos; | |
72 | if( m_ds2_pos != m_ds2->size() ) | |
73 | ++m_iter2; | |
74 | else { | |
75 | m_ds2_pos = 0; | |
76 | ++m_iter1; | |
77 | m_iter2 = std::move( m_ds2->begin() ); | |
78 | } | |
79 | } | |
80 | ||
81 | private: | |
82 | // Data members | |
83 | dataset1_iter m_iter1; | |
84 | dataset2_iter m_iter2; | |
85 | dataset2_decay const* m_ds2; | |
86 | data::size_t m_ds2_pos; | |
87 | }; | |
88 | ||
89 | public: | |
90 | enum { arity = boost::decay<DataSet1>::type::arity + boost::decay<DataSet2>::type::arity }; | |
91 | ||
92 | //! Constructor | |
93 | grid( DataSet1&& ds1, DataSet2&& ds2 ) | |
94 | : m_ds1( std::forward<DataSet1>( ds1 ) ) | |
95 | , m_ds2( std::forward<DataSet2>( ds2 ) ) | |
96 | {} | |
97 | ||
98 | //! Move constructor | |
99 | grid( grid&& j ) | |
100 | : m_ds1( std::forward<DataSet1>( j.m_ds1 ) ) | |
101 | , m_ds2( std::forward<DataSet2>( j.m_ds2 ) ) | |
102 | {} | |
103 | ||
104 | // dataset interface | |
105 | data::size_t size() const { return m_ds1.size() * m_ds2.size(); } | |
106 | iterator begin() const { return iterator( m_ds1.begin(), m_ds2 ); } | |
107 | ||
108 | private: | |
109 | // Data members | |
110 | DataSet1 m_ds1; | |
111 | DataSet2 m_ds2; | |
112 | }; | |
113 | ||
114 | //____________________________________________________________________________// | |
115 | ||
116 | // A grid dataset is a dataset | |
117 | template<typename DataSet1, typename DataSet2> | |
118 | struct is_dataset<grid<DataSet1,DataSet2>> : mpl::true_ {}; | |
119 | ||
120 | //____________________________________________________________________________// | |
121 | ||
122 | namespace result_of { | |
123 | ||
124 | /// Result type of the grid operation on dataset. | |
125 | template<typename DS1Gen, typename DS2Gen> | |
126 | struct grid { | |
127 | typedef monomorphic::grid<typename DS1Gen::type,typename DS2Gen::type> type; | |
128 | }; | |
129 | ||
130 | } // namespace result_of | |
131 | ||
132 | //____________________________________________________________________________// | |
133 | ||
134 | //! Grid operation | |
135 | template<typename DataSet1, typename DataSet2> | |
136 | inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && is_dataset<DataSet2>::value, | |
137 | result_of::grid<mpl::identity<DataSet1>,mpl::identity<DataSet2>> | |
138 | >::type | |
139 | operator*( DataSet1&& ds1, DataSet2&& ds2 ) | |
140 | { | |
141 | BOOST_TEST_DS_ASSERT( !ds1.size().is_inf() && !ds2.size().is_inf(), "Grid axes can't have infinite size" ); | |
142 | ||
143 | return grid<DataSet1,DataSet2>( std::forward<DataSet1>( ds1 ), std::forward<DataSet2>( ds2 ) ); | |
144 | } | |
145 | ||
146 | //____________________________________________________________________________// | |
147 | ||
148 | //! @overload boost::unit_test::data::operator* | |
149 | template<typename DataSet1, typename DataSet2> | |
150 | inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && !is_dataset<DataSet2>::value, | |
151 | result_of::grid<mpl::identity<DataSet1>,data::result_of::make<DataSet2>> | |
152 | >::type | |
153 | operator*( DataSet1&& ds1, DataSet2&& ds2 ) | |
154 | { | |
155 | return std::forward<DataSet1>(ds1) * data::make(std::forward<DataSet2>(ds2)); | |
156 | } | |
157 | ||
158 | //____________________________________________________________________________// | |
159 | ||
160 | //! @overload boost::unit_test::data::operator* | |
161 | template<typename DataSet1, typename DataSet2> | |
162 | inline typename boost::lazy_enable_if_c<!is_dataset<DataSet1>::value && is_dataset<DataSet2>::value, | |
163 | result_of::grid<data::result_of::make<DataSet1>,mpl::identity<DataSet2>> | |
164 | >::type | |
165 | operator*( DataSet1&& ds1, DataSet2&& ds2 ) | |
166 | { | |
167 | return data::make(std::forward<DataSet1>(ds1)) * std::forward<DataSet2>(ds2); | |
168 | } | |
169 | ||
170 | } // namespace monomorphic | |
171 | ||
172 | } // namespace data | |
173 | } // namespace unit_test | |
174 | } // namespace boost | |
175 | ||
176 | #include <boost/test/detail/enable_warnings.hpp> | |
177 | ||
178 | #endif // BOOST_TEST_NO_GRID_COMPOSITION_AVAILABLE | |
179 | ||
180 | #endif // BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER | |
181 |