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