
#include "comb/partition.h"

#include "fxttypes.h"

#include "jjassert.h"
#include "demo/nextarg.h"

#include "fxtio.h"

//% Generate all integer partitions, iterative algorithm.

//#define TIMING // uncomment to disable printing

int
main(int argc, char **argv)
{
    ulong n = 6;
    NXARG(n, "Partitions of n");
    bool sq = 0;
    NXARG(sq, "Whether to print cumulative sums");
    partition P(n);

    ulong ct = 0;

#ifdef TIMING
    if ( !sq ) // here: whether forward or backward
    {
        cout << "forward:" << endl;
        P.first();
        do  { ++ct; }  while ( P.next() );
    }
    else
    {
        cout << "backward:" << endl;
        P.last();
        do  { ++ct; }  while ( P.prev() );
    }
#else // TIMING

    do
    {
#if 0
        if ( (P.num_parts()%3) != 2 )  --ct;
#endif

        cout << "    #" << setw(2) << ct << ": ";
//        cout << " [" << P.num_parts() << "]  ";
//        cout << " [" << P.num_sorts() << "]  ";
        cout << setw(4) << P.n_ << " == ";
        P.print2();
        cout << "  ==  ";  P.print();
        cout << endl;

        if ( sq )
        {
            cout << "       sums: ";
            for (ulong k=2; k<=n; ++k)  cout << setw(7) << P.s_[k];
            cout << endl;
            cout << endl;
        }
        jjassert( P.OK() );
        ++ct;
    }
    while ( P.next() );
#endif // TIMING


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

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

/*
Timing:

% time ./bin 110 0
arg 1: 110 == n  [Partitions of n]  default=6
arg 2: 0 == sq  [Whether to print cumulative sums]  default=0
forward:
  ct=607163746
./bin 110 0  3.02s user 0.00s system 99% cpu 3.032 total
 ==> 607163746/3.02 == 201,047,598 per second

// line  q = (z>=i ? z/i : 0);  :
arg 1: 100 == n  [Partitions of n]  default=6
arg 2: 1 == sq  [Whether to print cumulative sums]  default=0
backward:
  ct=190569292
./bin 100 1  2.70s user 0.00s system 99% cpu 2.705 total
 ==> 190569292/2.70 == 70,581,219 per second

// line  q = z/i;  :
% time ./bin 100 1
arg 1: 100 == n  [Partitions of n]  default=6
arg 2: 1 == sq  [Whether to print cumulative sums]  default=0
backward:
  ct=190569292
./bin 100 1  3.03s user 0.01s system 99% cpu 3.055 total
 ==> 190569292/3.03 == 62,894,155 per second

*/

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

