Changeset 900
- Timestamp:
- Jun 7, 2004, 1:18:21 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psDlist.c (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psDlist.c
r874 r900 1 /** @file psDlist. h1 /** @file psDlist.c 2 2 * @brief Support for doubly linked lists 3 3 * @ingroup DataContainers … … 6 6 * @author Robert Daniel DeSonia, MHPCC 7 7 * 8 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $9 * @date $Date: 2004-06-0 4 23:44:36$8 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2004-06-07 23:18:21 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 22 22 #include "psDlist.h" 23 23 #include "psTrace.h" 24 #include "psLogMsg.h" 24 25 25 26 #define ITER_INIT_HEAD ((void *)1) // next iteration should return head … … 88 89 } 89 90 90 psDlist *psDlistAdd(psDlist *list, void *data, int where)91 psDlist* psDlistAdd(psDlist *list, void *data, int where) 91 92 { 92 93 psDlistElem* position; 93 94 psDlistElem* elem = dlistElemAlloc(); 95 int cursorIndex = 0; 94 96 95 97 if (list == NULL) { 96 list = psDlistAlloc(NULL); 98 return NULL; 99 } 100 101 if (data == NULL) { 102 return list; 97 103 } 98 104 … … 102 108 if (where <= PS_DLIST_UNKNOWN) { 103 109 /// XXX What is the better way to communicate this failure to the caller? 104 psError(__func__,"The given insert location (%i) for psDlistAdd is invalid. " 105 "Adding to head instead.",where); 110 psLogMsg(__func__,PS_LOG_WARN, 111 "The given insert location (%i) for psDlistAdd is invalid. Adding to head instead.", 112 where); 106 113 where = PS_DLIST_HEAD; // given I can't tell caller about this, should just add it somewhere??? 107 114 } 108 115 109 116 if (where > 0 && where > list->size) { 110 psError(__FILE__, "Invalid index %d (only %d elements in psDList); assuming tail.", where, 111 list->size); 117 psLogMsg(__func__,PS_LOG_WARN, 118 "Invalid index %d (only %d elements in psDList); assuming tail.", 119 where, list->size); 112 120 where = PS_DLIST_TAIL; 113 121 } … … 134 142 dlistSetIterator(list, where, false); 135 143 position = dlistGetIterator(list); 144 cursorIndex = dlistGetIteratorIndex(list); 136 145 137 146 if (position == NULL) { … … 153 162 list->size++; 154 163 list->iter = elem; 164 list->iterIndex = cursorIndex; 155 165 } 156 166 … … 177 187 { 178 188 psDlistElem *elem = NULL; // element to remove 189 int cursorIndex = 0; 190 179 191 if (list == NULL) { 180 192 psError(__func__,"list parameter found to be NULL in %s",__func__); … … 207 219 dlistSetIterator(list,which,false); 208 220 elem = dlistGetIterator(list); 221 cursorIndex = dlistGetIteratorIndex(list); 209 222 210 223 if (elem == NULL) { … … 234 247 elem->next->prev = elem->prev; 235 248 list->iter = elem->next; 249 list->iterIndex = cursorIndex; 236 250 } 237 251 … … 268 282 if (lockList) { 269 283 pthread_mutex_lock(&list->lock) 270 ; // don't want the list changing on us while we move about 271 } 272 273 if (where >= list->size) { 274 psError(__func__,"Tried to access an element beyond list end. " 275 "Moved to last element of list instead."); 276 where = list->size-1; 277 278 switch (where) { 279 case PS_DLIST_HEAD: 280 list->iter = ITER_INIT_HEAD; 281 break; 282 283 case PS_DLIST_TAIL: 284 list->iter = ITER_INIT_TAIL; 285 break; 286 287 case PS_DLIST_PREVIOUS: 284 ; 285 // don't want the list changing on us while we move about 286 } 287 288 if (where >= (int)list->size) { 289 list->iter = NULL; 290 return; 291 } 292 293 switch (where) { 294 case PS_DLIST_HEAD: 295 list->iter = ITER_INIT_HEAD; 296 break; 297 298 case PS_DLIST_TAIL: 299 list->iter = ITER_INIT_TAIL; 300 break; 301 302 case PS_DLIST_PREVIOUS: 303 cursor = dlistGetIterator(list); 304 position = dlistGetIteratorIndex(list); 305 306 if (cursor != NULL) { 307 list->iter = cursor->prev; 308 list->iterIndex = position-1; 309 } 310 break; 311 312 case PS_DLIST_NEXT: 313 cursor = dlistGetIterator(list); 314 position = dlistGetIteratorIndex(list); 315 316 if (cursor != NULL) { 317 list->iter = cursor->next; 318 list->iterIndex = position+1; 319 } 320 break; 321 322 case PS_DLIST_CURRENT: 323 break; 324 325 default: 326 if (where <= PS_DLIST_HEAD) { // bascially same as PS_DLIST_UNKNOWN above 327 psError(__func__,"Can't move to an unknown position. Not moving the iterator position."); 328 } else { 288 329 cursor = dlistGetIterator(list); 289 position = dlistGetIteratorIndex(list); 290 291 if (cursor == NULL) { // list empty? 292 ((psDlist *)list)->iter = ITER_INIT_HEAD; 330 if (cursor == NULL) { // reset the iterator if it is invalid 331 list->iter = ITER_INIT_HEAD; 332 list->iterIndex = 0; 333 } 334 335 int position = dlistGetIteratorIndex(list); 336 337 if (where < position) { 338 int diff = position-where; 339 for (int count=0;count < diff; count++) { 340 dlistSetIterator(list,PS_DLIST_PREVIOUS,false); 341 } 293 342 } else { 294 if (cursor->prev != NULL) { // don't go past head 295 list->iter = cursor->prev; 296 list->iterIndex = position-1; 297 break; 343 int diff = where-position; 344 for (int count=0;count < diff; count++) { 345 dlistSetIterator(list,PS_DLIST_NEXT,false); 298 346 } 299 347 } 300 301 case PS_DLIST_NEXT: 302 cursor = dlistGetIterator(list); 303 position = dlistGetIteratorIndex(list); 304 305 if (cursor == NULL) { // list empty? 306 ((psDlist *)list)->iter = ITER_INIT_HEAD; 307 } else { 308 if (cursor->next != NULL) { // don't go pase tail 309 list->iter = cursor->next; 310 list->iterIndex = position+1; 311 break; 312 } 313 } 314 315 case PS_DLIST_UNKNOWN: 316 psError(__func__,"Can't move to the PS_DLIST_UNKNOWN position. Not moving the iterator position."); 317 break; 318 case PS_DLIST_CURRENT: 319 break; 320 321 default: 322 if (where < PS_DLIST_HEAD) { // bascially same as PS_DLIST_UNKNOWN above 323 psError(__func__,"Can't move to an unknown position. Not moving the iterator position."); 324 break; 325 } else { 326 int position = dlistGetIteratorIndex(list); 327 328 if (where < position) { 329 int diff = position-where; 330 for (int count=0;count < diff; count++) { 331 dlistSetIterator(list,PS_DLIST_PREVIOUS,false); 332 } 333 } else { 334 int diff = where-position; 335 for (int count=0;count < diff; count++) { 336 dlistSetIterator(list,PS_DLIST_NEXT,false); 337 } 338 } 339 } 340 break; 341 } 342 343 if (lockList) { 344 pthread_mutex_unlock(&list->lock) 345 ; 346 } 348 } 349 break; 350 } 351 352 if (lockList) { 353 pthread_mutex_unlock(&list->lock) 354 ; 347 355 } 348 356 } … … 350 358 psDlistElem* dlistGetIterator(psDlist* list) 351 359 { 360 if (list == NULL) { 361 return NULL; 362 } 363 352 364 if (list->iter == ITER_INIT_HEAD) { 353 365 return list->head;
Note:
See TracChangeset
for help on using the changeset viewer.
