Changeset 578 for trunk/psLib/src/collections/psBitSet.c
- Timestamp:
- May 5, 2004, 10:43:57 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psBitSet.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.
