Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

psList.h

Go to the documentation of this file.
00001 /** @file psList.h
00002  *  @brief Support for doubly linked lists
00003  *
00004  *  @author Robert Lupton, Princeton University
00005  *  @author Robert Daniel DeSonia, MHPCC
00006  *
00007  *  @ingroup LinkedList
00008  *
00009  *  @version $Revision: 1.37 $ $Name: rel12 $
00010  *  @date $Date: 2006/03/02 23:04:22 $
00011  *
00012  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
00013  */
00014 
00015 #ifndef PS_LIST_H
00016 #define PS_LIST_H
00017 
00018 #include "psCompare.h"
00019 #include "psArray.h"
00020 
00021 /** @addtogroup LinkedList
00022  *  @{
00023  */
00024 
00025 /** Special values of index into list
00026  *
00027  *  This list of possible list position values should be contiguous non-positive values ending with
00028  *  PS_LIST_UNKNOWN.  Any value less-than-or-equal-to PS_LIST_UNKNOWN is considered a undefined position.
00029  *
00030  */
00031 enum {
00032     PS_LIST_HEAD = 0,                  ///< at head
00033     PS_LIST_TAIL = -1,                 ///< at tail
00034 };
00035 
00036 /** Doubly-linked list element */
00037 typedef struct psListElem
00038 {
00039     struct psListElem* prev;           ///< previous link in list
00040     struct psListElem* next;           ///< next link in list
00041     psPtr data;                        ///< real data item
00042 }
00043 psListElem;
00044 
00045 /** The psList Linked list structure.  User should not allocate this struct
00046  *  directly; rather the psListAlloc should be used.
00047  *
00048  *  @see psListAlloc
00049  */
00050 typedef struct
00051 {
00052     long n;                            ///< number of elements on list
00053     psListElem* head;                  ///< first element on list (may be NULL)
00054     psListElem* tail;                  ///< last element on list (may be NULL)
00055     psArray* iterators;
00056     ///< array of all iterators associated with this list.  First iterator is
00057     ///< used internally to improve performance when using indexed access, all
00058     ///< others are user-level iterators created by psListIteratorAlloc.
00059     void *lock;                        ///< Optional lock for thread safety
00060 }
00061 psList;
00062 
00063 /** The psList iterator structure.  This should be allocated via
00064  *  psListIteratorAlloc and not directly.
00065  *
00066  *  The life span of a psListIterator object is ended by either a psFree
00067  *  of this structure OR psFree of the psList in which it operates on.
00068  *
00069  *  @see psListIteratorAlloc, psListIteratorSet, psListGetAndIncrement, psListGetAndDecrement
00070  */
00071 typedef struct
00072 {
00073     psList* list;                      ///< List iterator to works on
00074     psListElem* cursor;                ///< current cursor position
00075     bool offEnd;                       ///< Iterator off the end?
00076     long index;                         ///< the index number in the list
00077     bool mutable;                      ///< Is it permissible to modify the list?
00078 }
00079 psListIterator;
00080 
00081 
00082 /** Checks the type of a particular pointer.
00083  *
00084  *  Uses the appropriate deallocation function in psMemBlock to check the ptr datatype.
00085  *
00086  *  @return bool:       True if the pointer matches a psList structure, false otherwise.
00087  */
00088 bool psMemCheckList(
00089     psPtr ptr                          ///< the pointer whose type to check
00090 )
00091 ;
00092 
00093 /** Creates a psList linked list object.
00094  *
00095  *  @return psList* A new psList object.
00096  */
00097 psList* psListAlloc(
00098     psPtr data          ///< initial data item; may be NULL if an empty psList is desired
00099 );
00100 
00101 /** Creates a psListIterator object and associates it with a psList.
00102  *
00103  *  @return psListIterator* A new psListIterator object.
00104  */
00105 psListIterator* psListIteratorAlloc(
00106     psList* list,                      ///< the psList to iterate with
00107     long location,                     ///< the initial starting point.
00108     ///<  This can be a numeric index, PS_LIST_HEAD, or PS_LIST_TAIL.
00109     bool mutable                       ///< Is it permissible to modify list?
00110 );
00111 
00112 /** Set the iterator of the list to a given position.  If location is invalid the
00113  *  iterator position is not changed.
00114  *
00115  *  @return bool        TRUE if iterator successfully set, otherwise FALSE.
00116  */
00117 bool psListIteratorSet(
00118     psListIterator* iterator,          ///< list iterator
00119     long location                      ///< index number, PS_LIST_HEAD, or PS_LIST_TAIL
00120 );
00121 
00122 /** Adds an element to a psList at position given.
00123  *
00124  *  @return bool        TRUE if item was successfully added, otherwise FALSE.
00125  */
00126 bool psListAdd(
00127     psList* list,                      ///< list to add item to
00128     long location,                     ///< index, PS_LIST_HEAD, PS_LIST_TAIL, or numbered location.
00129     psPtr data                         ///< data item to add.  If NULL, list is not modified.
00130 );
00131 
00132 /** Adds an data item to a psList at position just after the list position given
00133  *
00134  *  @return bool        TRUE if item was successfully added, otherwise FALSE.
00135  */
00136 bool psListAddAfter(
00137     psListIterator* iterator,          ///< list position to add item to
00138     psPtr data                         ///< data item to add.  If NULL, list is not modified.
00139 );
00140 
00141 /** Adds an data item to a psList at position just before the list position given
00142  *
00143  *  @return bool        TRUE if item was successfully added, otherwise FALSE.
00144  */
00145 bool psListAddBefore(
00146     psListIterator* iterator,              ///< list position to add item to
00147     psPtr data                         ///< data item to add.  If NULL, list is not modified.
00148 );
00149 
00150 /** Remove an item at the specified location from a list.
00151  *
00152  *  @return bool        TRUE if element is successfully removed, otherwise FALSE.
00153  */
00154 bool psListRemove(
00155     psList* list,                      ///< list to remove element from
00156     long location                     ///< index of item
00157 );
00158 
00159 /** Remove an item from a list.
00160  *
00161  *  @return bool        TRUE if element is successfully removed, otherwise FALSE.
00162  */
00163 bool psListRemoveData(
00164     psList* list,                      ///< list to remove element from
00165     psPtr data                         ///< data item to find and remove
00166 );
00167 
00168 /** Retrieve an item from a list.
00169  *
00170  *  @return psPtr       the item corresponding to the location parameter.  If
00171  *                      location is invalid (e.g., a numbered index greater
00172  *                      than the list size or if the list is empty), a
00173  *                      NULL is returned.
00174  */
00175 psPtr psListGet(
00176     psList* list,                      ///< list to retrieve element from
00177     long location                     ///< index number, PS_LIST_HEAD, or PS_LIST_TAIL
00178 );
00179 
00180 /** Position the specified iterator to the next item in list.
00181  *
00182  *  @return psPtr       the data item at the original iterator position or NULL if the
00183  *                      iterator went past the end of the list.
00184  */
00185 psPtr psListGetAndIncrement(
00186     psListIterator* iterator           ///< iterator to move
00187 );
00188 
00189 /** Position the specified iterator to the previous item in list.
00190  *
00191  *  @return psPtr       the data item at the original iterator position or NULL if the
00192  *                      iterator went past the beginning of the list.
00193  */
00194 psPtr psListGetAndDecrement(
00195     psListIterator* iterator           ///< iterator to move
00196 );
00197 
00198 /** Convert a linked list to an array
00199  *
00200  *  @return psArray* A new psArray populated with elements from the list,
00201  *                      or NULL if the given dlist parameter is NULL.
00202  */
00203 psArray* psListToArray(
00204     const psList* list                      ///< List to convert
00205 );
00206 
00207 /** Convert array to a doubly-linked list
00208  *
00209  *  @return psList* A new psList populated with elements formt the psArray,
00210  *                      or NULL is the given arr parameter is NULL.
00211  */
00212 psList* psArrayToList(
00213     const psArray* array                       ///< vector to convert
00214 );
00215 
00216 /** Sort a list via a comparison function.
00217  *
00218  *  The comparison function must return an integer less than, equal to, or
00219  *  greater than zero if the first argument is considered to be respectively
00220  *  less than, equal to, or greater than the second.
00221  *
00222  *  If two members compare as equal, their order in the sorted array is
00223  *  undefined.
00224  *
00225  *  @return psList*     Sorted list.
00226  */
00227 psList* psListSort(
00228     psList* list,                      ///< the list to sort
00229     psComparePtrFunc func              ///< the comparison function
00230 );
00231 
00232 /** Get the number of elements in use from a specified psList. (list.n)
00233  *
00234  *  @return long:       The number of elements in use.
00235  */
00236 long psListLength(
00237     const psList *list                 ///< input psList
00238 );
00239 
00240 
00241 /// @} End of DataGroup Functions
00242 
00243 #endif // #ifndef PS_LIST_H
00244 

Generated on Mon Jul 3 14:13:44 2006 for Pan-STARRS Foundation Library by  doxygen 1.4.4