#if !defined  HAVE_PERMRAND_ORD_H__
#define       HAVE_PERMRAND_ORD_H__

#include "perm/permrand.h"  // random_permute()

#include "aux0/swap.h"
#include "fxttypes.h"


template <typename Type>
void random_ord01_permutation(Type *f, ulong n)
// Random permutation such that elements 0 and 1 are in order.
{
    random_permutation(f, n);
    ulong t = 0;
    while ( f[t]>1 )  ++t;
    if ( f[t]==0 )  return;  // already in correct order
    f[t] = 0;
    do { ++t; }  while ( f[t]!=0 );
    f[t] = 1;
}
// -------------------------


template <typename Type>
void random_ordm_permutation(Type *f, ulong n, ulong m)
// Random permutation such that the m smallest elements are in order.
// Must have m<=n.
{
    random_permutation(f, n);
    for (ulong t=0,j=0; j<m; ++t)  if ( f[t]<m )  { f[t]=j;  ++j; }
}
// -------------------------


template <typename Type>
void random_lastm_permutation(Type *f, ulong n, ulong m)
// Random permutation such that 0 appears as last of the m smallest elements.
// Must have m<=n.
{
    random_permutation(f, n);
    if ( m<=1 )  return;

    ulong p0=0, pl=0;  // position of 0, and last (in m smallest elements)
    for (ulong t=0, j=0;  j<m;  ++t)
    {
        if ( f[t]<m )
        {
            pl = t;  // update position of last
            if ( f[t]==0 )  { p0 = t; }  // record position of 0
            ++j;   // j out of m smallest found
        }
    }
    // here t is the position of the last of the m smallest elements
    swap2( f[p0], f[pl] );
}
// -------------------------


#endif  // !defined HAVE_PERMRAND_ORD_H__
