
#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
    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:

with PERM_LEX2_FIXARRAYS
 time ./bin 12
arg 1: 12 == n  [Permutations of n elements.]  default=4
  ct=479001600
./bin 12  3.59s user 0.01s system 99% cpu 3.598 total
 ==> 12!/3.59 == 133,426,629 per second

with pointers:
 time ./bin 12
arg 1: 12 == n  [Permutations of n elements.]  default=4
  ct=479001600
./bin 12  4.11s user 0.02s system 99% cpu 4.148 total
 ==> 12!/4.11 == 116,545,401 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:

