Changeset 1407 for trunk/psLib/src/collections/psBitSet.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psBitSet.c (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psBitSet.c
r1406 r1407 1 1 2 /** @file psBitSet.c 2 3 * … … 10 11 * @author Ross Harman, MHPCC 11 12 * 12 * @version $Revision: 1.1 4$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-08-0 6 22:34:05$13 * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-08-07 00:06:06 $ 14 15 * 15 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 17 18 18 19 /******************************************************************************/ 20 19 21 /* INCLUDE FILES */ 22 20 23 /******************************************************************************/ 21 24 #include <string.h> … … 31 34 32 35 /******************************************************************************/ 36 33 37 /* DEFINE STATEMENTS */ 34 /******************************************************************************/ 35 36 // None 37 38 /******************************************************************************/ 38 39 /******************************************************************************/ 40 41 // None 42 43 /******************************************************************************/ 44 39 45 /* TYPE DEFINITIONS */ 40 /******************************************************************************/ 41 42 // None 43 44 /*****************************************************************************/ 46 47 /******************************************************************************/ 48 49 // None 50 51 /*****************************************************************************/ 52 45 53 /* GLOBAL VARIABLES */ 46 /*****************************************************************************/ 47 48 // None 49 50 /*****************************************************************************/ 54 55 /*****************************************************************************/ 56 57 // None 58 59 /*****************************************************************************/ 60 51 61 /* FILE STATIC VARIABLES */ 52 /*****************************************************************************/ 53 54 // None 55 56 /*****************************************************************************/ 62 63 /*****************************************************************************/ 64 65 // None 66 67 /*****************************************************************************/ 68 57 69 /* FUNCTION IMPLEMENTATION - LOCAL */ 58 /*****************************************************************************/ 59 static void psBitSetFree(psBitSet *restrict inBitSet); 60 70 71 /*****************************************************************************/ 72 static void psBitSetFree(psBitSet * restrict inBitSet); 61 73 62 74 /** Private function to create a mask. … … 70 82 { 71 83 char mask = (char)0x01; 84 72 85 // Ignore splint warning about negative bit shifts 73 /* @i@*/74 mask = mask << (bit %8);86 /* @i@ */ 87 mask = mask << (bit % 8); 75 88 76 89 return mask; … … 78 91 79 92 /*****************************************************************************/ 93 80 94 /* FUNCTION IMPLEMENTATION - PUBLIC */ 81 /*****************************************************************************/ 82 psBitSet* psBitSetAlloc(int n) 95 96 /*****************************************************************************/ 97 psBitSet *psBitSetAlloc(int n) 83 98 { 84 99 int numBytes = 0; 85 100 psBitSet *newObj = NULL; 86 101 87 if (n <= 0) {102 if (n <= 0) { 88 103 psError(__func__, " : Line %d - Allocation size must be > 0: size = %d", __LINE__, n); 89 104 return 0; 90 105 } 91 106 92 numBytes = ceil(n /8.0);107 numBytes = ceil(n / 8.0); 93 108 newObj = psAlloc(sizeof(psBitSet)); 94 if (newObj == NULL) {95 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);96 } 97 p_psMemSetDeallocator(newObj, (psFreeFcn)psBitSetFree);109 if (newObj == NULL) { 110 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); 111 } 112 p_psMemSetDeallocator(newObj, (psFreeFcn) psBitSetFree); 98 113 newObj->n = numBytes; 99 114 100 115 // Ignore splint warning about releasing pointer members, since they've not been allocated yet 101 /* @i@*/102 newObj->bits = psAlloc(sizeof(char) *numBytes);103 if (newObj->bits == NULL) {104 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);116 /* @i@ */ 117 newObj->bits = psAlloc(sizeof(char) * numBytes); 118 if (newObj->bits == NULL) { 119 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); 105 120 } 106 121 … … 110 125 } 111 126 112 static void psBitSetFree(psBitSet * restrict inBitSet)113 { 114 if (inBitSet == NULL) {127 static void psBitSetFree(psBitSet * restrict inBitSet) 128 { 129 if (inBitSet == NULL) { 115 130 psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__); 116 131 return; … … 119 134 } 120 135 121 psBitSet * psBitSetSet(psBitSet *inBitSet, int bit)122 { 123 char *byte = NULL;124 125 if (inBitSet == NULL) {136 psBitSet *psBitSetSet(psBitSet * inBitSet, int bit) 137 { 138 char *byte = NULL; 139 140 if (inBitSet == NULL) { 126 141 psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__); 127 142 return inBitSet; 128 } else 129 if(bit < 0) { 130 psError(__func__, " : Line %d - Bit position too small: %d", __LINE__, bit); 131 return inBitSet; 132 } else 133 if(bit > inBitSet->n*8-1) { 134 psError(__func__, " : Line %d - Bit position too large: %d", __LINE__, bit); 135 return inBitSet; 136 } 137 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); 148 return inBitSet; 149 } 138 150 // Variable byte is the byte in the array that contains the bit to be set 139 byte = inBitSet->bits +bit/8;151 byte = inBitSet->bits + bit / 8; 140 152 *byte |= mask(bit); 141 153 … … 143 155 } 144 156 145 bool psBitSetTest(const psBitSet *inBitSet, int bit) 146 { 147 char* byte = NULL; 148 149 if(inBitSet == NULL) { 150 psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__); 151 return 0; 152 } else 153 if(bit < 0) { 154 psError(__func__, " : Line %d - Bit position too small: %d", __LINE__, bit); 155 return 0; 156 } else 157 if(bit > inBitSet->n*8-1) { 158 psError(__func__, " : Line %d - Bit position too large: %d", __LINE__, bit); 159 return 0; 160 } 161 157 bool psBitSetTest(const psBitSet * inBitSet, int bit) 158 { 159 char *byte = NULL; 160 161 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 } 162 171 // Variable byte is the byte in the array that contains the bit to be tested 163 byte = inBitSet->bits +bit/8;164 if ((int)(*byte&mask(bit)) == 0) {172 byte = inBitSet->bits + bit / 8; 173 if ((int)(*byte & mask(bit)) == 0) { 165 174 return 0; 166 175 } … … 169 178 } 170 179 171 psBitSet * psBitSetOp(psBitSet *outBitSet, const psBitSet *restrict inBitSet1, char *operator,172 const psBitSet * restrict inBitSet2)180 psBitSet *psBitSetOp(psBitSet * outBitSet, const psBitSet * restrict inBitSet1, char *operator, 181 const psBitSet * restrict inBitSet2) 173 182 { 174 183 int i = 0; … … 179 188 char *inBits2 = NULL; 180 189 181 if (inBitSet1 == NULL) {190 if (inBitSet1 == NULL) { 182 191 psError(__func__, " : Line %d - Null psBitSet for inBitSet1 argument", __LINE__); 183 192 return outBitSet; 184 193 } 185 194 186 if (operator == NULL) {195 if (operator == NULL) { 187 196 psError(__func__, " : Line %d - Null input operator\n", __LINE__); 188 197 return outBitSet; 189 198 } 190 199 191 if (inBitSet2 == NULL) {200 if (inBitSet2 == NULL) { 192 201 psError(__func__, " : Line %d - Null psBitSet for inBitSet2 argument", __LINE__); 193 202 return outBitSet; 194 203 } 195 204 196 if (outBitSet == NULL) {197 outBitSet = psBitSetAlloc(inBitSet1->n *8);198 } 199 200 if (inBitSet1->n != inBitSet2->n || outBitSet->n != inBitSet1->n) {205 if (outBitSet == NULL) { 206 outBitSet = psBitSetAlloc(inBitSet1->n * 8); 207 } 208 209 if (inBitSet1->n != inBitSet2->n || outBitSet->n != inBitSet1->n) { 201 210 psError(__func__, " : Line %d - psBitSet sizes not the same", __LINE__); 202 211 return outBitSet; … … 209 218 210 219 tempChar = toupper(operator[0]); 211 switch (tempChar) {220 switch (tempChar) { 212 221 case 'A': 213 for (i=0; i<n; i++) {222 for (i = 0; i < n; i++) { 214 223 outBits[i] = inBits1[i] & inBits2[i]; 215 224 } 216 225 break; 217 226 case 'O': 218 for (i=0; i<n; i++) {227 for (i = 0; i < n; i++) { 219 228 outBits[i] = inBits1[i] | inBits2[i]; 220 229 } 221 230 break; 222 231 case 'X': 223 for (i=0; i<n; i++) {232 for (i = 0; i < n; i++) { 224 233 outBits[i] = inBits1[i] ^ inBits2[i]; 225 234 } … … 232 241 } 233 242 234 psBitSet * psBitSetNot(psBitSet *outBitSet, const psBitSet *restrict inBitSet)243 psBitSet *psBitSetNot(psBitSet * outBitSet, const psBitSet * restrict inBitSet) 235 244 { 236 245 int i = 0; … … 239 248 char *inBits = NULL; 240 249 241 if (inBitSet == NULL) {250 if (inBitSet == NULL) { 242 251 psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__); 243 252 return outBitSet; … … 245 254 246 255 n = inBitSet->n; 247 if (n == 0) {256 if (n == 0) { 248 257 psError(__func__, " : Line %d - No elements in inBitSet", __LINE__); 249 258 return outBitSet; 250 259 } 251 260 252 if (outBitSet == NULL) {253 outBitSet = psBitSetAlloc(n *8);254 } 255 256 if (inBitSet->n != outBitSet->n) {261 if (outBitSet == NULL) { 262 outBitSet = psBitSetAlloc(n * 8); 263 } 264 265 if (inBitSet->n != outBitSet->n) { 257 266 psError(__func__, " : Line %d - psBitSet sizes not the same", __LINE__); 258 267 return outBitSet; … … 262 271 inBits = inBitSet->bits; 263 272 264 for (i=0; i<n; i++) {273 for (i = 0; i < n; i++) { 265 274 outBits[i] = ~inBits[i]; 266 275 } … … 269 278 } 270 279 271 char *psBitSetToString(const psBitSet * restrict inBitSet)280 char *psBitSetToString(const psBitSet * restrict inBitSet) 272 281 { 273 282 int i = 0; 274 int numBits = inBitSet->n*8; 275 char* outString = psAlloc((size_t)numBits+1); 276 if(outString == NULL) { 277 psAbort(__func__," : Line %d - Failed to allocate memory", __LINE__); 278 } 279 280 for(i=0; i<numBits; i++) { 281 outString[numBits-i-1] = (psBitSetTest(inBitSet, i) == 1)?'1':'0'; 283 int numBits = inBitSet->n * 8; 284 char *outString = psAlloc((size_t) numBits + 1); 285 286 if (outString == NULL) { 287 psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); 288 } 289 290 for (i = 0; i < numBits; i++) { 291 outString[numBits - i - 1] = (psBitSetTest(inBitSet, i) == 1) ? '1' : '0'; 282 292 } 283 293
Note:
See TracChangeset
for help on using the changeset viewer.
