
#include "fxtalloca.h"
#include "perm/rotate.h"
#include "fxttypes.h"

#include "jjassert.h"

//static ulong cycl_succ(const ulong *f, ulong len, ulong succ)
//{
//    ulong p = 0;
//    ulong v = succ;
//    for (ulong j=0; j<len; ++j)
//    {
//        if ( f[j]
//    }
//}
//// -------------------------
//
//
//void
//perm2ffact_rot(const ulong *x, ulong n, ulong *fc)
//{  // Todo: Algorithm: look for position of cyclic (mod n) successor
//    ALLOCA(ulong, t, n);  // copy of permutation
//    for (ulong k=0; k<n; ++k)  t[k] = x[k];
////    for (ulong k=0, len=n;  k<n-1;  ++k, --len)
//    for (ulong k=n-2, len=2;  len<=n;  --k, ++len)
//    {
////        ulong j = k;
////        while ( t[j]!=k )  { ++j; jjassert(j<n); }
////        ulong fa = j - k;
//        ulong fa = t[k] - k;
////        ulong fa = j;
//        jjassert( fa < len );
////        rotate_right(t+k, len, fa);
//        rotate_left(t+k, len, fa);
//        fc[k] = fa;
//    }
//}
//// -------------------------


void
ffact2perm_rot(const ulong *fc, ulong n, ulong *x)
// Convert the (n-1) digit falling factorial representation in fc[0,...,n-2].
// into permutation in x[0,...,n-1]
// Must have: fc[0]<n, fc[1]<n-1, ..., fc[n-2]<2 (falling radices)
{
    for (ulong k=0; k<n; ++k)  x[k] = k;
    for (ulong k=0, len=n;  k<n-1;  ++k, --len)
    {
        ulong fa = fc[k];
        rotate_left(x+k, len, fa);
    }
}
// -------------------------

//void perm2rfact_rot(const ulong *fc, ulong n, ulong *x)
//{
//}
//// -------------------------


void
rfact2perm_rot(const ulong *fc, ulong n, ulong *x)
// Convert the (n-1) digit rising factorial representation in fc[0,...,n-2].
//  into permutation in x[0,...,n-1]
// Must have: fc[0]<2, fc[1]<3, ..., fc[n-2]<n (rising radices)
{
    for (ulong k=0; k<n; ++k)  x[k] = k;
    for (ulong k=n-2, len=n;  len>1;  --k, --len)
    {
        ulong fa = fc[k];
        rotate_left(x+n-len, len, fa);
    }
}
// -------------------------
