
#include "perm/permq.h"
#include "perm/permapplyfunc.h"
#include "perm/permrand.h"
#include "bits/graycode.h"
#include "bits/bittransforms.h"
#include "bits/bit2pow.h"
#include "aux1/copy.h"

#include "bits/printbin.h"

#include "fxttypes.h"
#include "fxtiomanip.h"

#include "demo/nextarg.h"

//% Periods of a permutation.


int
main(int argc, char **argv)
{
    ulong n = 11;
    NXARG(n, "Size of permutation (n<0 ==> use 2**abs(n))");
    ulong ldn = ld(n);
    if ( (long)n<0 )
    {
        ldn = -n;
        n = 1UL<<ldn;
        cout << "ldn=" << ldn;
    }
//    cout << "  n=" << n << endl;


    ulong *y = new ulong[n];
    set_seq(y, n);


//    cout << " Apply some permutation(s):" << endl;
    random_permute(y, n);
//    apply_permutation(gray_code, y, n);

    // ... and print periods:
    ulong *p = new ulong[n];
    ulong nt = get_periods(y, n, p);
    for (ulong k=0; k<n; ++k)
    {
        cout << setw(4) << k << ": ";
        cout << "  " << setw(4) << y[k];
        print_bin("  ", y[k], ldn);
        cout << "  " << setw(4) << p[k];
        cout << endl;
    }
    cout << "nt=" << nt << endl;

    delete [] y;
    delete [] p;

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


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

