• 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 dataset join operation
10 // ***************************************************************************
11 
12 #ifndef BOOST_TEST_DATA_MONOMORPHIC_JOIN_HPP_112711GER
13 #define BOOST_TEST_DATA_MONOMORPHIC_JOIN_HPP_112711GER
14 
15 // Boost.Test
16 #include <boost/test/data/config.hpp>
17 #include <boost/test/data/monomorphic/fwd.hpp>
18 
19 #include <boost/core/enable_if.hpp>
20 #include <boost/mpl/identity.hpp>
21 
22 #include <boost/test/detail/suppress_warnings.hpp>
23 
24 //____________________________________________________________________________//
25 
26 namespace boost {
27 namespace unit_test {
28 namespace data {
29 namespace monomorphic {
30 
31 // ************************************************************************** //
32 // **************                      join                    ************** //
33 // ************************************************************************** //
34 
35 //! Defines a new dataset from the concatenation of two datasets
36 //!
37 //! The size of the resulting dataset is the sum of the two underlying datasets. The arity of the datasets
38 //! should match.
39 template<typename DataSet1, typename DataSet2>
40 class join {
41     typedef typename boost::decay<DataSet1>::type   dataset1_decay;
42     typedef typename boost::decay<DataSet2>::type   dataset2_decay;
43 
44     typedef typename dataset1_decay::iterator       dataset1_iter;
45     typedef typename dataset2_decay::iterator       dataset2_iter;
46 
47     using iter1_ret = decltype(*std::declval<DataSet1>().begin());
48     using iter2_ret = decltype(*std::declval<DataSet2>().begin());
49 
50 public:
51 
52     enum { arity = dataset1_decay::arity };
53 
54     using sample_t = typename std::conditional<
55         std::is_reference<iter1_ret>::value && std::is_reference<iter2_ret>::value && std::is_same<iter1_ret, iter2_ret>::value,
56         iter1_ret,
57         typename std::remove_reference<iter1_ret>::type
58         >::type
59         ;
60 
61     struct iterator {
62         // Constructor
iteratorboost::unit_test::data::monomorphic::join::iterator63         explicit    iterator( dataset1_iter&& it1, dataset2_iter&& it2, data::size_t first_size )
64         : m_it1( std::move( it1 ) )
65         , m_it2( std::move( it2 ) )
66         , m_first_size( first_size )
67         {}
68 
69         // forward iterator interface
70         // The returned sample should be by value, as the operator* may return a temporary object
operator *boost::unit_test::data::monomorphic::join::iterator71         sample_t     operator*() const   { return m_first_size > 0 ? *m_it1 : *m_it2; }
operator ++boost::unit_test::data::monomorphic::join::iterator72         void         operator++()        { if( m_first_size > 0 ) { --m_first_size; ++m_it1; } else ++m_it2; }
73 
74     private:
75         // Data members
76         dataset1_iter       m_it1;
77         dataset2_iter       m_it2;
78         data::size_t        m_first_size;
79     };
80 
81     //! Constructor
join(DataSet1 && ds1,DataSet2 && ds2)82     join( DataSet1&& ds1, DataSet2&& ds2 )
83     : m_ds1( std::forward<DataSet1>( ds1 ) )
84     , m_ds2( std::forward<DataSet2>( ds2 ) )
85     {}
86 
87     //! Move constructor
join(join && j)88     join( join&& j )
89     : m_ds1( std::forward<DataSet1>( j.m_ds1 ) )
90     , m_ds2( std::forward<DataSet2>( j.m_ds2 ) )
91     {}
92 
93     //! dataset interface
size() const94     data::size_t    size() const    { return m_ds1.size() + m_ds2.size(); }
begin() const95     iterator        begin() const   { return iterator( m_ds1.begin(), m_ds2.begin(), m_ds1.size() ); }
96 
97 private:
98     // Data members
99     DataSet1        m_ds1;
100     DataSet2        m_ds2;
101 };
102 
103 //____________________________________________________________________________//
104 
105 // A joined dataset  is a dataset.
106 template<typename DataSet1, typename DataSet2>
107 struct is_dataset<join<DataSet1,DataSet2>> : mpl::true_ {};
108 
109 //____________________________________________________________________________//
110 
111 namespace result_of {
112 
113 //! Result type of the join operation on datasets.
114 template<typename DataSet1Gen, typename DataSet2Gen>
115 struct join {
116     typedef monomorphic::join<typename DataSet1Gen::type,typename DataSet2Gen::type> type;
117 };
118 
119 } // namespace result_of
120 
121 //____________________________________________________________________________//
122 
123 template<typename DataSet1, typename DataSet2>
124 inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
125                                         result_of::join<mpl::identity<DataSet1>,mpl::identity<DataSet2>>
126 >::type
operator +(DataSet1 && ds1,DataSet2 && ds2)127 operator+( DataSet1&& ds1, DataSet2&& ds2 )
128 {
129     return join<DataSet1,DataSet2>( std::forward<DataSet1>( ds1 ),  std::forward<DataSet2>( ds2 ) );
130 }
131 
132 //____________________________________________________________________________//
133 
134 template<typename DataSet1, typename DataSet2>
135 inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && !is_dataset<DataSet2>::value,
136                                         result_of::join<mpl::identity<DataSet1>,data::result_of::make<DataSet2>>
137 >::type
operator +(DataSet1 && ds1,DataSet2 && ds2)138 operator+( DataSet1&& ds1, DataSet2&& ds2 )
139 {
140     return std::forward<DataSet1>( ds1 ) + data::make( std::forward<DataSet2>( ds2 ) );
141 }
142 
143 //____________________________________________________________________________//
144 
145 template<typename DataSet1, typename DataSet2>
146 inline typename boost::lazy_enable_if_c<!is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
147                                         result_of::join<data::result_of::make<DataSet1>,mpl::identity<DataSet2>>
148 >::type
operator +(DataSet1 && ds1,DataSet2 && ds2)149 operator+( DataSet1&& ds1, DataSet2&& ds2 )
150 {
151     return data::make( std::forward<DataSet1>(ds1) ) + std::forward<DataSet2>( ds2 );
152 }
153 
154 } // namespace monomorphic
155 } // namespace data
156 } // namespace unit_test
157 } // namespace boost
158 
159 #include <boost/test/detail/enable_warnings.hpp>
160 
161 #endif // BOOST_TEST_DATA_MONOMORPHIC_JOIN_HPP_112711GER
162 
163