#if !defined  HAVE_WALSHBASEFUNC_H__
#define       HAVE_WALSHBASEFUNC_H__


#include "bits/parity.h"
#include "bits/graycode.h"
#include "bits/grsnegative.h"
//#include "bits/revgraycode.h"
#include "bits/revbin.h"  // revbin()
#include "bits/bit2pow.h"  // ld()
#include "fxttypes.h"  // ulong


template <typename Type>
void walsh_wak_basefunc(Type *f, ulong n, ulong k)
// Basis functions for Walsh-Kronecker basis (wak-functions).
{
    for (ulong i=0; i<n; ++i)
    {
        ulong x = i & k;
        x = parity(x);
        f[i] = ( 0==x ? +1 : -1 );
    }
}
// -------------------------

template <typename Type>
void walsh_pal_basefunc(Type *f, ulong n, ulong k)
// Basis functions for Walsh-Paley basis (pal-functions).
{
    k = revbin(k, ld(n));
    for (ulong i=0; i<n; ++i)
    {
        ulong x = i & k;
        x = parity(x);
        f[i] = ( 0==x ? +1 : -1 );
    }
}
// -------------------------


template <typename Type>
void walsh_wal_basefunc(Type *f, ulong n, ulong k)
// Basis functions for Walsh-Kaczmarz basis (wal-functions).
// Basis functions are sequency-ordered.
{
    k = revbin(k, ld(n)+1);
    k = gray_code(k);
//    // =^=
//    k = revbin(k, ld(n));
//    k = rev_gray_code(k);
    for (ulong i=0; i<n; ++i)
    {
        ulong x = i & k;
        x = parity(x);
        f[i] = ( 0==x ? +1 : -1 );
    }
}
// -------------------------

template <typename Type>
void walsh_wal_rev_basefunc(Type *f, ulong n, ulong k)
// Basis functions for transform walsh_wal_rev()
{
    k = revbin(k, ld(n));
    k = gray_code(k);
    // =^=
//    k = rev_gray_code(k);
//    k = revbin(k, ld(n));
    for (ulong i=0; i<n; ++i)
    {
        ulong x = i & k;
        x = parity(x);
        f[i] = ( 0==x ? +1 : -1 );
    }
}
// -------------------------

template <typename Type>
void walsh_q1_basefunc(Type *f, ulong n, ulong k)
// Basis functions for transform walsh_q1()
{
    ulong qk = (grs_negative_q(k) ? 1 : 0);
    k = gray_code(k);
    k = revbin(k, ld(n));
    for (ulong i=0; i<n; ++i)
    {
        ulong x = i & k;
        x = parity(x);
        ulong qi = (grs_negative_q(i) ? 1 : 0);
        x ^= (qk ^ qi);
        f[i] = ( 0==x ? +1 : -1 );
    }
}
// -------------------------

template <typename Type>
void walsh_q2_basefunc(Type *f, ulong n, ulong k)
// Basis functions for transform walsh_q2()
{
    ulong qk = (grs_negative_q(k) ? 1 : 0);
    k = revbin(k, ld(n));
    k = gray_code(k);
    for (ulong i=0; i<n; ++i)
    {
        ulong x = i & k;
        x = parity(x);
        ulong qi = (grs_negative_q(i) ? 1 : 0);
        x ^= (qk ^ qi);
        f[i] = ( 0==x ? +1 : -1 );
    }
}
// -------------------------


#endif  // !defined HAVE_WALSHBASEFUNC_H__
