Advertisement
alfps

hum de dum III

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