grid.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 monomorphic dataset n+m dimentional *. Samples in this
  9. /// dataset is grid of elements in DataSet1 and DataSet2. There will be total
  10. /// |DataSet1| * |DataSet2| samples
  11. // ***************************************************************************
  12. #ifndef BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER
  13. #define BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER
  14. // Boost.Test
  15. #include <boost/test/data/config.hpp>
  16. #if !defined(BOOST_TEST_NO_GRID_COMPOSITION_AVAILABLE) || defined(BOOST_TEST_DOXYGEN_DOC__)
  17. #include <boost/test/data/monomorphic/fwd.hpp>
  18. #include <boost/test/data/monomorphic/sample_merge.hpp>
  19. #include <boost/core/enable_if.hpp>
  20. #include <boost/mpl/identity.hpp>
  21. #include <boost/test/detail/suppress_warnings.hpp>
  22. //____________________________________________________________________________//
  23. namespace boost {
  24. namespace unit_test {
  25. namespace data {
  26. namespace monomorphic {
  27. // ************************************************************************** //
  28. // ************** grid ************** //
  29. // ************************************************************************** //
  30. //! Implements the dataset resulting from a cartesian product/grid operation on datasets.
  31. //!
  32. //! The arity of the resulting dataset is the sum of the arity of its operands.
  33. template<typename DataSet1, typename DataSet2>
  34. class grid {
  35. typedef typename boost::decay<DataSet1>::type dataset1_decay;
  36. typedef typename boost::decay<DataSet2>::type dataset2_decay;
  37. typedef typename dataset1_decay::iterator dataset1_iter;
  38. typedef typename dataset2_decay::iterator dataset2_iter;
  39. public:
  40. struct iterator {
  41. // Constructor
  42. explicit iterator( dataset1_iter iter1, DataSet2 const& ds2 )
  43. : m_iter1( std::move( iter1 ) )
  44. , m_iter2( std::move( ds2.begin() ) )
  45. , m_ds2( &ds2 )
  46. , m_ds2_pos( 0 )
  47. {}
  48. using iterator_sample = decltype(
  49. sample_merge( *std::declval<dataset1_iter>(),
  50. *std::declval<dataset2_iter>()) );
  51. // forward iterator interface
  52. auto operator*() const -> iterator_sample {
  53. return sample_merge( *m_iter1, *m_iter2 );
  54. }
  55. void operator++()
  56. {
  57. ++m_ds2_pos;
  58. if( m_ds2_pos != m_ds2->size() )
  59. ++m_iter2;
  60. else {
  61. m_ds2_pos = 0;
  62. ++m_iter1;
  63. m_iter2 = std::move( m_ds2->begin() );
  64. }
  65. }
  66. private:
  67. // Data members
  68. dataset1_iter m_iter1;
  69. dataset2_iter m_iter2;
  70. dataset2_decay const* m_ds2;
  71. data::size_t m_ds2_pos;
  72. };
  73. public:
  74. enum { arity = boost::decay<DataSet1>::type::arity + boost::decay<DataSet2>::type::arity };
  75. //! Constructor
  76. grid( DataSet1&& ds1, DataSet2&& ds2 )
  77. : m_ds1( std::forward<DataSet1>( ds1 ) )
  78. , m_ds2( std::forward<DataSet2>( ds2 ) )
  79. {}
  80. //! Move constructor
  81. grid( grid&& j )
  82. : m_ds1( std::forward<DataSet1>( j.m_ds1 ) )
  83. , m_ds2( std::forward<DataSet2>( j.m_ds2 ) )
  84. {}
  85. // dataset interface
  86. data::size_t size() const {
  87. BOOST_TEST_DS_ASSERT( !m_ds1.size().is_inf() && !m_ds2.size().is_inf(), "Grid axes can't have infinite size" );
  88. return m_ds1.size() * m_ds2.size();
  89. }
  90. iterator begin() const { return iterator( m_ds1.begin(), m_ds2 ); }
  91. private:
  92. // Data members
  93. DataSet1 m_ds1;
  94. DataSet2 m_ds2;
  95. };
  96. //____________________________________________________________________________//
  97. // A grid dataset is a dataset
  98. template<typename DataSet1, typename DataSet2>
  99. struct is_dataset<grid<DataSet1,DataSet2>> : mpl::true_ {};
  100. //____________________________________________________________________________//
  101. namespace result_of {
  102. /// Result type of the grid operation on dataset.
  103. template<typename DS1Gen, typename DS2Gen>
  104. struct grid {
  105. typedef monomorphic::grid<typename DS1Gen::type,typename DS2Gen::type> type;
  106. };
  107. } // namespace result_of
  108. //____________________________________________________________________________//
  109. //! Grid operation
  110. template<typename DataSet1, typename DataSet2>
  111. inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
  112. result_of::grid<mpl::identity<DataSet1>,mpl::identity<DataSet2>>
  113. >::type
  114. operator*( DataSet1&& ds1, DataSet2&& ds2 )
  115. {
  116. return grid<DataSet1,DataSet2>( std::forward<DataSet1>( ds1 ), std::forward<DataSet2>( ds2 ) );
  117. }
  118. //____________________________________________________________________________//
  119. //! @overload boost::unit_test::data::operator*
  120. template<typename DataSet1, typename DataSet2>
  121. inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && !is_dataset<DataSet2>::value,
  122. result_of::grid<mpl::identity<DataSet1>,data::result_of::make<DataSet2>>
  123. >::type
  124. operator*( DataSet1&& ds1, DataSet2&& ds2 )
  125. {
  126. return std::forward<DataSet1>(ds1) * data::make(std::forward<DataSet2>(ds2));
  127. }
  128. //____________________________________________________________________________//
  129. //! @overload boost::unit_test::data::operator*
  130. template<typename DataSet1, typename DataSet2>
  131. inline typename boost::lazy_enable_if_c<!is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
  132. result_of::grid<data::result_of::make<DataSet1>,mpl::identity<DataSet2>>
  133. >::type
  134. operator*( DataSet1&& ds1, DataSet2&& ds2 )
  135. {
  136. return data::make(std::forward<DataSet1>(ds1)) * std::forward<DataSet2>(ds2);
  137. }
  138. } // namespace monomorphic
  139. } // namespace data
  140. } // namespace unit_test
  141. } // namespace boost
  142. #include <boost/test/detail/enable_warnings.hpp>
  143. #endif // BOOST_TEST_NO_GRID_COMPOSITION_AVAILABLE
  144. #endif // BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER