1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
#include <set>
#include <utility>
#include <type_traits>
#include <algorithm>
#include <ctime>
template < typename SET_A, typename SET_B >
auto cross_product( const SET_A& a, const SET_B& b )
{
using value_type_a = typename SET_A::value_type ;
using value_type_b = typename SET_B::value_type ;
using value_type_result = std::pair< value_type_a, value_type_b > ;
std::set<value_type_result> result ;
for( const auto& first : a ) for( const auto& second : b ) result.emplace( first, second ) ;
return result ;
}
template < typename SET_A, typename SET_B >
auto cross_cross_product( const SET_A& a, const SET_B& b )
{
const auto cp = cross_product( a, b ) ;
return cross_product( cp, cp ) ;
}
template < typename SET_A, typename SET_B >
auto operator_cap( const SET_A& a, const SET_B& b )
{
const auto ccp = cross_cross_product(a,b) ;
typename std::remove_const< decltype(ccp) >::type result ;
for( auto& v : ccp ) if( v.first.second != v.second.second )
{ result.emplace( std::max( v.first, v.second ), std::min( v.first, v.second ) ) ; }
return result ;
}
int main()
{
std::set<int> a { 1, 2 } ;
const std::set<char> b { 'a', 'b' } ;
const auto print_a_cap_b = [&]
{
for( auto pair : operator_cap( a, b ) )
{
std::cout << "{ " << pair.first.first << pair.first.second << ' '
<< pair.second.first << pair.second.second << " } " ;
}
std::cout << "\n\n" ;
};
print_a_cap_b() ;
a.insert(3) ;
print_a_cap_b() ;
{
std::set<int> x ;
std::set<int> y ;
constexpr int n = 40 ;
for( int i = 0 ; i < n ; ++i ) { x.insert(i) ; y.insert(n+i) ; }
const auto start = std::clock() ;
const auto set_size = operator_cap(x,y).size() ;
const auto end = std::clock() ;
std::cout << "sets of " << x.size() << " elements each: result has " << set_size << " quadruples.\n"
<< "brute force took " << double(end-start) / CLOCKS_PER_SEC << " processor seconds.\n" ;
}
}
|