/*---------------------------------------------------------------------------*/
/* List.c : perso list for AI.*/
/* author : Olivier Couvreur  28/11/1997*/
/*---------------------------------------------------------------------------*/
 
/* included in DsgMem.c so excluded from build in VC project*/
 
 
#include "AIUseCPA.h"
 
#include "AI_Mmg.h"
#include "specif/AIOption.h"
#include "AIMacros.h"
#include "AI_Erm.h"
#include "Convert.h"
#include "ai_struc.h"
#include "List.h"
 
/*XB*/
#include "Intell.h"
/*End XB*/
#include "gsparam.h" 
/***********************************************************************************************************************/
/* LIST                                                                                                                */
/***********************************************************************************************************************/
/* Body of functions for List of Perso*/
/*/////////////////////////////////////*/
void fn_vPutInList (tdstList *p_List, HIE_tdxHandleToSuperObject p_Perso)
/*   -------------*/
/* add perso at the end.*/
{
    if (p_List->ucNbElt < p_List->ucMaxSize){
        p_List->d_TabElt[p_List->ucNbElt++]=p_Perso;
    }
    else {
        M_AIFatalError(E_uwAIFatalPersoListFull);
    }
}
 
void fn_vInsertPersoAtPositionInList (tdstList *p_List, HIE_tdxHandleToSuperObject p_Perso, unsigned char ucPosition)
/*   -------------------------------*/
/*ucPosition belows [0,p_List->ucNbElt+1] & ucPosition < C_ucSizeOfMaximalList*/
{
    unsigned char ucI;
    
    ucPosition = (unsigned char) M_Min (p_List->ucNbElt+1, ucPosition);
    
    if (p_List->ucNbElt < p_List->ucMaxSize){		
        /* shift back the all element from ucPosition to the of the list*/
        for (ucI=p_List->ucNbElt; ucI>ucPosition; ucI--){
            p_List->d_TabElt [ucI]= p_List->d_TabElt[ucI-1];
        }
        p_List->d_TabElt[ucPosition]=p_Perso;
        p_List->ucNbElt++;
    }
    else {
        M_AIFatalError(E_uwAIFatalPersoListFull);
    }
}
 
Bool fn_bFindPersoInList(tdstList *p_List, HIE_tdxHandleToSuperObject p_Perso)
/*   -------------------*/
/* return TRUE if the perso was found*/
/* return FALSE if the perso was not found*/
{
    unsigned char ucPosition;
    
    /* No element in list */
    if (p_List->ucNbElt==0)
        return 0;
    
    for (ucPosition=0; (ucPosition < p_List->ucNbElt) && ( p_List->d_TabElt[ucPosition] != p_Perso); ucPosition++) {}; /*{} for lint */
    /*if (ucPosition == p_List->ucNbElt)*/
      /*return 0;*/
    /*return ((Bool) (p_List->d_TabElt[ucPosition] == p_Perso) );*/
    return ((Bool) (ucPosition < p_List->ucNbElt) );
}
 
/*
----------------------------------------------------------------------------------------
  Description   : fn_bFindModelInList
                  
  Returns (Bool ) TRUE if one perso of the model has been found in the list, else FALSE
----------------------------------------------------------------------------------------
  Author        : Fred 'Bart' Compagnon
  Date          : 07/05/98
----------------------------------------------------------------------------------------
*/
Bool fn_bFindModelInList(tdstList *p_List, AI_tdstAIModel *_p_stModel)
{
    unsigned char ucPosition;
    
    /* No element in list */
    if (p_List->ucNbElt==0)
        return 0;
    
    for (
      ucPosition=0;
      (ucPosition < p_List->ucNbElt) 
        && ( AI_M_p_stGetAIModel(M_pstGetMindOfBrain(M_GetMSHandle(p_List->d_TabElt[ucPosition],Brain))) != _p_stModel );
      ucPosition++) 
      {}; /*{} for lint */
    /*if (ucPosition == p_List->ucNbElt)*/
      /*return 0;*/
    /*return ((Bool) (AI_M_p_stGetAIModel(M_pstGetMindOfBrain(M_GetMSHandle(p_List->d_TabElt[ucPosition],Brain))) == _p_stModel) );*/
    return ((Bool) (ucPosition < p_List->ucNbElt) );
}
 
/*
----------------------------------------------------------------------------------------
  Description   : fn_bFindFamilyInList
                  
  Returns (Bool ) TRUE if one perso of the family has been found in the list, else FALSE
----------------------------------------------------------------------------------------
  Author        : Fred 'Bart#02' Compagnon
  Date          : 07/05/98
----------------------------------------------------------------------------------------
*/
Bool fn_bFindFamilyInList(tdstList *p_List, tdxHandleToFamilyList hFamily)
{
    unsigned char ucPosition;
    
    /* No element in list */
    if (p_List->ucNbElt==0)
        return 0;
    
    for (
      ucPosition=0;
      (ucPosition < p_List->ucNbElt) 
        && ( fn_h3dDataGetFamily( M_GetMSHandle( p_List->d_TabElt[ucPosition], 3dData) ) != hFamily );
      ucPosition++)
      {}; /*{} for lint */
    /*if (ucPosition == p_List->ucNbElt)*/
      /*return 0;*/
    /*return ((Bool) ( fn_h3dDataGetFamily( M_GetMSHandle( p_List->d_TabElt[ucPosition], 3dData) ) == hFamily ) );*/
    return ((Bool) (ucPosition < p_List->ucNbElt) );
}
 
 
 
Bool fn_bFindPersoAndDeleteInList (tdstList *p_List, HIE_tdxHandleToSuperObject p_Perso)
/*   ----------------------------*/
/* return TRUE if the perso was found and delete*/
/* return FALSE if the perso was not found*/
{
    unsigned char ucI;
    unsigned char ucPosition;
    
    for (ucPosition=0; (ucPosition < p_List->ucNbElt) && (p_List->d_TabElt[ucPosition] != p_Perso); ucPosition++) {}; /*{} for lint*/
    
    if (p_List->d_TabElt[ucPosition] == p_Perso){
        /* shift back the all element from ucPosition to the of the list*/
        for (ucI=ucPosition; ucI<=p_List->ucNbElt-2; ucI++){
            p_List->d_TabElt[ucI]= p_List->d_TabElt[ucI+1];
        }
        p_List->ucNbElt--;
        return 1; /*TRUE*/
    }
    else return 0; /*FALSE*/
}
 
void fn_vDeletePersoAtPositionInList (tdstList *p_List, unsigned char ucPosition)
/*ucPosition belows [0,p_List->ucNbElt-1] & ucPosition < C_ucSizeOfMaximalList
if ucPosition = C_ucEndOfList d*/
{
    unsigned char ucI;
    
    if (ucPosition == C_ucEndOfList) ucPosition = (unsigned char) (p_List->ucNbElt-1);
    
    if (ucPosition <= p_List->ucNbElt-1){
        /* shift back the all element from ucPosition to the of the list*/
        for (ucI=ucPosition; ucI<=p_List->ucNbElt-2; ucI++){
            p_List->d_TabElt[ucI]= p_List->d_TabElt[ucI+1];
        }
        p_List->ucNbElt--;
    }
    else{		
#ifdef __FATAL_ERR_AI__   
        Erm_M_UpdateLastError(AI, C_ucErmDefaultChannel, E_uwAIFatalPbDeleteInPersoList, C_lErmNoDebugData, C_ucErmOpenInfoWindow, C_ucAllowStopForDebug, NULL);
#endif /*__FATAL_ERR_AI__*/
    }
}
 
void fn_vSortListByFamily(tdstList *p_List, tdxHandleToFamilyList hFamily)
{
	unsigned char ucI, ucJ = 0;
	HIE_tdxHandleToSuperObject a_hSupObjElt[C_ucSizeOfMaximalList],hSupObjElt;
	tdxHandleToFamilyList hFamilyElt;
    
	/* Find SupObj of the good family and put it in a_hSupObjElt.*/
    for (ucI = 0; ucI < M_ucNbEltInList(p_List); ucI++)
	{
		hSupObjElt = p_List->d_TabElt[ucI];
		hFamilyElt = fn_h3dDataGetFamily(M_GetMSHandle(hSupObjElt,3dData));
		if (hFamilyElt == hFamily)
			a_hSupObjElt[ucJ++] = hSupObjElt;
    }
	/* Keep in the List only SupObj in a_hSupObjElt.*/
	M_ClearList(p_List);
    for (ucI = 0; ucI < ucJ; ucI++)
	{
		fn_vPutInList(p_List, a_hSupObjElt[ucI]);
	}
}
 
void fn_vSortListByModel(tdstList *p_stList, AI_tdstAIModel *_p_stModel)
{
	HIE_tdxHandleToSuperObject	hPerso;
	unsigned char				ucPos = 0;
	HIE_tdxHandleToSuperObject a_hSupObjElt[C_ucSizeOfMaximalList];
	unsigned char ucI, ucJ = 0;
 
	hPerso = fn_p_stGetPersoInList(p_stList, ucPos);
	while(hPerso)
	{
		if
		(
			(M_GetMSHandle(hPerso,Brain))
			&&	(AI_M_p_stGetAIModel(M_pstGetMindOfBrain(M_GetMSHandle(hPerso,Brain))) == _p_stModel)
		)
			a_hSupObjElt[ucJ++] = hPerso;
 
		ucPos++;
		hPerso = fn_p_stGetPersoInList(p_stList, ucPos);
	}
	/* Keep in the List only SupObj in a_hSupObjElt.*/
	M_ClearList(p_stList);
    for (ucI = 0; ucI < ucJ; ucI++)
	{
		fn_vPutInList(p_stList, a_hSupObjElt[ucI]);
	}
}
 
 
void fn_vUnionList (tdstList *p_ListA, tdstList *p_ListB, tdstList *p_ListC)
/*   -------------*/
/* A <- B U C*/
{
    unsigned char ucI, ucJ;
    HIE_tdxHandleToSuperObject p_Perso;
    
    /* A <- B*/
    for (ucI = 0; ucI < p_ListB->ucNbElt; ucI++)
    {
        fn_vPutInList(p_ListA, p_ListB->d_TabElt[ucI]);
    }
    /*A <- A + (C-B)*/
    for (ucI = 0; ucI < p_ListC->ucNbElt; ucI++){
        p_Perso = p_ListC->d_TabElt[ucI];
        /* test if p_Perso is in ListB*/
        for (	ucJ=0;
        (ucJ< p_ListB->ucNbElt) && (p_ListB->d_TabElt[ucJ] != p_Perso);
        ucJ++) {}; /*{} for lint*/
        if (p_ListB->d_TabElt[ucJ] != p_Perso) 
        {
            fn_vPutInList(p_ListA,p_Perso);
        }
    }
}
 
void fn_vInterList (tdstList *p_ListA, tdstList *p_ListB, tdstList *p_ListC)
/*   -------------*/
/* A <- B inter C*/
{
    unsigned char ucB, ucC;
    HIE_tdxHandleToSuperObject p_Perso;
    
    for (ucB=0; ucB<p_ListB->ucNbElt; ucB++)
    {
        p_Perso = p_ListB->d_TabElt[ucB];
        
        for (ucC=0; ucC<p_ListC->ucNbElt; ucC++)
        {
            if (p_ListC->d_TabElt[ucC] == p_Perso)
            {
                fn_vPutInList(p_ListA,p_Perso);
                break;
            }
        }
    }
}
 
void fn_vDiffList (tdstList *p_ListA, tdstList *p_ListB, tdstList *p_ListC)
/*   ------------*/
/* A <- B - C*/
{
    unsigned char ucB, ucC;
    HIE_tdxHandleToSuperObject p_Perso;
    
    for (ucB=0; ucB<p_ListB->ucNbElt; ucB++)
    {
        p_Perso = p_ListB->d_TabElt[ucB];
        for (	ucC=0;
        (ucC< p_ListC->ucNbElt) && (p_ListC->d_TabElt[ucC] != p_Perso);
        ucC++) {}; /*{} for lint*/
        if (p_ListC->d_TabElt[ucC] != p_Perso)
        {
            fn_vPutInList(p_ListA,p_Perso);
        }
    }
}
 
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Description: fn_vAddList*/
/*		Add the content of the a listB in a ListA, without double*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Methods: A <- A + (B-A)	*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Input: 	HIE_tdxHandleToSuperObject p_SuperObjPerso*/
/*			tdstNodeInterpret *p_stTree*/
/*			tdstGetSetParam *p_stValue*/
/* Output:	tdstNodeInterpret * : tree*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Creation date: Jan.14, 1997		Author: Fabien MORALES*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Modifications:*/
/* Modification date:			Modification Author: Fabien MORALES*/
/*/////////////////////////////////////////////////////////////////////////////////*/
void fn_vAddList (tdstList *p_ListA, tdstList *p_ListB)
{
    unsigned char ucA, ucB, ucSizeA;
    HIE_tdxHandleToSuperObject hPerso;
    
    ucSizeA = p_ListA->ucNbElt;
    for (ucB = 0; ucB < p_ListB->ucNbElt; ucB++) 
    {
        hPerso = p_ListB->d_TabElt[ucB];
        /* test if hPerso is already in List A*/
        /* (ucSizeA) we consider that there is no double in listB */
        for (ucA = 0; (ucA<ucSizeA) && p_ListA->d_TabElt[ucA] != hPerso; ucA++) {}; /*{} for lint*/
        
        /* add it in List A*/
        if (p_ListA->d_TabElt[ucA] != hPerso)
        {
            fn_vPutInList(p_ListA,hPerso);	
        }
    }
}
 
HIE_tdxHandleToSuperObject g_a_hPersoInZone[C_ucSizeOfMaximalList];
void fn_vAffectZddInList(tdstList *p_List, HIE_tdxHandleToSuperObject p_stPerso, 
						 unsigned char ucModulOrCharact, ACP_tdxIndex xZoneId)
{	
    unsigned short uwI, uwNbDetectedPerso;    
    M_ClearList(p_List);
    
    uwNbDetectedPerso = M_ucGetMaxSizeOfList(p_List);
    fn_vGetAllObjectsInZdd(p_stPerso, ucModulOrCharact, (unsigned char)xZoneId, g_a_hPersoInZone, &uwNbDetectedPerso);
    
    for (uwI=0; uwI < uwNbDetectedPerso; uwI++)
        fn_vPutInList(p_List, g_a_hPersoInZone[uwI]);
}
 
void fn_vAffectZdeInList(tdstList *p_List, HIE_tdxHandleToSuperObject p_stPerso, 
						 unsigned char ucModulOrCharact, ACP_tdxIndex xZoneId, GMT_tdxMask xMask)
{	
    unsigned short uwI, uwNbDetectedPerso;    
    M_ClearList(p_List);
    
    uwNbDetectedPerso = M_ucGetMaxSizeOfList(p_List);
    fn_vGetAllCharactersInContactWithThisZde(p_stPerso, ucModulOrCharact, (unsigned char)xZoneId, g_a_hPersoInZone, &uwNbDetectedPerso, (unsigned short) xMask);
    
    for (uwI=0; uwI < uwNbDetectedPerso; uwI++)
        fn_vPutInList(p_List, g_a_hPersoInZone[uwI]);
}
 
#if !defined(_AI_EXCLUDE_NEVER_USED_) /* MT {*/
/* used only by 1 ia function that is not used...*/
void fn_vAffectTypeZdeWithTypeZdeInList(tdstList *p_List, HIE_tdxHandleToSuperObject p_stPerso, GMT_tdxMask xPersoMask, GMT_tdxMask xOthersMask)
{	
    unsigned short uwI, uwNbDetectedPerso;    
    M_ClearList(p_List);
    
    uwNbDetectedPerso = M_ucGetMaxSizeOfList(p_List);
    fn_vThisTypeVsThisType(p_stPerso,xPersoMask,xOthersMask,g_a_hPersoInZone,&uwNbDetectedPerso);
 
    for (uwI=0; uwI < uwNbDetectedPerso; uwI++)
        fn_vPutInList(p_List, g_a_hPersoInZone[uwI]);
}
#endif
 
unsigned char fn_ucAtLeastOnePersoInZdd(HIE_tdxHandleToSuperObject p_stPerso, unsigned char ucModulOrCharact, ACP_tdxIndex xZoneId)
{
	unsigned short uwNbDetectedPerso = 1;
    fn_vGetAllObjectsInZdd(p_stPerso, ucModulOrCharact, (unsigned char)xZoneId, g_a_hPersoInZone, &uwNbDetectedPerso);
	return((unsigned char) (uwNbDetectedPerso!=0));
}
 
unsigned char fn_ucAtLeastOnePersoInZde(HIE_tdxHandleToSuperObject p_stPerso, unsigned char ucModulOrCharact, ACP_tdxIndex xZoneId, GMT_tdxMask xMask)
{
	unsigned short uwNbDetectedPerso = 1;
    fn_vGetAllCharactersInContactWithThisZde(p_stPerso, ucModulOrCharact, (unsigned char)xZoneId, g_a_hPersoInZone, &uwNbDetectedPerso, (unsigned short) xMask);
	return((unsigned char) (uwNbDetectedPerso!=0));
}
 
 
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Description: fn_p_stGetPersoInList*/
/*		get the Perso pointer at the given position in the list*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Methods:	*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Input: 	tdstList *p_List*/
/*			unsigned char ucPosition*/
/* Output:	HIE_tdxHandleToSuperObject : return handle could be NULL */
/*			if bad position is specified*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Creation date: Jan 1997		Author: Fabien MORALES*/
/*/////////////////////////////////////////////////////////////////////////////////*/
/* Modifications:*/
/* Modification date:			Modification Author: Fabien MORALES*/
/*/////////////////////////////////////////////////////////////////////////////////*/
HIE_tdxHandleToSuperObject fn_p_stGetPersoInList(tdstList *p_List, unsigned char ucPosition)
/*   -------------------*/
{
    /* empty list ?*/
    if (p_List->ucNbElt==0)
        return NULL;
    
    /* Bad position in list ? */
    if ((ucPosition>=p_List->ucNbElt) && (ucPosition!=C_ucEndOfList))
        return NULL;
    
    /* last elem ?*/
    if (ucPosition==C_ucEndOfList)
        return p_List->d_TabElt[p_List->ucNbElt-1];
    else
        return p_List->d_TabElt[ucPosition];
}
 
 
/* function to implement the ListSelect operator (with b_cInverse=0) and */
/*		the ListUnselect operator (with b_cInverse=1).*/
struct tdstNodeInterpret_ * fn_p_stListSelect(tdstList * p_List, char b_cInverse, struct tdstNodeInterpret_ *p_stTree)
{
    HIE_tdxHandleToSuperObject *p_p_stPerso;
    tdstList stWorkList;
    char b_cCondResult;
    struct tdstNodeInterpret_ * p_stTreeSv = p_stTree;	
    tdstGetSetParam stParam;
    
    M_InitScanList(p_List, p_p_stPerso);
    M_ClearList (&stWorkList);
    while (M_EndingCondScanList(p_List, p_p_stPerso)){
        /* eval the condition*/
		M_EvalOneParameter(*p_p_stPerso,p_stTree,p_stTreeSv,&stParam);
        b_cCondResult = (char) M_GetSetParam_lValue(&stParam);
        
        if (b_cCondResult ^ b_cInverse)
            fn_vPutInList (&stWorkList, *p_p_stPerso);
        
        M_GoNextScanList(p_List, p_p_stPerso);
    }
    /*copy the worklist into the p_List*/
    memcpy (p_List, &stWorkList, sizeof (tdstList1) + (stWorkList.ucNbElt -1) * C_SizeOfPersoInList);
    return (p_stTree);
}
 
/*
struct tdstNodeInterpret_ * fn_p_stListSort (tdstList * p_List, unsigned char ucWantedNbElt, char b_cIncrease, struct tdstNodeInterpret_ *p_stTree)
{
    HIE_tdxHandleToSuperObject *p_p_stPerso;
    HIE_tdxHandleToSuperObject p_stPerso;
    unsigned char ucI, ucJ;
    long a_lCriteraResult[C_ucSizeOfMaximalList];
    long *p_lCriteraResult;
    long lValue;
    unsigned char ucId = 0;
    struct tdstNodeInterpret_ * p_stTreeSv = p_stTree;
    tdstGetSetParam stParam;
    
    
    M_InitScanList(p_List, p_p_stPerso);
    p_lCriteraResult = a_lCriteraResult;
    
    // eval every critera
    while (M_EndingCondScanList(p_List, p_p_stPerso)){
		M_EvalOneParameter(*p_p_stPerso,p_stTree,p_stTreeSv,&stParam);
        *(p_lCriteraResult++) = M_GetSetParam_lValue(&stParam);
        
        M_GoNextScanList(p_List, p_p_stPerso);
    }
    
    if (b_cIncrease == 0){// sort for decrease case
        lValue = 0;
        for (ucI=0; (ucI < p_List->ucNbElt-1) && (ucI < ucWantedNbElt); ucI++){
            for (ucJ = ucI; ucJ<p_List->ucNbElt; ucJ++){
                if (a_lCriteraResult[ucJ]>lValue){
                    lValue = a_lCriteraResult[ucJ];
                    ucId = ucJ;
                }
            }
            // permut in order to move up the max 
            a_lCriteraResult[ucId] = a_lCriteraResult[ucI];
            //a_lCriteraResult[ucI] = lValue; // just for debug !!
            p_stPerso = p_List->d_TabElt[ucId];
            p_List->d_TabElt[ucId] = p_List->d_TabElt[ucI];
            p_List->d_TabElt[ucI] = p_stPerso;
        }
    }
    
    else{// sort for increase case
        lValue = (long) 0xFFFFFFFF;
        for (ucI=0; (ucI < p_List->ucNbElt-1) && (ucI < ucWantedNbElt); ucI++){
            for (ucJ = ucI; ucJ<p_List->ucNbElt; ucJ++){
                if (a_lCriteraResult[ucJ]<lValue){
                    lValue = a_lCriteraResult[ucJ];
                    ucId = ucJ;
                }
            }
            // permut in order to move up the min
            a_lCriteraResult[ucId] = a_lCriteraResult[ucI];
            p_stPerso = p_List->d_TabElt[ucId];
            p_List->d_TabElt[ucId] = p_List->d_TabElt[ucI];
            p_List->d_TabElt[ucI] = p_stPerso;
        }
    }
    
    p_List->ucNbElt = (unsigned char) M_Min(p_List->ucNbElt, ucWantedNbElt);
    return (p_stTree);
}
*/
 
struct tdstNodeInterpret_ * fn_p_stListSort (tdstList * p_List, unsigned char ucWantedNbElt, char b_cIncrease, struct tdstNodeInterpret_ *p_stTree)
{
    HIE_tdxHandleToSuperObject *p_p_stPerso;
    HIE_tdxHandleToSuperObject p_stPerso;
    unsigned char ucI, ucJ;
    MTH_tdxReal a_xCriteraResult[C_ucSizeOfMaximalList];
    MTH_tdxReal *p_xCriteraResult;
    MTH_tdxReal xValue;
    unsigned char ucId = 0;
    struct tdstNodeInterpret_ * p_stTreeSv = p_stTree;
    tdstGetSetParam stParam;
    
    
    M_InitScanList(p_List, p_p_stPerso);
    p_xCriteraResult = a_xCriteraResult;
    
    /* eval every critera*/
    while (M_EndingCondScanList(p_List, p_p_stPerso)){
		M_EvalOneParameter(*p_p_stPerso,p_stTree,p_stTreeSv,&stParam);
		
        *(p_xCriteraResult++) = M_ReturnParam_xValue(&stParam);
        
        M_GoNextScanList(p_List, p_p_stPerso);
    }
    
    if (b_cIncrease == 0)
	{/* sort for decrease case*/
		xValue=MTH_C_InfinitMinus;
        for (ucI=0; (ucI < p_List->ucNbElt-1) && (ucI < ucWantedNbElt); ucI++)
		{
            for (ucJ = ucI; ucJ<p_List->ucNbElt; ucJ++)
			{
                if (a_xCriteraResult[ucJ]>xValue)
				{
                    xValue = a_xCriteraResult[ucJ];
                    ucId = ucJ;
                }
            }
            /* permut in order to move up the max */
            a_xCriteraResult[ucId] = a_xCriteraResult[ucI];
            /*a_lCriteraResult[ucI] = lValue; // just for debug !!*/
            p_stPerso = p_List->d_TabElt[ucId];
            p_List->d_TabElt[ucId] = p_List->d_TabElt[ucI];
            p_List->d_TabElt[ucI] = p_stPerso;
        }
    }
    
    else
	{/* sort for increase case*/
        /*lValue = (long) 0xFFFFFFFF;*/
		xValue= MTH_C_InfinitPlus;
        for (ucI=0; (ucI < p_List->ucNbElt-1) && (ucI < ucWantedNbElt); ucI++)
		{
            for (ucJ = ucI; ucJ<p_List->ucNbElt; ucJ++)
			{
				if (a_xCriteraResult[ucJ]<xValue)
				{
                    xValue = a_xCriteraResult[ucJ];
                    ucId = ucJ;
                }
            }
            /* permut in order to move up the min*/
			a_xCriteraResult[ucId] = a_xCriteraResult[ucI];
            p_stPerso = p_List->d_TabElt[ucId];
            p_List->d_TabElt[ucId] = p_List->d_TabElt[ucI];
            p_List->d_TabElt[ucI] = p_stPerso;
        }
    }
    
    p_List->ucNbElt = (unsigned char) M_Min(p_List->ucNbElt, ucWantedNbElt);
    return (p_stTree);
}
