dbLkList.h

Go to the documentation of this file.
00001 #ifndef _LIST_H
00002 #define _LIST_H
00003 
00004 /** Implements a double linked list. I changed the name from list to DbLkList because of some naming conflicts with 
00005   * C++ apps, which defines the List name.
00006   */
00007 
00008 #ifdef __cplusplus
00009 extern "C" {
00010 #endif
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 
00015 /** 
00016   * this types indicates if we shall free contents
00017   */
00018 typedef enum { DBLIST_DM_KEEP=0, DBLIST_DM_FREE } DbLkDestroyMode;
00019 
00020 /**
00021   * the way we shall transverse the list 
00022   */
00023 typedef enum { DBLIST_TM_FROM_HEAD, DBLIST_TM_FROM_TAIL } DbLkTransMode;
00024 
00025 typedef struct _DbLkList *ListP;
00026 
00027 // function prototypes
00028 typedef void (ListVisitFunction)(ListP, void *object);
00029 typedef void (ListPrintFunction)(ListP, void *object);
00030 typedef void (ListDeleteFunction)(ListP, void *object);
00031 
00032 ///each node in the list contains this struct
00033 typedef struct _DbLkListNodeContent {
00034         void *content; ///the content
00035 } DbLkListNodeContent;
00036 
00037 ///a node in the list, has the content above and pointers to next and previous
00038 typedef struct _DbLkListNode {
00039         struct _DbLkListNode *previous, *next; ///pointers to the previous(ie before me) and next(after me) nodes
00040         DbLkListNodeContent nodeContent; ///the contents of this node
00041 } DbLkListNode;
00042 
00043 ///the list structure, holds pointer to tail and head, the number of elements, and callback functions
00044 typedef struct _DbLkList {
00045         int numElements; //< number of elements in the list
00046         DbLkListNode head, tail; //< pointers to the head and tail of the list, both are not true nodes
00047 
00048         //internal variables
00049         DbLkListNode *lastVisited;      //< last visited node(for iteration)
00050         
00051         //registered callback functions
00052         ListPrintFunction *printFunction; //< this will be called while printing the list
00053         ListDeleteFunction *deleteFunction; //< this will be called when deleting the list
00054         ListVisitFunction *visitFunction; //< this will get called by the listVisit function
00055 } DbLkList;
00056 
00057 /* -------------------functions -------------------------------------------*/
00058 /**
00059 All functios follow the same return patter: NULL or -1 if error found, or the object.
00060 Exception are for boolean returns, which 0 means false and 1 means true
00061 */
00062 
00063 /** Initializes the empty list.
00064  @return the empty list, just created
00065  */
00066 DbLkList *listCreate();
00067 
00068 /** Destroys the list. If theres a registered free function, it will be called for each node
00069  * @param list the address of the list pointer being destroyed. List pointer will be NULL after this
00070  * @param mode should the content be destroyed? If so, user function will get called, otherwise, a simple free is called on content
00071  */
00072 void listDestroy(DbLkList **list, DbLkDestroyMode mode);
00073 
00074 /** prepares the list for itaration. Should be called before getNext or getPrevious
00075   * @param list the list
00076   * @param from tail or head?
00077   * @return -1 if list is empty, 1 otherwise
00078   */
00079 int listPrepare(DbLkList *list, DbLkTransMode from);
00080 
00081 /** get next node 
00082  * @return -1 when pointer is at list last valid element, 1 otherwise
00083  */
00084 int listGetNext(DbLkList *list, void **object);
00085 
00086 /** get previous node
00087   * @return -1 when pointer is at list first valid element, 1 otherwise
00088   */
00089 int listGetPrevious(DbLkList *list, void **object);
00090 
00091 
00092 /** Inserts an object in the list, as the new head node. 
00093  * @param list the list being inserted
00094  * @param object the object being inserted
00095  * @return -1 on error, 1 otherwise
00096  */
00097 int listInsertHead(DbLkList *list, void *object);
00098 
00099 /** Inserts an object at the end of the list, as the new tail. 
00100  * @param list the list being inserted
00101  * @param object the object being inserted
00102  * @return -1 on error, 1 otherwise
00103  */
00104 int listInsertTail(DbLkList *list, void *object);
00105 
00106 /** Inserts the object at the given position. If position does not exist and is less than 0, it will be a head insertion.
00107  * Else is a tail insertion.
00108  * @param list the list being inserted
00109  * @param position where the object will be inserted
00110  * @param object the object being inserted
00111  * @return -1 on error, 1 otherwise
00112 */
00113 int listInsertAt(DbLkList *list, int position, void *object);
00114 
00115 /** Retrieves the object at the given position, but does not remove it from list.
00116  * @param list the list being searched
00117  * @param position the position we are getting. 0 is the list head(first element).
00118  * @param objectPointerAddress the address of an object pointer, so we can return a pointer to the object
00119  * @return 1 if successful, -1 otherwise(ie, position does not exist)
00120 */
00121 int listGetObjectAt(DbLkList *list, int position, void **objectPointerAddress);
00122 
00123 /** Retrieves the object at the given position, removing it from list.
00124  * @param list the list being searched
00125  * @param position the position we are getting. 0 is the list head(first element).
00126  * @param objectPointerAddress the address of an object pointer, so we can return a pointer to the object
00127  * @return 1 if successful, -1 otherwise(ie, position does not exist)
00128 */
00129 int listRemoveObjectAt(DbLkList *list, int position, void **objectPointerAddress);
00130 
00131 /** Removes the given object from the list 
00132  * @param list the list
00133  * @param object the object being removed
00134  * @return 1, -1 if object cant be found, 
00135  */
00136 int listRemoveObject(DbLkList *list, void *object);
00137 
00138 /** Concatenates 2 list. The source list is freed, the destination will hold all elements from the other.
00139  * Source list is concatenated at the end of the destination
00140  * @param destination the destination list, which will hold all info from src
00141  * @param sourceAddress the address of the src list pointer, which will be NULL after this
00142  * @return the number of elements of the destination list after concatenation, -1 if an error occurs
00143  */
00144 int listCat(DbLkList *destination, DbLkList **sourceAddress);
00145 
00146 /** Retrieves the first element in the list, ie, the head. Does not remove from the list
00147  * @param list the list being searched
00148  * @param objectAddress the address of an object pointer, so we can return a pointer to the object
00149  * @return 1 if successful, -1 otherwise(empty list)
00150 */
00151 int listGetHead(DbLkList *list, void **objectAddress);
00152 
00153 /** Retrieves the first element in the list, ie, the head. Does not remove from the list
00154  * @param list the list being searched
00155  * @param objectAddress the address of an object pointer, so we can return a pointer to the object
00156  * @return 1 if successful, -1 otherwise(empty list)
00157 */
00158 int listGetTail(DbLkList *list, void **objectAddress);
00159 
00160 /** Removes the list head, returning it at the object pointer argument
00161  * @return 1 if successful, -1 otherwise(list empty)
00162 */
00163 int listRemoveHead(DbLkList *list, void **object);
00164 
00165 /** Removes the list tail, returning it at the object pointer argument
00166  * @return 1 if successful, -1 otherwise(list empty)
00167 */
00168 int listRemoveTail(DbLkList *list, void **object);
00169 
00170 /** Registers a function to be called by the print function, in each list node.
00171  * If this function is not registered, calls to listPrint will return immediately.
00172  * @param list the list
00173  * @param printFunction a function, of prototype ListPrintFunction
00174  */
00175 void listRegisterPrintFunction(DbLkList *list, ListPrintFunction *printFunction);
00176 
00177 /** Registers a function to be called by the visit function, in each list node.
00178  * If this function is not registered, calls to listVisit will return immediately.
00179  * @param list the list
00180  * @param visitFunction a function, of prototype ListVisitFunction
00181  */
00182 void listRegisterPrintFunction(DbLkList *list, ListVisitFunction *visitFunction);
00183 
00184 /** Prints the list, from head to tail.
00185  * @param list the lsit being printed
00186  */
00187 void listPrint(DbLkList* list);
00188 
00189 /** Visits the list, from head to tail, calling the registered function. If none, return.
00190  * @param list the list being printed
00191  */
00192 void listVisit(DbLkList* list);
00193 
00194 /** Registers a function to be called by the destroy function, in each list node.
00195  * @param list the list
00196  * @param deleteFunction a function, of prototype ListPrintFunction
00197  */
00198 void listRegisterDeleteFunction(DbLkList *list, ListDeleteFunction *deleteFunction);
00199 
00200 
00201 #ifdef __cplusplus
00202 }
00203 #endif
00204 
00205 #endif
00206 
00207 

Generated on Tue Jan 17 19:18:38 2006 for Void by  doxygen 1.4.6