#include "header.i"


/********************************/
/* Makes a list of Item[Fp..Lp] */
/********************************/

ItemList MakeItemList(ItemNo Fp, ItemNo Lp)
/*       ------------   */
{
         ItemList IL;
	 
	 
         if( Fp > Lp ) return Nil;
	 
	 if( LIST_ILRecord == Nil )   
	 { 
            LIST_ILRecord = (ItemList) calloc(1, sizeof(ILRecord));
            LIST_ILRecord->next = Nil;
	 }

	 {
            IL = LIST_ILRecord;
	    LIST_ILRecord = LIST_ILRecord->next;
	    IL->item = Item[Fp];
	    IL->next = MakeItemList(Fp+1, Lp);
	    return IL;
	 }
}	 



/*************************************************************************************/
/* Sorts the ItemList List of length Len accordingly to the continuous attribute Att */
/* such that the ordering of elements with the same attribute value is unchanged.    */
/*************************************************************************************/

ItemList SortItemList(ItemList IL, int length, Attribute Att)
/*       ------------   */
{
         ItemList IL1, IL2, IH;
	 ItemNo i;
	 
	 
         if( length < 2 ) return IL;
	 
	 IL1 = IL;
	 IL2 = IL;
	 ForEach(i, 1, length/2-1)   IL2 = IL2->next;
	 IH = IL2;
	 IL2 = IL2->next;
	 IH->next = Nil;
	 
	 IL1 = SortItemList(IL1, length/2, Att);
	 IL2 = SortItemList(IL2, length - length/2, Att); 	 
	 
	 if( CVal(IL1->item, Att) <= CVal(IL2->item, Att) )
	 {
	    IL = IL1;
	    IL1 = IL1->next;
	 }
	 else
	 {
	    IL = IL2;
	    IL2 = IL2->next;
	 }
	 IH = IL;
	 
	 while( IL1 != Nil && IL2 != Nil )
	 {
	    if( CVal(IL1->item, Att) <= CVal(IL2->item, Att) )
	    {
	       IH->next = IL1;
	       IL1 = IL1->next;
	    }
	    else
	    {
	       IH->next = IL2;
	       IL2 = IL2->next;
	    }
	    IH = IH->next;
	 }
	 
	 if( IL1 == Nil )   IH->next = IL2;
	 else               IH->next = IL1;
	 
	 return IL;
}	  



/************************/
/* Releases an ItemList */
/************************/

void ReleaseItemList(ItemList IL)
/*   ---------------   */
{
     ItemList IP;
     
     
     if( IL == Nil ) return;
     
     IP = IL;
     while( IP->next != Nil )   IP = IP->next;

     IP->next = LIST_ILRecord;
     LIST_ILRecord = IL;
}









	    
	    
	    
	 	 
		 


















