Changeset 1807 for trunk/psLib/src/collections
- Timestamp:
- Sep 14, 2004, 10:01:52 AM (22 years ago)
- Location:
- trunk/psLib/src/collections
- Files:
-
- 8 edited
-
psArray.c (modified) (4 diffs)
-
psBitSet.c (modified) (12 diffs)
-
psBitSet.h (modified) (6 diffs)
-
psCollectionsErrors.dat (modified) (1 diff)
-
psCollectionsErrors.h (modified) (2 diffs)
-
psList.c (modified) (3 diffs)
-
psScalar.c (modified) (7 diffs)
-
psVector.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psArray.c
r1651 r1807 9 9 * @author Ross Harman, MHPCC 10 10 * 11 * @version $Revision: 1.1 4$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-0 8-27 23:54:33$11 * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-09-14 20:01:52 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 26 26 #include "psArray.h" 27 27 #include "psLogMsg.h" 28 29 #include "psCollectionsErrors.h" 28 30 29 31 /*****************************************************************************/ … … 43 45 psArray* psArr = NULL; 44 46 45 // Invalid nalloc46 if (nalloc < 1) {47 psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);48 return NULL;49 }50 47 // Create vector struct 51 48 psArr = (psArray* ) psAlloc(sizeof(psArray)); … … 63 60 psArray* psArrayRealloc(unsigned int nalloc, psArray* restrict in) 64 61 { 65 // Invalid nalloc66 if (nalloc < 1) {67 psError(__func__, "Invalid value for realloc (%d)\n", nalloc);68 return NULL;69 }70 71 62 if (in == NULL) { 72 63 return psArrayAlloc(nalloc); -
trunk/psLib/src/collections/psBitSet.c
r1440 r1807 1 2 1 /** @file psBitSet.c 3 2 * … … 10 9 * 11 10 * @author Ross Harman, MHPCC 12 * 13 * @version $Revision: 1.16 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-08-09 23:34:57 $ 11 * @author Robert DeSonia, MHPCC 12 * 13 * @version $Revision: 1.17 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-09-14 20:01:52 $ 15 15 * 16 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 17 17 */ 18 18 19 /******************************************************************************/20 21 /* INCLUDE FILES */22 23 /******************************************************************************/24 19 #include <string.h> 25 20 #include <stdio.h> … … 32 27 #include "psError.h" 33 28 #include "psAbort.h" 34 35 /******************************************************************************/ 36 37 /* DEFINE STATEMENTS */ 38 39 /******************************************************************************/ 40 41 // None 42 43 /******************************************************************************/ 44 45 /* TYPE DEFINITIONS */ 46 47 /******************************************************************************/ 48 49 // None 50 51 /*****************************************************************************/ 52 53 /* GLOBAL VARIABLES */ 54 55 /*****************************************************************************/ 56 57 // None 58 59 /*****************************************************************************/ 60 61 /* FILE STATIC VARIABLES */ 62 63 /*****************************************************************************/ 64 65 // None 66 67 /*****************************************************************************/ 68 69 /* FUNCTION IMPLEMENTATION - LOCAL */ 70 71 /*****************************************************************************/ 72 static void psBitSetFree(psBitSet* restrict inBitSet); 29 #include "psString.h" 30 31 #include "psCollectionsErrors.h" 32 33 enum { 34 UNKNOWN_OP, 35 AND_OP, 36 OR_OP, 37 XOR_OP, 38 NOT_OP 39 }; 40 41 static void bitSetFree(psBitSet* restrict inBitSet); 73 42 74 43 /** Private function to create a mask. … … 90 59 } 91 60 92 /*****************************************************************************/ 93 94 /* FUNCTION IMPLEMENTATION - PUBLIC */ 95 96 /*****************************************************************************/ 61 static void bitSetFree(psBitSet* restrict inBitSet) 62 { 63 if (inBitSet == NULL) { 64 return; 65 } 66 psFree(inBitSet->bits); 67 } 68 97 69 psBitSet* psBitSetAlloc(int n) 98 70 { … … 100 72 psBitSet* newObj = NULL; 101 73 102 if (n <= 0) { 103 psError(__func__, " : Line %d - Allocation size must be > 0: size = %d", __LINE__, n); 104 return 0; 74 if (n < 0) { 75 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetAlloc", 76 PS_ERR_BAD_PARAMETER_VALUE, true, 77 PS_ERRORTEXT_psBitSet_ALLOC_NEG_SIZE, 78 n); 79 return NULL; 105 80 } 106 81 107 82 numBytes = ceil(n / 8.0); 108 83 newObj = psAlloc(sizeof(psBitSet)); 109 if (newObj == NULL) { 110 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); 111 } 112 p_psMemSetDeallocator(newObj, (psFreeFcn) psBitSetFree); 84 p_psMemSetDeallocator(newObj, (psFreeFcn) bitSetFree); 113 85 newObj->n = numBytes; 114 86 … … 116 88 /* @i@ */ 117 89 newObj->bits = psAlloc(sizeof(char) * numBytes); 118 if (newObj->bits == NULL) { 119 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); 120 } 121 122 memset(newObj->bits, numBytes, 0); 90 91 memset(newObj->bits, 0, numBytes); 123 92 124 93 return newObj; 125 94 } 126 95 127 static void psBitSetFree(psBitSet* restrict inBitSet)128 {129 if (inBitSet == NULL) {130 psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__);131 return;132 }133 psFree(inBitSet->bits);134 }135 136 96 psBitSet* psBitSetSet(psBitSet* inBitSet, int bit) 137 97 { … … 139 99 140 100 if (inBitSet == NULL) { 141 psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__); 101 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetSet", 102 PS_ERR_BAD_PARAMETER_NULL, true, 103 PS_ERRORTEXT_psBitSet_SET_NULL); 142 104 return inBitSet; 143 } else if (bit < 0) { 144 psError(__func__, " : Line %d - Bit position too small: %d", __LINE__, bit); 145 return inBitSet; 146 } else if (bit > inBitSet->n * 8 - 1) { 147 psError(__func__, " : Line %d - Bit position too large: %d", __LINE__, bit); 105 } else if ( (bit < 0) || 106 (bit > inBitSet->n * 8 - 1) ) { 107 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetSet", 108 PS_ERR_BAD_PARAMETER_VALUE, true, 109 PS_ERRORTEXT_psBitSet_BIT_OUTOFRANGE, 110 bit,inBitSet->n * 8 - 1); 148 111 return inBitSet; 149 112 } … … 155 118 } 156 119 120 psBitSet* psBitSetClear(psBitSet* inBitSet, int bit) 121 { 122 char *byte = NULL; 123 124 if (inBitSet == NULL) { 125 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetClear", 126 PS_ERR_BAD_PARAMETER_NULL, true, 127 PS_ERRORTEXT_psBitSet_SET_NULL); 128 return inBitSet; 129 } else if ( (bit < 0) || 130 (bit > inBitSet->n * 8 - 1) ) { 131 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetClear", 132 PS_ERR_BAD_PARAMETER_VALUE, true, 133 PS_ERRORTEXT_psBitSet_BIT_OUTOFRANGE, 134 bit,inBitSet->n * 8 - 1); 135 return inBitSet; 136 } 137 // Variable byte is the byte in the array that contains the bit to be set 138 byte = inBitSet->bits + bit / 8; 139 *byte &= ! mask(bit); 140 141 return inBitSet; 142 } 143 157 144 bool psBitSetTest(const psBitSet* inBitSet, int bit) 158 145 { … … 160 147 161 148 if (inBitSet == NULL) { 162 psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__); 163 return 0; 164 } else if (bit < 0) { 165 psError(__func__, " : Line %d - Bit position too small: %d", __LINE__, bit); 166 return 0; 167 } else if (bit > inBitSet->n * 8 - 1) { 168 psError(__func__, " : Line %d - Bit position too large: %d", __LINE__, bit); 169 return 0; 170 } 149 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetTest", 150 PS_ERR_BAD_PARAMETER_NULL, true, 151 PS_ERRORTEXT_psBitSet_SET_NULL); 152 return false; 153 } else if ( (bit < 0) || 154 (bit > inBitSet->n * 8 - 1) ) { 155 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetTest", 156 PS_ERR_BAD_PARAMETER_VALUE, true, 157 PS_ERRORTEXT_psBitSet_BIT_OUTOFRANGE, 158 bit,inBitSet->n * 8 - 1); 159 return false; 160 } 161 171 162 // Variable byte is the byte in the array that contains the bit to be tested 172 163 byte = inBitSet->bits + bit / 8; 173 if ((int)(*byte & mask(bit)) == 0) { 174 return 0; 175 } 176 177 return 1; 164 return ((*byte & mask(bit)) != 0); 178 165 } 179 166 … … 183 170 int i = 0; 184 171 int n = 0; 185 char tempChar = '0';186 char *outBits= NULL;187 char *inBits1= NULL;188 char *inBits2 = NULL;172 char* outBits = NULL; 173 char* inBits1 = NULL; 174 char* inBits2 = NULL; 175 int op = UNKNOWN_OP; 189 176 190 177 if (inBitSet1 == NULL) { 191 psError(__func__, " : Line %d - Null psBitSet for inBitSet1 argument", __LINE__); 192 return outBitSet; 193 } 178 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp", 179 PS_ERR_BAD_PARAMETER_NULL, true, 180 PS_ERRORTEXT_psBitSet_FIRST_OPERAND_NULL); 181 psFree(outBitSet); 182 return NULL; 183 } 184 inBits1 = inBitSet1->bits; 194 185 195 186 if (operator == NULL) { 196 psError(__func__, " : Line %d - Null input operator\n", __LINE__); 197 return outBitSet; 198 } 199 200 if (inBitSet2 == NULL) { 201 psError(__func__, " : Line %d - Null psBitSet for inBitSet2 argument", __LINE__); 202 return outBitSet; 187 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp", 188 PS_ERR_BAD_PARAMETER_NULL, true, 189 PS_ERRORTEXT_psBitSet_OPERATOR_NULL); 190 psFree(outBitSet); 191 return NULL; 192 } 193 194 // make operator all caps 195 int tempStrLen = strlen(operator); 196 char* tempStr = psAlloc(tempStrLen+1); 197 198 for (int lcv=0;lcv<tempStrLen;lcv++) { 199 tempStr[lcv] = (char)toupper(operator[lcv]); 200 } 201 tempStr[tempStrLen] = '\0'; 202 203 // parse the operator 204 if (strcmp(operator,"AND")==0) { 205 op = AND_OP; 206 } else if (strcmp(operator,"OR")==0) { 207 op = OR_OP; 208 } else if (strcmp(operator,"XOR")==0) { 209 op = XOR_OP; 210 } else if (strcmp(operator,"NOT")==0) { 211 op = NOT_OP; 212 } else { 213 psFree(tempStr); 214 psFree(outBitSet); 215 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp", 216 PS_ERR_BAD_PARAMETER_VALUE, true, 217 PS_ERRORTEXT_psBitSet_OPERATOR_INVALID, 218 operator); 219 return NULL; 220 } 221 psFree(tempStr); 222 223 if (op != NOT_OP) { 224 if (inBitSet2 == NULL) { 225 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp", 226 PS_ERR_BAD_PARAMETER_NULL, true, 227 PS_ERRORTEXT_psBitSet_SECOND_OPERAND_NULL); 228 psFree(outBitSet); 229 return NULL; 230 } 231 232 if (inBitSet1->n != inBitSet2->n) { 233 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp", 234 PS_ERR_BAD_PARAMETER_SIZE, true, 235 PS_ERRORTEXT_psBitSet_OPERANDS_SIZE_DIFFER); 236 psFree(outBitSet); 237 return NULL; 238 } 239 inBits2 = inBitSet2->bits; 203 240 } 204 241 205 242 if (outBitSet == NULL) { 206 outBitSet = psBitSetAlloc(inBitSet1->n * 8); 207 } 208 209 if (inBitSet1->n != inBitSet2->n || outBitSet->n != inBitSet1->n) { 210 psError(__func__, " : Line %d - psBitSet sizes not the same", __LINE__); 211 return outBitSet; 243 outBitSet = psBitSetAlloc(inBitSet1->n*8); 244 } else if (outBitSet->n != inBitSet1->n) { 245 outBitSet->n = inBitSet1->n; 246 outBitSet->bits = psRealloc(outBitSet->bits, inBitSet1->n); 212 247 } 213 248 214 249 n = outBitSet->n; 215 250 outBits = outBitSet->bits; 216 inBits1 = inBitSet1->bits; 217 inBits2 = inBitSet2->bits; 218 219 tempChar = toupper(operator[0]); 220 switch (tempChar) { 221 case 'A': 251 252 switch (op) { 253 case AND_OP: 222 254 for (i = 0; i < n; i++) { 223 255 outBits[i] = inBits1[i] & inBits2[i]; 224 256 } 225 257 break; 226 case 'O':258 case OR_OP: 227 259 for (i = 0; i < n; i++) { 228 260 outBits[i] = inBits1[i] | inBits2[i]; 229 261 } 230 262 break; 231 case 'X':263 case XOR_OP: 232 264 for (i = 0; i < n; i++) { 233 265 outBits[i] = inBits1[i] ^ inBits2[i]; 234 266 } 235 267 break; 268 case NOT_OP: 269 for (i = 0; i < n; i++) { 270 outBits[i] = ~inBits1[i]; 271 } 272 break; 236 273 default: 237 psError(__func__, " : Line %d - Invalid psBitMask binary operation: %s", __LINE__, operator); 274 psAbort(PS_ERRORNAME_DOMAIN "psBitSetOp", 275 "Unexpected error - operator parsed successfully but not valid?"); 238 276 } 239 277 … … 243 281 psBitSet* psBitSetNot(psBitSet* outBitSet, const psBitSet* restrict inBitSet) 244 282 { 245 int i = 0; 246 int n = 0; 247 char *outBits = NULL; 248 char *inBits = NULL; 249 250 if (inBitSet == NULL) { 251 psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__); 252 return outBitSet; 253 } 254 255 n = inBitSet->n; 256 if (n == 0) { 257 psError(__func__, " : Line %d - No elements in inBitSet", __LINE__); 258 return outBitSet; 259 } 283 if (inBitSet == NULL) { 284 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetNot", 285 PS_ERR_BAD_PARAMETER_NULL, true, 286 PS_ERRORTEXT_psBitSet_OPERAND_NULL); 287 psFree(outBitSet); 288 return NULL; 289 } 290 291 outBitSet = psBitSetOp(outBitSet,inBitSet,"NOT",NULL); 260 292 261 293 if (outBitSet == NULL) { 262 outBitSet = psBitSetAlloc(n * 8); 263 } 264 265 if (inBitSet->n != outBitSet->n) { 266 psError(__func__, " : Line %d - psBitSet sizes not the same", __LINE__); 267 return outBitSet; 268 } 269 270 outBits = outBitSet->bits; 271 inBits = inBitSet->bits; 272 273 for (i = 0; i < n; i++) { 274 outBits[i] = ~inBits[i]; 294 psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetNot", 295 PS_ERR_UNKNOWN, false, 296 PS_ERRORTEXT_psBitSet_NOT_OP_FAILED); 275 297 } 276 298 … … 284 306 char *outString = psAlloc((size_t) numBits + 1); 285 307 286 if (outString == NULL) {287 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);288 }289 290 308 for (i = 0; i < numBits; i++) { 291 outString[numBits - i - 1] = (psBitSetTest(inBitSet, i) == 1) ? '1' : '0';309 outString[numBits - i - 1] = psBitSetTest(inBitSet, i) ? '1' : '0'; 292 310 } 293 311 -
trunk/psLib/src/collections/psBitSet.h
r1472 r1807 1 2 1 /** @file psBitSet.h 3 2 * … … 13 12 * @author Ross Harman, MHPCC 14 13 * 15 * @version $Revision: 1.1 3$ $Name: not supported by cvs2svn $16 * @date $Date: 2004-0 8-11 19:53:33$14 * @version $Revision: 1.14 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-09-14 20:01:52 $ 17 16 * 18 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 26 25 27 26 /******************************************************************************/ 28 29 27 /* TYPE DEFINITIONS */ 30 31 28 /******************************************************************************/ 32 29 … … 44 41 45 42 /*****************************************************************************/ 46 47 43 /* FUNCTION PROTOTYPES */ 48 49 44 /*****************************************************************************/ 50 45 … … 64 59 /** Set a bit. 65 60 * 66 * Sets a bit at a given bit location , either one or zero. The bit is set based on a zero index with the61 * Sets a bit at a given bit location. The bit is set based on a zero index with the 67 62 * first bit set in the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in 68 63 * an array with two elements would result in an psBitSet that looks like 00000000 00001000. … … 74 69 psBitSet* restrict inMask, ///< Pointer to psBitSet to be set. 75 70 int bit ///< Bit to be set. 71 ); 72 73 /** Clear a bit. 74 * 75 * Clear a bit at a given bit location. The bit is cleared based on a zero 76 * index with the first bit set in the zero bit slot of the zero element of 77 * the byte array. 78 * 79 * @return psBitSet* : Pointer to struct containing psBitSet. 80 */ 81 psBitSet* psBitSetClear( 82 /* @returned@ */ 83 psBitSet* restrict inMask, ///< Pointer to psBitSet to be cleared. 84 int bit ///< Bit to be cleared. 76 85 ); 77 86 -
trunk/psLib/src/collections/psCollectionsErrors.dat
r1762 r1807 7 7 # N.B. in code, the ERRORNAME appears as PS_ERRORTEXT_ERRORNAME 8 8 #################################################################### 9 psVector_NALLOC_NOT_POSITIVE Parameter nalloc (%d) must be positive. 9 # 10 psVector_REALLOC_NULL psVectorRealloc must a given a non-NULL psVector to resize. Desired datatype unknown. 11 psVector_SORT_NULL psVectorSort can not sort a NULL psVector. 12 psVector_UNSUPPORTED_TYPE Input psVector is an unsupported type (%d). 13 # 14 psBitSet_ALLOC_NEG_SIZE The number of bit in a psBitSet (%d) must be greater than zero. 15 psBitSet_SET_NULL Can not operate on a NULL psBitSet. 16 psBitSet_BIT_OUTOFRANGE The specified bit position (%d) is invalid. Position must be between 0 and %d. 17 psBitSet_OPERATOR_NULL Specified operator is NULL. Must specify desired operator. 18 psBitSet_OPERATOR_INVALID Specified operator, %s, is invalid. Valid operators are AND, OR, and XOR. 19 psBitSet_FIRST_OPERAND_NULL First psBitSet operand can not be NULL. 20 psBitSet_SECOND_OPERAND_NULL Second psBitSet operand can not be NULL. 21 psBitSet_OPERANDS_SIZE_DIFFER The psBitSet operand must be the same size. 22 psBitSet_NOT_OP_FAILED Could not perform NOT operation. 23 psBitSet_OPERAND_NULL Operand can not be NULL. 24 # 25 psScalar_UNSUPPORTED_TYPE Specified datatype (%d) is unsupported by psScalar. 26 psScalar_COPY_NULL Can not copy a NULL psScalar. -
trunk/psLib/src/collections/psCollectionsErrors.h
r1787 r1807 7 7 * @author Robert DeSonia, MHPCC 8 8 * 9 * @version $Revision: 1. 2$ $Name: not supported by cvs2svn $10 * @date $Date: 2004-09-1 1 00:43:54$9 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2004-09-14 20:01:52 $ 11 11 * 12 12 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 30 30 31 31 //~Start #define PS_ERRORTEXT_$1 "$2" 32 #define PS_ERRORTEXT_psVector_NALLOC_NOT_POSITIVE "Parameter nalloc (%d) must be positive" 32 #define PS_ERRORTEXT_psVector_REALLOC_NULL "psVectorRealloc must a given a non-NULL psVector to resize. Desired datatype unknown." 33 #define PS_ERRORTEXT_psVector_SORT_NULL "psVectorSort can not sort a NULL psVector." 34 #define PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE "Input psVector is an unsupported type (%d)." 35 #define PS_ERRORTEXT_psBitSet_ALLOC_NEG_SIZE "The number of bit in a psBitSet (%d) must be greater than zero." 36 #define PS_ERRORTEXT_psBitSet_SET_NULL "Can not operate on a NULL psBitSet." 37 #define PS_ERRORTEXT_psBitSet_BIT_OUTOFRANGE "The specified bit position (%d) is invalid. Position must be between 0 and %d." 38 #define PS_ERRORTEXT_psBitSet_OPERATOR_NULL "Specified operator is NULL. Must specify desired operator." 39 #define PS_ERRORTEXT_psBitSet_OPERATOR_INVALID "Specified operator, %s, is invalid. Valid operators are AND, OR, and XOR." 40 #define PS_ERRORTEXT_psBitSet_FIRST_OPERAND_NULL "First psBitSet operand can not be NULL." 41 #define PS_ERRORTEXT_psBitSet_SECOND_OPERAND_NULL "Second psBitSet operand can not be NULL." 42 #define PS_ERRORTEXT_psBitSet_OPERANDS_SIZE_DIFFER "The psBitSet operand must be the same size." 43 #define PS_ERRORTEXT_psBitSet_NOT_OP_FAILED "Could not perform NOT operation." 44 #define PS_ERRORTEXT_psBitSet_OPERAND_NULL "Operand can not be NULL." 45 #define PS_ERRORTEXT_psScalar_UNSUPPORTED_TYPE "Specified datatype (%d) is unsupported by psScalar." 46 #define PS_ERRORTEXT_psScalar_COPY_NULL "Can not copy a NULL psScalar." 33 47 //~End 34 48 -
trunk/psLib/src/collections/psList.c
r1747 r1807 1 2 1 /** @file psList.c 3 2 * @brief Support for doubly linked lists … … 7 6 * @author Robert Daniel DeSonia, MHPCC 8 7 * 9 * @version $Revision: 1.1 8$ $Name: not supported by cvs2svn $10 * @date $Date: 2004-09- 09 02:23:27$8 * @version $Revision: 1.19 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2004-09-14 20:01:52 $ 11 10 * 12 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 24 23 #include "psTrace.h" 25 24 #include "psLogMsg.h" 25 26 #include "psCollectionsErrors.h" 26 27 27 28 #define ITER_INIT_HEAD ((void *)1) // next iteration should return head -
trunk/psLib/src/collections/psScalar.c
r1648 r1807 1 2 1 /** @file psScalar.c 3 2 * … … 9 8 * @author Ross Harman, MHPCC 10 9 * 11 * @version $Revision: 1. 9$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-0 8-27 23:34:05$10 * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-09-14 20:01:52 $ 13 12 * 14 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 14 */ 16 15 17 /******************************************************************************/18 19 /* INCLUDE FILES */20 21 /******************************************************************************/22 16 #include "psMemory.h" 23 17 #include "psError.h" … … 26 20 #include "psAbort.h" 27 21 28 /******************************************************************************/ 22 #include "psCollectionsErrors.h" 29 23 30 /* DEFINE STATEMENTS */31 32 /******************************************************************************/33 34 // None35 36 /******************************************************************************/37 38 /* TYPE DEFINITIONS */39 40 /******************************************************************************/41 42 // None43 44 /*****************************************************************************/45 46 /* GLOBAL VARIABLES */47 48 /*****************************************************************************/49 50 // None51 52 /*****************************************************************************/53 54 /* FILE STATIC VARIABLES */55 56 /*****************************************************************************/57 58 // None59 60 /*****************************************************************************/61 62 /* FUNCTION IMPLEMENTATION - LOCAL */63 64 /*****************************************************************************/65 66 // None67 68 /*****************************************************************************/69 70 /* FUNCTION IMPLEMENTATION - PUBLIC */71 72 /*****************************************************************************/73 24 psScalar* psScalarAlloc(psC64 value, psElemType dataType) 74 25 { … … 77 28 // Create scalar 78 29 scalar = (psScalar* ) psAlloc(sizeof(psScalar)); 79 if (scalar == NULL) {80 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);81 }82 30 83 31 scalar->type.dimen = PS_DIMEN_SCALAR; … … 122 70 break; 123 71 default: 124 psError(__func__, ": Line %d - Invalid PS_TYPE: %d", __LINE__, dataType); 72 psErrorMsg(PS_ERRORNAME_DOMAIN "psScalarAlloc", 73 PS_ERR_BAD_PARAMETER_TYPE, true, 74 PS_ERRORTEXT_psScalar_UNSUPPORTED_TYPE, 75 dataType); 125 76 } 126 77 … … 134 85 135 86 if (scalar == NULL) { 136 psError(__func__, "Null scalar not allowed"); 87 psErrorMsg(PS_ERRORNAME_DOMAIN "psScalarCopy", 88 PS_ERR_BAD_PARAMETER_NULL, true, 89 PS_ERRORTEXT_psScalar_COPY_NULL); 90 return NULL; 137 91 } 138 92 … … 176 130 break; 177 131 default: 178 psError(__func__, ": Line %d - Invalid PS_TYPE: %d", __LINE__, dataType); 132 psErrorMsg(PS_ERRORNAME_DOMAIN "psScalarCopy", 133 PS_ERR_BAD_PARAMETER_TYPE, true, 134 PS_ERRORTEXT_psScalar_UNSUPPORTED_TYPE, 135 dataType); 179 136 } 180 137 -
trunk/psLib/src/collections/psVector.c
r1761 r1807 10 10 * @author Robert DeSonia, MHPCC 11 11 * 12 * @version $Revision: 1.2 3$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-09- 09 21:59:03$12 * @version $Revision: 1.24 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-09-14 20:01:52 $ 14 14 * 15 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 25 25 #include "psLogMsg.h" 26 26 #include "psCompare.h" 27 27 28 #include "psCollectionsErrors.h" 28 29 29 30 static void vectorFree(psVector* restrict psVec); 31 32 33 static void vectorFree(psVector* restrict psVec) 34 { 35 if (psVec == NULL) { 36 return; 37 } 38 39 psFree(psVec->data.V); 40 } 30 41 31 42 // FUNCTION IMPLEMENTATION - PUBLIC … … 35 46 psVector* psVec = NULL; 36 47 int elementSize = 0; 37 38 // Invalid nalloc39 if (nalloc < 1) {40 psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);41 return NULL;42 }43 48 44 49 elementSize = PSELEMTYPE_SIZEOF(elemType); … … 64 69 psElemType elemType; 65 70 66 // Invalid nalloc67 if (nalloc < 1) {68 psError(__func__, "Invalid value for realloc (%d)\n", nalloc);69 return NULL;70 }71 72 71 if (in == NULL) { 73 psError(__func__, "Null input vector\n"); 72 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorRealloc", 73 PS_ERR_BAD_PARAMETER_NULL, true, 74 PS_ERRORTEXT_psVector_REALLOC_NULL); 74 75 return NULL; 75 76 } else if (in->nalloc != nalloc) { // No need to realloc to same size … … 101 102 return in; 102 103 } 103 // Invalid nalloc104 if (nalloc < 1) {105 psError(__func__, "Invalid value for nalloc (%d)\n", nalloc);106 psFree(in);107 return NULL;108 }109 104 110 105 in->data.V = psRealloc(in->data.V, nalloc * PSELEMTYPE_SIZEOF(type)); … … 119 114 psVector* psVectorSort(psVector* restrict outVector, const psVector* restrict inVector) 120 115 { 121 int inN = 0; 122 int outN = 0; 116 int N = 0; 123 117 int elSize = 0; 124 118 void *inVec = NULL; … … 127 121 128 122 if (inVector == NULL) { 129 psError(__func__, " : Line %d - Null input vector\n", __LINE__); 130 return outVector; 123 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSort", 124 PS_ERR_BAD_PARAMETER_NULL, true, 125 PS_ERRORTEXT_psVector_SORT_NULL); 126 psFree(outVector); 127 return NULL; 131 128 } 132 129 133 130 inType = inVector->type.type; 134 inN = inVector->n;131 N = inVector->n; 135 132 inVec = inVector->data.V; 136 133 elSize = PSELEMTYPE_SIZEOF(inType); 137 134 138 135 if (outVector == NULL) { 139 outVector = psVectorAlloc(inN, inType); 140 outVector->n = inVector->n; 141 } 142 143 outN = outVector->n; 136 outVector = psVectorAlloc(N, inType); 137 } 138 139 // check to see if output vector needs to be resized/retyped 140 if ( (N > outVector->nalloc) || 141 (inType != outVector->type.type) ) { 142 // reshape the output vector to match the input vector's size/type. 143 outVector = psVectorRecycle(outVector,N,inType); 144 } 145 outVector->n = N; 144 146 outVec = outVector->data.V; 145 147 146 if (inN != outN) { 147 psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", 148 __LINE__, inN, outN); 148 if (N == 0) { 149 // no need to sort anything, as there are no elements in input vector. 149 150 return outVector; 150 151 } 151 152 152 if (inType != outVector->type.type) { 153 psError(__func__, " : Line %d - Input and output vectors are not same type: in=%d out=%d\n", __LINE__, 154 inType, outVector->type.type); 155 return outVector; 156 } 157 158 if (inN == 0) { 159 psError(__func__, " : Line %d - No elements in use for input vector\n", __LINE__); 160 return outVector; 161 } 162 163 if (outN == 0) { 164 psError(__func__, " : Line %d - No elements in use for output vector\n", __LINE__); 165 return outVector; 166 } 167 // Copy input vector values into output vector 168 memcpy(outVec, inVec, elSize * outN); 153 // Copy input vector values into output vector if not in-place sorting 154 if (inVector != outVector) { 155 memcpy(outVec, inVec, elSize * N); 156 } 169 157 170 158 // Sort output vector 171 159 switch (inType) { 172 160 case PS_TYPE_U8: 173 qsort(outVec, inN, elSize, psCompareU8);161 qsort(outVec, N, elSize, psCompareU8); 174 162 break; 175 163 case PS_TYPE_U16: 176 qsort(outVec, inN, elSize, psCompareU16);164 qsort(outVec, N, elSize, psCompareU16); 177 165 break; 178 166 case PS_TYPE_U32: 179 qsort(outVec, inN, elSize, psCompareU32);167 qsort(outVec, N, elSize, psCompareU32); 180 168 break; 181 169 case PS_TYPE_U64: 182 qsort(outVec, inN, elSize, psCompareU64);170 qsort(outVec, N, elSize, psCompareU64); 183 171 break; 184 172 case PS_TYPE_S8: 185 qsort(outVec, inN, elSize, psCompareS8);173 qsort(outVec, N, elSize, psCompareS8); 186 174 break; 187 175 case PS_TYPE_S16: 188 qsort(outVec, inN, elSize, psCompareS16);176 qsort(outVec, N, elSize, psCompareS16); 189 177 break; 190 178 case PS_TYPE_S32: 191 qsort(outVec, inN, elSize, psCompareS32);179 qsort(outVec, N, elSize, psCompareS32); 192 180 break; 193 181 case PS_TYPE_S64: 194 qsort(outVec, inN, elSize, psCompareS64);182 qsort(outVec, N, elSize, psCompareS64); 195 183 break; 196 184 case PS_TYPE_F32: 197 qsort(outVec, inN, elSize, psCompareF32);185 qsort(outVec, N, elSize, psCompareF32); 198 186 break; 199 187 case PS_TYPE_F64: 200 qsort(outVec, inN, elSize, psCompareF64);188 qsort(outVec, N, elSize, psCompareF64); 201 189 break; 202 190 default: 203 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 191 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSort", 192 PS_ERR_BAD_PARAMETER_TYPE, true, 193 PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE, 194 inType); 204 195 } 205 196 … … 207 198 } 208 199 209 #define SORT_INDICES(TYPE) \210 for(i=0; i<inN; i++) { \211 for(j=0; j<inN; j++) { \212 diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]); \213 if(diff < FLT_EPSILON) { \214 outVec[i] = j; \215 break; \216 } \217 } \218 }219 220 200 psVector* psVectorSortIndex(psVector* restrict outVector, const psVector* restrict inVector) 221 201 { 222 int inN = 0; 223 int outN = 0; 224 int i = 0; 225 int j = 0; 226 float *inVec = NULL; 227 int *outVec = NULL; 228 double diff = 0.0f; 202 int N = 0; 229 203 psVector* tmpVector = NULL; 230 204 psElemType inType = 0; 205 psU32* outVec; 231 206 232 207 if (inVector == NULL) { 233 psError(__func__, " : Line %d - Null input vector\n", __LINE__); 208 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSortIndex", 209 PS_ERR_BAD_PARAMETER_NULL, true, 210 PS_ERRORTEXT_psVector_SORT_NULL); 211 psFree(outVector); 212 return NULL; 213 } 214 215 N = inVector->n; 216 217 if (outVector == NULL) { 218 outVector = psVectorAlloc(N, PS_TYPE_U32); 219 } 220 221 // check to see if output vector needs to be resized/retyped 222 if ( (N > outVector->nalloc) || 223 (outVector->type.type != PS_TYPE_U32) ) { 224 // reshape the output vector to match the input vector's size/type. 225 outVector = psVectorRecycle(outVector,N,PS_TYPE_U32); 226 } 227 outVector->n = N; 228 outVec = outVector->data.U32; 229 230 if (N == 0) { 231 // no need to sort anything, as there are no elements in input vector. 234 232 return outVector; 235 233 } 236 234 237 inN = inVector->n;238 inVec = inVector->data.V;239 inType = inVector->type.type;240 241 if (outVector == NULL) {242 outVector = psVectorAlloc(inN, PS_TYPE_U32);243 outVector->n = inN;244 }245 246 outN = outVector->n;247 outVec = outVector->data.V;248 249 if (inN != outN) {250 psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",251 __LINE__, inN, outN);252 return outVector;253 }254 255 if (outVector->type.type != PS_TYPE_U32) {256 psError(__func__, " : Line %d - Output vector is not of type U32: out=%d\n",257 __LINE__, outVector->type.type);258 return outVector;259 }260 261 tmpVector = psVectorAlloc(inN, inType);262 tmpVector->n = inN;263 235 tmpVector = psVectorSort(tmpVector, inVector); 236 237 #define SORT_INDICES(TYPE,absfcn,maxError) { \ 238 ps##TYPE* inVec = inVector->data.TYPE; \ 239 ps##TYPE* tmpVec = tmpVector->data.TYPE; \ 240 ps##TYPE diff; \ 241 for(int i=0; i<N; i++) { \ 242 for(int j=0; j<N; j++) { \ 243 diff = absfcn(tmpVec[i] - inVec[j]); \ 244 if(diff < maxError) { \ 245 outVec[i] = j; \ 246 break; \ 247 } \ 248 } \ 249 } \ 250 } 264 251 265 252 // Sort output vector 266 253 switch (inType) { 267 254 case PS_TYPE_U8: 268 SORT_INDICES(U8 );255 SORT_INDICES(U8,/* no absfcn needed */,1); 269 256 break; 270 257 case PS_TYPE_U16: 271 SORT_INDICES(U16 );258 SORT_INDICES(U16,/* no absfcn needed */,1); 272 259 break; 273 260 case PS_TYPE_U32: 274 SORT_INDICES(U32 );261 SORT_INDICES(U32,/* no absfcn needed */,1); 275 262 break; 276 263 case PS_TYPE_U64: 277 SORT_INDICES(U64 );264 SORT_INDICES(U64,/* no absfcn needed */,1); 278 265 break; 279 266 case PS_TYPE_S8: 280 SORT_INDICES(S8 );267 SORT_INDICES(S8,/* no absfcn needed */,1); 281 268 break; 282 269 case PS_TYPE_S16: 283 SORT_INDICES(S16 );270 SORT_INDICES(S16,/* no absfcn needed */,1); 284 271 break; 285 272 case PS_TYPE_S32: 286 SORT_INDICES(S32 );273 SORT_INDICES(S32,/* no absfcn needed */,1); 287 274 break; 288 275 case PS_TYPE_S64: 289 SORT_INDICES(S64 );276 SORT_INDICES(S64,/* no absfcn needed */,1); 290 277 break; 291 278 case PS_TYPE_F32: 292 SORT_INDICES(F32 );279 SORT_INDICES(F32,fabsf,FLT_EPSILON); 293 280 break; 294 281 case PS_TYPE_F64: 295 SORT_INDICES(F64); 282 SORT_INDICES(F64,fabs,DBL_EPSILON); 283 break; 284 case PS_TYPE_C32: 285 SORT_INDICES(F64,cabsf,FLT_EPSILON); 286 break; 287 case PS_TYPE_C64: 288 SORT_INDICES(F64,cabs,DBL_EPSILON); 296 289 break; 297 290 default: 298 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 291 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSortIndex", 292 PS_ERR_BAD_PARAMETER_TYPE, true, 293 PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE, 294 inType); 299 295 } 300 296 … … 304 300 return outVector; 305 301 } 306 307 static void vectorFree(psVector* restrict psVec)308 {309 if (psVec == NULL) {310 return;311 }312 313 psFree(psVec->data.V);314 }
Note:
See TracChangeset
for help on using the changeset viewer.
