IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 2698


Ignore:
Timestamp:
Dec 10, 2004, 3:19:06 PM (22 years ago)
Author:
desonia
Message:

fixes so that psList test passes now.

Location:
trunk/psLib
Files:
3 edited

Legend:

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

    r2694 r2698  
    66 *  @author Robert Daniel DeSonia, MHPCC
    77 *
    8  *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2004-12-10 21:43:16 $
     8 *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2004-12-11 01:19:06 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4141    pthread_mutex_lock(&list->lock)
    4242    ;
     43
     44    // remove the free function of iterators to avoid double removal from list
     45    psArray* iterators = list->iterators;
     46    for (int i = 0; i < iterators->n; i++) {
     47        p_psMemSetDeallocator(iterators->data[i], NULL);
     48    }
    4349
    4450    psFree(list->iterators);
     
    128134    list->head = list->tail = NULL;
    129135    list->iterators = psArrayAlloc(16);
     136    list->iterators->n = 0;
    130137
    131138    // create a default iterator
    132     list->iterators->data[0] = psListIteratorAlloc(list,PS_LIST_HEAD);
    133     list->iterators->n = 1;
     139    psListIteratorAlloc(list,PS_LIST_HEAD);
    134140
    135141    pthread_mutex_init(&(list->lock), NULL)
     
    183189    psList* list = iterator->list;
    184190
    185     if (location >= (int)list->size) {
    186         psLogMsg(__func__, PS_LOG_WARN,
    187                  "Specified index, %d, is beyond the end of the psList, which "
    188                  "has only %d elements.  Assuming tail.",
    189                  location, list->size);
    190         location = PS_LIST_TAIL;
    191     }
    192 
    193191    if (location == PS_LIST_TAIL) {
    194192        iterator->cursor = list->tail;
     
    198196    }
    199197
    200     if (location <= 0) {   // Invalid index
    201         return false;
    202     }
    203 
     198    if (location == PS_LIST_HEAD) {
     199        iterator->cursor = list->head;
     200        iterator->index = 0;
     201        iterator->offEnd = false;
     202        return true;
     203    }
     204
     205    if (location < 0 || location >= (int)list->size) {
     206        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     207                PS_ERRORTEXT_psList_LOCATION_INVALID,
     208                location);
     209        return false;
     210    }
    204211
    205212    psListElem* cursor = iterator->cursor;
     
    250257    }
    251258
     259    if (location >= (int)list->size) {
     260        psLogMsg(__func__,PS_LOG_WARN,
     261                 "Specified location, %d, is beyond the end of the list.  "
     262                 "Adding data item to tail.",
     263                 location);
     264        location = PS_LIST_TAIL;
     265    }
     266
    252267    // move ourselves to the given position
    253     if (list->iterators->n < 1 ||
    254             ! psListIteratorSet(list->iterators->data[0],location)) {
    255         // oh no, I can't find where to add this!
    256         psError(PS_ERR_UNKNOWN, false,
    257                 PS_ERRORTEXT_psList_LOCATION_INVALID,
    258                 location);
     268    if (! psListIteratorSet(list->iterators->data[0],location)) {
    259269        return false;
    260270    }
     
    308318        if (elem->next == NULL) {
    309319            list->tail = elem;
     320        } else {
     321            elem->next->prev = elem;
    310322        }
    311323    }
     
    374386        if (elem->prev == NULL) {
    375387            list->head = elem;
     388        } else {
     389            elem->prev->next = elem;
    376390        }
    377391    }
     
    411425    // move ourselves to the given position
    412426    psListIterator* defaultIterator = list->iterators->data[0];
    413     if (list->iterators->n < 1 ||
    414             ! psListIteratorSet(defaultIterator,location)) {
    415         // oh no, I can't find where to add this!
    416         psError(PS_ERR_UNKNOWN, false,
    417                 PS_ERRORTEXT_psList_LOCATION_INVALID,
    418                 location);
     427    if (! psListIteratorSet(defaultIterator,location)) {
    419428        return false;
    420429    }
     
    438447    }
    439448
    440     psListIterator* iterator = list->iterators->data[0];
    441     psListIteratorSet(iterator,PS_LIST_HEAD);
    442 
    443     psPtr iteratorData = psListGetNext(iterator);
    444     while (iteratorData != NULL && iteratorData != data) {
    445         iteratorData = psListGetNext(iterator);
    446     }
    447 
    448     if (iteratorData == NULL) {
     449    psListElem* elem = list->head;
     450    int index = 0;
     451    while (elem != NULL && elem->data != data) {
     452        elem = elem->next;
     453        index++;
     454    }
     455    if (elem == NULL) {
    449456        psError(PS_ERR_BAD_PARAMETER_NULL, true,
    450457                PS_ERRORTEXT_psList_DATA_NOT_FOUND);
     
    452459    }
    453460
     461    psListIterator* iterator = (psListIterator*)list->iterators->data[0];
     462    iterator->index = index;
     463    iterator->cursor = elem;
     464
    454465    return listIteratorRemove(iterator);
    455466}
     
    460471        psError(PS_ERR_BAD_PARAMETER_NULL, true,
    461472                PS_ERRORTEXT_psList_LIST_NULL);
    462         return false;
     473        return NULL;
     474    }
     475
     476    if (list->head == NULL) { // list empty?
     477        return NULL;
    463478    }
    464479
  • trunk/psLib/src/types/psList.c

    r2694 r2698  
    66 *  @author Robert Daniel DeSonia, MHPCC
    77 *
    8  *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2004-12-10 21:43:16 $
     8 *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2004-12-11 01:19:06 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4141    pthread_mutex_lock(&list->lock)
    4242    ;
     43
     44    // remove the free function of iterators to avoid double removal from list
     45    psArray* iterators = list->iterators;
     46    for (int i = 0; i < iterators->n; i++) {
     47        p_psMemSetDeallocator(iterators->data[i], NULL);
     48    }
    4349
    4450    psFree(list->iterators);
     
    128134    list->head = list->tail = NULL;
    129135    list->iterators = psArrayAlloc(16);
     136    list->iterators->n = 0;
    130137
    131138    // create a default iterator
    132     list->iterators->data[0] = psListIteratorAlloc(list,PS_LIST_HEAD);
    133     list->iterators->n = 1;
     139    psListIteratorAlloc(list,PS_LIST_HEAD);
    134140
    135141    pthread_mutex_init(&(list->lock), NULL)
     
    183189    psList* list = iterator->list;
    184190
    185     if (location >= (int)list->size) {
    186         psLogMsg(__func__, PS_LOG_WARN,
    187                  "Specified index, %d, is beyond the end of the psList, which "
    188                  "has only %d elements.  Assuming tail.",
    189                  location, list->size);
    190         location = PS_LIST_TAIL;
    191     }
    192 
    193191    if (location == PS_LIST_TAIL) {
    194192        iterator->cursor = list->tail;
     
    198196    }
    199197
    200     if (location <= 0) {   // Invalid index
    201         return false;
    202     }
    203 
     198    if (location == PS_LIST_HEAD) {
     199        iterator->cursor = list->head;
     200        iterator->index = 0;
     201        iterator->offEnd = false;
     202        return true;
     203    }
     204
     205    if (location < 0 || location >= (int)list->size) {
     206        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     207                PS_ERRORTEXT_psList_LOCATION_INVALID,
     208                location);
     209        return false;
     210    }
    204211
    205212    psListElem* cursor = iterator->cursor;
     
    250257    }
    251258
     259    if (location >= (int)list->size) {
     260        psLogMsg(__func__,PS_LOG_WARN,
     261                 "Specified location, %d, is beyond the end of the list.  "
     262                 "Adding data item to tail.",
     263                 location);
     264        location = PS_LIST_TAIL;
     265    }
     266
    252267    // move ourselves to the given position
    253     if (list->iterators->n < 1 ||
    254             ! psListIteratorSet(list->iterators->data[0],location)) {
    255         // oh no, I can't find where to add this!
    256         psError(PS_ERR_UNKNOWN, false,
    257                 PS_ERRORTEXT_psList_LOCATION_INVALID,
    258                 location);
     268    if (! psListIteratorSet(list->iterators->data[0],location)) {
    259269        return false;
    260270    }
     
    308318        if (elem->next == NULL) {
    309319            list->tail = elem;
     320        } else {
     321            elem->next->prev = elem;
    310322        }
    311323    }
     
    374386        if (elem->prev == NULL) {
    375387            list->head = elem;
     388        } else {
     389            elem->prev->next = elem;
    376390        }
    377391    }
     
    411425    // move ourselves to the given position
    412426    psListIterator* defaultIterator = list->iterators->data[0];
    413     if (list->iterators->n < 1 ||
    414             ! psListIteratorSet(defaultIterator,location)) {
    415         // oh no, I can't find where to add this!
    416         psError(PS_ERR_UNKNOWN, false,
    417                 PS_ERRORTEXT_psList_LOCATION_INVALID,
    418                 location);
     427    if (! psListIteratorSet(defaultIterator,location)) {
    419428        return false;
    420429    }
     
    438447    }
    439448
    440     psListIterator* iterator = list->iterators->data[0];
    441     psListIteratorSet(iterator,PS_LIST_HEAD);
    442 
    443     psPtr iteratorData = psListGetNext(iterator);
    444     while (iteratorData != NULL && iteratorData != data) {
    445         iteratorData = psListGetNext(iterator);
    446     }
    447 
    448     if (iteratorData == NULL) {
     449    psListElem* elem = list->head;
     450    int index = 0;
     451    while (elem != NULL && elem->data != data) {
     452        elem = elem->next;
     453        index++;
     454    }
     455    if (elem == NULL) {
    449456        psError(PS_ERR_BAD_PARAMETER_NULL, true,
    450457                PS_ERRORTEXT_psList_DATA_NOT_FOUND);
     
    452459    }
    453460
     461    psListIterator* iterator = (psListIterator*)list->iterators->data[0];
     462    iterator->index = index;
     463    iterator->cursor = elem;
     464
    454465    return listIteratorRemove(iterator);
    455466}
     
    460471        psError(PS_ERR_BAD_PARAMETER_NULL, true,
    461472                PS_ERRORTEXT_psList_LIST_NULL);
    462         return false;
     473        return NULL;
     474    }
     475
     476    if (list->head == NULL) { // list empty?
     477        return NULL;
    463478    }
    464479
  • trunk/psLib/test/collections/tst_psList.c

    r2681 r2698  
    66 *  @author Robert DeSonia, MHPCC
    77 *
    8  *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2004-12-10 02:50:16 $
     8 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2004-12-11 01:19:06 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    981981
    982982    printf("original list = [");
    983     psListIteratorSet(iter,PS_LIST_HEAD);
     983    iter = psListIteratorAlloc(list,PS_LIST_HEAD);
    984984    while( (uValue=psListGetNext(iter)) != NULL ) {
    985985        printf(" %d",*uValue);
     
    999999    psU32* prevUValue = psListGetNext(iter);
    10001000    while( (uValue=psListGetNext(iter)) != NULL ) {
    1001         if (*prevUValue < *uValue) {
     1001        if (*prevUValue > *uValue) {
    10021002            psError(PS_ERR_UNKNOWN, true,"Hey, the list wasn't sorted!?");
    10031003            return 3;
Note: See TracChangeset for help on using the changeset viewer.