Azinix

list.h

Go to the documentation of this file.
00001 /**
00002  \file list.h
00003  
00004  \brief Double linked list code
00005  */
00006 
00007 #ifndef LS_DEFINED
00008 #define LS_DEFINED
00009 
00010 #ifdef __cplusplus
00011 extern "C" {
00012 #endif
00013 
00014 
00015 /* This can be typedef'ed to void if supported */
00016 /* typedef struct ls_dummy_defn {
00017     int dummy;          // Not used
00018 } ls_dummy;
00019 */
00020 
00021 typedef void ls_dummy;
00022 
00023 typedef ls_dummy *lsList;   /* List handle           */
00024 typedef ls_dummy *lsGen;    /* List generator handle */
00025 typedef ls_dummy *lsHandle; /* Handle to an item     */
00026 typedef int lsStatus;       /* Return codes          */
00027 typedef char *lsGeneric;    /* Generic pointer       */
00028 
00029 #define LS_NIL      0   /* Nil for lsList       */
00030 
00031 #define LS_BADSTATE -3  /* Bad generator state   */
00032 #define LS_BADPARAM -2  /* Bad parameter value   */
00033 #define LS_NOMORE   -1  /* No more items         */
00034 
00035 #define LS_OK       0
00036 
00037 #define LS_BEFORE   1   /* Set spot before object */
00038 #define LS_AFTER    2   /* Set spot after object  */
00039 #define LS_STOP     3   /* Stop generating items  */
00040 #define LS_DELETE   4   /* Delete generated item  */
00041 
00042 /*
00043  * For all those routines that take a handle,  this macro can be
00044  * used when no handle is required.
00045  */
00046 
00047 #define LS_NH       (lsHandle *) 0
00048 
00049 typedef lsGeneric (*LS_PFLSG) ();
00050 
00051 extern lsList lsCreate (void);
00052   /* Create a new list */
00053 extern lsStatus lsDestroy (lsList, void (*)());
00054   /* Delete a previously created list */
00055 extern lsList lsCopy (lsList, LS_PFLSG);
00056    /* Copies the contents of a list    */
00057 
00058 extern lsStatus lsFirstItem (lsList, lsGeneric *, lsHandle *);
00059   /* Gets the first item of a list */
00060 extern lsStatus lsLastItem (lsList, lsGeneric *, lsHandle *);
00061   /* Gets the last item of a list */
00062 
00063 extern lsStatus lsNewBegin (lsList, lsGeneric, lsHandle *);
00064   /* Add item to start of list */
00065 extern lsStatus lsNewEnd (lsList, lsGeneric, lsHandle *);
00066   /* Add item to end of list */
00067 
00068 extern lsStatus lsDelBegin (lsList, lsGeneric *);
00069   /* Delete first item of a list */
00070 extern lsStatus lsDelEnd (lsList, lsGeneric *);
00071   /* Delete last item of a list */
00072 
00073 extern int lsLength (lsList);
00074   /* Returns the length of the list */
00075 
00076 extern lsGen lsStart (lsList);
00077   /* Begin generation of items in a list */
00078 extern lsGen lsEnd (lsList);
00079   /* Begin generation at end of list */
00080 extern lsGen lsGenHandle (lsHandle, lsGeneric *, int);
00081   /* Produces a generator given a handle */
00082 extern lsStatus lsNext (lsGen, lsGeneric *, lsHandle *);
00083   /* Generate next item in sequence */
00084 extern lsStatus lsPrev (lsGen, lsGeneric *, lsHandle *);
00085   /* Generate previous item in sequence */
00086 extern lsStatus lsInBefore (lsGen, lsGeneric, lsHandle *);
00087   /* Insert an item before the most recently generated by lsNext */
00088 extern lsStatus lsInAfter (lsGen, lsGeneric, lsHandle *);
00089   /* Insert an item after the most recently generated by lsNext  */
00090 extern lsStatus lsDelBefore (lsGen, lsGeneric *);
00091   /* Delete the item before the current spot */
00092 extern lsStatus lsDelAfter (lsGen, lsGeneric *);
00093   /* Delete the item after the current spot */
00094 extern lsStatus lsFinish (lsGen);
00095   /* End generation of items in a list */
00096 
00097 extern lsList lsQueryHandle (lsHandle);
00098   /* Returns the list of a handle */
00099 extern lsGeneric lsFetchHandle (lsHandle);
00100   /* Returns data associated with handle */
00101 extern lsStatus lsRemoveItem (lsHandle, lsGeneric *);
00102   /* Removes item associated with handle from list */
00103 
00104 extern lsStatus lsSort (lsList, int (*)());
00105 
00106   /* Sorts a list */
00107 extern lsStatus lsUniq (lsList, int (*)(), void (*)());
00108   /* Removes duplicates from a sorted list */
00109 /*
00110  * Macro to iterate the items of a list.Note the following:
00111  * 1) in a for loop, the test is evaluate before the first time through the body
00112  * 2) the logical OR operator guarantees left to right evaluation, and the second
00113  *    operand is not evaluated if first operand evaluates to non-zero
00114  * 3) the comma operator returns the value of its second argument.
00115  */
00116 #define lsForEachItem(                                         \
00117   list,  /* lsList, list to iterate */                         \
00118   gen,   /* lsGen, local variable for iterator */              \
00119   data   /* lsGeneric, variable to return data */              \
00120 )                                              \
00121   for(gen = lsStart(list);                     \
00122       (lsNext(gen, (lsGeneric *) &data, LS_NH) == LS_OK)       \
00123       || ((void) lsFinish(gen), 0);                            \
00124       )
00125 
00126 #ifdef __cplusplus
00127 }
00128 #endif
00129 
00130 #endif