
#include "comb/perm-involution.h"

#include "perm/permq.h"
#include "comb/comb-print.h"
#include "perm/printcycles.h"

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


//% Generate all involutions (self-inverse permutations).

//#define TIMING // uncomment to disable printing


int
main(int argc, char **argv)
{
    ulong n = 5;
    NXARG(n, "Self-inverse permutations of n elements.");
    bool dfz= true; // whether to print dots for zeros

    perm_involution P(n);
    ulong ct = 0;

#ifdef TIMING
    while ( P.next() )  { ++ct; }

#else

//    ulong ix[64];
    const ulong *x = P.data();
    do
    {
        cout << setw(4) << ct << ":";
        ++ct;

//        for (ulong j=0, k=n-1; j<n; ++j, --k)  ix[k]=n-1-x[j];  // OK
//        for (ulong j=0, k=n-1; j<n; ++j, --k)  ix[k]=n-1-x[k];  // not OK
//        for (ulong j=0, k=n-1; j<n; ++j, --k)  ix[k]=x[j];  // not OK

        P.print("  ", dfz);

//        print_perm("    ", ix, n, true);

        cout << "    ";  print_cycles(x, n);

//        cout << "    ";  print_cycles(ix, n);

        cout << endl;

//        jjassert( is_involution(ix, n) );
        jjassert( is_involution(x, n) );
    }
    while ( P.next() );

#endif
    cout << " ct=" << ct << endl;

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

/*
Timing:

%  time ./bin 17
arg 1: 17 == n  [Self-inverse permutations of n elements.]  default=5
 ct=211799311
./bin 17  3.96s user 0.00s system 99% cpu 3.971 total
 ==> 211799311/3.96 == 53,484,674 per second

BENCHARGS=17

*/


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

