/*                        logic_io1.c                 23-11-89 




*************************************************************
*************************************************************

 *****  The routines for reading in setups and for      *****
 *****  making the appropriate initialisations.  Also   *****
 *****  for printing out the matrices found.            *****


************************************************************/

sep(x)  int *x;
{
   if ( *x ) 
   {
      if ( theJob->tty_out==UGLY ) printf(" -1\n");
      if ( theJob->fil_out==UGLY ) fprintf(outfil, " -1\n");
      *x = 0;
   }
}

/*****************************************************************/

got_siz()        /*** Return new size, or 0 if none ***/
{ 
   if ( theJob->f_n ) sep( &negno ); else sep( &ordno );
   fscanf(infil, "%d", &siz);
   if ( DONE || siz==theJob->sizmax || siz<0 ) siz = 0;
   if ( theJob->f_T ) subf[6].val = siz;
   if ( theJob->f_F ) subf[7].val = 0;
   return( siz );
}



/******************************************************/

got_neg()       /*** Read neg. Return 0 if none ***/
{
   int i,j;

   sep(&ordno);
   fscanf(infil, "%d", N);
   if ( DONE || *N < 0 ) return(0);
   for ( i = 1; i <=siz; i++ ) fscanf(infil, "%d", N+i);
   return(1);
}


/******************************************************/

got_ord()       /*** Read order.  Return 0 if none ***/
{
   int i,j,k;

   sep( &desno ); 
   fscanf(infil, "%d", *ord);
   if ( DONE || **ord < 0 ) return(0);
   FORALL(i)  FORALL(j)
   if ( i || j ) fscanf(infil, "%d", ord[i]+j);
   FORALL(i)
   {
      maximal[i] = 1;
      for ( j = i+1; j <=siz; j++ ) maximal[i] *= !ord[i][j];
   }
   if ( theJob->f_lat ) 
   {
      FORALL(i)
      { 
         for ( j = i; j <= siz; j++ )
         { 
            for ( k = j; !(ord[i][k] && ord[j][k]); k++ ) ;
            { A[i][j] = A[j][i] = k; }
            for ( k = i; !(ord[k][i] && ord[k][j]); k-- ) ;
            K[i][j] = K[j][i] = k;
         }
      }
   }
   Vlength = 0;
   FORALL(i) FORALL(j)
   if ( !( F_N && i < N[j] ))
   {
      cc[i][j] = Vlength++;
      if ( F_N ) cc[N[j]][N[i]] = cc[i][j];
   }
   return(1);
}


/*******************************************************/

got_des()         /*** Get new des. Return 0 if none ***/
{ 
   int i;

   if ( matno )
   {
      if ( theJob->tty_out == SUMMARY ) 
      {
         printf("     %d matri%s", matno, (matno>1? "ces": "x"));
         fflush(stdout);
      }
      if ( theJob->fil_out == SUMMARY ) 
      fprintf(outfil, "     %d matri%s", matno, (matno>1? "ces": "x"));
      sep( &matno );
   }
   FORALL(i) true[i] = 0;
   fscanf(infil, "%d", &des);
   if ( DONE || des < 0 ) return(0);
   if ( theJob->f_t ) 
   {
      FORALL(i) true[i] = ord[des][i];
      subf[4].val = des;
      if ( theJob->f_n ) subf[5].val = N[des];
   }
   else while ( des >= 0 )
   {
      true[des] = 1;
      fscanf(infil, "%d", &des);
   }
   return(1);
}


/*******************************************************/
  

mat_print()     /*** This is the "accept" routine ***/
{
  good++;
  if ( theJob->tty_out ) C_print(stdout,theJob->tty_out);
  if ( theJob->fil_out ) C_print(outfil,theJob->fil_out);
  if ( !(matno++) )
  { 
     if ( !(desno++) )
     { 
        if ( !(ordno++) ) { if ( theJob->f_n ) negno++; }
     }
  }
}



/*******************************************************/

siz_print(f,x)  FILE *f;  int x;
{
  switch(x)
  { 
     case UGLY:    fprintf(f, " %d\n", siz);                 break;
     case PRETTY:  fprintf(f, "\n\n\n\n\n Size: %d", siz+1); break;
     case SUMMARY: fprintf(f, "\n\n\n Size: %d", siz+1);
  }
}



/*******************************************************/

neg_print(f,x)  FILE *f;  int x;
{ int i;

  if ( !negno ) siz_print(f,x);
  switch(x)
  { 
     case UGLY:    FORALL(i) fprintf(f, " %d", N[i]);
                   fprintf(f, "\n");  break;
     case PRETTY:  fprintf
                   (f, "\n\n\n Negation table #%d   ~ |", negno+1);
                   FORALL(i) fprintf(f, " %x", i);
                   fprintf(f, "\n                     --+");
                   FORALL(i) fprintf(f, "--");  
                   fprintf(f, "\n                       |");
                   FORALL(i) fprintf(f, " %x", N[i]);
                   fprintf(f, "\n");  break;
     case SUMMARY: fprintf(f, "\n\n    Negation #%d", negno+1);
  }
}



/*******************************************************/

ord_print(f,x)  FILE *f;  int x;
{ int i,j;

  if ( !ordno ) 
  {
     if ( theJob->f_n ) neg_print(f,x);
     else siz_print(f,x);
  }
  switch(x)
  { 
     case UGLY:    FORALL(i) FORALL(j) 
                   fprintf(f, " %d", ord[i][j]); 
                   fprintf(f, "\n");  break;
     case PRETTY:  fprintf(f, "\n\n\n Order #%-3d   < |", ordno+1);
                   FORALL(i) fprintf(f, " %x", i);
                   fprintf(f, "\n              --+");
                   FORALL(i) fprintf(f, "--");
                   FORALL(i)
                   { 
                      fprintf(f, "\n%15x |", i);
                      FORALL(j) 
                      fprintf(f, " %c", (ord[i][j]? '+': '-'));
                   }
                   fprintf(f, "\n\n");  break;
     case SUMMARY: fprintf(f, "\n\n       Order #%d", ordno+1);
  }
}



/*******************************************************/

des_print(f,x)  FILE *f; int x;
{ 
  if ( !desno ) ord_print(f,x);
  if ( theJob->f_t ) switch(x)
  { 
     case UGLY:    fprintf(f, " %d\n", des);                 break;
     case PRETTY:  fprintf(f, "\n Choice of t: %x\n", des);  break;
     case SUMMARY: fprintf(f, "\n\n          t = %x", des);
  }
  else switch(x)
  {
     case UGLY:    FORALL(des) if ( true[des] ) 
                   fprintf(f, " %d", des); 
                   fprintf(f, " 0\n" );                      break;
     case PRETTY:  fprintf(f, "\n Choice of truths: ");
                   FORALL(des) if ( true[des] )
                   fprintf(f, " %x", des);
                   fprintf(f, "\n");  break;
     case SUMMARY: fprintf(f, "\n\n          truth =");
                   FORALL(des) if ( true[des] )
                   fprintf(f, " %x", des);
  }
}



/******************************************************/

C_print(f,x)  FILE *f; int x;
{ int i,j;
  
  if ( !matno ) des_print(f,x);
  switch(x)
  {
     case UGLY:   FORALL(i) FORALL(j) fprintf(f, " %d", C[i][j]);
                  fprintf(f, "\n");  break;
     case PRETTY: fprintf(f, "\n Matrix # %-6d  -> |", matno+1);
                  FORALL(i) fprintf(f, " %x", i);
                  fprintf(f, "\n                  ---+");
                  FORALL(i) fprintf(f, "--");
                  FORALL(i)
                  { 
                     fprintf(f, "\n%20x |", i);
                     FORALL(j) fprintf(f, " %x", C[i][j]);
                  }
                  if ( theJob->failure )
                  {
                     fprintf(f, "  ");
                     for ( i = 0; i < 4; i++ ) if ( badvalue[i] < SZ )
                     fprintf(f, "  %c = %d", 'p'+i, badvalue[i]);
                  }
                  fprintf(f, "\n\n");
  }
}



/******************************************************
******************************************************/

stats_print()
{ int tim;

  CLoCK(&tim);
  printf("\n\n\n\n\n Matrices generated by MaGIC:  "); fflush(stdout);
  system("date");
  printf("\n\n Good ones found:   %d\n", good);
      printf(" Bad ones tested:   %d\n", tot-good);
      printf(" Isomorphs omitted: %d\n", isoms);
#ifdef HASTIMES
  end_timer = time_buffer.tms_utime;
      printf(" Time (cpu):        %1.2f seconds\n",
      (end_timer - begin_timer)/60.0);
#endif
      printf(" Time (wall clock): %1.2f seconds\n\n\n", 
                            (tim - start_time)/(TICK*1.0));
}
