
#include "comb/perm-lex2.h"

#include "comb/comb-print.h"

#include "comb/fact2perm.h"
#include "perm/perminvert.h"

#include "jjassert.h"

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


//% Generate all permutations in lexicographic order


//#define TIMING // uncomment to disable printing


int
main(int argc, char **argv)
{
    ulong n = 4;
    NXARG(n, "Permutations of n elements, n>=2.");
    jjassert( n>=2 );  // if PERM_LEX2_OPT is defined
    bool dfz = true;  // whether to print dots for zeros

    perm_lex2 P(n);

    ulong ct = 0;
#ifdef TIMING
#ifdef PERM_LEX2_FIXARRAYS
    cout << "PERM_LEX2_FIXARRAYS is defined." << endl;
#endif
#ifdef PERM_LEX2_OPT
        cout << "PERM_LEX2_OPT is defined." << endl;
#endif
    do { ++ct; }  while ( P.next() );
#else
    const ulong *x = P.data();
    const ulong *d = P.d_;
    ulong *ix = new ulong[n]; // inverse permutations
    do
    {
        ++ct;
        cout << setw(4) << ct << ":";
        print_perm("    ", x, n, dfz);
        print_mixedradix("    ", d, n-1, dfz);

        make_inverse(x, ix, n);
        print_perm("    ", ix, n, dfz);

        cout << endl;
    }
    while ( P.next() );

    delete [] ix;
#endif // TIMING

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

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


/*
 Timing:

%  time ./bin 12
arg 1: 12 == n  [Permutations of n elements, n>=2.]  default=4
PERM_LEX2_FIXARRAYS is defined.
PERM_LEX2_OPT is defined.
  ct=479001600
./bin 12  1.56s user 0.00s system 99% cpu 1.562 total
 ==> 12!/1.56 == 307,052,307 per second

%  time ./bin 12
arg 1: 12 == n  [Permutations of n elements, n>=2.]  default=4
PERM_LEX2_OPT is defined.
  ct=479001600
./bin 12  2.66s user 0.00s system 99% cpu 2.669 total
 ==> 12!/2.66 == 180,075,789 per second

%  time ./bin 12
arg 1: 12 == n  [Permutations of n elements, n>=2.]  default=4
PERM_LEX2_FIXARRAYS is defined.
  ct=479001600
./bin 12  2.64s user 0.01s system 99% cpu 2.651 total
 ==> 12!/2.64 == 181,440,000 per second

%  time ./bin 12
arg 1: 12 == n  [Permutations of n elements, n>=2.]  default=4
  ct=479001600
./bin 12  3.76s user 0.00s system 99% cpu 3.770 total
 ==> 12!/3.76 == 127,394,042 per second

*/


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

