
#include "comb/paren-pref.h"


#include "demo/nextarg.h"
#include "fxtio.h"
#include "fxttypes.h"

#include "jjassert.h"

#include "comb/comb-print.h"


//% Generate all well-formed pairs of parentheses by prefix shifts

//#define TIMING // uncomment to disable printing


int
main(int argc, char **argv)
{
    ulong t = 5;
    NXARG(t, "Number of ones (t>0)");
    jjassert( t>0 );
    ulong s = t;
    NXARG(s, "Number of zeros (s<=t)");
    jjassert( t>=s );

    paren_pref C(t, s);
    const ulong *x = C.data();

    ulong ct = 0;
    do
    {
        ++ct;
#ifndef TIMING
        cout << setw(3) << ct << ":";
        if ( s==t )
        {
            cout << "  ";
            for (ulong j=0; j<s+t; ++j)  cout << ( x[j] ? '(' : ')' );
        }
        print_deltaset("  ", x, s+t);
        cout << endl;
        jjassert( C.OK() );
#endif // TIMING
    }
    while ( C.next() );

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

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




/*
Code as given in the paper:

CoolCat(t, s)
Require: t >= s > 0
 1: n = t + s
 2: b = array(1^t 0^s)  // note one-based array
 3: x = t
 4: y = t
 5: visit(b)
 6: while x < n - [[t == s]] do
 7:   b[x] = 0
 8:   b[y] = 1
 9:   x=x+1
10:    y =y+1
11:    if b[x] == 0
12:       if x == 2y - 2 { Case (c) }
13:          x=x+1
14:       else
15:          b[x] = 1 { Case (b) }
16:          b[2] = 0
17:          x=3
18:          y=2
19:       end
20:    end { else Case (a) }
21:    visit(b)
22: end
*/


/*
Timing:

// one-based array:
 time ./bin 18
arg 1: 18 == t  [Number of ones (t>0)]  default=5
arg 2: 18 == s  [Number of zeros (s<=t)]  default=18
 ct = 477638700
./bin 18  1.92s user 0.00s system 99% cpu 1.922 total
 ==> 477638700/1.92 == 248,770,156 per second

// with PAREN_PREF_FIXARRAYS defined:
./bin 18  1.71s user 0.00s system 99% cpu 1.722 total
 ==> 477638700/1.71 == 279,320,877 per second


// zero-based array:
 time ./bin 18
arg 1: 18 == t  [Number of ones (t>0)]  default=5
arg 2: 18 == s  [Number of zeros (s<=t)]  default=18
 ct = 477638700
./bin 18  2.09s user 0.01s system 99% cpu 2.107 total
 ==> 477638700/2.09 == 228,535,263  per second

// with PAREN_PREF_FIXARRAYS defined:
./bin 18  1.99s user 0.00s system 100% cpu 1.988 total
 ==> 477638700/1.99 == 240,019,447 per second

*/

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

