#include <cassert>
using namespace std;

#include <permutationfunc.h>

// Linear search to match the permutation with the
//   index.
uintc permutationfunc::inverse(bool & found, uintc * x) const
{
  for (uint i=0; i<N; ++i)
  {
    found=true;
    for (uint k=0; k<dim; ++k)
    {
      if (x[k]!=fop[i*dim+k])
      {
        found=false;
        k=dim;
      }
    }
    
    if (found==false)
      continue;

    return i;
  }

  return 0;
}


void permutationfunc::ik(uint * x, uintc i, uintc k) const
{
  assert(i<N);
  assert(k<N);
  for (uint w=0; w<dim; ++w)
    x[w] = fop[ i*dim + fop[ k*dim + w ] ];
}

uintc permutationfunc::ik(bool & found, uintc i, uintc k) const
{
  uint x[dim];
  ik(x,i,k);

  return inverse(found,x);
}


void permutationfunc::writeBinaryOperator
(
  bool & result, 
  uint * mat
) const
{
  result=true;
  bool found;
  for (uint i=0; i<N; ++i)
    for (uint k=0; k<N; ++k)
    {
      mat[i*N+k] = ik(found,i,k);
      result &= found;
    }
}





      


