
#include "comb/rgs-maxincr.h"

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

#include "demo/nextarg.h"

//% All restricted growth strings (RGS) s[0,...,n-1]
//%   so that s[k] <= max_{j<k}(s[j]+i)
//% Lexicographic order


//#define TIMING // uncomment to disable printing

//#define STATS // uncomment to count descends


int
main(int argc, char **argv)
{
    ulong n = 4;
    NXARG(n, "Length of restricted growth strings");
    ulong i = 2;
    NXARG(i, "Increment allowed (1==> set partitions)");
    // i=1 ==> Bell numbers A000110
    // i=2 ==> Number of symmetric positions of non-attacking rooks
    //          on upper-diagonal part of n X n chessboard.  A080337
    //         see also A080107

    rgs_maxincr R(n, i);
#ifdef STATS
    const ulong *x = R.data();
    ulong *st = new ulong[n];
    for (ulong k=0; k<n; ++k)  st[k] = 0;
#endif // STATS

    ulong ct = 0;
    do
    {
        ++ct;
#ifndef TIMING
        cout << setw(3) << ct << ":  ";
        R.print(true);
#ifdef STATS
        ulong nd = 0;
//        for (ulong k=1; k<n; ++k)  nd += ( x[k-1] > x[k] );  // descends
//        for (ulong k=1; k<n; ++k)  nd += ( x[k-1] >= x[k] );  // non-ascends
//        for (ulong k=1; k<n; ++k)  nd += ( x[k-1] < x[k] );  // ascends
//        for (ulong k=1; k<n; ++k)  nd += ( x[k-1] < x[k] );  // non-ascends
//        for (ulong k=1; k<n; ++k)  nd += ( x[k-1] == x[k] );  // equality
//        for (ulong k=1; k<n; ++k)  nd += ( x[k-1] != x[k] );  // non-equality
        nd = x[n-1]/i;  // last element
        cout << "  nd=" << nd;
        ++st[nd];
#endif // STATS
        cout << endl;
#endif // TIMING
    }
    while ( R.next() );

#ifdef STATS
    cout << "stats: ";
    for (ulong k=0; k<n; ++k)  cout << st[k] << ", ";
    delete [] st;
#endif // STATS

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

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


/*
Timing:
 time ./bin 15 1
 ct = 1382958545
./bin 15 1  12.09s user 0.06s system 99% cpu 12.150 total
 ==>  114,388,630 RGS/sec
*/

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

