• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 based on zipping of 2 other monomorphic datasets
10 // ***************************************************************************
11 
12 #ifndef BOOST_TEST_DATA_MONOMORPHIC_ZIP_HPP_102211GER
13 #define BOOST_TEST_DATA_MONOMORPHIC_ZIP_HPP_102211GER
14 
15 // Boost.Test
16 #include <boost/test/data/config.hpp>
17 
18 #if !defined(BOOST_TEST_NO_ZIP_COMPOSITION_AVAILABLE) || defined(BOOST_TEST_DOXYGEN_DOC__)
19 
20 #include <boost/test/data/monomorphic/fwd.hpp>
21 #include <boost/test/data/monomorphic/sample_merge.hpp>
22 
23 #include <boost/core/enable_if.hpp>
24 #include <boost/mpl/identity.hpp>
25 
26 #include <boost/test/detail/suppress_warnings.hpp>
27 
28 
29 namespace boost {
30 namespace unit_test {
31 namespace data {
32 namespace monomorphic {
33 
34 // ************************************************************************** //
35 // **************                       zip                    ************** //
36 // ************************************************************************** //
37 
38 //! Zip datasets
39 //!
40 //! A zip of two datasets is a dataset whose arity is the sum of the operand datasets arity. The size is given by
41 //! the function creating the instance (see @c operator^ on datasets).
42 template<typename DataSet1, typename DataSet2>
43 class zip {
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     enum { arity = dataset1_decay::arity + dataset2_decay::arity };
52 
53     struct iterator {
54         // Constructor
iteratorboost::unit_test::data::monomorphic::zip::iterator55         explicit    iterator( dataset1_iter iter1, dataset2_iter iter2 )
56         : m_iter1( std::move( iter1 ) )
57         , m_iter2( std::move( iter2 ) )
58         {}
59 
60         using iterator_sample = decltype(
61             sample_merge( *std::declval<dataset1_iter>(),
62                           *std::declval<dataset2_iter>()) );
63 
64         // forward iterator interface
operator *boost::unit_test::data::monomorphic::zip::iterator65         auto            operator*() const -> iterator_sample {
66             return sample_merge( *m_iter1, *m_iter2 );
67         }
operator ++boost::unit_test::data::monomorphic::zip::iterator68         void            operator++()        { ++m_iter1; ++m_iter2; }
69 
70     private:
71         // Data members
72         dataset1_iter   m_iter1;
73         dataset2_iter   m_iter2;
74     };
75 
76     //! Constructor
77     //!
78     //! The datasets are moved and not copied.
zip(DataSet1 && ds1,DataSet2 && ds2)79     zip( DataSet1&& ds1, DataSet2&& ds2/*, data::size_t size*/ )
80     : m_ds1( std::forward<DataSet1>( ds1 ) )
81     , m_ds2( std::forward<DataSet2>( ds2 ) )
82     //, m_size( size )
83     {}
84 
85     //! Move constructor
zip(zip && j)86     zip( zip&& j )
87     : m_ds1( std::forward<DataSet1>( j.m_ds1 ) )
88     , m_ds2( std::forward<DataSet2>( j.m_ds2 ) )
89     //, m_size( j.m_size )
90     {}
91 
92     // dataset interface
size() const93     data::size_t    size() const    { return zip_size(); }
begin() const94     iterator        begin() const   { return iterator( m_ds1.begin(), m_ds2.begin() ); }
95 
96 private:
97     // Data members
98     DataSet1        m_ds1;
99     DataSet2        m_ds2;
100     //data::size_t    m_size;
101 
102 
103     //! Handles the sise of the resulting zipped dataset.
zip_size() const104     data::size_t zip_size() const
105     {
106         data::size_t ds1_size = m_ds1.size();
107         data::size_t ds2_size = m_ds2.size();
108 
109         if( ds1_size == ds2_size )
110             return ds1_size;
111 
112         if( ds1_size == 1 || ds1_size.is_inf() )
113             return ds2_size;
114 
115         if( ds2_size == 1  || ds2_size.is_inf() )
116             return ds1_size;
117 
118         BOOST_TEST_DS_ERROR( "Can't zip datasets of different sizes" );
119     }
120 };
121 
122 //____________________________________________________________________________//
123 
124 //! Zipped datasets results in a dataset.
125 template<typename DataSet1, typename DataSet2>
126 struct is_dataset<zip<DataSet1,DataSet2>> : mpl::true_ {};
127 
128 //____________________________________________________________________________//
129 
130 namespace result_of {
131 
132 //! Result type of the zip operator.
133 template<typename DS1Gen, typename DS2Gen>
134 struct zip {
135     typedef monomorphic::zip<typename DS1Gen::type,typename DS2Gen::type> type;
136 };
137 
138 } // namespace result_of
139 
140 //____________________________________________________________________________//
141 
142 //! Overload operator for zip support
143 template<typename DataSet1, typename DataSet2>
144 inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
145                                         result_of::zip<mpl::identity<DataSet1>,mpl::identity<DataSet2>>
146 >::type
operator ^(DataSet1 && ds1,DataSet2 && ds2)147 operator^( DataSet1&& ds1, DataSet2&& ds2 )
148 {
149     return zip<DataSet1,DataSet2>( std::forward<DataSet1>( ds1 ),
150                                    std::forward<DataSet2>( ds2 )/*,
151                                    ds_detail::zip_size( ds1, ds2 )*/ );
152 }
153 
154 //____________________________________________________________________________//
155 
156 //! @overload boost::unit_test::data::monomorphic::operator^()
157 template<typename DataSet1, typename DataSet2>
158 inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && !is_dataset<DataSet2>::value,
159                                         result_of::zip<mpl::identity<DataSet1>,data::result_of::make<DataSet2>>
160 >::type
operator ^(DataSet1 && ds1,DataSet2 && ds2)161 operator^( DataSet1&& ds1, DataSet2&& ds2 )
162 {
163     return std::forward<DataSet1>( ds1 ) ^ data::make( std::forward<DataSet2>( ds2 ) );
164 }
165 
166 //____________________________________________________________________________//
167 
168 //! @overload boost::unit_test::data::monomorphic::operator^()
169 template<typename DataSet1, typename DataSet2>
170 inline typename boost::lazy_enable_if_c<!is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
171                                         result_of::zip<data::result_of::make<DataSet1>,mpl::identity<DataSet2>>
172 >::type
operator ^(DataSet1 && ds1,DataSet2 && ds2)173 operator^( DataSet1&& ds1, DataSet2&& ds2 )
174 {
175     return data::make( std::forward<DataSet1>( ds1 ) ) ^ std::forward<DataSet2>( ds2 );
176 }
177 
178 } // namespace monomorphic
179 } // namespace data
180 } // namespace unit_test
181 } // namespace boost
182 
183 #include <boost/test/detail/enable_warnings.hpp>
184 
185 #endif // BOOST_TEST_NO_ZIP_COMPOSITION_AVAILABLE
186 
187 #endif // BOOST_TEST_DATA_MONOMORPHIC_ZIP_HPP_102211GER
188 
189