Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

epList.c

Go to the documentation of this file.
00001 
00029 /*
00030  *    Undetermined         FIFO                  LIFO                 
00031  *    +-+-+-+-+-+         +-+-+-+-+-+           +-+-+-+-+-+             
00032  *    | | | | | |     --> | | | | | | -->       | | | | | | <==>        
00033  *    +-+-+-+-+-+         +-+-+-+-+-+           +-+-+-+-+-+             
00034  *     4 3 2 1 0           4 3 2 1 0             4 3 2 1 0              
00035  */
00036 
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00041 #include "epDefs.h"
00042 #include "epUtils.h"
00043 #include "epList.h"
00044 
00045 
00062 epList_t *epList_create(const epDir_t direction, 
00063                         int (*elementDeallocatorFunction)(void *elt))
00064 {
00065   epList_t *list =(epList_t *) malloc(sizeof(epList_t));
00066   
00067   _DEBUG2("entering...");
00068   _DEBUG3("direction              : %s", _DIR_TO_STRING(direction));
00069   _DEBUG3("eltDeallocFunc assigned: %s", 
00070           _BOOL_TO_STRING(elementDeallocatorFunction!=NULL));
00071 
00072   _IF_TRUE_RETURN(!_DIR_IS_VALID(direction), 0, NULL); //direction invalide
00073   _IF_TRUE_RETURN(list == NULL, 0, NULL); //le malloc a échoué
00074   
00075 
00076   list->direction = direction;
00077   list->head = NULL;
00078   list->tail = NULL;
00079   list->numOfElements = 0;
00080   
00081   list->elementDeallocatorFunction = (elementDeallocatorFunction != NULL?
00082                                       elementDeallocatorFunction:
00083                                       epUtils_ptrDeallocatorFunction);
00084   _RETURN(list);
00085 }
00086 
00101 int epList_destroy(epList_t *list)
00102 {
00103   struct epListElt_s *listElt,
00104                      *nextListElt;
00105 
00106   _DEBUG2("entering...");
00107   _IF_TRUE_RETURN(list == NULL, 0, -1); // pas de liste passée en argument
00108 
00109   for(listElt = list->head; listElt != NULL; listElt = nextListElt)
00110     {
00111       nextListElt = listElt->next;
00112       /* si la déallocation d'1 élément échoue(i.e. ne retourne pas 0), 
00113          erreur */
00114       _IF_TRUE_RETURN(list->elementDeallocatorFunction(listElt->elt), 0, -1);
00115       free(listElt);
00116     }
00117   
00118   free(list);
00119   _RETURN(0);
00120 }
00121 
00144 int epList_put(epList_t *list, const int indice, const void *elt)
00145 {
00146   int i;
00147   struct epListElt_s *listElt,
00148                      *curListElt;
00149 
00150   _DEBUG2("entering...");
00151 
00152   /* gerer les messages d'erreur LOCAUX(et leur internationalisation) */
00153   _IF_TRUE_RETURN(list == NULL, 0, -1);
00154   _IF_TRUE_RETURN(elt == NULL, 0, -1);
00155   _IF_TRUE_RETURN(indice < 0, 0, -1);
00156   
00157   listElt =(struct epListElt_s *) malloc(sizeof(struct epListElt_s));
00158   _IF_TRUE_RETURN(listElt == NULL, 0, -1);
00159 
00160 
00161   /* insertion into an empty list                                      */
00162   if(list->numOfElements == 0)
00163     {
00164       listElt->prev = NULL;
00165       listElt->next = NULL;
00166       
00167       list->head = listElt;
00168       list->tail = listElt;
00169     }
00170   /* _LIFO direction OR indice = 0              => insert at beginning */
00171   else if(list->direction == _LIFO || 
00172           (list->direction != _FIFO && indice == 0))
00173     { 
00174       listElt->prev = NULL;
00175       listElt->next = list->head;
00176       
00177       list->head->prev = listElt;
00178       list->head = listElt;
00179     }
00180   /* _FIFO direction OR indice >= numOfElements => insert at end      */
00181   else if(list->direction == _FIFO || indice >= list->numOfElements)
00182     { 
00183       listElt->prev = list->tail;
00184       listElt->next = NULL;
00185       
00186       list->tail->next = listElt;
00187       list->tail = listElt;
00188     }
00189   /* middle-of-the-list element                                      */
00190   else
00191     {
00192       for(i = 1, curListElt = list->head; 
00193           i < indice; 
00194           i++, curListElt = curListElt->next);
00195       
00196       listElt->prev = curListElt;
00197       listElt->next = curListElt->next;
00198       
00199       curListElt->next->prev = listElt;
00200       curListElt->next = listElt;
00201     }
00202 
00203   listElt->elt =(void *)elt;
00204   list->numOfElements++;
00205 
00206   _RETURN(0);
00207 }
00208 
00230 void *epList_get(epList_t *list, const int indice, 
00231                  const boolean_t removeFromList)
00232 {
00233   int i;
00234   void *retelt;
00235   struct epListElt_s *listElt;
00236 
00237   _DEBUG2("entering...");
00238   _IF_TRUE_RETURN(list == NULL, 0, NULL); // no list
00239   _IF_TRUE_RETURN(list->numOfElements == 0, 0, NULL); //empty list
00240   // invalid indice
00241   _IF_TRUE_RETURN(indice >= list->numOfElements || indice < 0, 0, NULL); 
00242 
00243   if(list->direction == _FIFO || list->direction == _LIFO || indice == 0) 
00244     {
00245       listElt = list->head;
00246     } else {
00247       for(i = 0, listElt = list->head; 
00248           i < indice; 
00249           i++, listElt = listElt->next);
00250     }
00251   
00252   retelt = listElt->elt;
00253   
00254   if(removeFromList)
00255     { 
00256       _DEBUG3("removing the %dth element from the list", indice);
00257       if(listElt->prev == NULL)
00258         { list->head = listElt->next; }
00259       else
00260         { listElt->prev->next = listElt->next; }
00261       
00262       if(listElt->next == NULL)
00263         { list->tail = listElt->prev; }
00264       else
00265         { listElt->next->prev = listElt->prev; }
00266       
00267       free(listElt);
00268       list->numOfElements--;
00269     }
00270   
00271   _RETURN(retelt);      
00272 }
00273 
00287 void *epList_access(epList_t *list, const int indice)
00288 {
00289   _DEBUG2("entering...");
00290   /* ici, on n'utilise pas le warper _RETURN, car le code d'erreur est fixé
00291      par al fonction appelée epList_get()
00292   */
00293   return(epList_get(list, indice, _false));
00294 }
00295 
00330 void *__epList_search(const epList_t *list, const searchDir_t direction, 
00331                       int from, int to, const void *criterion, 
00332                       boolean_t (*comparisonFunction)
00333                       (const void *element, const void *criterion), 
00334                       int *indice)
00335 {
00336   int i;
00337   void *retelt = NULL;
00338   struct epListElt_s *listElt;
00339   
00340   _DEBUG2("entering...");
00341   _IF_TRUE_RETURN(list == NULL, 0, NULL);      // no list
00342   _IF_TRUE_RETURN(criterion == NULL, 0, NULL); //no search criterion
00343 
00344   if(indice)
00345     { *indice = -1; }
00346   if(from == -1)
00347     { from = (direction ==_NORMAL)?0:(list->numOfElements - 1); }
00348   if(to == -1)
00349     { to = (direction ==_NORMAL)?(list->numOfElements - 1):0; }
00350 
00351   // parcours avant
00352   if(direction == _NORMAL)
00353     {
00354       for(listElt = list->head, i = 0; i < to && i < from; 
00355           listElt = listElt->next, i++);
00356 
00357       for(; i <= to; listElt = listElt->next, i++)
00358         { 
00359           if(comparisonFunction(listElt->elt, criterion))
00360             { 
00361               retelt = listElt->elt;
00362               if(indice)
00363                 { *indice = i; }
00364 
00365               break;
00366             }
00367         }
00368   // parcours arrière
00369     } else if(direction == _REVERSE) {
00370       for(listElt = list->tail, i = list->numOfElements - 1;
00371           i > to && i > from; 
00372           listElt = listElt->prev, i--);
00373 
00374       for(; i >= to; listElt = listElt->prev, i--)
00375         { 
00376           if(comparisonFunction(listElt->elt, criterion))
00377             { 
00378               retelt = listElt->elt;
00379               if(indice)
00380                 { *indice = i; }
00381 
00382               break;
00383             }
00384         }
00385     }
00386 
00387   return(retelt);
00388 }
00389 
00406 void *epList_search(const epList_t *list, const void *criterion, 
00407                     boolean_t (*comparisonFunction)
00408                     (const void *element, const void *criterion))
00409 {
00410   _DEBUG2("entering...");
00411   return(__epList_search(list, _NORMAL, -1, -1, criterion, 
00412                          comparisonFunction, NULL));
00413 }
00414 
00415 
00432 void *epList_rsearch(const epList_t *list, const void *criterion, 
00433                      boolean_t (*comparisonFunction)
00434                      (const void *element, const void *criterion))
00435 {
00436   _DEBUG2("entering...");
00437   return(__epList_search(list, _REVERSE, -1, -1, criterion, 
00438                          comparisonFunction, NULL));
00439 }
00440 
00449 int epList_size(epList_t *list)
00450 {
00451   _DEBUG2("entering...");
00452   _IF_TRUE_RETURN(list == NULL, 0, -1);
00453 
00454   _RETURN(list->numOfElements);
00455 }
00456 
00472 boolean_t epList_isEmpty(epList_t *list)
00473 {
00474   _DEBUG2("entering...");
00475   _IF_TRUE_RETURN(list == NULL, 0, -1); //??!!?? _error
00476 
00477   _RETURN(list->numOfElements == 0? _true: _false);
00478 }
00479 
00495 int epList_setElementPrintFunction(
00496   epList_t *list,
00497   char *(*elementPrintFunction)(void *elt))
00498 {
00499   _DEBUG2("entering...");
00500   _IF_TRUE_RETURN(list == NULL, 0, -1); // NULL list
00501 
00502   list->elementPrintFunction = elementPrintFunction;
00503   _RETURN(0);
00504 }
00505 
00520 int epList_printIt(epList_t *list, char *title)
00521 {
00522   int i;
00523 
00524   _DEBUG2("entering...");
00525   _IF_TRUE_RETURN(list == NULL, 0, -1); // NULL list
00526 
00527   if(title == NULL)
00528     { fprintf(stdout, "- List ------------------------------\n"); }
00529   else
00530     {
00531       fprintf(stdout, "- List - %s ", title);
00532       for(i = 0; i < (27 - strlen(title)); fprintf(stdout, "-"), i++);
00533       fprintf(stdout, "\n");
00534     }
00535 
00536   fprintf(stdout, "  * Orientation       : %s\n", 
00537           _DIR_TO_STRING(list->direction));
00538   fprintf(stdout, "  * number of Elements: %2d\n", list->numOfElements);
00539 
00540   epList_printElements(list);
00541   fprintf(stdout, "- End List --------------------------\n");
00542   _RETURN(0);
00543 }
00544 
00558 void epList_printElements(epList_t *list)
00559 {
00560   int i;
00561   char *eltString,
00562        *token;
00563   struct epListElt_s *listElt;
00564 
00565   _DEBUG2("entering...");
00566   if(list->elementPrintFunction != NULL)
00567     {
00568       fprintf(stdout, "\n  - Elements --\n");
00569       
00570       for(listElt = list->head, i = 0; listElt != NULL; 
00571           listElt = listElt->next, i++)
00572       {
00573         fprintf(stdout, "  (%2d)\n", i);
00574 
00575         eltString = strdup(list->elementPrintFunction(listElt->elt));
00576         token = strtok(eltString, "\n");
00577         
00578         do {
00579           fprintf(stdout, "        %s\n", token);  
00580         } while((token = strtok(NULL, "\n")) != NULL);
00581 
00582         free(eltString);
00583       }
00584 
00585       fprintf(stdout, "  - End -------\n");
00586     }  
00587 }

Generated at Sun Nov 25 14:05:07 2001 for ExtendedPersonnalLibrary by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000