00001 /** @file psArray.h 00002 * 00003 * @brief Contains basic array definitions and operations 00004 * 00005 * This file defines the basic type for a array struct and functions useful 00006 * in manupulating arrays. 00007 * 00008 * @author Robert DeSonia, MHPCC 00009 * @author Ross Harman, MHPCC 00010 * 00011 * @version $Revision: 1.45 $ $Name: $ 00012 * @date $Date: 2007/01/23 22:47:23 $ 00013 * 00014 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 00015 */ 00016 00017 #ifndef PS_ARRAY_H 00018 #define PS_ARRAY_H 00019 00020 #include "psType.h" 00021 #include "psCompare.h" 00022 00023 /// @addtogroup DataContainer Data Containers 00024 /// @{ 00025 00026 /** An array to support primitive types. 00027 * 00028 * Struct for maintaining an array of frequently used primitive types. 00029 * 00030 */ 00031 typedef struct 00032 { 00033 long n; ///< Number of elements in use. 00034 const long nalloc; ///< Total number of elements available. 00035 psPtr* data; ///< An Array of pointer elements 00036 void *lock; ///< Optional lock for thread safety 00037 } 00038 psArray; 00039 00040 #define P_PSARRAY_SET_NALLOC(vec,n) *(long*)&vec->nalloc = n 00041 00042 /*****************************************************************************/ 00043 00044 /* FUNCTION PROTOTYPES */ 00045 00046 /*****************************************************************************/ 00047 00048 /** Checks the type of a particular pointer. 00049 * 00050 * Uses the appropriate deallocation function in psMemBlock to check the ptr datatype. 00051 * 00052 * @return bool: True if the pointer matches a psArray structure, false otherwise. 00053 */ 00054 bool psMemCheckArray( 00055 psPtr ptr ///< the pointer whose type to check 00056 ) 00057 ; 00058 00059 00060 /** Allocate an array, set the length to the number of allocated elements 00061 * 00062 * Uses psLib memory allocation functions to create an array collection of 00063 * data 00064 * 00065 * @return psArray* : Pointer to psArray. 00066 * 00067 */ 00068 psArray* psArrayAlloc( 00069 long nalloc ///< Total number of elements to make available. 00070 ); 00071 00072 /** Allocate an array, set the length to zero. 00073 * 00074 * Uses psLib memory allocation functions to create an array collection of 00075 * data 00076 * 00077 * @return psArray* : Pointer to psArray. 00078 * 00079 */ 00080 psArray* psArrayAllocEmpty( 00081 long nalloc ///< Total number of elements to make available. 00082 ); 00083 00084 /** Reallocate an array. 00085 * 00086 * Uses psLib memory allocation functions to reallocate an array collection 00087 * of data. 00088 * 00089 * @return psArray* : Pointer to psArray. 00090 * 00091 */ 00092 psArray* psArrayRealloc( 00093 psArray* array, ///< array to reallocate. 00094 long nalloc ///< Total number of elements to make available. 00095 ); 00096 00097 /** Add an element to the end the array, expanding the array storage if 00098 * necessary. 00099 * 00100 * @return psArray* The array with the element added 00101 */ 00102 psArray* psArrayAdd( 00103 psArray* array, ///< array to operate on 00104 long delta, 00105 ///< the amount to expand array, if necessary. If less than one, 10 will be used. 00106 psPtr data ///< the data pointer to add to psArray 00107 ); 00108 00109 /** Remove an element from the array by it's pointer 00110 * 00111 * Finds and removes the specified data pointer from the list. 00112 * 00113 * @return bool: TRUE if the specified data pointer was found and removed, 00114 * otherwise FALSE. 00115 * 00116 */ 00117 bool psArrayRemoveData( 00118 psArray* array, ///< array to operate on 00119 const psPtr data ///< the data pointer to remove from psArray 00120 ); 00121 00122 /** Remove an element from the array 00123 * 00124 * Finds and removes the elements as the specified position 00125 * 00126 * @return bool: TRUE if the specified data pointer was found and removed, 00127 * otherwise FALSE. 00128 * 00129 */ 00130 bool psArrayRemoveIndex( 00131 psArray* array, ///< array to operate on 00132 long index ///< the element to remove 00133 ); 00134 00135 /** Deallocate/Dereference elements of an array. 00136 * 00137 * Uses psLib memory allocation functions to deallocate/dereference elements 00138 * of a array of void pointers. The array psArr is not freed, and its elements 00139 * will all be set to NULL. Additionaly, the array size (n) is set to zero. 00140 * 00141 */ 00142 void psArrayElementsFree( 00143 psArray* array ///< Void pointer array to destroy. 00144 ); 00145 00146 /** Sort the array according to an external compare function. 00147 * 00148 * Sorts an array via the specification of a comparison function 00149 * to specify how the objects on the array should be sorted. 00150 * 00151 * The comparison function must return an integer less than, equal to, or 00152 * greater than zero if the first argument is considered to be respectively 00153 * less than, equal to, or greater than the second. 00154 * 00155 * If two members compare as equal, their order in the sorted array is 00156 * undefined. 00157 * 00158 * @return psArray* The sorted array. 00159 */ 00160 psArray* psArraySort( 00161 psArray* array, ///< input array to sort. 00162 psComparePtrFunc func ///< the compare function 00163 ); 00164 00165 /** Set an element in the array. If the current element is non-NULL, the old 00166 * element is freed. 00167 * 00168 * @return psBool TRUE if the element was set successfully, otherwise FALSE 00169 */ 00170 bool psArraySet( 00171 psArray* array, ///< input array to set element in 00172 long position, ///< the element position to set 00173 psPtr data ///< the value to set it to 00174 ); 00175 00176 /** Get an element from the array. 00177 * 00178 * @return void* the element at given position. 00179 */ 00180 psPtr psArrayGet( 00181 const psArray* array, ///< input array to get element from 00182 long position ///< the element position to get 00183 ); 00184 00185 /** Get the number of elements in use from a specified psArray. (array.n) 00186 * 00187 * @return long: The number of elements in use. 00188 */ 00189 long psArrayLength( 00190 const psArray *array ///< input psArray 00191 ); 00192 00193 00194 // Some assertions 00195 00196 #define PS_ASSERT_ARRAY_NON_NULL(NAME, RETURNVAL) PS_ASSERT_GENERAL_ARRAY_NON_NULL(NAME, return RETURNVAL) 00197 #define PS_ASSERT_GENERAL_ARRAY_NON_NULL(NAME, CLEANUP) \ 00198 if ((NAME) == NULL || (NAME)->data == NULL) { \ 00199 psError(PS_ERR_BAD_PARAMETER_NULL, true, \ 00200 "Unallowable operation: psArray %s or its data is NULL.", \ 00201 #NAME); \ 00202 CLEANUP; \ 00203 } \ 00204 00205 #define PS_ASSERT_ARRAY_NON_EMPTY(NAME, RETURNVAL) PS_ASSERT_GENERAL_ARRAY_NON_EMPTY(NAME, return RETURNVAL) 00206 #define PS_ASSERT_GENERAL_ARRAY_NON_EMPTY(NAME, CLEANUP) \ 00207 if ((NAME)->n < 1) { \ 00208 psError(PS_ERR_BAD_PARAMETER_SIZE, true, \ 00209 "Unallowable operation: psArray %s has no elements.", \ 00210 #NAME); \ 00211 CLEANUP; \ 00212 } \ 00213 00214 #define PS_ASSERT_ARRAYS_SIZE_EQUAL(ARRAY1, ARRAY2, RVAL) \ 00215 if ((ARRAY1)->n != (ARRAY2)->n) { \ 00216 psError(PS_ERR_BAD_PARAMETER_SIZE, true, \ 00217 "psArray %s has size %ld, psArray %s has size %ld.", \ 00218 #ARRAY1, (ARRAY1)->n, #ARRAY2, (ARRAY2)->n); \ 00219 return(RVAL); \ 00220 } 00221 00222 #define PS_ASSERT_ARRAY_SIZE(ARRAY, SIZE, RVAL) \ 00223 if ((ARRAY)->n != (SIZE)) { \ 00224 psError(PS_ERR_BAD_PARAMETER_SIZE, true, \ 00225 "psArray %s has size %ld instead of expected size %ld.", \ 00226 #ARRAY, (ARRAY)->n, SIZE); \ 00227 return RVAL; \ 00228 } 00229 00230 /// @} 00231 #endif // #ifndef PS_ARRAY_H
1.5.1