1 //  (C) Copyright Gennadiy Rozental 2011-2014.
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/dataset.hpp>
18 
19 #include <boost/test/detail/suppress_warnings.hpp>
20 
21 //____________________________________________________________________________//
22 
23 namespace boost {
24 namespace unit_test {
25 namespace data {
26 namespace monomorphic {
27 
28 // ************************************************************************** //
29 // **************                      join                    ************** //
30 // ************************************************************************** //
31 
32 //! Defines a new dataset from the concatenation of two datasets
33 //!
34 //! The size of the resulting dataset is the sum of the two underlying datasets. The arity of the datasets
35 //! should match.
36 template<typename DS1, typename DS2>
37 class join : public monomorphic::dataset<typename boost::decay<DS1>::type::data_type> {
38     typedef typename boost::decay<DS1>::type::data_type T;
39     typedef monomorphic::dataset<T> base;
40     typedef typename base::iter_ptr iter_ptr;
41 
42     struct iterator : public base::iterator {
43         // Constructor
iteratorboost::unit_test::data::monomorphic::join::iterator44         explicit    iterator( iter_ptr it1, iter_ptr it2, data::size_t first_size )
45 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
46         : m_it1( std::move(it1) )
47         , m_it2( std::move(it2) )
48 #else
49         : m_it1( it1 )
50         , m_it2( it2 )
51 #endif
52         , m_first_size( first_size )
53         {}
54 
55         // forward iterator interface
operator *boost::unit_test::data::monomorphic::join::iterator56         virtual T const&    operator*()     { return m_first_size > 0 ? **m_it1 : **m_it2; }
operator ++boost::unit_test::data::monomorphic::join::iterator57         virtual void        operator++()    { m_first_size > 0 ? (--m_first_size,++(*m_it1)) : ++(*m_it2); }
58 
59     private:
60         // Data members
61         iter_ptr        m_it1;
62         iter_ptr        m_it2;
63         data::size_t    m_first_size;
64     };
65 
66 public:
67     enum { arity = boost::decay<DS1>::type::arity };
68 
69 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
70     // Constructor
join(DS1 && ds1,DS2 && ds2)71     join( DS1&& ds1, DS2&& ds2 )
72     : m_ds1( std::forward<DS1>( ds1 ) )
73     , m_ds2( std::forward<DS2>( ds2 ) )
74     {}
75 
76     // Move constructor
join(join && j)77     join( join&& j )
78     : m_ds1( std::forward<DS1>( j.m_ds1 ) )
79     , m_ds2( std::forward<DS2>( j.m_ds2 ) )
80     {}
81 #else
82     // Constructor
join(DS1 const & ds1,DS2 const & ds2)83     join( DS1 const& ds1, DS2 const& ds2 )
84     : m_ds1( ds1 )
85     , m_ds2( ds2 )
86     {}
87 #endif
88 
89     // dataset interface
size() const90     virtual data::size_t    size() const            { return m_ds1.size() + m_ds2.size(); }
begin() const91     virtual iter_ptr        begin() const           { return boost::make_shared<iterator>( m_ds1.begin(),
92                                                                                            m_ds2.begin(),
93                                                                                            m_ds1.size() ); }
94 
95 private:
96     // Data members
97     DS1 m_ds1;
98     DS2 m_ds2;
99 };
100 
101 //____________________________________________________________________________//
102 
103 // A joined dataset  is a dataset.
104 template<typename DS1, typename DS2>
105 struct is_dataset<join<DS1,DS2> > : mpl::true_ {};
106 
107 //____________________________________________________________________________//
108 
109 namespace result_of {
110 
111 //! Result type of the join operation on datasets.
112 template<typename DS1Gen, typename DS2Gen>
113 struct join {
114     typedef monomorphic::join<typename DS1Gen::type,typename DS2Gen::type> type;
115 };
116 
117 } // namespace result_of
118 
119 //____________________________________________________________________________//
120 
121 template<typename DS1, typename DS2>
122 inline typename boost::lazy_enable_if_c<is_dataset<DS1>::value && is_dataset<DS2>::value,
123                                         result_of::join<mpl::identity<DS1>,mpl::identity<DS2> >
124 >::type
125 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
operator +(DS1 && ds1,DS2 && ds2)126 operator+( DS1&& ds1, DS2&& ds2 )
127 {
128     return join<DS1,DS2>( std::forward<DS1>( ds1 ),  std::forward<DS2>( ds2 ) );
129 }
130 #else
131 operator+( DS1 const& ds1, DS2 const& ds2 )
132 {
133     return join<DS1,DS2>( ds1,  ds2 );
134 }
135 #endif
136 
137 //____________________________________________________________________________//
138 
139 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
140 
141 template<typename DS1, typename DS2>
142 inline typename boost::lazy_enable_if_c<is_dataset<DS1>::value && !is_dataset<DS2>::value,
143                                         result_of::join<mpl::identity<DS1>,data::result_of::make<DS2> >
144 >::type
operator +(DS1 && ds1,DS2 && ds2)145 operator+( DS1&& ds1, DS2&& ds2 )
146 {
147     return std::forward<DS1>(ds1) + data::make(std::forward<DS2>(ds2));
148 }
149 #else
150 template<typename DS1, typename DS2>
151 inline typename boost::lazy_enable_if_c<is_dataset<DS1>::value && !is_dataset<DS2>::value,
152                                         result_of::join<mpl::identity<DS1>,data::result_of::make<DS2> >
153 >::type
operator +(DS1 const & ds1,DS2 const & ds2)154 operator+( DS1 const& ds1, DS2 const& ds2 )
155 {
156     return ds1 + data::make(ds2);
157 }
158 #endif
159 
160 //____________________________________________________________________________//
161 
162 
163 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
164 template<typename DS1, typename DS2>
165 inline typename boost::lazy_enable_if_c<!is_dataset<DS1>::value && is_dataset<DS2>::value,
166                                         result_of::join<data::result_of::make<DS1>,mpl::identity<DS2> >
167 >::type
operator +(DS1 && ds1,DS2 && ds2)168 operator+( DS1&& ds1, DS2&& ds2 )
169 {
170     return data::make(std::forward<DS1>(ds1)) + std::forward<DS2>(ds2);
171 }
172 #else
173 template<typename DS1, typename DS2>
174 inline typename boost::lazy_enable_if_c<!is_dataset<DS1>::value && is_dataset<DS2>::value,
175                                         result_of::join<data::result_of::make<DS1>,mpl::identity<DS2> >
176 >::type
operator +(DS1 const & ds1,DS2 const & ds2)177 operator+( DS1 const& ds1, DS2 const& ds2 )
178 {
179     return data::make(ds1) + ds2;
180 }
181 
182 #endif
183 
184 
185 } // namespace monomorphic
186 
187 } // namespace data
188 } // namespace unit_test
189 } // namespace boost
190 
191 #include <boost/test/detail/enable_warnings.hpp>
192 
193 #endif // BOOST_TEST_DATA_MONOMORPHIC_JOIN_HPP_112711GER
194 
195