00001 /** @file psBitSet.h 00002 * 00003 * @brief Creates an array of bytes of arbitrary length for storing individual bits. 00004 * 00005 * Bit masks are useful tools for toggling various flags and options. This set of functions module provides 00006 * a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary 00007 * operations. A print function is also provided to display the entire set of bits in binary format as a 00008 * string. 00009 * 00010 * @ingroup BitSet 00011 * 00012 * @author Ross Harman, MHPCC 00013 * 00014 * @version $Revision: 1.1.1.1 $ $Name: $ 00015 * @date $Date: 2005/09/14 20:42:48 $ 00016 * 00017 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 00018 */ 00019 00020 #ifndef PSBITSET_H 00021 #define PSBITSET_H 00022 00023 #include "psType.h" 00024 00025 /// @addtogroup BitSet 00026 /// @{ 00027 00028 /******************************************************************************/ 00029 /* TYPE DEFINITIONS */ 00030 /******************************************************************************/ 00031 00032 /** Struct containing array of bytes to hold bit data and corresponding array length. 00033 * 00034 * The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are 00035 * arranged with the LSB in first (right most) position of the first array element. 00036 */ 00037 typedef struct 00038 { 00039 long n; ///< Number of bytes in the array 00040 psU8 *bits; ///< Aray of bytes holding bits 00041 void *lock; ///< Optional lock for thread safety 00042 } 00043 psBitSet; 00044 00045 /*****************************************************************************/ 00046 /* FUNCTION PROTOTYPES */ 00047 /*****************************************************************************/ 00048 00049 /** Checks the type of a particular pointer. 00050 * 00051 * Uses the appropriate deallocation function in psMemBlock to check the ptr datatype. 00052 * 00053 * @return bool: True if the pointer matches a psBitSet structure, false otherwise. 00054 */ 00055 bool psMemCheckBitSet( 00056 psPtr ptr ///< the pointer whose type to check 00057 ) 00058 ; 00059 00060 00061 /** Allocate a psBitSet. 00062 * 00063 * Create a psBitSet with the number of bits specified by the user. All bits are set to zero upon 00064 * allocation. 00065 * 00066 * @return psBitSet* : Pointer to struct containing array of bits and size of array. 00067 */ 00068 psBitSet* psBitSetAlloc( 00069 long nalloc ///< Number of bits in psBitSet array 00070 ); 00071 00072 /** Set a bit. 00073 * 00074 * Sets a bit at a given bit location. The bit is set based on a zero index with the 00075 * first bit set in the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in 00076 * an array with two elements would result in an psBitSet that looks like 00000000 00001000. 00077 * 00078 * @return psBitSet* : Pointer to struct containing psBitSet. 00079 */ 00080 psBitSet* psBitSetSet( 00081 psBitSet* bitSet, ///< Pointer to psBitSet to be set. 00082 long bit ///< Bit to be set. 00083 ); 00084 00085 /** Clear a bit. 00086 * 00087 * Clear a bit at a given bit location. The bit is cleared based on a zero 00088 * index with the first bit set in the zero bit slot of the zero element of 00089 * the byte array. 00090 * 00091 * @return psBitSet* : Pointer to struct containing psBitSet. 00092 */ 00093 psBitSet* psBitSetClear( 00094 /* @returned@ */ 00095 psBitSet* bitSet, ///< Pointer to psBitSet to be cleared. 00096 long bit ///< Bit to be cleared. 00097 ); 00098 00099 /** Test the value of a bit. 00100 * 00101 * Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a 00102 * zero index format with the first bit set in the zero bit slot of the zero element of the byte array 00103 * As an example, testing bit 3 in a psBitSet with two bytes that looks like 00000000 00001000 would return a 00104 * value of one, since that is the value that was set. 00105 * 00106 * @return bool: True if successful, otherwise false 00107 */ 00108 00109 bool psBitSetTest( 00110 const psBitSet* bitSet, ///< Pointer psBitSet to be tested. 00111 long bit ///< Bit to be tested. 00112 ); 00113 00114 /** Perform a binary operation on two psBitSets 00115 * 00116 * Perform an AND, OR, or XOR on two psBitSets. If the BitMasks are not the same size, the operation will not 00117 * be performed and an error message will be logged. 00118 * 00119 * @return psBitSet* : Pointer to struct containing result of binary operation. 00120 */ 00121 psBitSet* psBitSetOp( 00122 /* @returned@ */ 00123 psBitSet* outBitSet, ///< Resulting psBitSet from binary operation 00124 const psBitSet* inBitSet1, ///< First psBitSet on which to operate 00125 const char *operator, ///< Bit operation 00126 const psBitSet* inBitSet2 ///< First psBitSet on which to operate 00127 ); 00128 00129 /** Perform a not operation on a psBitSet 00130 * 00131 * Toggles bits in a psBitset. All zero bits are set to one and all one bits are set to zero. 00132 * 00133 * @return psBitSet* : Pointer to struct containing result of operation. 00134 */ 00135 00136 psBitSet* psBitSetNot( 00137 psBitSet* outBitSet, ///< Resulting psBitSet from operation 00138 const psBitSet* inBitSet ///< Input psBitSet 00139 ); 00140 00141 /** Convert the psBitSet to a string of ones and zeros. 00142 * 00143 * Converts the contents of a psBitSet to a string representation of its binary form of ones and zeros. The 00144 * LSB is the right-most chracter. Each set of eight characters represents one byte. 00145 * 00146 * @return char*: Pointer to character array containing string data. 00147 */ 00148 00149 psString psBitSetToString( 00150 const psBitSet* bitSet ///< psBitSet to convert */ 00151 ); 00152 00153 /// @} 00154 00155 #endif // #ifndef PSBITSET_H
1.4.2