#if !defined HAVE_GRAY_2CLOSE_REC_H__
#define      HAVE_GRAY_2CLOSE_REC_H__
// This file is part of the FXT library.
// Copyright (C) 2026 Joerg Arndt
// License: GNU General Public License version 3 or later,
// see the file COPYING.txt in the main directory.

// code and algorithm generated with AI (Claude Opus 5.5)

// Recursive construction of 2-close Gray paths for binary words
// of any length n.
//
// A Gray path is given by its sequence of changed positions
// (start word 0...0).  Let T(m) be the set of pairs (f, l) such that
// there is a 2-close Gray path for length m with first change at
// position f and last change at position l.
//
// Concatenation: if G and H are 2-close Gray paths for length m,
// with G ending in the word e, then the sequence of positions
//     G,  c,  H        (c the new position)
// is a Gray path for length m+1: it lists the words of G (new bit 0),
// then the words of e XOR H (new bit 1).
// - Back:  c = m (old positions unchanged); 2-close iff the last change
//   of G and the first change of H are in {m-2, m-1}.
// - Front: c = 0 (old positions shifted up by one); 2-close iff the
//   last change of G and the first change of H are in {0, 1}
//   (before shifting).
//
// Theorem: for m >= 3, T(m) contains all pairs (f, l) in [0, m-1]^2
// except (0, m-1) and (m-1, 0)  [for m = 3 all nine pairs].
// Proof by induction, with the base case m = 3 (table below):
//   pairs (f, l) with f, l <= m-1:  G in (f, m-2), H in (m-2, l), back;
//   pairs (f, l) with f, l >= 1:    G in (f-1, 1), H in (1, l-1), front;
//   all four pairs used are in T(m) (for m >= 3).
//   Only (0, m) and (m, 0) are not covered for length m+1.
// Hence 2-close Gray paths exist for all n.

#include "fxttypes.h"

#include <cstdint>
#include <vector>


class gray_2close_rec
// Generation of a 2-close Gray path for length n with first change f
// and last change l, (f, l) != (0, n-1), (n-1, 0) for n >= 4.
//
// The visitor (template parameter Visit) receives the path as its
// sequence of changed positions: visit(p) is called once for each
// transition, in order, with p the position changed to obtain the next
// word (0 <= p < n, plus the shift s).  The start word 0...0 itself is
// not visited, so there are 2^n - 1 calls.  The visitor is passed by
// reference: after gen() its state is the one after the last call.
// gen() cannot be stopped early (a return value of visit is ignored).
// Any callable object works, e.g. a lambda:
//     uint64_t w = 0;   // current word
//     auto visit = [&](ulong p)  { w ^= (1ULL << p);  /* use w */ };
//     gray_2close_rec::gen(n, visit);
// or a struct (this also works with gray_2close_rec_opt, which needs
// a copy-assignable visitor; lambdas are not):
//     struct visit
//     {
//         uint64_t w = 0;   // current word
//         void operator () (ulong p)  { w ^= (1ULL << p);  /* use w */ }
//     };
//     visit v;
//     gray_2close_rec::gen(n, v);   // afterwards v.w is the last word
{
public:
    // Base table: 2-close Gray paths for n = 3,
    // B3[f][l] has first change f and last change l.
    static constexpr uint8_t B3[3][3][7] =
    {
        { {0,1,0,2,0,1,0}, {0,1,0,2,1,0,1}, {0,1,2,1,0,1,2} },
        { {1,0,1,2,0,1,0}, {1,0,1,2,1,0,1}, {1,0,2,0,1,0,2} },
        { {2,1,0,1,2,1,0}, {2,1,2,0,1,2,1}, {2,1,2,0,2,1,2} }
    };

    static bool valid(ulong n, ulong f, ulong l)
    // Whether the construction covers (n, f, l).
    // Also used by gray-2close-rec-opt.h
    {
        if ( n == 0 )  return true;  // single empty word, no transitions
        if ( (f >= n) || (l >= n) )  return false;
        if ( n == 1 )  return true;  // only (0,0)
        if ( n == 2 )  return ( f == l );  // 0 1 0  or  1 0 1
        if ( n == 3 )  return true;
        if ( (f == 0) && (l == n-1) )  return false;
        if ( (f == n-1) && (l == 0) )  return false;
        return true;
    }

    // Default pair (f, l) = (0, n-2) for n >= 2, and (0, 0) for n <= 1:
    // the nested paths.  The sequence of changed positions for length n
    // is the first half of the one for length n+1, for all n >= 0
    // (for n >= 2: in R(n+1, 0, n-1) step R3 performs R(n, 0, n-2) first,
    // see remark "Nested paths" in doc/gray-2close.pdf), so all of them
    // are prefixes of one infinite sequence 0 1 0 2 1 0 1 3 1 0 2 0 ...
    static ulong default_first(ulong)  { return 0; }
    static ulong default_last(ulong n)  { return ( n >= 2 ? n - 2 : 0 ); }

    template <typename Visit>
    static void gen(ulong n, Visit &visit)
    // Generate the nested 2-close Gray path for length n, that is,
    // gen(n, default_first(n), default_last(n), 0, visit).
    // Valid for all n.
    // visit(p) is called for each changed position p (see class comment).
    { gen(n, default_first(n), default_last(n), 0, visit); }

    template <typename Visit>
    static void gen(ulong n, ulong f, ulong l, ulong s, Visit &visit)
    // Generate the 2-close Gray path for length n, first change f,
    // last change l, all positions shifted by s.
    // Requires valid(n, f, l).
    // visit(p) is called for each changed position p (see class comment).
    {
        if ( n >= 4 )
        {
            const ulong m = n - 1;  // length of the halves
            if ( (f <= m-1) && (l <= m-1) )  // back: new position m
            {
                gen(m, f, m-2, s, visit);
                visit( m + s );
                gen(m, m-2, l, s, visit);
            }
            else  // front: new position 0 (here f, l >= 1)
            {
                gen(m, f-1, 1, s+1, visit);
                visit( s );
                gen(m, 1, l-1, s+1, visit);
            }
            return;
        }

        switch ( n )  // base cases
        {
        case 3:
        {
            const uint8_t *b = B3[f][l];
            for (ulong i=0; i<7; ++i)  visit( b[i] + s );
            break;
        }
        case 2:  // f == l
            visit( f + s );  visit( 1 - f + s );  visit( f + s );
            break;
        case 1:
            visit( s );
            break;
        default:  // n == 0: no transitions
            break;
        }
    }
};
// -------------------------

// Out-of-class definition of B3, needed only before C++17 (there the
// declaration in the class is no definition, and line "b = B3[f][l]"
// needs one); since C++17 static constexpr members are implicitly inline,
// and this redeclaration is redundant (and deprecated):
//constexpr uint8_t gray_2close_rec::B3[3][3][7];


#endif  // !defined HAVE_GRAY_2CLOSE_REC_H__
