Changeset 578
- Timestamp:
- May 5, 2004, 10:43:57 AM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 10 edited
-
collections/psArray.c (modified) (4 diffs)
-
collections/psArray.h (modified) (17 diffs)
-
collections/psBitSet.c (modified) (8 diffs)
-
collections/psBitSet.h (modified) (4 diffs)
-
collections/psSort.c (modified) (8 diffs)
-
collections/psSort.h (modified) (4 diffs)
-
types/psArray.c (modified) (4 diffs)
-
types/psArray.h (modified) (17 diffs)
-
types/psBitSet.c (modified) (8 diffs)
-
types/psBitSet.h (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psArray.c
r502 r578 8 8 * @author Ross Harman, MHPCC 9 9 * 10 * @version $Revision: 1. 6$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-0 4-22 21:15:01$10 * @version $Revision: 1.7 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-05-05 20:43:22 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 48 48 /*****************************************************************************/ 49 49 50 static void* p_psArrayAlloc(ps Type *arrType, psElemType elemType, int nalloc, int elemSize)50 static void* p_psArrayAlloc(psArray *restrict psArr, psElemType elemType, int nalloc, int elemSize) 51 51 { 52 52 void *arr = NULL; 53 53 54 arrType->dimen = PS_DIMEN_VECTOR; 55 arrType->type = elemType; 54 psArr->type.dimen = PS_DIMEN_VECTOR; 55 psArr->type.type = elemType; 56 psArr->nalloc = nalloc; 57 psArr->n = 0; 56 58 57 59 if(nalloc != 0) { … … 62 64 } 63 65 64 static psVoidPtrArray *p_psVoidPtrArrayRealloc(psVoidPtrArray *restrict psArr, int nalloc) 65 { 66 if(nalloc < psArr->n) { // For decrease in psArray size 66 static void* p_psArrayRealloc(psArray *restrict psArr, void *arr, int nalloc, int elemSize) 67 { 68 // For decrease in psArray size 69 if(nalloc < psArr->n) { 67 70 psArr->n = nalloc; 68 psArr->nalloc = nalloc; 69 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(void*)); // For increase in psArray size 70 } else if(nalloc > psArr->nalloc) { 71 psArr->nalloc = nalloc; 72 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(void*)); 73 } else if(psArr == NULL) { // For creating new psArray with realloc 71 } 72 73 psArr->nalloc = nalloc; 74 75 return psRealloc(arr, nalloc*elemSize); 76 } 77 78 /*****************************************************************************/ 79 /* FUNCTION IMPLEMENTATION - PUBLIC */ 80 /*****************************************************************************/ 81 psArray* psIntArrayAlloc(int nalloc) 82 { 83 psArray *psArr = NULL; 84 psArr = psAlloc(sizeof(psArray)); 85 psArr->arr.intArr = (int*)p_psArrayAlloc(psArr, PS_TYPE_INT, nalloc, sizeof(int)); 86 return psArr; 87 } 88 89 psArray *psIntArrayRealloc(psArray *restrict psArr, int nalloc) 90 { 91 if(psArr == NULL) { // For creating new psArray with realloc 92 psArr = psIntArrayAlloc(nalloc); 93 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 94 psArr->arr.intArr = p_psArrayRealloc(psArr, psArr->arr.intArr, nalloc, sizeof(int)); 95 } 96 97 return psArr; 98 } 99 100 void psIntArrayFree(psArray *restrict psArr) 101 { 102 if (psArr == NULL) { 103 return; 104 } 105 106 psFree(psArr->arr.intArr); 107 psFree(psArr); 108 } 109 110 psArray* psFloatArrayAlloc(int nalloc) 111 { 112 psArray *psArr = NULL; 113 psArr = psAlloc(sizeof(psArray)); 114 psArr->arr.fltArr = (float*)p_psArrayAlloc(psArr, PS_TYPE_FLOAT, nalloc, sizeof(float)); 115 return psArr; 116 } 117 118 psArray *psFloatArrayRealloc(psArray *restrict psArr, int nalloc) 119 { 120 if(psArr == NULL) { // For creating new psArray with realloc 121 psArr = psFloatArrayAlloc(nalloc); 122 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 123 psArr->arr.fltArr = p_psArrayRealloc(psArr, psArr->arr.fltArr, nalloc, sizeof(float)); 124 } 125 126 return psArr; 127 } 128 129 void psFloatArrayFree(psArray *restrict psArr) 130 { 131 if (psArr == NULL) { 132 return; 133 } 134 psFree(psArr->arr.fltArr); 135 psFree(psArr); 136 } 137 138 psArray* psDoubleArrayAlloc(int nalloc) 139 { 140 psArray *psArr = NULL; 141 psArr = psAlloc(sizeof(psArray)); 142 psArr->arr.dblArr = (double*)p_psArrayAlloc(psArr, PS_TYPE_DOUBLE, nalloc, sizeof(double)); 143 return psArr; 144 } 145 146 psArray *psDoubleArrayRealloc(psArray *restrict psArr, int nalloc) 147 { 148 if(psArr == NULL) { // For creating new psArray with realloc 149 psArr = psDoubleArrayAlloc(nalloc); 150 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 151 psArr->arr.dblArr = p_psArrayRealloc(psArr, psArr->arr.dblArr, nalloc, sizeof(double)); 152 } 153 154 return psArr; 155 } 156 157 void psDoubleArrayFree(psArray *restrict psArr) 158 { 159 if (psArr == NULL) { 160 return; 161 } 162 psFree(psArr->arr.dblArr); 163 psFree(psArr); 164 } 165 166 psArray* psComplexArrayAlloc(int nalloc) 167 { 168 psArray *psArr = NULL; 169 psArr = psAlloc(sizeof(psArray)); 170 psArr->arr.cFltArr = (complex float*)p_psArrayAlloc(psArr, PS_TYPE_COMPLEX, nalloc, sizeof(complex float)); 171 return psArr; 172 } 173 174 psArray *psComplexArrayRealloc(psArray *restrict psArr, int nalloc) 175 { 176 if(psArr == NULL) { // For creating new psArray with realloc 177 psArr = psComplexArrayAlloc(nalloc); 178 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 179 psArr->arr.cFltArr = p_psArrayRealloc(psArr, psArr->arr.cFltArr, nalloc, sizeof(complex float)); 180 } 181 182 return psArr; 183 } 184 185 void psComplexArrayFree(psArray *restrict psArr) 186 { 187 if (psArr == NULL) { 188 return; 189 } 190 psFree(psArr->arr.cFltArr); 191 psFree(psArr); 192 } 193 194 psArray* psVoidPtrArrayAlloc(int nalloc) 195 { 196 psArray *psArr = NULL; 197 psArr = psAlloc(sizeof(psArray)); 198 psArr->arr.ptrArr = p_psArrayAlloc(psArr, PS_TYPE_OTHER, nalloc, sizeof(void *)); 199 return psArr; 200 } 201 202 psArray *psVoidPtrArrayRealloc(psArray *restrict psArr, int nalloc) 203 { 204 int i = 0; 205 206 for (i = nalloc; i < psArr->n; i++) { // For reduction in array size 207 psMemDecrRefCounter(psArr->arr.ptrArr[i]); 208 } 209 210 if(psArr == NULL) { // For creating new psArray with realloc 74 211 psArr = psVoidPtrArrayAlloc(nalloc); 75 } 76 77 return psArr; 78 } 79 80 static void p_psVoidPtrArrayFree(psVoidPtrArray *restrict psArr) 81 { 82 if (psArr == NULL) { 83 return; 84 } 85 psFree(psArr->arr); 86 psFree(psArr); 87 } 88 89 /*****************************************************************************/ 90 /* FUNCTION IMPLEMENTATION - PUBLIC */ 91 /*****************************************************************************/ 92 93 psIntArray* psIntArrayAlloc(int nalloc) 94 { 95 psIntArray *psArr = NULL; 96 psArr = (psIntArray*)psAlloc(sizeof(psIntArray)); 97 psArr->nalloc = nalloc; 98 psArr->n = 0; 99 psArr->arr = (int*)p_psArrayAlloc(&psArr->type, PS_TYPE_INT, nalloc, sizeof(int)); 100 101 return psArr; 102 } 103 104 psIntArray *psIntArrayRealloc(psIntArray *psArr, int nalloc) 105 { 106 if(nalloc < psArr->n) { // For decrease in psArray size 107 psArr->n = nalloc; 108 psArr->nalloc = nalloc; 109 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(int)); // For increase in psArray size 110 } else if(nalloc > psArr->nalloc) { 111 psArr->nalloc = nalloc; 112 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(int)); 113 } else if(psArr == NULL) { // For creating new psArray with realloc 114 psArr = psIntArrayAlloc(nalloc); 115 } 116 117 return psArr; 118 } 119 120 void psIntArrayFree(psIntArray *restrict psArr) 121 { 122 if (psArr == NULL) { 123 return; 124 } 125 126 psFree(psArr->arr); 127 psFree(psArr); 128 } 129 130 psFloatArray* psFloatArrayAlloc(int nalloc) 131 { 132 psFloatArray *psArr = NULL; 133 psArr = (psFloatArray*)psAlloc(sizeof(psFloatArray)); 134 psArr->nalloc = nalloc; 135 psArr->n = 0; 136 psArr->arr = (float*)p_psArrayAlloc(&psArr->type, PS_TYPE_FLOAT, nalloc, sizeof(float)); 137 138 return psArr; 139 } 140 141 psFloatArray *psFloatArrayRealloc(psFloatArray *psArr, int nalloc) 142 { 143 if(nalloc < psArr->n) { // For decrease in psArray size 144 psArr->n = nalloc; 145 psArr->nalloc = nalloc; 146 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(float)); // For increase in psArray size 147 } else if(nalloc > psArr->nalloc) { 148 psArr->nalloc = nalloc; 149 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(float)); 150 } else if(psArr == NULL) { // For creating new psArray with realloc 151 psArr = psFloatArrayAlloc(nalloc); 152 } 153 154 return psArr; 155 } 156 157 void psFloatArrayFree(psFloatArray *restrict psArr) 158 { 159 if (psArr == NULL) { 160 return; 161 } 162 psFree(psArr->arr); 163 psFree(psArr); 164 } 165 166 psDoubleArray* psDoubleArrayAlloc(int nalloc) 167 { 168 psDoubleArray *psArr = NULL; 169 psArr = (psDoubleArray*)psAlloc(sizeof(psDoubleArray)); 170 psArr->nalloc = nalloc; 171 psArr->n = 0; 172 psArr->arr = (double*)p_psArrayAlloc(&psArr->type, PS_TYPE_DOUBLE, nalloc, sizeof(double)); 173 174 return psArr; 175 } 176 177 psDoubleArray *psDoubleArrayRealloc(psDoubleArray *psArr, int nalloc) 178 { 179 if(nalloc < psArr->n) { // For decrease in psArray size 180 psArr->n = nalloc; 181 psArr->nalloc = nalloc; 182 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(double)); // For increase in psArray size 183 } else if(nalloc > psArr->nalloc) { 184 psArr->nalloc = nalloc; 185 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(double)); 186 } else if(psArr == NULL) { // For creating new psArray with realloc 187 psArr = psDoubleArrayAlloc(nalloc); 188 } 189 190 return psArr; 191 } 192 193 void psDoubleArrayFree(psDoubleArray *restrict psArr) 194 { 195 if (psArr == NULL) { 196 return; 197 } 198 psFree(psArr->arr); 199 psFree(psArr); 200 } 201 202 psComplexArray* psComplexArrayAlloc(int nalloc) 203 { 204 psComplexArray *psArr = NULL; 205 psArr = (psComplexArray*)psAlloc(sizeof(psComplexArray)); 206 psArr->nalloc = nalloc; 207 psArr->n = 0; 208 psArr->arr = (complex float*)p_psArrayAlloc(&psArr->type, PS_TYPE_COMPLEX, nalloc, sizeof(complex float)); 209 210 return psArr; 211 } 212 213 psComplexArray *psComplexArrayRealloc(psComplexArray *psArr, int nalloc) 214 { 215 if(nalloc < psArr->n) { // For decrease in psArray size 216 psArr->n = nalloc; 217 psArr->nalloc = nalloc; 218 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(complex float)); // For increase in psArray size 219 } else if(nalloc > psArr->nalloc) { 220 psArr->nalloc = nalloc; 221 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(complex float)); 222 } else if(psArr == NULL) { // For creating new psArray with realloc 223 psArr = psComplexArrayAlloc(nalloc); 224 } 225 226 return psArr; 227 } 228 229 void psComplexArrayFree(psComplexArray *restrict psArr) 230 { 231 if (psArr == NULL) { 232 return; 233 } 234 psFree(psArr->arr); 235 psFree(psArr); 236 } 237 238 psVoidPtrArray* psVoidPtrArrayAlloc(int nalloc) 239 { 240 psVoidPtrArray *psArr = NULL; 241 psArr = (psVoidPtrArray*)psAlloc(sizeof(psVoidPtrArray)); 242 psArr->nalloc = nalloc; 243 psArr->n = 0; 244 psArr->arr = (void*)p_psArrayAlloc(&psArr->type, PS_TYPE_OTHER, nalloc, sizeof(void*)); 245 246 return psArr; 247 } 248 249 psVoidPtrArray *psVoidPtrArrayRealloc(psVoidPtrArray *restrict psArr, int nalloc, void (*elemFree)(void *)) 250 { 251 int i = 0; 252 253 if(psArr == NULL) { 254 return psArr; 255 } 256 257 for (i = nalloc; i < psArr->n; i++) { 258 if(elemFree == NULL) { 259 psMemDecrRefCounter(psArr->arr[i]); 260 } else { 261 elemFree(psMemDecrRefCounter(psArr->arr[i])); 262 } 263 } 264 265 return p_psVoidPtrArrayRealloc(psArr, nalloc); 266 } 267 268 void psVoidPtrArrayFree(psVoidPtrArray *restrict psArr, void (*elemFree)(void *)) 212 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 213 psArr->arr.ptrArr = p_psArrayRealloc(psArr, psArr->arr.ptrArr, nalloc, sizeof(void *)); 214 } 215 216 return psArr; 217 } 218 219 void psVoidPtrArrayFree(psArray *restrict psArr, void (*elemFree)(void *)) 269 220 { 270 221 int i = 0; … … 276 227 for(i = 0; i < psArr->nalloc; i++) { 277 228 if(elemFree == NULL) { 278 psMemDecrRefCounter(psArr->arr [i]);229 psMemDecrRefCounter(psArr->arr.ptrArr[i]); 279 230 } else { 280 elemFree(psMemDecrRefCounter(psArr->arr [i]));231 elemFree(psMemDecrRefCounter(psArr->arr.ptrArr[i])); 281 232 } 282 233 } 283 p_psVoidPtrArrayFree(psArr); 284 } 285 234 235 // Free pointer array 236 psFree(psArr->arr.ptrArr); 237 psFree(psArr); 238 } 239 -
trunk/psLib/src/collections/psArray.h
r511 r578 8 8 * @author Ross Harman, MHPCC 9 9 * 10 * @version $Revision: 1. 7$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-0 4-23 21:33:59$10 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-05-05 20:43:57 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 75 75 psType; 76 76 77 /** An array of integers.78 * 79 * Struct for maintaining an array of integer numbers.77 /** An array of generic primitive types. 78 * 79 * Struct for maintaining an array of frequently used primitive types. 80 80 * 81 81 */ 82 82 typedef struct 83 83 { 84 psType type; ///< Type of data. 85 int nalloc; ///< Total number of elements available. 86 int n; ///< Number of elements in use. 87 int *arr; ///< Array data. 84 psType type; ///< Type of data. 85 int nalloc; ///< Total number of elements available. 86 int n; ///< Number of elements in use. 87 88 union { 89 int *intArr; ///< Integer array. 90 float *fltArr; ///< Single precision floating point array. 91 double *dblArr; ///< Double precision floating point array. 92 complex float *cFltArr; ///< Single precision floating pont complex array. 93 void **ptrArr; ///< Void pointer array. 94 }arr; ///< Union with array data. 88 95 } 89 psIntArray; 90 91 /** An array of floats. 92 * 93 * Struct for maintaining an array of single precision floating point numbers. 94 * 95 */ 96 typedef struct 97 { 98 psType type; ///< Type of data. 99 int nalloc; ///< Total number of elements available. 100 int n; ///< Number of elements in use. 101 float *arr; ///< Array data. 102 } 103 psFloatArray; 104 105 /** An array of doubles. 106 * 107 * Struct for maintaining an array of double precision floating point numbers. 108 * 109 */ 110 typedef struct 111 { 112 psType type; ///< Type of data. 113 int nalloc; ///< Total number of elements available. 114 int n; ///< Number of elements in use. 115 double *arr; ///< Array data. 116 } 117 psDoubleArray; 118 119 /** An array of complex numbers. 120 * 121 * Struct for maintaining an array of single precision floating point complex numbers. 122 * 123 */ 124 typedef struct 125 { 126 psType type; ///< Type of data. 127 int nalloc; ///< Total number of elements available. 128 int n; ///< Number of elements in use 129 complex float *arr; ///< Array data. 130 } 131 psComplexArray; 132 133 /** An array of void pointers. 134 * 135 * Struct for maintaining an array of void pointers. This struct needs a deallocation function that accepts 136 * deallocation of the array elements. 137 * 138 */ 139 typedef struct 140 { 141 psType type; ///< Type of data. 142 int nalloc; ///< Number of total elements. 143 int n; ///< Number of elements in use. 144 void **arr; ///< Aray data. 145 } 146 psVoidPtrArray; 147 96 psArray; 148 97 149 98 /*****************************************************************************/ … … 155 104 * Uses psLib memory allocation functions to create an array of integers as defined by the psIntArray struct. 156 105 * 157 */ 158 psIntArray *psIntArrayAlloc( 106 * @return psArray* Pointer to psArray. 107 * 108 */ 109 psArray *psIntArrayAlloc( 159 110 int nalloc ///< Total number of elements to make available. 160 111 ); … … 165 116 * struct. 166 117 * 167 */ 168 psIntArray *psIntArrayRealloc( 169 psIntArray *myArray, ///< Array to reallocate. 118 * @return psArray* Pointer to psArray. 119 * 120 */ 121 psArray *psIntArrayRealloc( 122 psArray *myArray, ///< Array to reallocate. 170 123 int nalloc ///< Total number of elements to make available. 171 124 ); … … 177 130 * struct. 178 131 * 132 * @return void 133 * 179 134 */ 180 135 void psIntArrayFree( 181 ps IntArray *restrict psArr ///< Array to free.136 psArray *restrict psArr ///< Array to free. 182 137 ); 183 138 … … 186 141 * Uses psLib memory allocation functions to create an array of floats as defined by the psFloatArray struct. 187 142 * 188 */ 189 psFloatArray *psFloatArrayAlloc( 143 * @return psArray* Pointer to psArray. 144 * 145 */ 146 psArray *psFloatArrayAlloc( 190 147 int nalloc ///< Total number of elements to make available. 191 148 ); … … 196 153 * struct. 197 154 * 198 */ 199 psFloatArray *psFloatArrayRealloc( 200 psFloatArray *psArr, ///< Array to reallocate. 155 * @return psArray* Pointer to psArray. 156 * 157 */ 158 psArray *psFloatArrayRealloc( 159 psArray *psArr, ///< Array to reallocate. 201 160 int nalloc ///< Total number of elements to make available. 202 161 ); … … 207 166 * struct. 208 167 * 168 * @return void 169 * 209 170 */ 210 171 void psFloatArrayFree( 211 ps FloatArray *restrict myArray ///< Array to free.172 psArray *restrict myArray ///< Array to free. 212 173 ); 213 174 … … 217 178 * struct. 218 179 * 219 */ 220 psDoubleArray *psDoubleArrayAlloc( 180 * @return psArray* Pointer to psArray. 181 * 182 */ 183 psArray *psDoubleArrayAlloc( 221 184 int nalloc ///< Total number of elements to make available. 222 185 ); … … 227 190 * struct. 228 191 * 229 */ 230 psDoubleArray *psDoubleArrayRealloc( 231 psDoubleArray *psArr, ///< Array to reallocate. 192 * @return psArray* Pointer to psArray. 193 * 194 */ 195 psArray *psDoubleArrayRealloc( 196 psArray *psArr, ///< Array to reallocate. 232 197 int nalloc ///< Total number of elements to make available. 233 198 ); … … 238 203 * struct. 239 204 * 205 * @return void 206 * 240 207 */ 241 208 void psDoubleArrayFree( 242 ps DoubleArray *restrict psArr ///< Array to free.209 psArray *restrict psArr ///< Array to free. 243 210 ); 244 211 … … 248 215 * numbers as defined by the psComplexArray struct. 249 216 * 250 */ 251 psComplexArray *psComplexArrayAlloc(int nalloc) ///< Total number of elements to make available. 217 * @return psArray* Pointer to psArray. 218 * 219 */ 220 psArray *psComplexArrayAlloc(int nalloc) ///< Total number of elements to make available. 252 221 ; 253 222 … … 257 226 * numbers as defined by the psComplexArray struct. 258 227 * 259 */ 260 psComplexArray *psComplexArrayRealloc( 261 psComplexArray *myArray, ///< Array to reallocate. 228 * @return psArray* Pointer to psArray. 229 * 230 */ 231 psArray *psComplexArrayRealloc( 232 psArray *psArr, ///< Array to reallocate. 262 233 int nalloc ///< Total number of elements to make available. 263 234 ); … … 268 239 * numbers as defined by the psComplexArray struct. 269 240 * 241 * @return void 242 * 270 243 */ 271 244 void psComplexArrayFree( 272 ps ComplexArray *restrict psArr ///< Array to free.245 psArray *restrict psArr ///< Array to free. 273 246 ); 274 247 … … 278 251 * struct. 279 252 * 280 */ 281 psVoidPtrArray *psVoidPtrArrayAlloc( 253 * @return psArray* Pointer to psArray. 254 * 255 */ 256 psArray *psVoidPtrArrayAlloc( 282 257 int nalloc ///< Number of elements to use. 283 258 ); … … 286 261 * 287 262 * Uses psLib memory allocation functions to reallocate an array of void pointers as defined by the 288 * psVoidPtrArray struct. 289 * 290 */ 291 psVoidPtrArray *psVoidPtrArrayRealloc( 292 psVoidPtrArray *restrict psArr, ///< Array to reallocate. 293 int nalloc, ///< Number of elements. 294 void (*elemFree)(void *) ///< Callback function responsible for removing array data. 263 * psVoidPtrArray struct. If the array size is increased or decreased, this function does not allocate or 264 * deallocate the contents of individual array elements. The user must do this after calling this function. 265 * 266 * @return psArray* Pointer to psArray. 267 * 268 */ 269 psArray *psVoidPtrArrayRealloc( 270 psArray *restrict psArr, ///< Void pointer array to destroy. 271 int nalloc ///< Number of elements. 295 272 ); 296 273 … … 298 275 * 299 276 * Uses psLib memory allocation functions to deallocate an array of void pointers as defined by the 300 * psVoidPtrArray struct. 277 * psVoidPtrArray struct. This function does not free the array elements unless the user provides a callback 278 * function. NULL may be passed as an alternative to supplying a callback, but the user must remember to 279 * delete the array elements afterwards. The user must not delete or deallocate the array elements prior to 280 * calling this function, as it requires valid elements for memory reference decrementation operations. 281 * 282 * @return void 301 283 * 302 284 */ 303 285 void psVoidPtrArrayFree( 304 ps VoidPtrArray *restrict psArr, ///< Array to destroy.305 void (*elemFree)(void *) ///< Callback function responsible for removing array data.286 psArray *restrict psArr, ///< Void pointer array to destroy. 287 void (*elemFree)(void *) ///< Optional callback function to remove array elements. 306 288 ); 307 289 -
trunk/psLib/src/collections/psBitSet.c
r542 r578 1 /** @file psBit Mask.c1 /** @file psBitSet.c 2 2 * 3 * @brief Creates an array of b its of arbitrary length.3 * @brief Creates an array of bytes of arbitrary length for storing individual bits. 4 4 * 5 * Bit masks are useful for turning options on and off. This module provides functions to create an array of 6 * bits of arbitrary length and manipulate them with basic binary operations. A print function is also 7 * provided to display the entire set of bits in binary form. 5 * Bit masks are useful tools for toggling various flags and options. This set of functions module provides 6 * a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary 7 * operations. A print function is also provided to display the entire set of bits in binary format as a 8 * string. 8 9 * 9 10 * @author Ross Harman, MHPCC 10 11 * 11 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-0 4-28 17:47:24$12 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-05-05 20:43:22 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 22 23 #include <ctype.h> 23 24 24 #include "psBit Mask.h"25 #include "psBitSet.h" 25 26 #include "psMemory.h" 27 #include "psError.h" 28 29 /******************************************************************************/ 30 /* DEFINE STATEMENTS */ 31 /******************************************************************************/ 32 33 // None 34 35 /******************************************************************************/ 36 /* TYPE DEFINITIONS */ 37 /******************************************************************************/ 38 39 // None 40 41 /*****************************************************************************/ 42 /* GLOBAL VARIABLES */ 43 /*****************************************************************************/ 44 45 // None 46 47 /*****************************************************************************/ 48 /* FILE STATIC VARIABLES */ 49 /*****************************************************************************/ 50 51 // None 26 52 27 53 /*****************************************************************************/ 28 54 /* FUNCTION IMPLEMENTATION - LOCAL */ 29 55 /*****************************************************************************/ 30 static char* getByte(int bit, const psBitMask *restrict inMask) 56 57 /** Private function to return a byte. 58 * 59 * Finds the byte containing the bit within the byte array. 60 * 61 * @return char*: Pointer to byte in which bit is contained. 62 */ 63 static char* getByte(int bit, const psBitSet *restrict inMask) 31 64 { 32 65 int index = 0; … … 38 71 } 39 72 73 /** Private function to create a mask. 74 * 75 * Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses 76 * zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position. 77 * 78 * @return char*: Pointer to byte in which bit is contained. 79 */ 40 80 static char mask(int bit) 41 81 { … … 49 89 /* FUNCTION IMPLEMENTATION - PUBLIC */ 50 90 /*****************************************************************************/ 51 psBit Mask* psBitMaskAlloc(int n)91 psBitSet* psBitSetAlloc(int n) 52 92 { 53 psBit Mask *newObj = psAlloc(sizeof(psBitMask));93 psBitSet *newObj = psAlloc(sizeof(psBitSet)); 54 94 newObj->n = n; 55 95 newObj->bits = psAlloc(sizeof(char)*n); … … 59 99 } 60 100 61 void psBit MaskFree(psBitMask *restrict inMask)101 void psBitSetFree(psBitSet *restrict inBitSet) 62 102 { 63 psFree(in Mask->bits);64 psFree(in Mask);103 psFree(inBitSet->bits); 104 psFree(inBitSet); 65 105 } 66 106 67 psBit Mask* psBitMaskSet(psBitMask *inMask, int bit)107 psBitSet* psBitSetSet(psBitSet *inBitSet, int bit) 68 108 { 69 char* byte = getByte(bit, in Mask);109 char* byte = getByte(bit, inBitSet); 70 110 *byte |= mask(bit); 71 111 72 return in Mask;112 return inBitSet; 73 113 } 74 114 75 int psBit MaskTest(const psBitMask *inMask, int bit)115 int psBitSetTest(const psBitSet *inBitSet, int bit) 76 116 { 77 char* byte = getByte(bit, in Mask);117 char* byte = getByte(bit, inBitSet); 78 118 if((*byte&mask(bit)) == 0) { 79 119 return 0; … … 83 123 } 84 124 85 psBit Mask* psBitMaskOp(psBitMask *outMask, const psBitMask *restrict inMask1, char *operator,86 const psBitMask *restrict inMask2)125 psBitSet* psBitSetOp(psBitSet *outBitSet, const psBitSet *restrict inBitSet1, char *operator, 126 const psBitSet *restrict inBitSet2) 87 127 { 88 128 int i = 0; … … 93 133 char *inBits2 = NULL; 94 134 95 if(out Mask== NULL) {96 p rintf("Null output psBitMask\n");97 return out Mask;135 if(outBitSet == NULL) { 136 psError(__func__, " : Line %d - Null psBitSet for outBitSet argument\n", __LINE__); 137 return outBitSet; 98 138 } 99 139 100 if(in Mask1 == NULL) {101 p rintf("Null input psBitMask1\n");102 return out Mask;140 if(inBitSet1 == NULL) { 141 psError(__func__, " : Line %d - Null psBitSet for inBitSet1 argument\n", __LINE__); 142 return outBitSet; 103 143 } 104 144 105 145 if(operator == NULL) { 106 printf("Null input operator\n"); 107 return outMask; 108 146 psError(__func__, " : Line %d - Null input operator\n", __LINE__); 147 return outBitSet; 109 148 } 110 149 111 if(in Mask2 == NULL) {112 p rintf("Null input psBitMask2\n");113 return out Mask;150 if(inBitSet2 == NULL) { 151 psError(__func__, " : Line %d - Null psBitSet for inBitSet2 argument\n", __LINE__); 152 return outBitSet; 114 153 } 115 154 116 if(in Mask1->n != inMask2->n || outMask->n != inMask1->n) {117 p rintf("Mask sizes not the same\n");118 return out Mask;155 if(inBitSet1->n != inBitSet2->n || outBitSet->n != inBitSet1->n) { 156 psError(__func__, " : Line %d - psBitSet sizes not the same\n", __LINE__); 157 return outBitSet; 119 158 } 120 159 121 n = out Mask->n;122 outBits = out Mask->bits;123 inBits1 = in Mask1->bits;124 inBits2 = in Mask2->bits;160 n = outBitSet->n; 161 outBits = outBitSet->bits; 162 inBits1 = inBitSet1->bits; 163 inBits2 = inBitSet2->bits; 125 164 126 165 tempChar = toupper(*operator); … … 142 181 break; 143 182 default: 144 p rintf("Invalid psBitMask option %s\n", operator);183 psError(__func__, " : Line %d - Invalid psBitMask binary operation: %s\n", __LINE__); 145 184 } 146 185 147 return out Mask;186 return outBitSet; 148 187 } 149 188 150 char *psBit MaskToString(const psBitMask *restrict inMask)189 char *psBitSetToString(const psBitSet *restrict inBitSet) 151 190 { 152 191 int i = 0; 153 int numBits = in Mask->n*8;154 char* outString = (char*)malloc(numBits);192 int numBits = inBitSet->n*8; 193 char* outString = psAlloc(numBits); 155 194 for(i=0; i<numBits; i++) { 156 outString[numBits-i-1] = (psBit MaskTest(inMask, i))?'1':'0';195 outString[numBits-i-1] = (psBitSetTest(inBitSet, i))?'1':'0'; 157 196 } 158 197 -
trunk/psLib/src/collections/psBitSet.h
r542 r578 1 /** @file psBit Mask.h1 /** @file psBitSet.h 2 2 * 3 * @brief Creates an array of b its of arbitrary length.3 * @brief Creates an array of bytes of arbitrary length for storing individual bits. 4 4 * 5 * Bit masks are useful for turning options on and off. This module provides functions to create an array of 6 * bits of arbitrary length and manipulate them with basic binary operations. A print function is also 7 * provided to display the entire set of bits in binary form. 5 * Bit masks are useful tools for toggling various flags and options. This set of functions module provides 6 * a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary 7 * operations. A print function is also provided to display the entire set of bits in binary format as a 8 * string. 8 9 * 9 10 * @author Ross Harman, MHPCC 10 11 * 11 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-0 4-28 17:47:56$12 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-05-05 20:43:57 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 16 */ 16 17 17 #ifndef PSBIT MASK_H18 #define PSBIT MASK_H18 #ifndef PSBITSET_H 19 #define PSBITSET_H 19 20 20 21 /******************************************************************************/ … … 22 23 /******************************************************************************/ 23 24 24 /** Struct containing array of b its and itslength.25 /** Struct containing array of bytes to hold bit data and corresponding array length. 25 26 * 26 27 * The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are 27 * arranged with the LSB in first position of the first array element.28 * arranged with the LSB in first (right most) position of the first array element. 28 29 */ 29 30 typedef struct … … 32 33 char *bits; /**< Aray of bytes holding bits */ 33 34 } 34 psBit Mask;35 psBitSet; 35 36 36 37 /*****************************************************************************/ … … 38 39 /*****************************************************************************/ 39 40 40 /** Allocate a psBit Mask.41 /** Allocate a psBitSet. 41 42 * 42 * Create a psBitMask with number of bytes specified by the user. All bits are set to zero upon allocation. 43 * Create a psBitSet with the number of bytes specified by the user. All bits are set to zero upon 44 * allocation. 43 45 * 44 * @return psBit Mask*: Pointer to struct containing array of bits and size of array.46 * @return psBitSet*: Pointer to struct containing array of bits and size of array. 45 47 */ 46 psBit Mask* psBitMaskAlloc(47 int n /**< Number of bytes in array */48 psBitSet* psBitSetAlloc( 49 int n /**< Number of bytes in psBitSet array */ 48 50 ); 49 51 50 /** Free a psBit Mask52 /** Free a psBitSet 51 53 * 52 * Deletes a psBit Mask array and its byte count.54 * Deletes a psBitSet array. 53 55 */ 54 void psBit MaskFree(55 psBit Mask *restrict inMask /**< Pointer to psBitMask struct to be deleted. */56 void psBitSetFree( 57 psBitSet *restrict inMask /**< Pointer to psBitSet to be deleted. */ 56 58 ); 57 59 58 60 /** Set a bit. 59 61 * 60 * Sets a bit at a given bit location . The bit is set based on a zero index with the first bit set in61 * the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in an array with62 * two elements would result in an psBitMaskthat looks like 00000000 00001000.62 * Sets a bit at a given bit location, either one or zero. The bit is set based on a zero index with the 63 * first bit set in the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in 64 * an array with two elements would result in an psBitSet that looks like 00000000 00001000. 63 65 * 64 * @return psBit Mask*: Pointer to struct containing array with bit set.66 * @return psBitSet*: Pointer to struct containing psBitSet. 65 67 */ 66 psBit Mask* psBitMaskSet(67 psBit Mask *inMask, /**< Pointer to struct to be set. */68 int bit /**< Bit to be set. */68 psBitSet* psBitSetSet( 69 psBitSet *restrict inMask, /**< Pointer to psBitSet to be set. */ 70 int bit /**< Bit to be set. */ 69 71 ); 70 72 71 73 /** Test the value of a bit. 72 74 * 73 * Prints the value of a bit at a given bit location. The bit printed based on a zero index with the first 74 * bit set in the zero bit slot of the zero element of the byte array. As an example, testing bit 3 in an array with 75 * two elements that looks like 00000000 00001000 would retrun a value of one, since that value was already set. 75 * Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a 76 * zero index format with the first bit set in the zero bit slot of the zero element of the byte array 77 * As an example, testing bit 3 in a psBitSet with two bytes that looks like 00000000 00001000 would return a 78 * value of one, since that is the value that was set. 76 79 * 77 80 * @return int: Value of bit, either one or zero. 78 81 */ 79 int psBit MaskTest(80 const psBit Mask *inMask, /**< Pointer to struct to be tested. */81 int bit /**< Bit to be tested. */82 int psBitSetTest( 83 const psBitSet *restrict inMask, /**< Pointer psBitSet to be tested. */ 84 int bit /**< Bit to be tested. */ 82 85 ); 83 86 84 /** Perform a binary operation on two psBit Masks87 /** Perform a binary operation on two psBitSets 85 88 * 86 * Perform an AND, OR, or XOR on two psBit Masks. If the BitMasks are not the same size, the operation will not87 * be performed and an error message will be printed.89 * Perform an AND, OR, or XOR on two psBitSets. If the BitMasks are not the same size, the operation will not 90 * be performed and an error message will be logged. 88 91 * 89 * @return psBit Mask*: Pointer to struct containing result of binary operation.92 * @return psBitSet*: Pointer to struct containing result of binary operation. 90 93 */ 91 psBit Mask* psBitMaskOp(92 psBit Mask *outMask, /**< Resulting psBitMaskfrom binary operation */93 const psBit Mask *restrict inMask1, /**< First psBitMaskon which to operate */94 char *operator, /**< Bit operation */95 const psBit Mask *restrict inMask2 /**< First psBitMaskon which to operate */94 psBitSet* psBitSetOp( 95 psBitSet *restrict outMask, /**< Resulting psBitSet from binary operation */ 96 const psBitSet *restrict inMask1, /**< First psBitSet on which to operate */ 97 char *operator, /**< Bit operation */ 98 const psBitSet *restrict inMask2 /**< First psBitSet on which to operate */ 96 99 ); 97 100 98 /** Print the contents of a psBitMask.101 /** Convert the psBitSet to a string of ones and zeros. 99 102 * 100 * Prints the contents of a psBitMask in its binary form of ones and zeros. The LSB is the left-most chracter. 103 * Converts the contents of a psBitSet to a string representation of its binary form of ones and zeros. The 104 * LSB is the right-most chracter. Each set of eight characters represents one byte. 101 105 * 102 * @return char*: Pointer to character array containing binary formatteddata.106 * @return char*: Pointer to character array containing string data. 103 107 */ 104 char *psBitMaskToString( 105 const psBitMask *restrict inMask /**< psBitMask to print */ 106 ); 107 108 /** Private function to return a byte. 109 * 110 * Finds the byte containing the bit within the byte array. 111 * 112 * @return char*: Pointer to byte in which bit is contained. 113 */ 114 static char* getByte( 115 int bit, /**< Bit to index to search. */ 116 const psBitMask *restrict inMask /**< psBitMask to search. */ 117 ); 118 119 /** Private function to create a mask. 120 * 121 * Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses 122 * zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position. 123 * 124 * @return char*: Pointer to byte in which bit is contained. 125 */ 126 static char mask( 127 int bit /**< Bit to set within mask */ 108 char *psBitSetToString( 109 const psBitSet *restrict inMask /**< psBitSet to convert */ 128 110 ); 129 111 -
trunk/psLib/src/collections/psSort.c
r486 r578 14 14 * @author Ross Harman, MHPCC 15 15 * 16 * @version $Revision: 1. 5$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-0 4-21 00:40:32 $16 * @version $Revision: 1.6 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-05-05 20:43:22 $ 18 18 * 19 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 104 104 /*****************************************************************************/ 105 105 106 ps FloatArray *psSort(107 ps FloatArray *restrict outArray, /**< Sorted output array. */108 const ps FloatArray *restrict inArray /**< Input array to sort. */106 psArray *psSort( 107 psArray *restrict outArray, /**< Sorted output array. */ 108 const psArray *restrict inArray /**< Input array to sort. */ 109 109 ) 110 110 { … … 129 129 inN = inArray->n; 130 130 outN = outArray->n; 131 inArr = inArray->arr ;132 outArr = outArray->arr ;131 inArr = inArray->arr.fltArr; 132 outArr = outArray->arr.fltArr; 133 133 elSize = sizeof(float); 134 134 … … 139 139 } 140 140 141 if(inN == 0) { 142 psError(__func__, " : Line %d - No elements in use for input array\n", __LINE__); 143 return outArray; 144 } 145 146 if(outN == 0) { 147 psError(__func__, " : Line %d - No elements in use for output array\n", __LINE__); 148 return outArray; 149 } 150 141 151 // Copy input array values into output array 142 152 for(i=0; i<inN; i++) { … … 150 160 } 151 161 152 ps IntArray *psSortIndex(153 ps IntArray *restrict outArray,/**< Output array of sorted indices. */154 const ps FloatArray *restrict inArray/**< Input array to be sorted. */162 psArray *psSortIndex( 163 psArray *restrict outArray, /**< Output array of sorted indices. */ 164 const psArray *restrict inArray /**< Input array to be sorted. */ 155 165 ) 156 166 { … … 163 173 int *outArr = NULL; 164 174 float diff = 0.0f; 165 ps FloatArray *tmpFloatArray = NULL;175 psArray *tmpFloatArray = NULL; 166 176 167 177 if(outArray == NULL) { … … 178 188 inN = inArray->n; 179 189 outN = outArray->n; 180 inArr = inArray->arr ;181 outArr = outArray->arr ;190 inArr = inArray->arr.fltArr; 191 outArr = outArray->arr.intArr; 182 192 183 193 if(inN != outN) { … … 188 198 189 199 tmpFloatArray = psFloatArrayAlloc(inN); 200 tmpFloatArray->n = inN; 190 201 tmpFloatArray = psSort(tmpFloatArray, inArray); 191 202 192 203 for(i=0; i<inN; i++) { 193 tempVal = tmpFloatArray->arr [i];204 tempVal = tmpFloatArray->arr.fltArr[i]; 194 205 for(j=0; j<inN; j++) { 195 206 diff = fabsf(tempVal - inArr[j]); -
trunk/psLib/src/collections/psSort.h
r450 r578 14 14 * @author Ross Harman, MHPCC 15 15 * 16 * @version $Revision: 1. 2$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-0 4-19 20:10:46$16 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-05-05 20:43:57 $ 18 18 * 19 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 20 20 */ 21 21 22 22 #ifndef PSSORT_H 23 23 #define PSSORT_H … … 38 38 /* FUNCTION PROTOTYPES */ 39 39 /*****************************************************************************/ 40 40 41 41 /** Sort an array of floats. 42 42 * … … 46 46 */ 47 47 48 ps FloatArray *psSort(psFloatArray *restrict out, const psFloatArray *restrict inArray);48 psArray *psSort(psArray *restrict out, const psArray *restrict inArray); 49 49 50 50 /** Creates an array of indices based on sort odred of float array. … … 56 56 */ 57 57 58 ps IntArray *psSortIndex(psIntArray *restrict out, const psFloatArray *restrict inArray);58 psArray *psSortIndex(psArray *restrict out, const psArray *restrict inArray); 59 59 60 60 #endif -
trunk/psLib/src/types/psArray.c
r502 r578 8 8 * @author Ross Harman, MHPCC 9 9 * 10 * @version $Revision: 1. 6$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-0 4-22 21:15:01$10 * @version $Revision: 1.7 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-05-05 20:43:22 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 48 48 /*****************************************************************************/ 49 49 50 static void* p_psArrayAlloc(ps Type *arrType, psElemType elemType, int nalloc, int elemSize)50 static void* p_psArrayAlloc(psArray *restrict psArr, psElemType elemType, int nalloc, int elemSize) 51 51 { 52 52 void *arr = NULL; 53 53 54 arrType->dimen = PS_DIMEN_VECTOR; 55 arrType->type = elemType; 54 psArr->type.dimen = PS_DIMEN_VECTOR; 55 psArr->type.type = elemType; 56 psArr->nalloc = nalloc; 57 psArr->n = 0; 56 58 57 59 if(nalloc != 0) { … … 62 64 } 63 65 64 static psVoidPtrArray *p_psVoidPtrArrayRealloc(psVoidPtrArray *restrict psArr, int nalloc) 65 { 66 if(nalloc < psArr->n) { // For decrease in psArray size 66 static void* p_psArrayRealloc(psArray *restrict psArr, void *arr, int nalloc, int elemSize) 67 { 68 // For decrease in psArray size 69 if(nalloc < psArr->n) { 67 70 psArr->n = nalloc; 68 psArr->nalloc = nalloc; 69 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(void*)); // For increase in psArray size 70 } else if(nalloc > psArr->nalloc) { 71 psArr->nalloc = nalloc; 72 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(void*)); 73 } else if(psArr == NULL) { // For creating new psArray with realloc 71 } 72 73 psArr->nalloc = nalloc; 74 75 return psRealloc(arr, nalloc*elemSize); 76 } 77 78 /*****************************************************************************/ 79 /* FUNCTION IMPLEMENTATION - PUBLIC */ 80 /*****************************************************************************/ 81 psArray* psIntArrayAlloc(int nalloc) 82 { 83 psArray *psArr = NULL; 84 psArr = psAlloc(sizeof(psArray)); 85 psArr->arr.intArr = (int*)p_psArrayAlloc(psArr, PS_TYPE_INT, nalloc, sizeof(int)); 86 return psArr; 87 } 88 89 psArray *psIntArrayRealloc(psArray *restrict psArr, int nalloc) 90 { 91 if(psArr == NULL) { // For creating new psArray with realloc 92 psArr = psIntArrayAlloc(nalloc); 93 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 94 psArr->arr.intArr = p_psArrayRealloc(psArr, psArr->arr.intArr, nalloc, sizeof(int)); 95 } 96 97 return psArr; 98 } 99 100 void psIntArrayFree(psArray *restrict psArr) 101 { 102 if (psArr == NULL) { 103 return; 104 } 105 106 psFree(psArr->arr.intArr); 107 psFree(psArr); 108 } 109 110 psArray* psFloatArrayAlloc(int nalloc) 111 { 112 psArray *psArr = NULL; 113 psArr = psAlloc(sizeof(psArray)); 114 psArr->arr.fltArr = (float*)p_psArrayAlloc(psArr, PS_TYPE_FLOAT, nalloc, sizeof(float)); 115 return psArr; 116 } 117 118 psArray *psFloatArrayRealloc(psArray *restrict psArr, int nalloc) 119 { 120 if(psArr == NULL) { // For creating new psArray with realloc 121 psArr = psFloatArrayAlloc(nalloc); 122 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 123 psArr->arr.fltArr = p_psArrayRealloc(psArr, psArr->arr.fltArr, nalloc, sizeof(float)); 124 } 125 126 return psArr; 127 } 128 129 void psFloatArrayFree(psArray *restrict psArr) 130 { 131 if (psArr == NULL) { 132 return; 133 } 134 psFree(psArr->arr.fltArr); 135 psFree(psArr); 136 } 137 138 psArray* psDoubleArrayAlloc(int nalloc) 139 { 140 psArray *psArr = NULL; 141 psArr = psAlloc(sizeof(psArray)); 142 psArr->arr.dblArr = (double*)p_psArrayAlloc(psArr, PS_TYPE_DOUBLE, nalloc, sizeof(double)); 143 return psArr; 144 } 145 146 psArray *psDoubleArrayRealloc(psArray *restrict psArr, int nalloc) 147 { 148 if(psArr == NULL) { // For creating new psArray with realloc 149 psArr = psDoubleArrayAlloc(nalloc); 150 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 151 psArr->arr.dblArr = p_psArrayRealloc(psArr, psArr->arr.dblArr, nalloc, sizeof(double)); 152 } 153 154 return psArr; 155 } 156 157 void psDoubleArrayFree(psArray *restrict psArr) 158 { 159 if (psArr == NULL) { 160 return; 161 } 162 psFree(psArr->arr.dblArr); 163 psFree(psArr); 164 } 165 166 psArray* psComplexArrayAlloc(int nalloc) 167 { 168 psArray *psArr = NULL; 169 psArr = psAlloc(sizeof(psArray)); 170 psArr->arr.cFltArr = (complex float*)p_psArrayAlloc(psArr, PS_TYPE_COMPLEX, nalloc, sizeof(complex float)); 171 return psArr; 172 } 173 174 psArray *psComplexArrayRealloc(psArray *restrict psArr, int nalloc) 175 { 176 if(psArr == NULL) { // For creating new psArray with realloc 177 psArr = psComplexArrayAlloc(nalloc); 178 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 179 psArr->arr.cFltArr = p_psArrayRealloc(psArr, psArr->arr.cFltArr, nalloc, sizeof(complex float)); 180 } 181 182 return psArr; 183 } 184 185 void psComplexArrayFree(psArray *restrict psArr) 186 { 187 if (psArr == NULL) { 188 return; 189 } 190 psFree(psArr->arr.cFltArr); 191 psFree(psArr); 192 } 193 194 psArray* psVoidPtrArrayAlloc(int nalloc) 195 { 196 psArray *psArr = NULL; 197 psArr = psAlloc(sizeof(psArray)); 198 psArr->arr.ptrArr = p_psArrayAlloc(psArr, PS_TYPE_OTHER, nalloc, sizeof(void *)); 199 return psArr; 200 } 201 202 psArray *psVoidPtrArrayRealloc(psArray *restrict psArr, int nalloc) 203 { 204 int i = 0; 205 206 for (i = nalloc; i < psArr->n; i++) { // For reduction in array size 207 psMemDecrRefCounter(psArr->arr.ptrArr[i]); 208 } 209 210 if(psArr == NULL) { // For creating new psArray with realloc 74 211 psArr = psVoidPtrArrayAlloc(nalloc); 75 } 76 77 return psArr; 78 } 79 80 static void p_psVoidPtrArrayFree(psVoidPtrArray *restrict psArr) 81 { 82 if (psArr == NULL) { 83 return; 84 } 85 psFree(psArr->arr); 86 psFree(psArr); 87 } 88 89 /*****************************************************************************/ 90 /* FUNCTION IMPLEMENTATION - PUBLIC */ 91 /*****************************************************************************/ 92 93 psIntArray* psIntArrayAlloc(int nalloc) 94 { 95 psIntArray *psArr = NULL; 96 psArr = (psIntArray*)psAlloc(sizeof(psIntArray)); 97 psArr->nalloc = nalloc; 98 psArr->n = 0; 99 psArr->arr = (int*)p_psArrayAlloc(&psArr->type, PS_TYPE_INT, nalloc, sizeof(int)); 100 101 return psArr; 102 } 103 104 psIntArray *psIntArrayRealloc(psIntArray *psArr, int nalloc) 105 { 106 if(nalloc < psArr->n) { // For decrease in psArray size 107 psArr->n = nalloc; 108 psArr->nalloc = nalloc; 109 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(int)); // For increase in psArray size 110 } else if(nalloc > psArr->nalloc) { 111 psArr->nalloc = nalloc; 112 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(int)); 113 } else if(psArr == NULL) { // For creating new psArray with realloc 114 psArr = psIntArrayAlloc(nalloc); 115 } 116 117 return psArr; 118 } 119 120 void psIntArrayFree(psIntArray *restrict psArr) 121 { 122 if (psArr == NULL) { 123 return; 124 } 125 126 psFree(psArr->arr); 127 psFree(psArr); 128 } 129 130 psFloatArray* psFloatArrayAlloc(int nalloc) 131 { 132 psFloatArray *psArr = NULL; 133 psArr = (psFloatArray*)psAlloc(sizeof(psFloatArray)); 134 psArr->nalloc = nalloc; 135 psArr->n = 0; 136 psArr->arr = (float*)p_psArrayAlloc(&psArr->type, PS_TYPE_FLOAT, nalloc, sizeof(float)); 137 138 return psArr; 139 } 140 141 psFloatArray *psFloatArrayRealloc(psFloatArray *psArr, int nalloc) 142 { 143 if(nalloc < psArr->n) { // For decrease in psArray size 144 psArr->n = nalloc; 145 psArr->nalloc = nalloc; 146 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(float)); // For increase in psArray size 147 } else if(nalloc > psArr->nalloc) { 148 psArr->nalloc = nalloc; 149 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(float)); 150 } else if(psArr == NULL) { // For creating new psArray with realloc 151 psArr = psFloatArrayAlloc(nalloc); 152 } 153 154 return psArr; 155 } 156 157 void psFloatArrayFree(psFloatArray *restrict psArr) 158 { 159 if (psArr == NULL) { 160 return; 161 } 162 psFree(psArr->arr); 163 psFree(psArr); 164 } 165 166 psDoubleArray* psDoubleArrayAlloc(int nalloc) 167 { 168 psDoubleArray *psArr = NULL; 169 psArr = (psDoubleArray*)psAlloc(sizeof(psDoubleArray)); 170 psArr->nalloc = nalloc; 171 psArr->n = 0; 172 psArr->arr = (double*)p_psArrayAlloc(&psArr->type, PS_TYPE_DOUBLE, nalloc, sizeof(double)); 173 174 return psArr; 175 } 176 177 psDoubleArray *psDoubleArrayRealloc(psDoubleArray *psArr, int nalloc) 178 { 179 if(nalloc < psArr->n) { // For decrease in psArray size 180 psArr->n = nalloc; 181 psArr->nalloc = nalloc; 182 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(double)); // For increase in psArray size 183 } else if(nalloc > psArr->nalloc) { 184 psArr->nalloc = nalloc; 185 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(double)); 186 } else if(psArr == NULL) { // For creating new psArray with realloc 187 psArr = psDoubleArrayAlloc(nalloc); 188 } 189 190 return psArr; 191 } 192 193 void psDoubleArrayFree(psDoubleArray *restrict psArr) 194 { 195 if (psArr == NULL) { 196 return; 197 } 198 psFree(psArr->arr); 199 psFree(psArr); 200 } 201 202 psComplexArray* psComplexArrayAlloc(int nalloc) 203 { 204 psComplexArray *psArr = NULL; 205 psArr = (psComplexArray*)psAlloc(sizeof(psComplexArray)); 206 psArr->nalloc = nalloc; 207 psArr->n = 0; 208 psArr->arr = (complex float*)p_psArrayAlloc(&psArr->type, PS_TYPE_COMPLEX, nalloc, sizeof(complex float)); 209 210 return psArr; 211 } 212 213 psComplexArray *psComplexArrayRealloc(psComplexArray *psArr, int nalloc) 214 { 215 if(nalloc < psArr->n) { // For decrease in psArray size 216 psArr->n = nalloc; 217 psArr->nalloc = nalloc; 218 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(complex float)); // For increase in psArray size 219 } else if(nalloc > psArr->nalloc) { 220 psArr->nalloc = nalloc; 221 psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(complex float)); 222 } else if(psArr == NULL) { // For creating new psArray with realloc 223 psArr = psComplexArrayAlloc(nalloc); 224 } 225 226 return psArr; 227 } 228 229 void psComplexArrayFree(psComplexArray *restrict psArr) 230 { 231 if (psArr == NULL) { 232 return; 233 } 234 psFree(psArr->arr); 235 psFree(psArr); 236 } 237 238 psVoidPtrArray* psVoidPtrArrayAlloc(int nalloc) 239 { 240 psVoidPtrArray *psArr = NULL; 241 psArr = (psVoidPtrArray*)psAlloc(sizeof(psVoidPtrArray)); 242 psArr->nalloc = nalloc; 243 psArr->n = 0; 244 psArr->arr = (void*)p_psArrayAlloc(&psArr->type, PS_TYPE_OTHER, nalloc, sizeof(void*)); 245 246 return psArr; 247 } 248 249 psVoidPtrArray *psVoidPtrArrayRealloc(psVoidPtrArray *restrict psArr, int nalloc, void (*elemFree)(void *)) 250 { 251 int i = 0; 252 253 if(psArr == NULL) { 254 return psArr; 255 } 256 257 for (i = nalloc; i < psArr->n; i++) { 258 if(elemFree == NULL) { 259 psMemDecrRefCounter(psArr->arr[i]); 260 } else { 261 elemFree(psMemDecrRefCounter(psArr->arr[i])); 262 } 263 } 264 265 return p_psVoidPtrArrayRealloc(psArr, nalloc); 266 } 267 268 void psVoidPtrArrayFree(psVoidPtrArray *restrict psArr, void (*elemFree)(void *)) 212 } else if(psArr->nalloc != nalloc) { // No need to realloc to same size 213 psArr->arr.ptrArr = p_psArrayRealloc(psArr, psArr->arr.ptrArr, nalloc, sizeof(void *)); 214 } 215 216 return psArr; 217 } 218 219 void psVoidPtrArrayFree(psArray *restrict psArr, void (*elemFree)(void *)) 269 220 { 270 221 int i = 0; … … 276 227 for(i = 0; i < psArr->nalloc; i++) { 277 228 if(elemFree == NULL) { 278 psMemDecrRefCounter(psArr->arr [i]);229 psMemDecrRefCounter(psArr->arr.ptrArr[i]); 279 230 } else { 280 elemFree(psMemDecrRefCounter(psArr->arr [i]));231 elemFree(psMemDecrRefCounter(psArr->arr.ptrArr[i])); 281 232 } 282 233 } 283 p_psVoidPtrArrayFree(psArr); 284 } 285 234 235 // Free pointer array 236 psFree(psArr->arr.ptrArr); 237 psFree(psArr); 238 } 239 -
trunk/psLib/src/types/psArray.h
r511 r578 8 8 * @author Ross Harman, MHPCC 9 9 * 10 * @version $Revision: 1. 7$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-0 4-23 21:33:59$10 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-05-05 20:43:57 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 75 75 psType; 76 76 77 /** An array of integers.78 * 79 * Struct for maintaining an array of integer numbers.77 /** An array of generic primitive types. 78 * 79 * Struct for maintaining an array of frequently used primitive types. 80 80 * 81 81 */ 82 82 typedef struct 83 83 { 84 psType type; ///< Type of data. 85 int nalloc; ///< Total number of elements available. 86 int n; ///< Number of elements in use. 87 int *arr; ///< Array data. 84 psType type; ///< Type of data. 85 int nalloc; ///< Total number of elements available. 86 int n; ///< Number of elements in use. 87 88 union { 89 int *intArr; ///< Integer array. 90 float *fltArr; ///< Single precision floating point array. 91 double *dblArr; ///< Double precision floating point array. 92 complex float *cFltArr; ///< Single precision floating pont complex array. 93 void **ptrArr; ///< Void pointer array. 94 }arr; ///< Union with array data. 88 95 } 89 psIntArray; 90 91 /** An array of floats. 92 * 93 * Struct for maintaining an array of single precision floating point numbers. 94 * 95 */ 96 typedef struct 97 { 98 psType type; ///< Type of data. 99 int nalloc; ///< Total number of elements available. 100 int n; ///< Number of elements in use. 101 float *arr; ///< Array data. 102 } 103 psFloatArray; 104 105 /** An array of doubles. 106 * 107 * Struct for maintaining an array of double precision floating point numbers. 108 * 109 */ 110 typedef struct 111 { 112 psType type; ///< Type of data. 113 int nalloc; ///< Total number of elements available. 114 int n; ///< Number of elements in use. 115 double *arr; ///< Array data. 116 } 117 psDoubleArray; 118 119 /** An array of complex numbers. 120 * 121 * Struct for maintaining an array of single precision floating point complex numbers. 122 * 123 */ 124 typedef struct 125 { 126 psType type; ///< Type of data. 127 int nalloc; ///< Total number of elements available. 128 int n; ///< Number of elements in use 129 complex float *arr; ///< Array data. 130 } 131 psComplexArray; 132 133 /** An array of void pointers. 134 * 135 * Struct for maintaining an array of void pointers. This struct needs a deallocation function that accepts 136 * deallocation of the array elements. 137 * 138 */ 139 typedef struct 140 { 141 psType type; ///< Type of data. 142 int nalloc; ///< Number of total elements. 143 int n; ///< Number of elements in use. 144 void **arr; ///< Aray data. 145 } 146 psVoidPtrArray; 147 96 psArray; 148 97 149 98 /*****************************************************************************/ … … 155 104 * Uses psLib memory allocation functions to create an array of integers as defined by the psIntArray struct. 156 105 * 157 */ 158 psIntArray *psIntArrayAlloc( 106 * @return psArray* Pointer to psArray. 107 * 108 */ 109 psArray *psIntArrayAlloc( 159 110 int nalloc ///< Total number of elements to make available. 160 111 ); … … 165 116 * struct. 166 117 * 167 */ 168 psIntArray *psIntArrayRealloc( 169 psIntArray *myArray, ///< Array to reallocate. 118 * @return psArray* Pointer to psArray. 119 * 120 */ 121 psArray *psIntArrayRealloc( 122 psArray *myArray, ///< Array to reallocate. 170 123 int nalloc ///< Total number of elements to make available. 171 124 ); … … 177 130 * struct. 178 131 * 132 * @return void 133 * 179 134 */ 180 135 void psIntArrayFree( 181 ps IntArray *restrict psArr ///< Array to free.136 psArray *restrict psArr ///< Array to free. 182 137 ); 183 138 … … 186 141 * Uses psLib memory allocation functions to create an array of floats as defined by the psFloatArray struct. 187 142 * 188 */ 189 psFloatArray *psFloatArrayAlloc( 143 * @return psArray* Pointer to psArray. 144 * 145 */ 146 psArray *psFloatArrayAlloc( 190 147 int nalloc ///< Total number of elements to make available. 191 148 ); … … 196 153 * struct. 197 154 * 198 */ 199 psFloatArray *psFloatArrayRealloc( 200 psFloatArray *psArr, ///< Array to reallocate. 155 * @return psArray* Pointer to psArray. 156 * 157 */ 158 psArray *psFloatArrayRealloc( 159 psArray *psArr, ///< Array to reallocate. 201 160 int nalloc ///< Total number of elements to make available. 202 161 ); … … 207 166 * struct. 208 167 * 168 * @return void 169 * 209 170 */ 210 171 void psFloatArrayFree( 211 ps FloatArray *restrict myArray ///< Array to free.172 psArray *restrict myArray ///< Array to free. 212 173 ); 213 174 … … 217 178 * struct. 218 179 * 219 */ 220 psDoubleArray *psDoubleArrayAlloc( 180 * @return psArray* Pointer to psArray. 181 * 182 */ 183 psArray *psDoubleArrayAlloc( 221 184 int nalloc ///< Total number of elements to make available. 222 185 ); … … 227 190 * struct. 228 191 * 229 */ 230 psDoubleArray *psDoubleArrayRealloc( 231 psDoubleArray *psArr, ///< Array to reallocate. 192 * @return psArray* Pointer to psArray. 193 * 194 */ 195 psArray *psDoubleArrayRealloc( 196 psArray *psArr, ///< Array to reallocate. 232 197 int nalloc ///< Total number of elements to make available. 233 198 ); … … 238 203 * struct. 239 204 * 205 * @return void 206 * 240 207 */ 241 208 void psDoubleArrayFree( 242 ps DoubleArray *restrict psArr ///< Array to free.209 psArray *restrict psArr ///< Array to free. 243 210 ); 244 211 … … 248 215 * numbers as defined by the psComplexArray struct. 249 216 * 250 */ 251 psComplexArray *psComplexArrayAlloc(int nalloc) ///< Total number of elements to make available. 217 * @return psArray* Pointer to psArray. 218 * 219 */ 220 psArray *psComplexArrayAlloc(int nalloc) ///< Total number of elements to make available. 252 221 ; 253 222 … … 257 226 * numbers as defined by the psComplexArray struct. 258 227 * 259 */ 260 psComplexArray *psComplexArrayRealloc( 261 psComplexArray *myArray, ///< Array to reallocate. 228 * @return psArray* Pointer to psArray. 229 * 230 */ 231 psArray *psComplexArrayRealloc( 232 psArray *psArr, ///< Array to reallocate. 262 233 int nalloc ///< Total number of elements to make available. 263 234 ); … … 268 239 * numbers as defined by the psComplexArray struct. 269 240 * 241 * @return void 242 * 270 243 */ 271 244 void psComplexArrayFree( 272 ps ComplexArray *restrict psArr ///< Array to free.245 psArray *restrict psArr ///< Array to free. 273 246 ); 274 247 … … 278 251 * struct. 279 252 * 280 */ 281 psVoidPtrArray *psVoidPtrArrayAlloc( 253 * @return psArray* Pointer to psArray. 254 * 255 */ 256 psArray *psVoidPtrArrayAlloc( 282 257 int nalloc ///< Number of elements to use. 283 258 ); … … 286 261 * 287 262 * Uses psLib memory allocation functions to reallocate an array of void pointers as defined by the 288 * psVoidPtrArray struct. 289 * 290 */ 291 psVoidPtrArray *psVoidPtrArrayRealloc( 292 psVoidPtrArray *restrict psArr, ///< Array to reallocate. 293 int nalloc, ///< Number of elements. 294 void (*elemFree)(void *) ///< Callback function responsible for removing array data. 263 * psVoidPtrArray struct. If the array size is increased or decreased, this function does not allocate or 264 * deallocate the contents of individual array elements. The user must do this after calling this function. 265 * 266 * @return psArray* Pointer to psArray. 267 * 268 */ 269 psArray *psVoidPtrArrayRealloc( 270 psArray *restrict psArr, ///< Void pointer array to destroy. 271 int nalloc ///< Number of elements. 295 272 ); 296 273 … … 298 275 * 299 276 * Uses psLib memory allocation functions to deallocate an array of void pointers as defined by the 300 * psVoidPtrArray struct. 277 * psVoidPtrArray struct. This function does not free the array elements unless the user provides a callback 278 * function. NULL may be passed as an alternative to supplying a callback, but the user must remember to 279 * delete the array elements afterwards. The user must not delete or deallocate the array elements prior to 280 * calling this function, as it requires valid elements for memory reference decrementation operations. 281 * 282 * @return void 301 283 * 302 284 */ 303 285 void psVoidPtrArrayFree( 304 ps VoidPtrArray *restrict psArr, ///< Array to destroy.305 void (*elemFree)(void *) ///< Callback function responsible for removing array data.286 psArray *restrict psArr, ///< Void pointer array to destroy. 287 void (*elemFree)(void *) ///< Optional callback function to remove array elements. 306 288 ); 307 289 -
trunk/psLib/src/types/psBitSet.c
r542 r578 1 /** @file psBit Mask.c1 /** @file psBitSet.c 2 2 * 3 * @brief Creates an array of b its of arbitrary length.3 * @brief Creates an array of bytes of arbitrary length for storing individual bits. 4 4 * 5 * Bit masks are useful for turning options on and off. This module provides functions to create an array of 6 * bits of arbitrary length and manipulate them with basic binary operations. A print function is also 7 * provided to display the entire set of bits in binary form. 5 * Bit masks are useful tools for toggling various flags and options. This set of functions module provides 6 * a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary 7 * operations. A print function is also provided to display the entire set of bits in binary format as a 8 * string. 8 9 * 9 10 * @author Ross Harman, MHPCC 10 11 * 11 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-0 4-28 17:47:24$12 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-05-05 20:43:22 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 22 23 #include <ctype.h> 23 24 24 #include "psBit Mask.h"25 #include "psBitSet.h" 25 26 #include "psMemory.h" 27 #include "psError.h" 28 29 /******************************************************************************/ 30 /* DEFINE STATEMENTS */ 31 /******************************************************************************/ 32 33 // None 34 35 /******************************************************************************/ 36 /* TYPE DEFINITIONS */ 37 /******************************************************************************/ 38 39 // None 40 41 /*****************************************************************************/ 42 /* GLOBAL VARIABLES */ 43 /*****************************************************************************/ 44 45 // None 46 47 /*****************************************************************************/ 48 /* FILE STATIC VARIABLES */ 49 /*****************************************************************************/ 50 51 // None 26 52 27 53 /*****************************************************************************/ 28 54 /* FUNCTION IMPLEMENTATION - LOCAL */ 29 55 /*****************************************************************************/ 30 static char* getByte(int bit, const psBitMask *restrict inMask) 56 57 /** Private function to return a byte. 58 * 59 * Finds the byte containing the bit within the byte array. 60 * 61 * @return char*: Pointer to byte in which bit is contained. 62 */ 63 static char* getByte(int bit, const psBitSet *restrict inMask) 31 64 { 32 65 int index = 0; … … 38 71 } 39 72 73 /** Private function to create a mask. 74 * 75 * Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses 76 * zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position. 77 * 78 * @return char*: Pointer to byte in which bit is contained. 79 */ 40 80 static char mask(int bit) 41 81 { … … 49 89 /* FUNCTION IMPLEMENTATION - PUBLIC */ 50 90 /*****************************************************************************/ 51 psBit Mask* psBitMaskAlloc(int n)91 psBitSet* psBitSetAlloc(int n) 52 92 { 53 psBit Mask *newObj = psAlloc(sizeof(psBitMask));93 psBitSet *newObj = psAlloc(sizeof(psBitSet)); 54 94 newObj->n = n; 55 95 newObj->bits = psAlloc(sizeof(char)*n); … … 59 99 } 60 100 61 void psBit MaskFree(psBitMask *restrict inMask)101 void psBitSetFree(psBitSet *restrict inBitSet) 62 102 { 63 psFree(in Mask->bits);64 psFree(in Mask);103 psFree(inBitSet->bits); 104 psFree(inBitSet); 65 105 } 66 106 67 psBit Mask* psBitMaskSet(psBitMask *inMask, int bit)107 psBitSet* psBitSetSet(psBitSet *inBitSet, int bit) 68 108 { 69 char* byte = getByte(bit, in Mask);109 char* byte = getByte(bit, inBitSet); 70 110 *byte |= mask(bit); 71 111 72 return in Mask;112 return inBitSet; 73 113 } 74 114 75 int psBit MaskTest(const psBitMask *inMask, int bit)115 int psBitSetTest(const psBitSet *inBitSet, int bit) 76 116 { 77 char* byte = getByte(bit, in Mask);117 char* byte = getByte(bit, inBitSet); 78 118 if((*byte&mask(bit)) == 0) { 79 119 return 0; … … 83 123 } 84 124 85 psBit Mask* psBitMaskOp(psBitMask *outMask, const psBitMask *restrict inMask1, char *operator,86 const psBitMask *restrict inMask2)125 psBitSet* psBitSetOp(psBitSet *outBitSet, const psBitSet *restrict inBitSet1, char *operator, 126 const psBitSet *restrict inBitSet2) 87 127 { 88 128 int i = 0; … … 93 133 char *inBits2 = NULL; 94 134 95 if(out Mask== NULL) {96 p rintf("Null output psBitMask\n");97 return out Mask;135 if(outBitSet == NULL) { 136 psError(__func__, " : Line %d - Null psBitSet for outBitSet argument\n", __LINE__); 137 return outBitSet; 98 138 } 99 139 100 if(in Mask1 == NULL) {101 p rintf("Null input psBitMask1\n");102 return out Mask;140 if(inBitSet1 == NULL) { 141 psError(__func__, " : Line %d - Null psBitSet for inBitSet1 argument\n", __LINE__); 142 return outBitSet; 103 143 } 104 144 105 145 if(operator == NULL) { 106 printf("Null input operator\n"); 107 return outMask; 108 146 psError(__func__, " : Line %d - Null input operator\n", __LINE__); 147 return outBitSet; 109 148 } 110 149 111 if(in Mask2 == NULL) {112 p rintf("Null input psBitMask2\n");113 return out Mask;150 if(inBitSet2 == NULL) { 151 psError(__func__, " : Line %d - Null psBitSet for inBitSet2 argument\n", __LINE__); 152 return outBitSet; 114 153 } 115 154 116 if(in Mask1->n != inMask2->n || outMask->n != inMask1->n) {117 p rintf("Mask sizes not the same\n");118 return out Mask;155 if(inBitSet1->n != inBitSet2->n || outBitSet->n != inBitSet1->n) { 156 psError(__func__, " : Line %d - psBitSet sizes not the same\n", __LINE__); 157 return outBitSet; 119 158 } 120 159 121 n = out Mask->n;122 outBits = out Mask->bits;123 inBits1 = in Mask1->bits;124 inBits2 = in Mask2->bits;160 n = outBitSet->n; 161 outBits = outBitSet->bits; 162 inBits1 = inBitSet1->bits; 163 inBits2 = inBitSet2->bits; 125 164 126 165 tempChar = toupper(*operator); … … 142 181 break; 143 182 default: 144 p rintf("Invalid psBitMask option %s\n", operator);183 psError(__func__, " : Line %d - Invalid psBitMask binary operation: %s\n", __LINE__); 145 184 } 146 185 147 return out Mask;186 return outBitSet; 148 187 } 149 188 150 char *psBit MaskToString(const psBitMask *restrict inMask)189 char *psBitSetToString(const psBitSet *restrict inBitSet) 151 190 { 152 191 int i = 0; 153 int numBits = in Mask->n*8;154 char* outString = (char*)malloc(numBits);192 int numBits = inBitSet->n*8; 193 char* outString = psAlloc(numBits); 155 194 for(i=0; i<numBits; i++) { 156 outString[numBits-i-1] = (psBit MaskTest(inMask, i))?'1':'0';195 outString[numBits-i-1] = (psBitSetTest(inBitSet, i))?'1':'0'; 157 196 } 158 197 -
trunk/psLib/src/types/psBitSet.h
r542 r578 1 /** @file psBit Mask.h1 /** @file psBitSet.h 2 2 * 3 * @brief Creates an array of b its of arbitrary length.3 * @brief Creates an array of bytes of arbitrary length for storing individual bits. 4 4 * 5 * Bit masks are useful for turning options on and off. This module provides functions to create an array of 6 * bits of arbitrary length and manipulate them with basic binary operations. A print function is also 7 * provided to display the entire set of bits in binary form. 5 * Bit masks are useful tools for toggling various flags and options. This set of functions module provides 6 * a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary 7 * operations. A print function is also provided to display the entire set of bits in binary format as a 8 * string. 8 9 * 9 10 * @author Ross Harman, MHPCC 10 11 * 11 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-0 4-28 17:47:56$12 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-05-05 20:43:57 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 16 */ 16 17 17 #ifndef PSBIT MASK_H18 #define PSBIT MASK_H18 #ifndef PSBITSET_H 19 #define PSBITSET_H 19 20 20 21 /******************************************************************************/ … … 22 23 /******************************************************************************/ 23 24 24 /** Struct containing array of b its and itslength.25 /** Struct containing array of bytes to hold bit data and corresponding array length. 25 26 * 26 27 * The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are 27 * arranged with the LSB in first position of the first array element.28 * arranged with the LSB in first (right most) position of the first array element. 28 29 */ 29 30 typedef struct … … 32 33 char *bits; /**< Aray of bytes holding bits */ 33 34 } 34 psBit Mask;35 psBitSet; 35 36 36 37 /*****************************************************************************/ … … 38 39 /*****************************************************************************/ 39 40 40 /** Allocate a psBit Mask.41 /** Allocate a psBitSet. 41 42 * 42 * Create a psBitMask with number of bytes specified by the user. All bits are set to zero upon allocation. 43 * Create a psBitSet with the number of bytes specified by the user. All bits are set to zero upon 44 * allocation. 43 45 * 44 * @return psBit Mask*: Pointer to struct containing array of bits and size of array.46 * @return psBitSet*: Pointer to struct containing array of bits and size of array. 45 47 */ 46 psBit Mask* psBitMaskAlloc(47 int n /**< Number of bytes in array */48 psBitSet* psBitSetAlloc( 49 int n /**< Number of bytes in psBitSet array */ 48 50 ); 49 51 50 /** Free a psBit Mask52 /** Free a psBitSet 51 53 * 52 * Deletes a psBit Mask array and its byte count.54 * Deletes a psBitSet array. 53 55 */ 54 void psBit MaskFree(55 psBit Mask *restrict inMask /**< Pointer to psBitMask struct to be deleted. */56 void psBitSetFree( 57 psBitSet *restrict inMask /**< Pointer to psBitSet to be deleted. */ 56 58 ); 57 59 58 60 /** Set a bit. 59 61 * 60 * Sets a bit at a given bit location . The bit is set based on a zero index with the first bit set in61 * the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in an array with62 * two elements would result in an psBitMaskthat looks like 00000000 00001000.62 * Sets a bit at a given bit location, either one or zero. The bit is set based on a zero index with the 63 * first bit set in the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in 64 * an array with two elements would result in an psBitSet that looks like 00000000 00001000. 63 65 * 64 * @return psBit Mask*: Pointer to struct containing array with bit set.66 * @return psBitSet*: Pointer to struct containing psBitSet. 65 67 */ 66 psBit Mask* psBitMaskSet(67 psBit Mask *inMask, /**< Pointer to struct to be set. */68 int bit /**< Bit to be set. */68 psBitSet* psBitSetSet( 69 psBitSet *restrict inMask, /**< Pointer to psBitSet to be set. */ 70 int bit /**< Bit to be set. */ 69 71 ); 70 72 71 73 /** Test the value of a bit. 72 74 * 73 * Prints the value of a bit at a given bit location. The bit printed based on a zero index with the first 74 * bit set in the zero bit slot of the zero element of the byte array. As an example, testing bit 3 in an array with 75 * two elements that looks like 00000000 00001000 would retrun a value of one, since that value was already set. 75 * Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a 76 * zero index format with the first bit set in the zero bit slot of the zero element of the byte array 77 * As an example, testing bit 3 in a psBitSet with two bytes that looks like 00000000 00001000 would return a 78 * value of one, since that is the value that was set. 76 79 * 77 80 * @return int: Value of bit, either one or zero. 78 81 */ 79 int psBit MaskTest(80 const psBit Mask *inMask, /**< Pointer to struct to be tested. */81 int bit /**< Bit to be tested. */82 int psBitSetTest( 83 const psBitSet *restrict inMask, /**< Pointer psBitSet to be tested. */ 84 int bit /**< Bit to be tested. */ 82 85 ); 83 86 84 /** Perform a binary operation on two psBit Masks87 /** Perform a binary operation on two psBitSets 85 88 * 86 * Perform an AND, OR, or XOR on two psBit Masks. If the BitMasks are not the same size, the operation will not87 * be performed and an error message will be printed.89 * Perform an AND, OR, or XOR on two psBitSets. If the BitMasks are not the same size, the operation will not 90 * be performed and an error message will be logged. 88 91 * 89 * @return psBit Mask*: Pointer to struct containing result of binary operation.92 * @return psBitSet*: Pointer to struct containing result of binary operation. 90 93 */ 91 psBit Mask* psBitMaskOp(92 psBit Mask *outMask, /**< Resulting psBitMaskfrom binary operation */93 const psBit Mask *restrict inMask1, /**< First psBitMaskon which to operate */94 char *operator, /**< Bit operation */95 const psBit Mask *restrict inMask2 /**< First psBitMaskon which to operate */94 psBitSet* psBitSetOp( 95 psBitSet *restrict outMask, /**< Resulting psBitSet from binary operation */ 96 const psBitSet *restrict inMask1, /**< First psBitSet on which to operate */ 97 char *operator, /**< Bit operation */ 98 const psBitSet *restrict inMask2 /**< First psBitSet on which to operate */ 96 99 ); 97 100 98 /** Print the contents of a psBitMask.101 /** Convert the psBitSet to a string of ones and zeros. 99 102 * 100 * Prints the contents of a psBitMask in its binary form of ones and zeros. The LSB is the left-most chracter. 103 * Converts the contents of a psBitSet to a string representation of its binary form of ones and zeros. The 104 * LSB is the right-most chracter. Each set of eight characters represents one byte. 101 105 * 102 * @return char*: Pointer to character array containing binary formatteddata.106 * @return char*: Pointer to character array containing string data. 103 107 */ 104 char *psBitMaskToString( 105 const psBitMask *restrict inMask /**< psBitMask to print */ 106 ); 107 108 /** Private function to return a byte. 109 * 110 * Finds the byte containing the bit within the byte array. 111 * 112 * @return char*: Pointer to byte in which bit is contained. 113 */ 114 static char* getByte( 115 int bit, /**< Bit to index to search. */ 116 const psBitMask *restrict inMask /**< psBitMask to search. */ 117 ); 118 119 /** Private function to create a mask. 120 * 121 * Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses 122 * zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position. 123 * 124 * @return char*: Pointer to byte in which bit is contained. 125 */ 126 static char mask( 127 int bit /**< Bit to set within mask */ 108 char *psBitSetToString( 109 const psBitSet *restrict inMask /**< psBitSet to convert */ 128 110 ); 129 111
Note:
See TracChangeset
for help on using the changeset viewer.
