
#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
//    P.first();  do  { ++ct; }  while ( P.next() );
    P.last();   do  { ++ct; }  while ( P.prev() );
#else // TIMING

    do
    {
#if 0
        { bool q = true;
            const ulong *c = P.data();
            for (ulong k=1; k<=n; ++k)  if ( c[k] > 1 )  { q = false;  break; }
            if ( !q )  continue;

            for (ulong k=1; k<=n-2; ++k)
            {
                const ulong  ck0 = c[k],  ck1 = c[k+1],  ck2 = c[k+2];
                const bool  q0 = !!ck0,  q1 = !!ck1,  q2 = !!ck2;
                if ( q0 && q1 )  { q = false;  break }
                if ( q0 && q2 )  { q = false;  break; }
            }
            if ( !q )  continue;
        }
#endif

        cout << "    #" << setw(2) << ct << ": ";
        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 << " " << n << ":  ct=" << ct << endl;
    cout << endl;

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

/*
Timing:
 % time ./bin 100
arg 1: 100 == n  [Partitions of n]  default=6

forward:
 100:  ct=190569292
./bin 100  1.18s user 0.00s system 99% cpu 1.181 total
 ==> 190569292/1.18 == 161,499,400 per second

backwards:
 100:  ct=190569292
./bin 100  3.30s user 0.01s system 99% cpu 3.308 total  // line  q = z/i;
 ==> 190569292/3.30 == 57,748,270 per second
./bin 100  2.86s user 0.00s system 99% cpu 2.865 total  // line  q = (z>=i ? z/i : 0);
 ==> 190569292/2.86 == 66,632,619 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:

