
#include "comb/ksubset-rec.h"
//#include "fxtiomanip.h"
//#include "fxttypes.h"


void
ksubset_rec::next_rec(long d)
{
    if ( d>kmax_ )  return;

    ++rct_;  // measure computational work
    long rv1 = rv_[d-1];  // left neighbor
    bool q = 1;
    switch ( rq_ % 4 )
    {
    case 0:  q = 1;  break;
    case 1:  q = !(d&1);  break;
    case 2:  q = rv1&1;  break;
    case 3:  q = (d^rv1)&1;  break;
    }

    if ( nq_ )  q = !q;

    long x0 = rv1 + 1;
    long rx = n_ - (kmin_ - d);
    long x1 = min2( n_-1, rx );

#define PCOND(x) if ( (pq_==x) && (d>=kmin_) )  { visit_(*this, d);  ++ct_; }
    PCOND(0);
    if ( q )  // forward:
    {
        PCOND(1);
        for (long x=x0; x<=x1; ++x)  { rv_[d] = x;  next_rec(d+1); }
        PCOND(2);
    }
    else  // backward:
    {
        PCOND(2);
        for (long x=x1; x>=x0; --x)  { rv_[d] = x;  next_rec(d+1); }
        PCOND(1);
    }
    PCOND(3);
#undef PCOND
}
// -------------------------
