IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 900


Ignore:
Timestamp:
Jun 7, 2004, 1:18:21 PM (22 years ago)
Author:
desonia
Message:

fixed various small bugs found in testing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/collections/psDlist.c

    r874 r900  
    1 /** @file psDlist.h
     1/** @file psDlist.c
    22 *  @brief Support for doubly linked lists
    33 *  @ingroup DataContainers
     
    66 *  @author Robert Daniel DeSonia, MHPCC
    77 *
    8  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2004-06-04 23:44:36 $
     8 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2004-06-07 23:18:21 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2222#include "psDlist.h"
    2323#include "psTrace.h"
     24#include "psLogMsg.h"
    2425
    2526#define ITER_INIT_HEAD ((void *)1) // next iteration should return head
     
    8889}
    8990
    90 psDlist *psDlistAdd(psDlist *list, void *data, int where)
     91psDlist* psDlistAdd(psDlist *list, void *data, int where)
    9192{
    9293    psDlistElem* position;
    9394    psDlistElem* elem = dlistElemAlloc();
     95    int cursorIndex = 0;
    9496
    9597    if (list == NULL) {
    96         list = psDlistAlloc(NULL);
     98        return NULL;
     99    }
     100
     101    if (data == NULL) {
     102        return list;
    97103    }
    98104
     
    102108    if (where <= PS_DLIST_UNKNOWN) {
    103109        /// 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);
    106113        where = PS_DLIST_HEAD; // given I can't tell caller about this, should just add it somewhere???
    107114    }
    108115
    109116    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);
    112120        where = PS_DLIST_TAIL;
    113121    }
     
    134142        dlistSetIterator(list, where, false);
    135143        position = dlistGetIterator(list);
     144        cursorIndex = dlistGetIteratorIndex(list);
    136145
    137146        if (position == NULL) {
     
    153162        list->size++;
    154163        list->iter = elem;
     164        list->iterIndex = cursorIndex;
    155165    }
    156166
     
    177187{
    178188    psDlistElem *elem = NULL;  // element to remove
     189    int cursorIndex = 0;
     190
    179191    if (list == NULL) {
    180192        psError(__func__,"list parameter found to be NULL in %s",__func__);
     
    207219    dlistSetIterator(list,which,false);
    208220    elem = dlistGetIterator(list);
     221    cursorIndex = dlistGetIteratorIndex(list);
    209222
    210223    if (elem == NULL) {
     
    234247        elem->next->prev = elem->prev;
    235248        list->iter = elem->next;
     249        list->iterIndex = cursorIndex;
    236250    }
    237251
     
    268282    if (lockList) {
    269283        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 {
    288329            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                }
    293342            } 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);
    298346                }
    299347            }
    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        ;
    347355    }
    348356}
     
    350358psDlistElem* dlistGetIterator(psDlist* list)
    351359{
     360    if (list == NULL) {
     361        return NULL;
     362    }
     363
    352364    if (list->iter == ITER_INIT_HEAD) {
    353365        return list->head;
Note: See TracChangeset for help on using the changeset viewer.