Exemplo n.º 1
0
/**
 * Find in/out
 */
void find_inout()
{
   struct block *b = first_block;
   struct block *tmpb = (struct block *) malloc(sizeof(struct block));
   int i;
   int change = 1;
   int oldlen;
   
   calc_pred();
   DEBUG(print_pred());

   while(b) {
      b->defin.ini = 0;
      copy_set(b, OUT_SET, b, GEN_SET);
      b = b->next;
   }

   while(change) {
      change = 0;
      b = first_block;

      while(b) {
	 for(i = 0; i < b->predi; i++)
	    union_set(b, IN_SET, b->pred[i], OUT_SET);

	 copy_set(tmpb, IN_SET, b, IN_SET);
	 sub_set(tmpb, IN_SET, b, KILL_SET);
	 union_set(tmpb, IN_SET, b, GEN_SET);
	 
	 oldlen = b->defin.outi;
	 
	 // According to algorithm I should do what is commented belove
	 /*
	   copy_set(oldb, OUT_SET, b, OUT_SET);
	   copy_set(b, OUT_SET, tmpb, IN_SET);
	 
	   if(!equal_set(oldb, OUT_SET, b, OUT_SET))
	   change = 1;
	 */
	 // but since out[b] = gen[b] we can do
	 // 1. union agains copy
	 // 2. compare set's length to see if it's changed
	 
	 union_set(b, OUT_SET, tmpb, IN_SET);
	 if(oldlen != b->defin.outi) 
	    change = 1;
	 
	 b = b -> next;
      }
   }
}
void sub_set(char* charset, int position_count, int sequence[], int length)
{
  int lcv;//loop control variable to check every elements in the array
 
  if(position_count == length)//Base case: if the sequence array has been fully updated then print the array
  {
    for(lcv = 0; lcv < length; lcv++)//print out the elements in the character array which have been selected by the sequence integer array
    {
      if(sequence[lcv])//if the value at the same position in the integer array is one, then we can print this character
      {
        printf("%c ", charset[lcv]);
      }
    }
    printf("\n");
  }
  else//Inductive case: if not,we need to keep updating the sequency array to select which characters in the charset array to print out
  {
    sequence[position_count] = 0;//first case:set the value to zero, so that we don't print the character
    sub_set(charset, position_count + 1, sequence, length);//recursive call to deal with the rest characters in the charset array
    sequence[position_count] = 1;//second case:set the value to one, so that we can print the character
    sub_set(charset, position_count + 1, sequence, length);//recursively call to deal with the rest characters in the charset array
  }
}
void subset(char * charset, int length)
{
  int sequence[length];//the integer array keep tracking which element in the charset should be print out
  int lcv;//loop control variable to check all the elements in the integer array and character array 
  int position_count = 0;//the position count variable to represent which position in the character array needed to be printed out

  for(lcv = 0;lcv < length;lcv++)//initialize the integer array
  {
    sequence[lcv] = 0; 
  } 
  printf("subset of ");
  for(lcv = 0;lcv < length;lcv++)//first print out all the characters in the charset array
  {
    printf("%c ", charset[lcv]);
  }
  sub_set(charset, position_count, sequence, length);//call the recursive function
}