IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 6, 2004, 2:06:06 PM (22 years ago)
Author:
desonia
Message:

another attempt to get astyle to get it right.

File:
1 edited

Legend:

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

    r1406 r1407  
     1
    12/** @file psList.c
    23 *  @brief Support for doubly linked lists
     
    67 *  @author Robert Daniel DeSonia, MHPCC
    78 *
    8  *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2004-08-06 22:34:05 $
     9 *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2004-08-07 00:06:06 $
    1011 *
    1112 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1516#include <stdbool.h>
    1617#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.
    1819
    1920#include "psError.h"
     
    2425#include "psLogMsg.h"
    2526
    26 #define ITER_INIT_HEAD ((void *)1) // next iteration should return head
    27 #define ITER_INIT_TAIL ((void *)2) // next iteration should return tail
     27#define ITER_INIT_HEAD ((void *)1)         // next iteration should return head
     28#define ITER_INIT_TAIL ((void *)2)         // next iteration should return tail
    2829
    2930// 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 
     31static psListElem *listGetIterator(psList * list);
     32static int listGetIteratorIndex(psList * list);
     33static void listSetIterator(psList * list, int where, bool lockList);
     34static void listFree(psList * list);
    3535
    3636psList *psListAlloc(void *data)
    3737{
    3838    psList *list = psAlloc(sizeof(psList));
    39     p_psMemSetDeallocator(list,(psFreeFcn)listFree);
     39
     40    p_psMemSetDeallocator(list, (psFreeFcn) listFree);
    4041
    4142    list->size = 0;
     
    4445    list->iterIndex = PS_LIST_HEAD;
    4546
    46     pthread_mutex_init(&(list->lock),NULL)
     47    pthread_mutex_init(&(list->lock), NULL)
    4748    ;
    4849
     
    5455}
    5556
    56 static void listFree(psList *list)
     57static void listFree(psList * list)
    5758{
    5859    if (list == NULL) {
     
    6364    ;
    6465
    65     for(psListElem *ptr = list->head; ptr != NULL; ) {
     66    for (psListElem * ptr = list->head; ptr != NULL;) {
    6667        psListElem *next = ptr->next;
    6768
     
    8081}
    8182
    82 bool psListAdd(psList *list, void *data, int where)
    83 {
    84     psListElem* position;
    85     psListElem* elem;
     83bool psListAdd(psList * list, void *data, int where)
     84{
     85    psListElem *position;
     86    psListElem *elem;
    8687    int cursorIndex = 0;
    8788
     
    9596
    9697    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);
    101100        return false;
    102101    }
     
    108107
    109108    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);
    113111        where = PS_LIST_TAIL;
    114112    }
     
    138136
    139137        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        }
    144141        // insert our new element in front of the given position
    145142        elem->prev = position->prev;
     
    147144        position->prev = elem;
    148145
    149         if (elem->prev == NULL) { // must be front of list
     146        if (elem->prev == NULL) {          // must be front of list
    150147            list->head = elem;
    151148        } else {
     
    167164
    168165/*****************************************************************************/
     166
    169167/*
    170168 * Remove an element from a list
    171169 */
    172 bool psListRemove(psList *list, void *data, int which)
    173 {
    174     psListElem *elem = NULL;  // element to remove
     170bool psListRemove(psList * list, void *data, int which)
     171{
     172    psListElem *elem = NULL;    // element to remove
    175173    int cursorIndex = 0;
    176174
    177175    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__);
    179177        return false;
    180178    }
    181 
    182179    // get exclusive access to list so that other threads will not get in the way.
    183180    pthread_mutex_lock(&list->lock)
     
    187184        // search list for the data item.
    188185
    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) {
    191189            if (ptr->data == data) {
    192190                which = i;
     
    201199        }
    202200    }
    203 
    204201    // position the list's cursor to the desired location
    205     listSetIterator(list,which,false);
     202    listSetIterator(list, which, false);
    206203    elem = listGetIterator(list);
    207204    cursorIndex = listGetIteratorIndex(list);
    208205
    209206    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);
    211208        return false;
    212209    }
     
    214211    list->size--;
    215212
    216     if (elem->prev == NULL) { // head of list?
     213    if (elem->prev == NULL) {              // head of list?
    217214        list->head = elem->next;
    218215    } else {
     
    220217    }
    221218
    222     if (elem->next == NULL) { // tail of list?
     219    if (elem->next == NULL) {              // tail of list?
    223220        list->tail = elem->prev;
    224221
     
    246243}
    247244
    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;
     245void psListSetIterator(psList * list, int where)
     246{
     247    listSetIterator(list, where, true);
     248}
     249
     250void listSetIterator(psList * list, int where, bool lockList)
     251{
     252    psListElem *cursor;
    256253    int position;
    257254
    258255    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__);
    260257        return;
    261258    }
     
    291288        if (cursor != NULL) {
    292289            list->iter = cursor->prev;
    293             list->iterIndex = position-1;
     290            list->iterIndex = position - 1;
    294291        }
    295292        break;
     
    301298        if (cursor != NULL) {
    302299            list->iter = cursor->next;
    303             list->iterIndex = position+1;
     300            list->iterIndex = position + 1;
    304301        }
    305302        break;
     
    309306
    310307    default:
    311         if (where <= PS_LIST_HEAD) { // bascially same as PS_LIST_UNKNOWN above
    312             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.");
    313310        } else {
    314311            cursor = listGetIterator(list);
    315             if (cursor == NULL) { // reset the iterator if it is invalid
     312            if (cursor == NULL) {      // reset the iterator if it is invalid
    316313                list->iter = ITER_INIT_HEAD;
    317314                list->iterIndex = 0;
     
    321318
    322319            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);
    326324                }
    327325            } 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);
    331330                }
    332331            }
     
    341340}
    342341
    343 psListElem* listGetIterator(psList* list)
     342psListElem *listGetIterator(psList * list)
    344343{
    345344    if (list == NULL) {
     
    349348    if (list->iter == ITER_INIT_HEAD) {
    350349        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
     357int listGetIteratorIndex(psList * list)
    360358{
    361359    if (list->iter == ITER_INIT_HEAD) {
    362360        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
     368void *psListGet(psList * list, int which)
     369{
     370    psListElem *element;
     371
     372    psListSetIterator(list, which);
    376373    element = listGetIterator(list);
    377374
     
    382379    }
    383380}
     381
    384382/*
    385383 * and now return the previous/next element of the list
    386384 */
    387 void *psListGetNext(psList *list)
     385void *psListGetNext(psList * list)
    388386{
    389387    return psListGet(list, PS_LIST_NEXT);
    390388}
    391389
    392 void *psListGetPrevious(psList *list)
     390void *psListGetPrevious(psList * list)
    393391{
    394392    return psListGet(list, PS_LIST_PREVIOUS);
    395393}
    396394
    397 void *psListGetCurrent(psList *list)
     395void *psListGetCurrent(psList * list)
    398396{
    399397    return psListGet(list, PS_LIST_CURRENT);
     
    403401 * Convert a psList to/from a psVoidPtrArray
    404402 */
    405 psArray* psListToArray(psList* restrict list)
    406 {
    407     psListElem* ptr;
     403psArray *psListToArray(psList * restrict list)
     404{
     405    psListElem *ptr;
    408406    unsigned int n;
    409     psArray* restrict arr;
     407    psArray *restrict arr;
    410408
    411409    if (list == NULL) {
     
    431429}
    432430
    433 psList* psArrayToList(psArray* arr)
     431psList *psArrayToList(psArray * arr)
    434432{
    435433    unsigned int n;
    436     psList* list; // list of elements
     434    psList *list;              // list of elements
    437435
    438436    if (arr == NULL) {
     
    443441    n = arr->n;
    444442    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);
    446444    }
    447445
     
    449447}
    450448
    451 
    452 psList* psListSort(psList* list, psComparePtrFcn compare)
    453 {
    454     psArray* arr;
     449psList *psListSort(psList * list, psComparePtrFcn compare)
     450{
     451    psArray *arr;
     452
    455453    if (list == NULL) {
    456454        return NULL;
    457455    }
    458 
    459456    // convert to indexable vector for use by qsort.
    460457    arr = psListToArray(list);
    461458    psFree(list);
    462459
    463     arr = psArraySort(arr,compare);
     460    arr = psArraySort(arr, compare);
    464461
    465462    // convert back to linked list
Note: See TracChangeset for help on using the changeset viewer.