IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 9, 2004, 2:53:44 PM (22 years ago)
Author:
rhl
Message:

1/ Add ability to remove by item's address to psDlistRemove()
2/ Rename New/Del to Alloc/Free
3/ Added psDlistSetIterator/psDlistGetNext/psDlistGetPrev

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/pslib/src/Utils/dlist.c

    r141 r169  
    77#include "psUtils.h"
    88
    9 static psDlistElem *dlistElemNew(void)
     9static psDlistElem *dlistElemAlloc(void)
    1010{
    1111    return(psAlloc(sizeof(psDlistElem)));
    1212}
    1313
    14 static void dlistElemDel(psDlistElem *elem)
     14static void dlistElemFree(psDlistElem *elem)
    1515{
    1616    psFree(elem);
     
    1919/*****************************************************************************/
    2020
    21 psDlist *psDlistNew(void *data)         // initial data item; may be NULL
     21psDlist *psDlistAlloc(void *data)       // initial data item; may be NULL
    2222{
    2323    psDlist *list = psAlloc(sizeof(psDlist));
     
    3333}
    3434
    35 void psDlistDel(psDlist *list,          // list to destroy
    36                 void (*elemDel)(void *)) // destructor for data on list
     35void psDlistFree(psDlist *list,         // list to destroy
     36                 void (*elemFree)(void *)) // destructor for data on list
    3737{
    3838    if (list == NULL) {
     
    4444        psDlistElem *next = ptr->next;
    4545
    46         if (elemDel == NULL) {
     46        if (elemFree == NULL) {
    4747            psMemDecrRefCounter(ptr->data);
    4848        } else {
    49             elemDel(psMemDecrRefCounter(ptr->data));
    50         }
    51         dlistElemDel(ptr);
     49            elemFree(psMemDecrRefCounter(ptr->data));
     50        }
     51        dlistElemFree(ptr);
    5252       
    5353        ptr = next;
     
    6666                                        // PS_DLIST_TAIL, or an integer
    6767{
    68     psDlistElem *elem = dlistElemNew();
     68    psDlistElem *elem = dlistElemAlloc();
    6969
    7070    if (list == NULL) {
    71         list = psDlistNew(NULL);
     71        list = psDlistAlloc(NULL);
    7272    }
    7373
     
    7575        fprintf(stderr, "Invalid index %d (only %d elements)\n",
    7676                where, list->n);
    77         dlistElemDel(elem);             // cleanup
     77        dlistElemFree(elem);            // cleanup
    7878       
    7979        return list;
     
    134134                                        // or PS_DLIST_TAIL
    135135{
     136    psDlistElem *elem = NULL;           // element to remove
    136137    assert (list != NULL);
    137138
     
    139140        assert (which == PS_DLIST_UNKNOWN);
    140141
    141         fprintf(stderr,"psDlistRemove does not yet support removal by data");
     142        int i = 0;                      // index
     143        for (psDlistElem *ptr = list->head; ptr != NULL; ptr = ptr->next, i++) {
     144            if (ptr->data == data) {
     145                return psDlistRemove(list, NULL, i);
     146            }
     147        }
     148
     149        psTrace("utils.dlist.remove", 5, "Failed to find 0x%x on psDlist 0x%x", data, list);
     150       
    142151        return NULL;
    143152    }
    144153
    145     if (which == PS_DLIST_HEAD) {
     154    if (which == PS_DLIST_HEAD || which == 0) {
    146155        if (list->head == NULL) {
    147156            return NULL;
    148157        } else {
    149             data = list->head->data;
     158            elem = list->head;
     159           
    150160            if (list->head->next != NULL) {
    151161                list->head->next->prev = NULL;
     
    158168            }
    159169        }
    160     } else if (which == PS_DLIST_TAIL) {
     170    } else if (which == PS_DLIST_TAIL || which == list->n - 1) {
    161171        if (list->tail == NULL) {
    162172            return NULL;
    163173        } else {
    164             data = list->tail->data;
     174            elem = list->tail;
    165175            if (list->tail->prev != NULL) {
    166176                list->tail->prev->next = NULL;
     
    173183            }
    174184        }
    175     } else if (which != PS_DLIST_UNKNOWN) {
    176         fprintf(stderr,"psDlistRemove does not yet support removal by index");
    177         return NULL;
    178     }
     185    } else if (which != PS_DLIST_UNKNOWN) { // an index in the middle of the list
     186        assert (which != 0 && which != list->n - 1);
     187       
     188        if (which < 0 || which >= list->n) { // out of bounds
     189            psError(__func__, "Index %d is not in range [0..%d] for list 0x%x", which, list->n, list);
     190            return NULL;
     191        }
     192       
     193        int i = 0;                      // index
     194        for (psDlistElem *ptr = list->head; ptr != NULL; ptr = ptr->next, i++) {
     195            if (i == which) {
     196                ptr->prev->next = ptr->next;
     197                ptr->next->prev = ptr->prev;
     198
     199                elem = ptr;             
     200            }
     201        }
     202    }
     203    /*
     204     * OK, delete list element and return the data
     205     */
     206    assert (elem != NULL);
     207    data = elem->data;
     208    dlistElemFree(elem);
    179209
    180210    return psMemDecrRefCounter(data);
     
    242272/*****************************************************************************/
    243273/*
     274 * Some wrappers for those iteration calls using psDlistGet.
     275 *
     276 * First a call to set the iteration pointer to the head of the list
     277 */
     278void psDlistSetIterator(psDlist *list,  // the list in question
     279                        int where,      // where on the list should I start?
     280                        int which)      // @notused@ the desired iterator
     281{
     282    if (where != PS_DLIST_HEAD && where != PS_DLIST_TAIL) {
     283        psError(__func__, "Invalid where argument: %d; assuming PS_DLIST_HEAD", where);
     284        where = PS_DLIST_HEAD;
     285    }
     286   
     287    psDlistGet(list, where);    // initialise iterator at head/tail
     288}
     289
     290/*
     291 * and now return the previous/next element of the list
     292 */
     293void *psDlistGetNext(psDlist *list,     // the list in question
     294                     int which)         // @notused@ the desired iterator
     295{
     296    return psDlistGet(list, PS_DLIST_NEXT);
     297}
     298
     299void *psDlistGetPrev(psDlist *list,     // the list in question
     300                     int which)         // @notused@ the desired iterator
     301{
     302    return psDlistGet(list, PS_DLIST_PREV);
     303}
     304
     305/*****************************************************************************/
     306/*
    244307 * Convert a psDlist to/from a psVoidPtrArray
    245308 */
     
    250313    }
    251314   
    252     psVoidPtrArray *restrict arr = psVoidPtrArrayNew(dlist->n, dlist->n);
     315    psVoidPtrArray *restrict arr = psVoidPtrArrayAlloc(dlist->n, dlist->n);
    253316
    254317    psDlistElem *ptr = dlist->head;
     
    260323    }
    261324
    262     psDlistDel(dlist, NULL);
     325    psDlistFree(dlist, NULL);
    263326
    264327    return arr;   
     
    267330psDlist *psArrayToDlist(psVoidPtrArray *arr)
    268331{
    269     psDlist *list = psDlistNew(NULL);   // list of elements
     332    psDlist *list = psDlistAlloc(NULL); // list of elements
    270333
    271334    for (int i = 0, n = arr->n; i < n; i++) {
     
    275338    }
    276339
    277     psVoidPtrArrayDel(arr, NULL);
     340    psVoidPtrArrayFree(arr, NULL);
    278341
    279342    return list;
     
    284347
    285348typedef struct { int i; } X;
    286 static X *xNew(int i) { X *x = psAlloc(sizeof(X)); x->i = i; return x; }
    287 static void xDel(X *x) { psFree(x); }
     349static X *xAlloc(int i) { X *x = psAlloc(sizeof(X)); x->i = i; return x; }
     350static void xFree(X *x) { psFree(x); }
    288351
    289352int main(void)
    290353{
    291     psDlist *list = psDlistNew(NULL);   // list of metadata
    292 
    293     psDlistAppend(list, xNew(1));
    294     psDlistAppend(list, xNew(2));
    295     psDlistAppend(list, xNew(3));
     354    psDlist *list = psDlistAlloc(NULL); // list of metadata
     355
     356    psDlistAppend(list, xAlloc(1));
     357    psDlistAppend(list, xAlloc(2));
     358    psDlistAppend(list, xAlloc(3));
    296359    /*
    297360     * Print that data
     
    334397    printf("\n");
    335398#endif
    336     psVoidPtrArrayDel(arr, (void (*)(void *))xDel);
     399    psVoidPtrArrayFree(arr, (void (*)(void *))xFree);
    337400#endif
    338401
     
    349412     * Clean up
    350413     */
    351     psDlistDel(list, (void (*)(void *))xDel);
     414    psDlistFree(list, (void (*)(void *))xFree);
    352415    /*
    353416     * Check for memory leaks
Note: See TracChangeset for help on using the changeset viewer.