
#include "comb/gray-2close-rec-opt.h"

#include "nextarg.h"

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;

//% 2-close Gray path for binary words of length n (recursive construction,
//% optimized generator with precomputed blocks).
//% Default: first change f=0 and last change l=n-2, the nested paths
//% (the path for n is the first half of the path for n+1).

// Cf. gray-2close-rec-demo.cc (the same path, plain recursion)

//#define TIMING  // uncomment to disable printing (or: make timing);
                  // rates: gray-2close-rec-bench

#if defined TIMING
// The visitor must be copy-assignable: a struct, not a lambda.
struct visit  // update the word, count the transitions
{
    uint64_t w = 0;    // current word
    uint64_t ct = 0;   // number of words so far (without the start word)
    void operator () (ulong p)  { w ^= (1ULL << p);  ++ct; }
};
#else  // TIMING
struct visit  // update and print the word
{
    ulong n = 0;       // length of the words
    uint64_t w = 0;    // current word
    uint64_t ct = 0;   // number of the current word
    void print(long p)  const  // p: changed position (-1: none)
    {
        cout << setw(4) << ct << ":  [ ";
        for (ulong j=0; j<n; ++j)  cout << ( ((w >> j) & 1) ? '1' : '.' ) << " ";
        cout << "]";
        if ( p >= 0 )  cout << "  " << setw(2) << p << "  " << ( ((w >> p) & 1) ? '+' : '-' );
        cout << endl;
    }
    void operator () (ulong p)  { w ^= (1ULL << p);  ++ct;  print((long)p); }
};
#endif  // TIMING
// -------------------------


int
main(int argc, char **argv)
{
    ulong n = 6;
    NXARG(n, "Length of binary words (n <= 63)");
    long f = -1;
    NXARG(f, "First changed position (-1: default 0)");
    long l = -1;
    NXARG(l, "Last changed position (-1: default n-2, 0 for n <= 1)");
    ulong bm = 12;
    NXARG(bm, "Length of the precomputed blocks (>= 4)");

    if ( n > 63 )  { cout << "n must be <= 63" << endl;  return 1; }
    const bool dflt = ( f < 0 ) && ( l < 0 );
    if ( f < 0 )  f = (long)gray_2close_rec_opt::default_first(n);
    if ( l < 0 )  l = (long)gray_2close_rec_opt::default_last(n);
    if ( ! gray_2close_rec_opt::valid(n, (ulong)f, (ulong)l) )
    {
        cout << "(f,l) = (" << f << "," << l << ") not covered for n=" << n << endl;
        return 1;
    }

    visit v;
    // Default pair and default block length: the static gen(n, visit)
    // with the shared generator; otherwise a generator with blocks
    // of length bm.
    auto gen = [&]()
    {
        if ( dflt && bm == 12 )  gray_2close_rec_opt::gen(n, v);
        else
        {
            const gray_2close_rec_opt G(bm);
            G.gen(n, (ulong)f, (ulong)l, 0, v);
        }
    };

#if defined TIMING
    gen();
    cout << " last word=" << v.w << endl;
#else  // TIMING
    v.n = n;
    v.print(-1);  // the start word 0...0
    gen();
#endif  // TIMING

    cout << " ct=" << v.ct + 1;  // == 2^n
    cout << endl;

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

/*
Timing: (Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz)
GCC 14.2

time ./bin 32
arg 1: 32 == n  [Length of binary words (n <= 63)]  default=5
 last word=4160731058
 ct=4294967296
2.64user 0.00system 0:02.64elapsed 100%CPU (0avgtext+0avgdata 3736maxresident)k
 ==> 4294967296/2.64 == 1,626,881,551 per second
*/

/// Emacs:
/// Local Variables:
/// MyRelDir: "demo/comb"
/// makefile-dir: "../../"
/// make-target: "1demo DSRC=demo/comb/gray-2close-rec-opt-demo.cc"
/// make-target2: "1demo DSRC=demo/comb/gray-2close-rec-opt-demo.cc DEMOFLAGS=-DTIMING"
/// End:
