IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 460


Ignore:
Timestamp:
Apr 19, 2004, 3:38:34 PM (22 years ago)
Author:
desonia
Message:

cleanup of the IP3 code -- not complete implementation yet.

Location:
trunk/psLib/src/sysUtils
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sysUtils/psDList.c

    r454 r460  
    1 /*
    2  * Minimal support for doubly linked lists
    3  */
     1/** @file psDlist.h
     2 *  @brief Support for doubly linked lists
     3 *  @ingroup DataContainers
     4 *
     5 *  @author Robert Lupton, Princeton University
     6 *  @author Robert Daniel DeSonia, MHPCC
     7 *
     8 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2004-04-20 01:38:34 $
     10 *
     11 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     12 */
     13
    414#include <stdlib.h>
    515#include <stdio.h>
    616#include <assert.h>
    7 #include "psLib.h"
     17#include "psError.h"
     18#include "psAbort.h"
     19#include "psMemory.h"
     20#include "psDList.h"
     21#include "psTrace.h"
    822
    923static psDlistElem *dlistElemAlloc(void)
     
    1731}
    1832
    19 /*****************************************************************************/
    20 
    21 psDlist *psDlistAlloc(void *data) // initial data item; may be NULL
     33psDlist *psDlistAlloc(void *data)
    2234{
    2335    psDlist *list = psAlloc(sizeof(psDlist));
     
    2638    list->head = list->tail = NULL;
    2739    list->iter = NULL;
    28     if (data != NULL)
    29     {
     40    list->iterIndex = 0;
     41    if (data != NULL) {
    3042        psDlistAdd(list, data, PS_DLIST_TAIL);
    3143    }
     
    3446}
    3547
    36 void psDlistFree(psDlist *list,  // list to destroy
    37                  void (*elemFree)(void *)) // destructor for data on list
    38 {
    39     if (list == NULL)
    40     {
     48void psDlistFree(psDlist *list, void (*elemFree)(void *))
     49{
     50    if (list == NULL) {
    4151        return;
    4252    }
    4353
    44     psDlistElem *ptr = list->head;
    45     while (ptr != NULL)
    46     {
     54    for(psDlistElem *ptr = list->head; ptr != NULL; ) {
    4755        psDlistElem *next = ptr->next;
    4856
     
    6068}
    6169
    62 /*****************************************************************************/
    63 /*
    64  * Add an element to a list
    65  */
    66 psDlist *psDlistAdd(psDlist *list, // list to add to; may be NULL
    67                     void *data,  // data to add
    68                     int where)  // where to add data. PS_DLIST_HEAD,
    69 // PS_DLIST_TAIL, or an integer
     70psDlist *psDlistAdd(psDlist *list, void *data, int where)
    7071{
    7172    psDlistElem *elem = dlistElemAlloc();
    7273
    73     if (list == NULL)
    74     {
     74    if (list == NULL) {
    7575        list = psDlistAlloc(NULL);
    7676    }
    7777
    78     if (where > list->n)
    79     {  // XXX need better diagnostic here
    80         fprintf(stderr, "Invalid index %d (only %d elements)\n",
    81                 where, list->n);
    82         dlistElemFree(elem);  // cleanup
     78    if (where > list->n) {
     79        psError(__FILE__, "Invalid index %d (only %d elements in psDList).", where, list->n);
     80        dlistElemFree(elem);  // cleanup just allocated element
    8381
    8482        return list;
    8583    }
    8684
    87     if (where == PS_DLIST_HEAD)
    88     { // easy
     85    if (where == PS_DLIST_HEAD) {
    8986        elem->prev = NULL;
    9087        elem->next = list->head;
     
    10097        }
    10198        list->n++;
    102     } else if (where == PS_DLIST_TAIL)
    103     { // also easy
     99    } else if (where == PS_DLIST_TAIL) {
    104100        elem->prev = list->tail;
    105101        elem->next = NULL;
     
    115111        }
    116112        list->n++;
    117     } else
    118     {    // XXX
    119         fprintf(stderr, "Insertion into psDlists is not yet supported\n");
    120         return psDlistAdd(list, data, PS_DLIST_TAIL);
     113    } else if (where == PS_DLIST_PREVIOUS) {
     114        if (list->iter == NULL) { // if no position, just insert it in the front of the list.
     115            return psDlistAdd(list,data,PS_DLIST_HEAD);
     116        }
     117        elem->prev = list->iter->prev;
     118        elem->next = list->iter;
     119        elem->data = psMemIncrRefCounter(data);
     120
     121        list->iter->prev = elem;
     122        if (elem->prev != NULL) {
     123            elem->prev->next = elem;
     124        }
     125
     126        list->n++;
     127    } else if (where == PS_DLIST_NEXT) {
     128        if (list->iter == NULL) {
     129            return psDlistAdd(list,data,PS_DLIST_TAIL);
     130        }
     131        elem->prev = list->iter;
     132        elem->next = list->iter->next;
     133        elem->data = psMemIncrRefCounter(data);
     134
     135        list->iter->next = elem;
     136        if (elem->next != NULL) {
     137            elem->next->prev = elem;
     138        }
     139
     140        list->n++;
     141    } else if (where < 0) {
     142        /// XXX What is the best way to communicate this failure to the caller?
     143        psError(__func__,"The given insert location (%i) for psDlistAdd is invalid.",where);
     144    } else {
    121145    }
    122146
     
    156180        }
    157181
    158         psTrace("utils.dlist.remove", 5, "Failed to find 0x%x on psDlist 0x%x", data, list);
     182        psTrace(__FILE__, 5, "Failed to find 0x%x on psDlist 0x%x", data, list);
    159183
    160184        return NULL;
     
    199223
    200224        if (which < 0 || which >= list->n) { // out of bounds
    201             psError(__func__, PS_ERR_BAD_INDEX, 1, "Index %d is not in range [0..%d] for list 0x%x", which, list->n,
     225            psError(__func__,"Index %d is not in range [0..%d] for list 0x%x", which, list->n,
    202226                    list);
    203227            return NULL;
     
    229253 *
    230254 * If which is psDlist{Next,Prev} return the next/previous
    231  * data and move the iteration cursor 
    232  *
    233  * If which is psDlist{Head,Tail} initialise the iteration pointer
     255 * data and move the iteration cursor
     256 *
     257 * If which is psDlist{Head,Tail} initialize the iteration pointer
    234258 * and return the head/tail
    235259 *
     
    239263#define ITER_INIT_TAIL ((void *)2) // next iteration should return tail
    240264
    241 void *psDlistGet(
    242     const psDlist *list,  // list to retrieve element from
    243     int which)    // index of item, or PS_DLIST_NEXT,
    244 // or PS_DLIST_PREV
    245 {
    246     assert (list != NULL);
     265void *psDlistGet(const psDlist *list, int which)
     266{
     267    if (list == NULL) {
     268        psError(__func__,"Unexpected null pointer for psDlist parameter (%s:%d).",__FILE__,__LINE__);
     269        return NULL;
     270    }
    247271
    248272    switch (which) {
    249     case PS_DLIST_HEAD:  // easy
     273    case PS_DLIST_HEAD:
    250274        ((psDlist *)list)->iter = ITER_INIT_HEAD;
    251275        return (list->head == NULL) ? NULL : list->head->data;
    252     case PS_DLIST_TAIL:  // also easy
     276
     277    case PS_DLIST_TAIL:
    253278        ((psDlist *)list)->iter = ITER_INIT_TAIL;
    254279        return (list->tail == NULL) ? NULL : list->tail->data;
    255     case PS_DLIST_PREV:  // use iterator
     280
     281    case PS_DLIST_PREVIOUS:  // use iterator
    256282    case PS_DLIST_NEXT:
    257283        if (list->iter == NULL) {
     
    261287        } else if (list->iter == ITER_INIT_TAIL) {
    262288            ((psDlist *)list)->iter = list->tail;
    263         } else if (which == PS_DLIST_PREV) {
     289        } else if (which == PS_DLIST_PREVIOUS) {
    264290            ((psDlist *)list)->iter = list->iter->prev;
    265291        } else if (which == PS_DLIST_NEXT) {
    266292            ((psDlist *)list)->iter = list->iter->next;
    267293        } else {
    268             fprintf(stderr,"You cannot get here\n");
    269             abort();
     294            psAbort(__func__,"Unreachable line was reached in %s:%d?",__FILE__,__LINE__);
    270295        }
    271296
     
    276301
    277302        break;
    278     default:    // XXX
    279         fprintf(stderr,
    280                 "Retrieval from psDlists by index is not yet supported\n");
    281         return NULL;
     303    default:
     304        if (which < 0) {
     305            psError(__func__,"the position is invalid for %s",__func__);
     306            return NULL;
     307        }
     308        ((psDlist *)list)->iter = list->head;
     309        for (int i=0;i<which;i++) {}
     310
     311        if (list->iter == NULL) {
     312            return NULL;
     313        }
     314        return list->iter->data; // XXX what if data is NULL?
    282315    }
    283316}
     
    289322 * First a call to set the iteration pointer to the head of the list
    290323 */
    291 void psDlistSetIterator(psDlist *list, // the list in question
    292                         int where, // where on the list should I start?
    293                         int which) // @notused@ the desired iterator
    294 {
    295     if (where != PS_DLIST_HEAD && where != PS_DLIST_TAIL)
    296     {
    297         psError(__func__, PS_ERR_UNKNOWN, 1, "Invalid where argument: %d; assuming PS_DLIST_HEAD", where);
     324void psDlistSetIterator(psDlist *list,
     325                        int where)
     326{
     327    if (where != PS_DLIST_HEAD && where != PS_DLIST_TAIL) {
     328        psError(__func__, "Invalid where argument: %d; assuming PS_DLIST_HEAD", where);
    298329        where = PS_DLIST_HEAD;
    299330    }
     
    305336 * and now return the previous/next element of the list
    306337 */
    307 void *psDlistGetNext(psDlist *list, // the list in question
    308                      int which)  // @notused@ the desired iterator
     338void *psDlistGetNext(psDlist *list)
    309339{
    310340    return psDlistGet(list, PS_DLIST_NEXT);
    311341}
    312342
    313 void *psDlistGetPrev(psDlist *list, // the list in question
    314                      int which)  // @notused@ the desired iterator
    315 {
    316     return psDlistGet(list, PS_DLIST_PREV);
    317 }
    318 
    319 /*****************************************************************************/
     343void *psDlistGetPrevious(psDlist *list)
     344{
     345    return psDlistGet(list, PS_DLIST_PREVIOUS);
     346}
     347
     348void *psDlistGetCurrent(psDlist *list)
     349{
     350    return psDlistGet(list, PS_DLIST_CURRENT);
     351}
     352
     353#if 0
    320354/*
    321355 * Convert a psDlist to/from a psVoidPtrArray
     
    356390    return list;
    357391}
     392
     393#endif
  • trunk/psLib/src/sysUtils/psDList.h

    r455 r460  
    99 *  @author Robert Daniel DeSonia, MHPCC
    1010 *
    11  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-19 21:06:37 $
     11 *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-04-20 01:38:34 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1515 */
    1616
     17/** Special values of index into list */
     18enum {
     19    PS_DLIST_HEAD = 0,                  ///< at head
     20    PS_DLIST_TAIL = -1,                 ///< at tail
     21    PS_DLIST_UNKNOWN = -2,              ///< unknown position
     22    PS_DLIST_PREVIOUS = -3,             ///< previous element
     23    PS_DLIST_CURRENT = -4,              ///< current element
     24    PS_DLIST_NEXT = -5                  ///< next element
     25};
     26
    1727/** Doubly-linked list element */
    1828typedef struct psDlistElem
    1929{
    20     struct psDlistElem *prev;  ///< previous link in list
    21     struct psDlistElem *next;  ///< next link in list
    22     void *data;    ///< real data item
     30    struct psDlistElem *prev;           ///< previous link in list
     31    struct psDlistElem *next;           ///< next link in list
     32    void *data;                         ///< real data item
    2333}
    2434psDlistElem;
     
    2737typedef struct
    2838{
    29     int n;    ///< number of elements on list
    30     psDlistElem *head;   ///< first element on list (may be NULL)
    31     psDlistElem *tail;   ///< last element on list (may be NULL)
    32     psDlistElem *iter;   ///< iteration cursor
     39    int n;                              ///< number of elements on list
     40    psDlistElem *head;                  ///< first element on list (may be NULL)
     41    psDlistElem *tail;                  ///< last element on list (may be NULL)
     42    psDlistElem *iter;                  ///< iteration cursor
     43    int iterIndex;
    3344}
    3445psDlist;
    3546
    36 /** Special values of index into list */
    37 enum {
    38     PS_DLIST_HEAD = 0,   ///< at head
    39     PS_DLIST_TAIL = -1,   ///< at tail
    40     PS_DLIST_UNKNOWN = -2,  ///< unknown position
    41     PS_DLIST_PREV = -3,   ///< previous element
    42     PS_DLIST_NEXT = -4   ///< next element
    43 };
    44 
    45 /** Functions **************************************************************/
    46 /** \addtogroup DataGroup General Data Container Utilities
    47  *  \{
     47/** @addtogroup DataContainers General Data Container Utilities
     48 *  @{
    4849 */
    4950
    5051/** Constructor */
    51 psDlist *psDlistAlloc(void *data) ///< initial data item; may be NULL
    52 ;
     52psDlist *psDlistAlloc(
     53    void *data                      ///< initial data item; may be NULL
     54);
    5355
    5456/** Destructor */
    55 void psDlistFree(psDlist *list,  ///< list to destroy
    56                  void (*elemFree)(void *)) ///< destructor for data on list
    57 ;
    58 
    59 /**** List maintainence functions ****/
     57void psDlistFree(
     58    psDlist *list,                  ///< list to destroy
     59    void (*elemFree)(void *)        ///< destructor for data on list
     60);
    6061
    6162/** Add to list */
    62 psDlist *psDlistAdd(psDlist *list, ///< list to add to (may be NULL)
    63                     void *data,  ///< data item to add
    64                     int where)  ///< index, PS_DLIST_HEAD, or PS_DLIST_TAIL
    65 ;
     63psDlist *psDlistAdd(
     64    psDlist *list,                  ///< list to add to (may be NULL)
     65    void *data,                     ///< data item to add
     66    int where                       ///< index, PS_DLIST_HEAD, or PS_DLIST_TAIL
     67);
    6668
    6769/** Append to a list */
    68 psDlist *psDlistAppend(psDlist *list, ///< list to append to (may be NULL)
    69                        void *data) ///< data item to add
    70 ;
     70psDlist *psDlistAppend(
     71    psDlist *list,                  ///< list to append to (may be NULL)
     72    void *data                      ///< data item to add
     73);
    7174
    7275/** Remove from a list */
    73 void *psDlistRemove(psDlist *list, ///< list to remove element from
    74                     void *data,  ///< data item to remove
    75                     int which)  ///< index of item, or PS_DLIST_UNKNOWN, PS_DLIST_NEXT, PS_DLIST_PREV
    76 ;
     76void *psDlistRemove(
     77    psDlist *list,                  ///< list to remove element from
     78    void *data,                     ///< data item to remove
     79    int which                       ///< index of item, or PS_DLIST_UNKNOWN, PS_DLIST_NEXT, PS_DLIST_PREV
     80);
    7781
    7882/** Retrieve from a list */
    79 void *psDlistGet(const psDlist *list, ///< list to retrieve element from
    80                  int which)  ///< index of item, or PS_DLIST_NEXT, PS_DLIST_PREV, PS_DLIST_UNKNOWN
    81 ;
     83void *psDlistGet(
     84    const psDlist *list,            ///< list to retrieve element from
     85    int which                       ///< index of item, or PS_DLIST_NEXT, PS_DLIST_PREV, PS_DLIST_UNKNOWN
     86);
    8287
    8388/** Set the iterator */
    84 void psDlistSetIterator(psDlist *list, ///< list to retrieve element from
    85                         int where) ///< index, PS_DLIST_HEAD, or PS_DLIST_TAIL
    86 ;
     89void psDlistSetIterator(
     90    psDlist *list,                  ///< list to retrieve element from
     91    int where                       ///< index, PS_DLIST_HEAD, or PS_DLIST_TAIL
     92);
    8793
    8894/** Get next element relative to iter */
    89 void *psDlistGetNext(psDlist *list) ///< list to retrieve element from
    90 ;
     95void *psDlistGetNext(
     96    psDlist *list                   ///< list to retrieve element from
     97);
     98
     99/** Get current element at iter */
     100void *psDlistGetCurrent(
     101    psDlist *list                   ///< list to retrieve element from
     102);
    91103
    92104/** Get prev element relative to iter */
    93 void *psDlistGetPrev(psDlist *list) ///< list to retrieve element from
    94 ;
     105void *psDlistGetPrevious(
     106    psDlist *list                   ///< list to retrieve element from
     107);
    95108
    96109/** Convert doubly-linked list to an array */
    97 psVoidPtrArray *psDlistToArray(psDlist *dlist) ///< List to convert
    98 ;
     110#if 0
     111psVoidPtrArray *psDlistToArray(
     112    psDlist *dlist                  ///< List to convert
     113);
    99114
    100115/** Convert array to a doubly-linked list */
    101 psDlist *psArrayToDlist(psVoidPtrArray *arr) ///< Array to convert
    102 ;
     116psDlist *psArrayToDlist(
     117    psVoidPtrArray *arr             ///< Array to convert
     118);
     119#endif
    103120
    104 /* \} */ // End of DataGroup Functions
     121/// @} End of DataGroup Functions
    105122
    106123#endif
Note: See TracChangeset for help on using the changeset viewer.