1 2 /* List object 3 * 4 * The list object is a generic container for storing collections 5 * of things in order. 6 */ 7 #ifndef HTLIST_H 8 #define HTLIST_H 9 10 #ifndef HTUTILS_H 11 #include <HTUtils.h> 12 #endif 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 typedef struct _HTList HTList; 18 19 struct _HTList { 20 void *object; 21 HTList *next; 22 }; 23 24 /* Fast macro to traverse a list. Call it first with copy of the list 25 * header. It returns the first object and increments the passed list 26 * pointer. Call it with the same variable until it returns NULL. 27 */ 28 #define HTList_nextObject(me) \ 29 ((me) && ((me) = (me)->next) ? (me)->object : NULL) 30 31 /* Macro to find object pointed to by the head (returns NULL 32 * if list is empty, OR if it doesn't exist - Yuk!) 33 */ 34 #define HTList_lastObject(me) \ 35 ((me) && (me)->next ? (me)->next->object : NULL) 36 37 /* Macro to check if a list is empty (or doesn't exist - Yuk!) 38 */ 39 #define HTList_isEmpty(me) ((me) ? ((me)->next == NULL) : YES) 40 41 /* Create list. 42 */ 43 extern HTList *HTList_new(void); 44 45 /* Delete list. 46 */ 47 extern void HTList_delete(HTList *me); 48 49 /* Reverse a list. 50 */ 51 extern HTList *HTList_reverse(HTList *start); 52 53 /* Append two lists, making second list empty. 54 */ 55 extern HTList *HTList_appendList(HTList *start, 56 HTList *tail); 57 58 /* Add object to START of list (so it is pointed to by the head). 59 */ 60 extern void HTList_addObject(HTList *me, 61 void *newObject); 62 63 /* Append object to END of list (furthest from the head). 64 */ 65 extern void HTList_appendObject(HTList *me, 66 void *newObject); 67 68 /* Insert an object into the list at a specified position. 69 * If position is 0, this places the object at the head of the list 70 * and is equivalent to HTList_addObject(). 71 */ 72 extern void HTList_insertObjectAt(HTList *me, 73 void *newObject, 74 int pos); 75 76 /* Remove specified object from list. 77 */ 78 extern BOOL HTList_removeObject(HTList *me, 79 void *oldObject); 80 81 /* Remove object at a given position in the list, where 0 is the 82 * object pointed to by the head (returns a pointer to the element 83 * (->object) for the object, and NULL if the list is empty, or 84 * if it doesn't exist - Yuk!). 85 */ 86 extern void *HTList_removeObjectAt(HTList *me, 87 int position); 88 89 /* Remove object from START of list (the Last one inserted 90 * via HTList_addObject(), and pointed to by the head). 91 */ 92 extern void *HTList_removeLastObject(HTList *me); 93 94 /* Remove object from END of list (the First one inserted 95 * via HTList_addObject(), and furthest from the head). 96 */ 97 extern void *HTList_removeFirstObject(HTList *me); 98 99 /* Determine total number of objects in the list, 100 * not counting the head. 101 */ 102 extern int HTList_count(HTList *me); 103 104 /* Determine position of an object in the list (a value of 0 105 * means it is pointed to by the head; returns -1 if not found). 106 */ 107 extern int HTList_indexOf(HTList *me, 108 void *object); 109 110 /* Return pointer to the object at a specified position in the list, 111 * where 0 is the object pointed to by the head (returns NULL if 112 * the list is empty, or if it doesn't exist - Yuk!). 113 */ 114 extern void *HTList_objectAt(HTList *me, 115 int position); 116 117 /* Link object to START of list (so it is pointed to by the head). 118 * 119 * Unlike HTList_addObject(), it does not malloc memory for HTList entry, 120 * it use already allocated memory which should not be free'd by any 121 * list operations (optimization). 122 */ 123 extern void HTList_linkObject(HTList *me, 124 void *newObject, 125 HTList *newNode); 126 127 /* Unlink object from START of list (the Last one inserted 128 * via HTList_linkObject(), and pointed to by the head). 129 * It does not free memory. 130 */ 131 extern void *HTList_unlinkLastObject(HTList *me); 132 133 /* Unlink specified object from list. 134 * It does not free memory. 135 */ 136 extern BOOL HTList_unlinkObject(HTList *me, 137 void *oldObject); 138 139 #ifdef __cplusplus 140 } 141 #endif 142 #endif /* HTLIST_H */ 143