
#include "comb/catalan-gslex.h"

#include "comb/paren-string-to-rgs.h"
#include "comb/comb-print.h"
#include "bits/print-bin.h"

#include "fxtio.h"
#include "fxttypes.h"
#include "jjassert.h"

#include "nextarg.h"


//% Catalan restricted growth strings (RGS) and parentheses strings in gslex order.

// Cf. comb/catalan-subset-lex-demo.cc for subset-lex order

//#define TIMING // uncomment to disable printing


int
main(int argc, char **argv)
{
    ulong n = 5;
#ifdef CATALAN_GSLEX_SC
    NXARG(n, "Number of paren pairs");
#else
    NXARG(n, "Number of paren pairs (n>=2)");
    jjassert( n>=2 );
#endif

    catalan_gslex C(n);
//    bool bw = 0;
//    NXARG(bw, "Whether to generate backward order");
//    if ( bw )  C.last();

    ulong ct = 0;
#ifdef TIMING
//    if ( bw )
//    {
//        cout << "backward:" << endl;
//        do  { ++ct; }  while ( C.prev() );
//    }
//    else
//    {
//        cout << "forward:" << endl;
    do  { ++ct; }  while ( C.next() );
//    }

#else

    const ulong * rgs = C.data();
    ulong *rgs2 = new ulong[n];  // RGS for reversed strings
    ulong *str2 = new ulong[2*n];  // RGS for reversed strings
    do
    {
        ++ct;
#if 1
        cout << setw(4) << ct << ":";

        // print RGS:
        print_vec("    ", rgs, n, true);

        cout << setw(4) << C.tr_;

        const char *str = C.str();
        // print paren string:
//        cout << "    " << str;

        // print delta set (2-ary Dyck word):
        cout << "    ";
        for (ulong k=0; k<2*n; ++k)  cout << ( str[k]=='(' ? '1' : '.' );

        jjassert( paren_string_to_rgs( str, rgs2, true ) );
        print_vec("    ", rgs2, n, true);

        cout << "    ";
        jjassert( is_catalan_rgs(rgs2, n) );
        rgs_to_paren_bit_string(rgs2, n, str2);
        for (ulong k=0; k<2*n; ++k)  cout << ( str2[k] ? '1' : '.' );
//        print_vec("    ", str2, 2*n, true);

        cout << endl;
#endif
    }
    while ( C.next() );
#endif  // TIMING

    cout << " ct=" << ct;  // Catalan number n
    cout << endl;


    return 0;
}
// -------------------------


/*
Timing: (AMD Phenom II X4 945 3000MHz)

 time ./bin 19
 ct=1767263190
./bin 19  5.73s user 0.00s system 99% cpu 5.735 total

 ==> 1767263190/5.73 == 308,422,895 per second

*/



/// Emacs:
/// Local Variables:
/// MyRelDir: "demo/comb"
/// makefile-dir: "../../"
/// make-target: "1demo DSRC=demo/comb/catalan-gslex-demo.cc"
/// make-target2: "1demo DSRC=demo/comb/catalan-gslex-demo.cc DEMOFLAGS=-DTIMING"
/// End:

