Changeset 1407 for trunk/psLib/src/collections/psList.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psList.c (modified) (29 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psList.c
r1406 r1407 1 1 2 /** @file psList.c 2 3 * @brief Support for doubly linked lists … … 6 7 * @author Robert Daniel DeSonia, MHPCC 7 8 * 8 * @version $Revision: 1.1 3$ $Name: not supported by cvs2svn $9 * @date $Date: 2004-08-0 6 22:34:05$9 * @version $Revision: 1.14 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2004-08-07 00:06:06 $ 10 11 * 11 12 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 15 16 #include <stdbool.h> 16 17 #include <stdio.h> 17 #include <pthread.h> // we need a mutex to make this stuff thread safe.18 #include <pthread.h> // we need a mutex to make this stuff thread safe. 18 19 19 20 #include "psError.h" … … 24 25 #include "psLogMsg.h" 25 26 26 #define ITER_INIT_HEAD ((void *)1) // next iteration should return head27 #define ITER_INIT_TAIL ((void *)2) // next iteration should return tail27 #define ITER_INIT_HEAD ((void *)1) // next iteration should return head 28 #define ITER_INIT_TAIL ((void *)2) // next iteration should return tail 28 29 29 30 // private functions. 30 static psListElem* listGetIterator(psList* list); 31 static int listGetIteratorIndex(psList* list); 32 static void listSetIterator(psList *list, int where, bool lockList); 33 static void listFree(psList *list); 34 31 static psListElem *listGetIterator(psList * list); 32 static int listGetIteratorIndex(psList * list); 33 static void listSetIterator(psList * list, int where, bool lockList); 34 static void listFree(psList * list); 35 35 36 36 psList *psListAlloc(void *data) 37 37 { 38 38 psList *list = psAlloc(sizeof(psList)); 39 p_psMemSetDeallocator(list,(psFreeFcn)listFree); 39 40 p_psMemSetDeallocator(list, (psFreeFcn) listFree); 40 41 41 42 list->size = 0; … … 44 45 list->iterIndex = PS_LIST_HEAD; 45 46 46 pthread_mutex_init(&(list->lock), NULL)47 pthread_mutex_init(&(list->lock), NULL) 47 48 ; 48 49 … … 54 55 } 55 56 56 static void listFree(psList * list)57 static void listFree(psList * list) 57 58 { 58 59 if (list == NULL) { … … 63 64 ; 64 65 65 for (psListElem *ptr = list->head; ptr != NULL;) {66 for (psListElem * ptr = list->head; ptr != NULL;) { 66 67 psListElem *next = ptr->next; 67 68 … … 80 81 } 81 82 82 bool psListAdd(psList * list, void *data, int where)83 { 84 psListElem *position;85 psListElem *elem;83 bool psListAdd(psList * list, void *data, int where) 84 { 85 psListElem *position; 86 psListElem *elem; 86 87 int cursorIndex = 0; 87 88 … … 95 96 96 97 if (where <= PS_LIST_UNKNOWN) { 97 /// XXX What is the better way to communicate this failure to the caller? 98 psLogMsg(__func__,PS_LOG_WARN, 99 "The given insert location (%i) for psListAdd is invalid.", 100 where); 98 // / XXX What is the better way to communicate this failure to the caller? 99 psLogMsg(__func__, PS_LOG_WARN, "The given insert location (%i) for psListAdd is invalid.", where); 101 100 return false; 102 101 } … … 108 107 109 108 if (where > 0 && where > list->size) { 110 psLogMsg(__func__,PS_LOG_WARN, 111 "Invalid index %d (only %d elements in psList); assuming tail.", 112 where, list->size); 109 psLogMsg(__func__, PS_LOG_WARN, 110 "Invalid index %d (only %d elements in psList); assuming tail.", where, list->size); 113 111 where = PS_LIST_TAIL; 114 112 } … … 138 136 139 137 if (position == NULL) { 140 psError(__func__,"%s failed to move cursor to specified location (%d)",__func__,where); 141 position = list->head; // since we no list->size != 0, this must be non-NULL 142 } 143 138 psError(__func__, "%s failed to move cursor to specified location (%d)", __func__, where); 139 position = list->head; // since we no list->size != 0, this must be non-NULL 140 } 144 141 // insert our new element in front of the given position 145 142 elem->prev = position->prev; … … 147 144 position->prev = elem; 148 145 149 if (elem->prev == NULL) { // must be front of list146 if (elem->prev == NULL) { // must be front of list 150 147 list->head = elem; 151 148 } else { … … 167 164 168 165 /*****************************************************************************/ 166 169 167 /* 170 168 * Remove an element from a list 171 169 */ 172 bool psListRemove(psList * list, void *data,int which)173 { 174 psListElem *elem = NULL; // element to remove170 bool psListRemove(psList * list, void *data, int which) 171 { 172 psListElem *elem = NULL; // element to remove 175 173 int cursorIndex = 0; 176 174 177 175 if (list == NULL) { 178 psError(__func__, "list parameter found to be NULL in %s",__func__);176 psError(__func__, "list parameter found to be NULL in %s", __func__); 179 177 return false; 180 178 } 181 182 179 // get exclusive access to list so that other threads will not get in the way. 183 180 pthread_mutex_lock(&list->lock) … … 187 184 // search list for the data item. 188 185 189 int i = 0; // index 190 for (psListElem *ptr = list->head; ptr != NULL; ptr = ptr->next) { 186 int i = 0; // index 187 188 for (psListElem * ptr = list->head; ptr != NULL; ptr = ptr->next) { 191 189 if (ptr->data == data) { 192 190 which = i; … … 201 199 } 202 200 } 203 204 201 // position the list's cursor to the desired location 205 listSetIterator(list, which,false);202 listSetIterator(list, which, false); 206 203 elem = listGetIterator(list); 207 204 cursorIndex = listGetIteratorIndex(list); 208 205 209 206 if (elem == NULL) { 210 psError(__func__, "Couldn't position to given index (%d) to remove element from list.", which);207 psError(__func__, "Couldn't position to given index (%d) to remove element from list.", which); 211 208 return false; 212 209 } … … 214 211 list->size--; 215 212 216 if (elem->prev == NULL) { // head of list?213 if (elem->prev == NULL) { // head of list? 217 214 list->head = elem->next; 218 215 } else { … … 220 217 } 221 218 222 if (elem->next == NULL) { // tail of list?219 if (elem->next == NULL) { // tail of list? 223 220 list->tail = elem->prev; 224 221 … … 246 243 } 247 244 248 void psListSetIterator(psList * list, int where)249 { 250 listSetIterator(list, where,true);251 } 252 253 void listSetIterator(psList * list, int where, bool lockList)254 { 255 psListElem *cursor;245 void psListSetIterator(psList * list, int where) 246 { 247 listSetIterator(list, where, true); 248 } 249 250 void listSetIterator(psList * list, int where, bool lockList) 251 { 252 psListElem *cursor; 256 253 int position; 257 254 258 255 if (list == NULL) { 259 psError(__func__, "Unexpected null pointer for psList parameter (%s:%d).",__FILE__,__LINE__);256 psError(__func__, "Unexpected null pointer for psList parameter (%s:%d).", __FILE__, __LINE__); 260 257 return; 261 258 } … … 291 288 if (cursor != NULL) { 292 289 list->iter = cursor->prev; 293 list->iterIndex = position -1;290 list->iterIndex = position - 1; 294 291 } 295 292 break; … … 301 298 if (cursor != NULL) { 302 299 list->iter = cursor->next; 303 list->iterIndex = position +1;300 list->iterIndex = position + 1; 304 301 } 305 302 break; … … 309 306 310 307 default: 311 if (where <= PS_LIST_HEAD) { // bascially same as PS_LIST_UNKNOWN above312 psError(__func__, "Can't move to an unknown position. Not moving the iterator position.");308 if (where <= PS_LIST_HEAD) { // bascially same as PS_LIST_UNKNOWN above 309 psError(__func__, "Can't move to an unknown position. Not moving the iterator position."); 313 310 } else { 314 311 cursor = listGetIterator(list); 315 if (cursor == NULL) { // reset the iterator if it is invalid312 if (cursor == NULL) { // reset the iterator if it is invalid 316 313 list->iter = ITER_INIT_HEAD; 317 314 list->iterIndex = 0; … … 321 318 322 319 if (where < position) { 323 int diff = position-where; 324 for (int count=0;count < diff; count++) { 325 listSetIterator(list,PS_LIST_PREVIOUS,false); 320 int diff = position - where; 321 322 for (int count = 0; count < diff; count++) { 323 listSetIterator(list, PS_LIST_PREVIOUS, false); 326 324 } 327 325 } else { 328 int diff = where-position; 329 for (int count=0;count < diff; count++) { 330 listSetIterator(list,PS_LIST_NEXT,false); 326 int diff = where - position; 327 328 for (int count = 0; count < diff; count++) { 329 listSetIterator(list, PS_LIST_NEXT, false); 331 330 } 332 331 } … … 341 340 } 342 341 343 psListElem * listGetIterator(psList* list)342 psListElem *listGetIterator(psList * list) 344 343 { 345 344 if (list == NULL) { … … 349 348 if (list->iter == ITER_INIT_HEAD) { 350 349 return list->head; 351 } else 352 if (list->iter == ITER_INIT_TAIL) { 353 return list->tail; 354 } else { 355 return list->iter; 356 } 357 } 358 359 int listGetIteratorIndex(psList* list) 350 } else if (list->iter == ITER_INIT_TAIL) { 351 return list->tail; 352 } else { 353 return list->iter; 354 } 355 } 356 357 int listGetIteratorIndex(psList * list) 360 358 { 361 359 if (list->iter == ITER_INIT_HEAD) { 362 360 return 0; 363 } else 364 if (list->iter == ITER_INIT_TAIL) { 365 return list->size-1; 366 } else { 367 return list->iterIndex; 368 } 369 } 370 371 void* psListGet(psList* list,int which) 372 { 373 psListElem* element; 374 375 psListSetIterator(list,which); 361 } else if (list->iter == ITER_INIT_TAIL) { 362 return list->size - 1; 363 } else { 364 return list->iterIndex; 365 } 366 } 367 368 void *psListGet(psList * list, int which) 369 { 370 psListElem *element; 371 372 psListSetIterator(list, which); 376 373 element = listGetIterator(list); 377 374 … … 382 379 } 383 380 } 381 384 382 /* 385 383 * and now return the previous/next element of the list 386 384 */ 387 void *psListGetNext(psList * list)385 void *psListGetNext(psList * list) 388 386 { 389 387 return psListGet(list, PS_LIST_NEXT); 390 388 } 391 389 392 void *psListGetPrevious(psList * list)390 void *psListGetPrevious(psList * list) 393 391 { 394 392 return psListGet(list, PS_LIST_PREVIOUS); 395 393 } 396 394 397 void *psListGetCurrent(psList * list)395 void *psListGetCurrent(psList * list) 398 396 { 399 397 return psListGet(list, PS_LIST_CURRENT); … … 403 401 * Convert a psList to/from a psVoidPtrArray 404 402 */ 405 psArray * psListToArray(psList* restrict list)406 { 407 psListElem *ptr;403 psArray *psListToArray(psList * restrict list) 404 { 405 psListElem *ptr; 408 406 unsigned int n; 409 psArray *restrict arr;407 psArray *restrict arr; 410 408 411 409 if (list == NULL) { … … 431 429 } 432 430 433 psList * psArrayToList(psArray* arr)431 psList *psArrayToList(psArray * arr) 434 432 { 435 433 unsigned int n; 436 psList * list;// list of elements434 psList *list; // list of elements 437 435 438 436 if (arr == NULL) { … … 443 441 n = arr->n; 444 442 for (int i = 0; i < n; i++) { 445 psListAdd(list, arr->data[i],PS_LIST_TAIL);443 psListAdd(list, arr->data[i], PS_LIST_TAIL); 446 444 } 447 445 … … 449 447 } 450 448 451 452 psList* psListSort(psList* list, psComparePtrFcn compare) 453 { 454 psArray* arr; 449 psList *psListSort(psList * list, psComparePtrFcn compare) 450 { 451 psArray *arr; 452 455 453 if (list == NULL) { 456 454 return NULL; 457 455 } 458 459 456 // convert to indexable vector for use by qsort. 460 457 arr = psListToArray(list); 461 458 psFree(list); 462 459 463 arr = psArraySort(arr, compare);460 arr = psArraySort(arr, compare); 464 461 465 462 // convert back to linked list
Note:
See TracChangeset
for help on using the changeset viewer.
