
/*******************************************************************************
*
* File blk_queries.h
*
* Copyright (C) 2005 Martin Luescher
*
* This software is distributed under the terms of the GNU General Public
* License (GPL)
*
* Block query descriptions
*
*******************************************************************************/

#define BLK_QUERIES_H

#if (defined FLAGS_C)

static int (*blk_query_fcts[QUERIES])(int nb)={NULL};


static int BlkQueryUAllocated(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].u==0)&&((*gf).shu==0))
            nt=0;
      }
   }

   return nt;
}


static int BlkQueryUdAllocated(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud==0)&&((*gf).shud==0))
            nt=0;
      }
   }

   return nt;
}


static int BlkQueryBcFlipped(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if (bf[n].bc==0)
            nt=0;
      }
   }

   return nt;
}


static int BlkQueryBcdFlipped(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if (bf[n].bcd==0)
            nt=0;
      }
   }

   return nt;
}


static int BlkQueryUblkMatchUd(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].u!=lat.ud)||(bf[n].ub!=(*gf).ubd)||(bf[n].bc!=lat.bcd))
            nt=0;
      }
   }

   return nt;
}


static int BlkQueryUdblkMatchUd(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud!=lat.ud)||(bf[n].ubd!=(*gf).ubd)||(bf[n].bcd!=lat.bcd))
              nt=0;
      }
   }

   return nt;
}


static int BlkQuerySwUp2date(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].u==0)||(bf[n].sw[0]!=bf[n].u))
            nt=0;
      }
   }

   return nt;
}


static int BlkQuerySwEInverted(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if (bf[n].sw[1]!=1)
            nt=0;
      }
   }

   return nt;   
}


static int BlkQuerySwOInverted(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if (bf[n].sw[2]!=1)
            nt=0;
      }
   }

   return nt;      
}


static int BlkQuerySwdUp2date(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if ((bf[n].ud==0)||(bf[n].swd[0]!=bf[n].ud))
            nt=0;
      }
   }

   return nt;
}


static int BlkQuerySwbUp2date(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if (bf[n].swb!=bf[n].ubd)
            nt=0;
      }
   }

   return nt;
}


static int BlkQuerySwdEInverted(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if (bf[n].swd[1]!=1)
            nt=0;
      }
   }

   return nt;   
}


static int BlkQuerySwdOInverted(int nb)
{
   int n,nbf,nt;
   blk_flags_t *bf;

   nbf=(*gf).nb;
   bf=(*gf).bf;
   nt=1;

   for (n=0;n<nbf;n++)
   {
      if ((nb==nbf)||(nb==n))
      {
         if (bf[n].swd[2]!=1)
            nt=0;
      }
   }

   return nt;      
}


static void set_blk_queries(void)
{
   blk_query_fcts[U_ALLOCATED]=BlkQueryUAllocated;
   blk_query_fcts[UD_ALLOCATED]=BlkQueryUdAllocated;
   blk_query_fcts[BC_FLIPPED]=BlkQueryBcFlipped;
   blk_query_fcts[BCD_FLIPPED]=BlkQueryBcdFlipped;
   blk_query_fcts[UBLK_MATCH_UD]=BlkQueryUblkMatchUd;
   blk_query_fcts[UDBLK_MATCH_UD]=BlkQueryUdblkMatchUd;
   blk_query_fcts[SW_UP2DATE]=BlkQuerySwUp2date;
   blk_query_fcts[SW_E_INVERTED]=BlkQuerySwEInverted;
   blk_query_fcts[SW_O_INVERTED]=BlkQuerySwOInverted;   
   blk_query_fcts[SWD_UP2DATE]=BlkQuerySwdUp2date;
   blk_query_fcts[SWB_UP2DATE]=BlkQuerySwbUp2date;
   blk_query_fcts[SWD_E_INVERTED]=BlkQuerySwdEInverted;
   blk_query_fcts[SWD_O_INVERTED]=BlkQuerySwdOInverted;
}

#endif
