
#include "comb/dyck-rgs.h"

#include "comb/comb-print.h"


//#include "jjassert.h"
#include "fxtiomanip.h"
#include "fxttypes.h"

#include "demo/nextarg.h"

//% All restricted growth strings (RGS) for k-ary Dyck words in lexicographic order:
//% strings s[0,...,n-1] such that s[k] <= s[k-1]+i (where i=k-1).


//#define TIMING // uncomment to disable printing

int
main(int argc, char **argv)
{
    ulong n = 4;
    NXARG(n, "Length of restricted growth strings");
    ulong i = 2;
    NXARG(i, "Increment allowed (1==> RGS for parentheses strings)");
    // i=1 ==> A000108 = binomial(2n,n)/(n+1) (Catalan numbers)
    // i=2 ==> A001764 = binomial(3n,n)/(2n+1)
    // i=3 ==> A002293 = binomial(4n,n)/(3n+1)
    // i=4 ==> A002294 = binomial(5n,n)/(4n+1)
    // i=x ==> binomial(x*n,n)/((x-1)*n+1)

    const ulong k = i + 1;  // k-ary Dyck words (or trees)

    dyck_rgs R(n, i);
    const ulong *a = R.data();

    ulong *ds = new ulong[n];  // positions of ones in Dyck word
    const ulong N = k * n;  // size of delta set

    ulong ct = 0;
    do
    {
        ++ct;
#ifndef TIMING
        cout << setw(4) << ct << ":  ";
        R.print();

        for (ulong j=0; j<n; ++j)  ds[j] = k*j - a[j];
        print_set_as_deltaset("    ", ds, n, N);
        print_vec("    ", ds, n);

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

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


    delete [] ds;

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


/*
Timing:


% time ./bin 18 1
arg 1: 18 == n  [Length of restricted growth strings]  default=4
arg 2: 1 == i  [Increment allowed (1==> RGS for parentheses strings)]  default=2
 ct = 477638700
./bin 18 1  2.84s user 0.00s system 99% cpu 2.851 total
 ==> 477638700/2.84 == 168,182,640 per second

%  time ./bin 13 2
arg 1: 13 == n  [Length of restricted growth strings]  default=4
arg 2: 2 == i  [Increment allowed (1==> RGS for parentheses strings)]  default=2
 ct = 300830572
./bin 13 2  1.55s user 0.00s system 99% cpu 1.561 total
 ==> 300830572/1.55 == 194,084,240 per second

%   time ./bin 12 3
arg 1: 12 == n  [Length of restricted growth strings]  default=4
arg 2: 3 == i  [Increment allowed (1==> RGS for parentheses strings)]  default=2
 ct = 1882933364
./bin 12 3  8.61s user 0.00s system 99% cpu 8.632 total
 ==> 1882933364/8.61 == 218,691,447 per second

*/


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

