
#include "comb/paren.h"

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

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

#include "nextarg.h"
#include "jjassert.h"


//% Generate all well-formed pairs of parentheses in co-lexicographic order.

//#define TIMING // uncomment to disable printing


int
main(int argc, char **argv)
{
    ulong k = 5;
    NXARG(k, "Number of paren pairs >=2");
    jjassert( k>= 2 );

#ifdef TIMING
    bool bwq = 0;
    NXARG(bwq, "Whether to generate in backward direction");
#endif

    paren C(k);
    ulong *rgs = new ulong[k];

    ulong ct = 0;

#ifdef TIMING
    if ( bwq )
    {
        C.last();
        do { ++ct; } while ( C.prev() );
    }
    else
    {
        C.first();
        do { ++ct; } while ( C.next() );
    }

#else
    ulong n = 2*k;
    do
    {
        ++ct;
        cout << setw(3) << ct << ":  ";

        const char * str = C.string();
        cout << str << "  ";

        print_set_as_deltaset("", C.data(), k, n );

        jjassert( paren_string_to_rgs( str, rgs, false) );
        print_vec("    ", rgs, k, true);

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

    cout << " ct = " << ct << endl;

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


/*
Timing:

 time ./bin 18
arg 1: 18 == k  [Number of paren pairs >=2]  default=5
arg 2: 0 == bwq  [Whether to also generate in backward direction]  default=0
 ct = 477638700
./bin 18  7.08s user 0.02s system 100% cpu 7.095 total
 ==> 477638700/7.08 == 67,463,093 per second

*/

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

