IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 1794


Ignore:
Timestamp:
Sep 10, 2004, 4:34:50 PM (22 years ago)
Author:
Paul Price
Message:

Reworked psList to employ multiple iterators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/pslib/psLibSDRS.tex

    r1770 r1794  
    1 %%% $Id: psLibSDRS.tex,v 1.121 2004-09-10 00:33:54 price Exp $
     1%%% $Id: psLibSDRS.tex,v 1.122 2004-09-11 02:34:50 price Exp $
    22\documentclass[panstarrs,spec]{panstarrs}
    33
     
    14191419\label{sec:psList}
    14201420
    1421 \PS{} supports doubly linked lists through a type \code{psList}:
     1421\PS{} shall support doubly linked lists through a type \code{psList}:
    14221422%
    14231423\begin{verbatim}
     
    14261426   psListElem *head;                   ///< first element on list (may be NULL)
    14271427   psListElem *tail;                   ///< last element on list (may be NULL)
    1428    psListElem *iter;                   ///< iteration cursor
    1429    unsigned int iterIndex;             ///< the numeric position of the iteration cursor in the list
     1428   unsigned int nIter;                 ///< number of iterators
     1429   psListIterator **iter;              ///< iteration cursors
    14301430   pthread_mutex_t lock;               ///< mutex to lock a node during changes
    14311431} psList;
    14321432\end{verbatim}
    14331433%
    1434 The type \code{psList} represents the container of the list.  It has
    1435 a pointer to the first element in the linked list (\code{head}), a
     1434The type \code{psList} represents the container of the list.  It has a
     1435pointer to the first element in the linked list (\code{head}), a
    14361436pointer to the last element in the list (\code{tail}), an entry for
    14371437the current cursor location (\code{iter}), and an entry to define the
    1438 number of elements in the list (\code{n}).
     1438number of elements in the list (\code{size}).  Iteration on the list
     1439is achieved by means of an array of \code{nIter} iteration cursors,
     1440\code{iter}.  The \code{lock} should be applied during changes to the
     1441list to maintain thread safety.
    14391442
    14401443The elements of the list are defined by the type \code{psListElem}:
     
    14611464\code{psList.head} and \code{psList.tail}.  If the data entry is
    14621465\code{NULL}, then an empty list, with both pointers are set to
    1463 \code{NULL} should be created.
    1464 
    1465 The destructor function for \code{psList} must free the data
    1466 associated with the entire list.
     1466\code{NULL} should be created.  The number of iterators in the list is
     1467initially set to zero.
     1468
     1469The destructor function for \code{psList} must call \code{psFree} for
     1470all the the data associated with the list.
     1471
     1472All data items placed onto lists must have their reference counters
     1473(section \ref{secMemRefcounter}) incremented.  When elements are
     1474removed from a list, they must have their reference counters
     1475decremented.  The action of retrieving data from a list (with one of
     1476the three \code{psListGet} functions) also increments the reference
     1477counter.
     1478
     1479Iteration on the list shall be achieved by means of a list iterator
     1480type:
     1481\begin{verbatim}
     1482typedef struct {
     1483    psList *list;                      ///< List iterator works on
     1484    unsigned int num;                  ///< Iterator number
     1485    psListElem *cursor;                ///< The current iterator cursor
     1486    bool offEnd;                       ///< Is the iterator off the end?
     1487} psListIterator;
     1488\end{verbatim}
     1489The \code{psListIterator} keeps track of which list element the
     1490iterator \code{cursor} is currently pointing at.  \code{num} is the
     1491number of the list iterator in the parent \code{list}'s array of
     1492iterators, so \code{list->iter[num]} points to the list iterator.  The
     1493boolean member, \code{offEnd}, indicates whether the iterator has
     1494progressed off the end of the list (i.e., beyond the last item).
     1495
     1496The corresponding constructor shall be:
     1497\begin{verbatim}
     1498psListIterator *psListIteratorAlloc(psList *list, int location);
     1499\end{verbatim}
     1500Here, \code{list} is the \code{psList} on which the iterator will
     1501iterate, and \code{location} is the initial starting point, and may be
     1502a numerical index or it may be one of the special values:
     1503\code{PS_LIST_HEAD} or \code{PS_LIST_TAIL}, which are defined as
     1504negative integers.  Of course, the \code{psListElem} pointed to by the
     1505iterator shall have its \code{refCounter} incremented.
     1506
     1507The destructor for \code{psListIterator} shall, after freeing the
     1508\code{psListIterator} and decrementing the \code{refCounter} of the
     1509\code{psListElem} that it pointed to, also reorganise the \code{iter}
     1510array (replacing the element being removed with the last element) and
     1511resizing the array appropriately.
     1512
     1513A list \code{iterator} shall be set to a specific \code{location} on
     1514the list upon calling \code{psListIteratorSet}:
     1515\begin{verbatim}
     1516bool psListIteratorSet(psListIterator *iterator, int location);
     1517\end{verbatim}
     1518Again, the \code{location} may be a numerical index or it may be one
     1519of the special values: \code{PS_LIST_HEAD} or \code{PS_LIST_TAIL},
     1520which are defined as negative integers.  The function shall return
     1521\code{true} if the reset was successful, or \code{false} otherwise.
    14671522
    14681523\begin{verbatim}
    14691524bool psListAdd(psList *list, int location, void *data);
    1470 \end{verbatim}
    1471 Add an entry to the list with this function, which takes a pointer to
    1472 the list and returns a boolean giving the success or failure of the
    1473 operation. The value of \code{location} specifies if the specified
    1474 data item should be placed on the front of the list
    1475 (\code{PS_LIST_HEAD}), at the end of the list (\code{PS_LIST_TAIL}),
    1476 to add after (\code{PS_LIST_NEXT}) or before (\code{PS_LIST_PREV}) the
    1477 current element (specified by the iteration cursor), or an index that
    1478 the new \code{data} should inhabit.
     1525bool psListAddAfter(psListIterator *iterator, void *data);
     1526bool psListAddBefore(psListIterator *iterator, void *data);
     1527\end{verbatim}
     1528the first function, \code{psListAdd}, adds an entry to the \code{list}
     1529and returns a boolean giving the success or failure of the
     1530operation. The value of \code{location} may be a numerical index the
     1531\code{data} is to inhabit (if \code{location} is greater than the
     1532number of items on the list, then the function shall generate a
     1533warning and add the \code{data} to the tail) or it may be one of the
     1534special values: \code{PS_LIST_HEAD} or \code{PS_LIST_TAIL}, which are
     1535defined as negative integers.  The other two functions,
     1536\code{psListAddAfter} and \code{psListAddBefore} specify that the
     1537\code{data} shall be added after or before (respectively) the current
     1538cursor position of the \code{iterator}.
    14791539
    14801540\begin{verbatim}
    14811541void *psListGet(psList *list, int location);
    1482 \end{verbatim}
    1483 A data item may be retrieved from the list with this function.  The
    1484 value of \code{location} may be the numerical index or it may be one
    1485 of the special values: \code{PS_LIST_HEAD}, \code{PS_LIST_TAIL},
    1486 \code{PS_LIST_PREV}, and \code{PS_LIST_NEXT}, all of which are defined
    1487 as negative integers, allowing \code{location} to also be the index of
    1488 one of the data items.
    1489 
    1490 \begin{verbatim}
    1491 bool psListRemove(psList *list, int location, void *data);
    1492 \end{verbatim}
    1493 A data item may be removed from the list with this function.  The
    1494 value of \code{location} may be the numerical index or it may be one
    1495 of the special values: \code{PS_LIST_HEAD}, \code{PS_LIST_TAIL},
    1496 \code{PS_LIST_PREV}, \code{PS_LIST_UNKNOWN}, and \code{PS_LIST_NEXT},
    1497 all of which are defined as negative integers.  If the value of
    1498 \code{location} is \code{PS_LIST_UNKNOWN}, then the data item is
    1499 identified by matching the pointer value with \code{void *data}.  The
    1500 function returns a value of \code{true} if the operation was
    1501 successful, and \code{false} otherwise.
    1502 
    1503 All data items placed onto lists (\code{psListAdd}) must have their
    1504 reference counters (section \ref{secMemRefcounter}) incremented.  When
    1505 elements are removed from a list with \code{psListRemove}, they must
    1506 have their reference counters decremented.  The action of retrieving
    1507 data from a list (with \code{psListGet}) also increments the reference
    1508 counter.
     1542void *psListGetNext(psListIterator *iterator);
     1543void *psListGetPrevious(psListIterator *iterator);
     1544\end{verbatim}
     1545A data item may be retrieved from the list with these functions.  The
     1546first function, \code{psListGet} simply returns the value specified
     1547by its \code{location}, which may be a numerical index or it may be
     1548one of the special values: \code{PS_LIST_HEAD} or \code{PS_LIST_TAIL},
     1549which are defined as negative integers, allowing \code{location} to
     1550also be the index of one of the data items.  The other two functions,
     1551\code{psListGetNext} and \code{psListGetPrevious} return the item
     1552under the iteration cursor before advancing to the next or previous
     1553item, respectively.
     1554
     1555In the event that the iteration cursor is at the tail of the list,
     1556\code{psListGetNext} shall return the tail item and then set the
     1557\code{cursor} to \code{NULL} and \code{offEnd} to \code{true}.  In the
     1558event that the iteration cursor is at the head of the list,
     1559\code{psListGetPrevious} shall return the head item and then set the
     1560\code{cursor} to \code{NULL} (and \code{offEnd} should already be
     1561\code{false}).  In the event that the iteration \code{cursor} is
     1562\code{NULL}, \code{psListGetNext} and \code{psListGetPrevious} shall
     1563return \code{NULL}, and advance the iteration \code{cursor} only if
     1564the intended direction places the cursor back on the list; otherwise a
     1565warning shall be generated, and no change shall be made.  If
     1566\code{psListGetPrevious} was called with \code{offEnd} as \code{true},
     1567then \code{offEnd} shall also be toggled back to \code{false} to
     1568indicate that the \code{cursor} is no longer off the end of the list.
     1569When setting the \code{cursor}, the appropriate \code{refCounter}s
     1570shall be incremented and decremented.
     1571
     1572\begin{verbatim}
     1573bool psListRemove(psList *list, int location)
     1574bool psListRemoveData(psList *list, void *data);
     1575bool psListRemoveAfter(psListIterator *iterator);
     1576bool psListRemoveBefore(psListIterator *iterator);
     1577\end{verbatim}
     1578A data item may be removed from the list with these functions.  For
     1579\code{psListRemove}, the value of \code{location} may be the numerical
     1580index or it may be one of the special values: \code{PS_LIST_HEAD} or
     1581\code{PS_LIST_TAIL}, which are defined as negative integers.  For
     1582\code{psListRemoveData}, the data item to be removed is identified by
     1583matching the pointer value with \code{void *data}.  The functions
     1584return a value of \code{true} if the operation was successful, and
     1585\code{false} otherwise.  The other two functions,
     1586\code{psListRemoveBefore} and \code{psListRemoveAfter} remove the item
     1587before or after (respectively) the iteration cursor.  In all cases, if
     1588any iterators are currently pointing at the item to be removed, or the
     1589item to be removed is not on the list, an error shall be generated,
     1590the item shall not be removed, and the function shall return
     1591\code{false}.
     1592
    15091593
    15101594\begin{verbatim}
     
    15161600free the elements or destroy the input collection.  Rather, they
    15171601increment the reference counter for each of the elements.
    1518 
    1519 \begin{verbatim}
    1520 void psListSetIterator(psList *list, int iterator, int location);
    1521 void *psListGetNext(psList *list, int iterator);
    1522 void *psListGetPrevious(psList *list, int iterator);
    1523 \end{verbatim}
    1524 Iteration over all elements of the list using the iteration cursor
    1525 \code{iter} is provided by these functions.  The first of these
    1526 functions uses the value of \code{location} to set the iteration
    1527 cursor for the given list to the beginning \code{PS_LIST_HEAD} or the
    1528 end \code{PS_LIST_TAIL} for the iterator specified by \code{iterator}.
    1529 The next two functions move the iteration cursor forward or backwards,
    1530 returning the data item from the resulting list entry, or returning
    1531 \code{NULL} at the end of the list.  Explicit traversal of the list
    1532 using the \code{psListElem}s \code{prev} and \code{next} pointers is
    1533 also supported.
    15341602
    15351603\begin{verbatim}
Note: See TracChangeset for help on using the changeset viewer.