Advertisement
alfps

hum de dum II

May 8th, 2012
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <algorithm>    // std::for_each
  2. #include <bitset>       // std::bitset
  3. #include <iomanip>      // std::setw
  4. #include <iostream>     // std::wcout, std::endl
  5. #include <stddef.h>     // ptrdiff_t
  6. #include <stdexcept>    // std::exception, std::runtime_error
  7. #include <stdlib.h>     // EXIT_SUCCESS, EXIT_FAILURE
  8. #include <string>       // std::wstring
  9. #include <utility>      // std::begin, std::end
  10. #include <vector>       // std::vector
  11.  
  12. using namespace std;
  13. typedef ptrdiff_t   Size;
  14.  
  15. bool throwX( string const& s ) { throw runtime_error( s ); }
  16.  
  17. template< class Elem >
  18. void operator+=( vector<Elem>& v, Elem&& newElem )
  19. {
  20.     v.emplace_back( move( newElem ) );
  21. }
  22.  
  23. template< class Container >
  24. Size countOf( Container const& c ) { return end( c ) - begin( c ); }
  25.  
  26. namespace growDirection { enum Enum{ prepend, append }; }
  27. namespace patternType { enum Enum { pure, gray }; }
  28.  
  29. struct Options
  30. {
  31.     growDirection::Enum     direction;
  32.     patternType::Enum       type;
  33.  
  34.     Options(): direction(), type() {}
  35.  
  36.     Options( int const argc, char const* const argv[] )
  37.         : direction()
  38.         , type()
  39.     {
  40.         for( int i = 1;  i < argc;  ++i )
  41.         {
  42.             if( strcmp( argv[i], "-append" ) == 0 )
  43.             {
  44.                 direction = growDirection::append;
  45.             }
  46.             else if( strcmp( argv[i], "-reflect" ) == 0 )
  47.             {
  48.                 type = patternType::gray;
  49.             }
  50.             else
  51.             {
  52.                 throwX( string() + "Usage: " + argv[0] + " [-reflect][-append]" );
  53.             }
  54.         }
  55.     }
  56. };
  57.  
  58. template< class C >
  59. auto rbegin( C& c ) -> decltype( c.rbegin() ) { return c.rbegin(); }
  60.  
  61. template< class C >
  62. auto rend( C& c ) -> decltype( c.rend() ) { return c.rend(); }
  63.  
  64. template< class It >
  65. void addExtendedStringsTo(
  66.     vector<wstring>&    result,
  67.     wchar_t const       d,
  68.     It const            itSourceBegin,
  69.     It const            itSourceEnd,
  70.     growDirection::Enum direction
  71.     )
  72. {
  73.     for_each( itSourceBegin, itSourceEnd, [&]( wstring const& s )
  74.     {
  75.         result += (direction == growDirection::append? s + d : d + s);
  76.     } );
  77. }
  78.  
  79. vector<wstring> extended( vector<wstring> const& patterns, Options const& opt )
  80. {
  81.     vector<wstring> result;
  82.  
  83.     addExtendedStringsTo(
  84.         result, L'0', begin( patterns ), end( patterns ), opt.direction
  85.         );
  86.  
  87.     switch( opt.type )
  88.     {
  89.     case patternType::pure:
  90.         addExtendedStringsTo(
  91.             result, L'1', begin( patterns ), end( patterns ), opt.direction
  92.             );
  93.         break;
  94.     case patternType::gray:
  95.         addExtendedStringsTo(
  96.             result, L'1', rbegin( patterns ), rend( patterns ), opt.direction
  97.             );
  98.     }
  99.     return result;
  100. }
  101.  
  102. void display( vector<wstring> const& patterns )
  103. {
  104.     for( auto it = begin( patterns); it != end( patterns ); ++it )
  105.     {
  106.         if( it > begin( patterns ) ) { wcout << L" "; }
  107.         wcout << *it;
  108.     }
  109.     wcout << endl;
  110. }
  111.  
  112. void cppMain( Options const& options )
  113. {
  114.     vector<wstring>     patterns( 1, L"" );
  115.  
  116.     for( int i = 1;  i <= 3;  ++i )
  117.     {
  118.         patterns = extended( patterns, options );
  119.         display( patterns );
  120.     }
  121. }
  122.  
  123. int main( int argc, char* argv[] )
  124. {
  125.     try
  126.     {
  127.         cppMain( Options( argc, argv ) );
  128.         return EXIT_SUCCESS;
  129.     }
  130.     catch( exception const& x )
  131.     {
  132.         wcerr << "!" << x.what() << endl;
  133.     }
  134.     return EXIT_FAILURE;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement