Changeset 169
- Timestamp:
- Mar 9, 2004, 2:53:44 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/archive/pslib/src/Utils/dlist.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/pslib/src/Utils/dlist.c
r141 r169 7 7 #include "psUtils.h" 8 8 9 static psDlistElem *dlistElem New(void)9 static psDlistElem *dlistElemAlloc(void) 10 10 { 11 11 return(psAlloc(sizeof(psDlistElem))); 12 12 } 13 13 14 static void dlistElem Del(psDlistElem *elem)14 static void dlistElemFree(psDlistElem *elem) 15 15 { 16 16 psFree(elem); … … 19 19 /*****************************************************************************/ 20 20 21 psDlist *psDlist New(void *data)// initial data item; may be NULL21 psDlist *psDlistAlloc(void *data) // initial data item; may be NULL 22 22 { 23 23 psDlist *list = psAlloc(sizeof(psDlist)); … … 33 33 } 34 34 35 void psDlist Del(psDlist *list, // list to destroy36 void (*elemDel)(void *)) // destructor for data on list35 void psDlistFree(psDlist *list, // list to destroy 36 void (*elemFree)(void *)) // destructor for data on list 37 37 { 38 38 if (list == NULL) { … … 44 44 psDlistElem *next = ptr->next; 45 45 46 if (elem Del== NULL) {46 if (elemFree == NULL) { 47 47 psMemDecrRefCounter(ptr->data); 48 48 } else { 49 elem Del(psMemDecrRefCounter(ptr->data));50 } 51 dlistElem Del(ptr);49 elemFree(psMemDecrRefCounter(ptr->data)); 50 } 51 dlistElemFree(ptr); 52 52 53 53 ptr = next; … … 66 66 // PS_DLIST_TAIL, or an integer 67 67 { 68 psDlistElem *elem = dlistElem New();68 psDlistElem *elem = dlistElemAlloc(); 69 69 70 70 if (list == NULL) { 71 list = psDlist New(NULL);71 list = psDlistAlloc(NULL); 72 72 } 73 73 … … 75 75 fprintf(stderr, "Invalid index %d (only %d elements)\n", 76 76 where, list->n); 77 dlistElem Del(elem); // cleanup77 dlistElemFree(elem); // cleanup 78 78 79 79 return list; … … 134 134 // or PS_DLIST_TAIL 135 135 { 136 psDlistElem *elem = NULL; // element to remove 136 137 assert (list != NULL); 137 138 … … 139 140 assert (which == PS_DLIST_UNKNOWN); 140 141 141 fprintf(stderr,"psDlistRemove does not yet support removal by data"); 142 int i = 0; // index 143 for (psDlistElem *ptr = list->head; ptr != NULL; ptr = ptr->next, i++) { 144 if (ptr->data == data) { 145 return psDlistRemove(list, NULL, i); 146 } 147 } 148 149 psTrace("utils.dlist.remove", 5, "Failed to find 0x%x on psDlist 0x%x", data, list); 150 142 151 return NULL; 143 152 } 144 153 145 if (which == PS_DLIST_HEAD ) {154 if (which == PS_DLIST_HEAD || which == 0) { 146 155 if (list->head == NULL) { 147 156 return NULL; 148 157 } else { 149 data = list->head->data; 158 elem = list->head; 159 150 160 if (list->head->next != NULL) { 151 161 list->head->next->prev = NULL; … … 158 168 } 159 169 } 160 } else if (which == PS_DLIST_TAIL ) {170 } else if (which == PS_DLIST_TAIL || which == list->n - 1) { 161 171 if (list->tail == NULL) { 162 172 return NULL; 163 173 } else { 164 data = list->tail->data;174 elem = list->tail; 165 175 if (list->tail->prev != NULL) { 166 176 list->tail->prev->next = NULL; … … 173 183 } 174 184 } 175 } else if (which != PS_DLIST_UNKNOWN) { 176 fprintf(stderr,"psDlistRemove does not yet support removal by index"); 177 return NULL; 178 } 185 } else if (which != PS_DLIST_UNKNOWN) { // an index in the middle of the list 186 assert (which != 0 && which != list->n - 1); 187 188 if (which < 0 || which >= list->n) { // out of bounds 189 psError(__func__, "Index %d is not in range [0..%d] for list 0x%x", which, list->n, list); 190 return NULL; 191 } 192 193 int i = 0; // index 194 for (psDlistElem *ptr = list->head; ptr != NULL; ptr = ptr->next, i++) { 195 if (i == which) { 196 ptr->prev->next = ptr->next; 197 ptr->next->prev = ptr->prev; 198 199 elem = ptr; 200 } 201 } 202 } 203 /* 204 * OK, delete list element and return the data 205 */ 206 assert (elem != NULL); 207 data = elem->data; 208 dlistElemFree(elem); 179 209 180 210 return psMemDecrRefCounter(data); … … 242 272 /*****************************************************************************/ 243 273 /* 274 * Some wrappers for those iteration calls using psDlistGet. 275 * 276 * First a call to set the iteration pointer to the head of the list 277 */ 278 void psDlistSetIterator(psDlist *list, // the list in question 279 int where, // where on the list should I start? 280 int which) // @notused@ the desired iterator 281 { 282 if (where != PS_DLIST_HEAD && where != PS_DLIST_TAIL) { 283 psError(__func__, "Invalid where argument: %d; assuming PS_DLIST_HEAD", where); 284 where = PS_DLIST_HEAD; 285 } 286 287 psDlistGet(list, where); // initialise iterator at head/tail 288 } 289 290 /* 291 * and now return the previous/next element of the list 292 */ 293 void *psDlistGetNext(psDlist *list, // the list in question 294 int which) // @notused@ the desired iterator 295 { 296 return psDlistGet(list, PS_DLIST_NEXT); 297 } 298 299 void *psDlistGetPrev(psDlist *list, // the list in question 300 int which) // @notused@ the desired iterator 301 { 302 return psDlistGet(list, PS_DLIST_PREV); 303 } 304 305 /*****************************************************************************/ 306 /* 244 307 * Convert a psDlist to/from a psVoidPtrArray 245 308 */ … … 250 313 } 251 314 252 psVoidPtrArray *restrict arr = psVoidPtrArray New(dlist->n, dlist->n);315 psVoidPtrArray *restrict arr = psVoidPtrArrayAlloc(dlist->n, dlist->n); 253 316 254 317 psDlistElem *ptr = dlist->head; … … 260 323 } 261 324 262 psDlist Del(dlist, NULL);325 psDlistFree(dlist, NULL); 263 326 264 327 return arr; … … 267 330 psDlist *psArrayToDlist(psVoidPtrArray *arr) 268 331 { 269 psDlist *list = psDlist New(NULL); // list of elements332 psDlist *list = psDlistAlloc(NULL); // list of elements 270 333 271 334 for (int i = 0, n = arr->n; i < n; i++) { … … 275 338 } 276 339 277 psVoidPtrArray Del(arr, NULL);340 psVoidPtrArrayFree(arr, NULL); 278 341 279 342 return list; … … 284 347 285 348 typedef struct { int i; } X; 286 static X *x New(int i) { X *x = psAlloc(sizeof(X)); x->i = i; return x; }287 static void x Del(X *x) { psFree(x); }349 static X *xAlloc(int i) { X *x = psAlloc(sizeof(X)); x->i = i; return x; } 350 static void xFree(X *x) { psFree(x); } 288 351 289 352 int main(void) 290 353 { 291 psDlist *list = psDlist New(NULL); // list of metadata292 293 psDlistAppend(list, x New(1));294 psDlistAppend(list, x New(2));295 psDlistAppend(list, x New(3));354 psDlist *list = psDlistAlloc(NULL); // list of metadata 355 356 psDlistAppend(list, xAlloc(1)); 357 psDlistAppend(list, xAlloc(2)); 358 psDlistAppend(list, xAlloc(3)); 296 359 /* 297 360 * Print that data … … 334 397 printf("\n"); 335 398 #endif 336 psVoidPtrArray Del(arr, (void (*)(void *))xDel);399 psVoidPtrArrayFree(arr, (void (*)(void *))xFree); 337 400 #endif 338 401 … … 349 412 * Clean up 350 413 */ 351 psDlist Del(list, (void (*)(void *))xDel);414 psDlistFree(list, (void (*)(void *))xFree); 352 415 /* 353 416 * Check for memory leaks
Note:
See TracChangeset
for help on using the changeset viewer.
