Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 3.08 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. generalize stl algorithms with more arguments using variadic template
  2. template<typename II1, typename II2, typename F>
  3. F for_each( II1 _ii1, II1 ii1_, II2 _ii2, F f )
  4. {
  5.     while ( _ii1 != ii1_ )
  6.     { f( *_ii1++, *_ii2++ ); }
  7.  
  8.     return f;
  9. }
  10.  
  11. template<typename II1, typename II2, typename II3, typename F>
  12. F for_each( II1 _ii1, II1 ii1_, II2 _ii2, II3 _ii3, F f )
  13. {
  14.     while ( _ii1 != ii1_ )
  15.     { f( *_ii1++, *_ii2++, *_ii3++ ); }
  16.  
  17.     return f;
  18. }
  19.  
  20. template<typename II1, typename II2, typename II3, typename II4, typename F>
  21. F for_each( II1 _ii1, II1 ii1_, II2 _ii2, II3 _ii3, II4 _ii4, F f )
  22. {
  23.     while ( _ii1 != ii1_ )
  24.     { f( *_ii1++, *_ii2++, *_ii3++, *_ii4++ ); }
  25.  
  26.     return f;
  27. }
  28.        
  29. void increase(){}
  30.  
  31. template< typename II1, typename ... IIn >
  32. void increase( II1& _ii1, IIn& ... _iin )
  33. {
  34.     ++_ii1;
  35.  
  36.     increase( _iin... );
  37. }
  38.  
  39. template<typename F, typename II1, typename ... IIn >
  40. F for_each( F f, II1 _ii1, II1 ii1_, IIn ... _iin )
  41. {
  42.     while ( _ii1 != ii1_ )
  43.     {
  44.         f( *_ii1, *_iin ... );
  45.         increase( _ii1, _iin... );
  46.     }
  47.  
  48.     return f;
  49. }
  50.  
  51. #include <iostream>
  52. int main()
  53. {
  54.     int A[10];
  55.     int B[10];
  56.     int i = 0;
  57.     for_each( [&i]( int & v ) { v = i++; }, A, A+10 );
  58.     for_each( []( int a, int & b ) { b = a; }, A, A+10, B );
  59.  
  60.     for ( int i = 0; i != 10; ++i )
  61.     { std::cout << A[i] << "t" << B[i] << "n"; }
  62.  
  63.     return 0;
  64. }
  65.        
  66. for_each( function, begin1, end1, begin2, begin3 );
  67.        
  68. for_each( begin1, end1, begin2, begin3, function );
  69.        
  70. namespace for_each_impl_private
  71. {
  72. template< typename F, typename InputIterator1, typename ... InputIteratorn >
  73. F _for_each( F f, InputIterator1 begin1, InputIterator1 end1, InputIteratorn ... beginn )
  74. {
  75.     while ( begin1 != end1 )
  76.         f( *begin1++, *beginn++... );
  77.     return f;
  78. }
  79.  
  80. struct dummy {};
  81.  
  82. template< typename S, typename ... T >
  83. void rotate_then_impl( S s, T ... t )
  84. {
  85.     rotate_then_impl( t..., s );
  86. }
  87.  
  88. template< typename S, typename ... T>
  89. void rotate_then_impl( S s, dummy, T ... t )
  90. {
  91.     _for_each( s, t... );
  92. }
  93.  
  94. }//namespace for_each_impl_private
  95. template< typename ... T >
  96. void for_each( T ... t )
  97. {
  98.     static_assert( sizeof ... ( t ) > 2, "for_each requires at least 3 arguments" );
  99.     for_each_impl_private::rotate_then_impl( t..., for_each_impl_private::dummy() );
  100. }
  101.  
  102. #include <iostream>
  103. int main()
  104. {
  105.     int A[10];
  106.     int B[10];
  107.     int i = 0;
  108.     for_each( A, A + 10, [&i]( int & v ) { v = i++; } );
  109.     for_each( A, A + 10, B, []( int a, int & b ) { b = a; } );
  110.     for_each( A, A + 10, B, []( int a, int b ) { std::cout << a << "t" << b << "n"; } );
  111.  
  112.     return 0;
  113. }
  114.        
  115. *_iin ...
  116.        
  117. *_iin++...
  118.        
  119. class dummy {};
  120.  
  121. template< typename S, typename T...>
  122. void inner_func(S s, T... t);
  123.  
  124. template< typename S, typename T...>
  125. void rotate(S s, T... t)
  126. {
  127.     rotate(t..., s);
  128. }
  129.  
  130. template< typename S, typename T...>
  131. void rotate(S s, dummy, T... t)
  132. {
  133.     inner_func(s, t...);
  134. }
  135.  
  136. template< typename T...>
  137. void outer_func(T... t)
  138. {
  139.    rotate(t..., dummy());
  140. }
  141.        
  142. auto i = zip(i1_start, i2, i3, i4, i5);
  143. for_each(i, i.end(i1_end), unzipper(function));
  144.        
  145. for(auto &x : zip_view(i1_start, i1_end, i2, i3, i4, i5) {
  146.     unzip(function, x);
  147. }