join.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. //! @file
  8. //! Defines dataset join operation
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_DATA_MONOMORPHIC_JOIN_HPP_112711GER
  11. #define BOOST_TEST_DATA_MONOMORPHIC_JOIN_HPP_112711GER
  12. // Boost.Test
  13. #include <boost/test/data/config.hpp>
  14. #include <boost/test/data/monomorphic/fwd.hpp>
  15. #include <boost/core/enable_if.hpp>
  16. #include <boost/mpl/identity.hpp>
  17. #include <boost/test/detail/suppress_warnings.hpp>
  18. //____________________________________________________________________________//
  19. namespace boost {
  20. namespace unit_test {
  21. namespace data {
  22. namespace monomorphic {
  23. // ************************************************************************** //
  24. // ************** join ************** //
  25. // ************************************************************************** //
  26. //! Defines a new dataset from the concatenation of two datasets
  27. //!
  28. //! The size of the resulting dataset is the sum of the two underlying datasets. The arity of the datasets
  29. //! should match.
  30. template<typename DataSet1, typename DataSet2>
  31. class join {
  32. typedef typename boost::decay<DataSet1>::type dataset1_decay;
  33. typedef typename boost::decay<DataSet2>::type dataset2_decay;
  34. typedef typename dataset1_decay::iterator dataset1_iter;
  35. typedef typename dataset2_decay::iterator dataset2_iter;
  36. using iter1_ret = decltype(*std::declval<DataSet1>().begin());
  37. using iter2_ret = decltype(*std::declval<DataSet2>().begin());
  38. public:
  39. enum { arity = dataset1_decay::arity };
  40. using sample_t = typename std::conditional<
  41. std::is_reference<iter1_ret>::value && std::is_reference<iter2_ret>::value && std::is_same<iter1_ret, iter2_ret>::value,
  42. iter1_ret,
  43. typename std::remove_reference<iter1_ret>::type
  44. >::type
  45. ;
  46. struct iterator {
  47. // Constructor
  48. explicit iterator( dataset1_iter&& it1, dataset2_iter&& it2, data::size_t first_size )
  49. : m_it1( std::move( it1 ) )
  50. , m_it2( std::move( it2 ) )
  51. , m_first_size( first_size )
  52. {}
  53. // forward iterator interface
  54. // The returned sample should be by value, as the operator* may return a temporary object
  55. sample_t operator*() const { return m_first_size > 0 ? *m_it1 : *m_it2; }
  56. void operator++() { if( m_first_size > 0 ) { --m_first_size; ++m_it1; } else ++m_it2; }
  57. private:
  58. // Data members
  59. dataset1_iter m_it1;
  60. dataset2_iter m_it2;
  61. data::size_t m_first_size;
  62. };
  63. //! Constructor
  64. join( DataSet1&& ds1, DataSet2&& ds2 )
  65. : m_ds1( std::forward<DataSet1>( ds1 ) )
  66. , m_ds2( std::forward<DataSet2>( ds2 ) )
  67. {}
  68. //! Move constructor
  69. join( join&& j )
  70. : m_ds1( std::forward<DataSet1>( j.m_ds1 ) )
  71. , m_ds2( std::forward<DataSet2>( j.m_ds2 ) )
  72. {}
  73. //! dataset interface
  74. data::size_t size() const { return m_ds1.size() + m_ds2.size(); }
  75. iterator begin() const { return iterator( m_ds1.begin(), m_ds2.begin(), m_ds1.size() ); }
  76. private:
  77. // Data members
  78. DataSet1 m_ds1;
  79. DataSet2 m_ds2;
  80. };
  81. //____________________________________________________________________________//
  82. // A joined dataset is a dataset.
  83. template<typename DataSet1, typename DataSet2>
  84. struct is_dataset<join<DataSet1,DataSet2>> : mpl::true_ {};
  85. //____________________________________________________________________________//
  86. namespace result_of {
  87. //! Result type of the join operation on datasets.
  88. template<typename DataSet1Gen, typename DataSet2Gen>
  89. struct join {
  90. typedef monomorphic::join<typename DataSet1Gen::type,typename DataSet2Gen::type> type;
  91. };
  92. } // namespace result_of
  93. //____________________________________________________________________________//
  94. template<typename DataSet1, typename DataSet2>
  95. inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
  96. result_of::join<mpl::identity<DataSet1>,mpl::identity<DataSet2>>
  97. >::type
  98. operator+( DataSet1&& ds1, DataSet2&& ds2 )
  99. {
  100. return join<DataSet1,DataSet2>( std::forward<DataSet1>( ds1 ), std::forward<DataSet2>( ds2 ) );
  101. }
  102. //____________________________________________________________________________//
  103. template<typename DataSet1, typename DataSet2>
  104. inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && !is_dataset<DataSet2>::value,
  105. result_of::join<mpl::identity<DataSet1>,data::result_of::make<DataSet2>>
  106. >::type
  107. operator+( DataSet1&& ds1, DataSet2&& ds2 )
  108. {
  109. return std::forward<DataSet1>( ds1 ) + data::make( std::forward<DataSet2>( ds2 ) );
  110. }
  111. //____________________________________________________________________________//
  112. template<typename DataSet1, typename DataSet2>
  113. inline typename boost::lazy_enable_if_c<!is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
  114. result_of::join<data::result_of::make<DataSet1>,mpl::identity<DataSet2>>
  115. >::type
  116. operator+( DataSet1&& ds1, DataSet2&& ds2 )
  117. {
  118. return data::make( std::forward<DataSet1>(ds1) ) + std::forward<DataSet2>( ds2 );
  119. }
  120. } // namespace monomorphic
  121. } // namespace data
  122. } // namespace unit_test
  123. } // namespace boost
  124. #include <boost/test/detail/enable_warnings.hpp>
  125. #endif // BOOST_TEST_DATA_MONOMORPHIC_JOIN_HPP_112711GER