KSquare Utilities
BitString.h
Go to the documentation of this file.
1 /* BitString.h -- Bit String management Class
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 
6 #ifndef _BITSTRING_
7 #define _BITSTRING_
8 
9 #include "Atom.h"
10 #include "KKBaseTypes.h"
11 #include "KKStr.h"
12 #include "XmlStream.h"
13 
14 namespace KKB
15 {
16  /**
17  *@class BitString
18  *@brief Allows you to manage very long bit strings.
19  *@author Kurt Kramer
20  *@details Useful when you need to deal with very large yes/no decisions. For example performing Feature Selection
21  * on a DNA dataset where you can have 50,000+ features. You need to keep a list of which feature combinations have
22  * been tried. You can use a BitString to do this where a particular bit indicates a particular feature. In the
23  * feature selection case you may want to track several thousand feature combinations. If you did this using arrays
24  * you would require very large amount of memory to accomplish this. With BitString's the memory requirement is
25  * reduced to 1/8'the allowing for more efficient use of memory.
26  *
27  * This class will manage Bit-Strings up to UINT_MAX in length. Logical operations such as bitwise AND, OR, and NOT
28  * are supported plus others. An example of where this class is used is in KKMLL::FeatureNumList.
29  */
30 
31  class BitString: public Atom // : public Atom
32  {
33  public:
34  /** @brief Instantiates a empty bit-string of length 0; needed for ReadXML */
35  BitString ();
36 
37 
38  /**
39  *@brief Construct a bit string of length _binLen with all bits set to '0'.
40  *@param[in] _bitLen Length of bit string to allocate.
41  */
42  BitString (kkuint32 _bitLen);
43 
44 
45  /** @brief Copy constructor */
46  BitString (const BitString& b);
47 
48 
49  /**
50  *@brief Construct a BitString of length _bitLen with bits indicated by '_bitNums' set to '1'.
51  *@param[in] _binLen Length of bit string.
52  *@param[in] _bitNums List if bit positions to set to '1'.
53  *@param[in] _bitNumsLen Size of '_bitNums' array.
54  */
55  BitString (kkuint32 _bitLen,
56  kkuint16* _bitNums,
57  kkuint32 _bitNumsLen
58  );
59 
60 
61  ~BitString ();
62 
63  /**@brief Returns the length of the bit-string */
64  kkuint32 BitLen () const {return bitLen;}
65 
66  const char* ClassName () const {return "BitString";}
67 
68  virtual
69  BitString* Duplicate () const;
70 
71  /**
72  *@brief Create a bit-string from a Hex String.
73  *@details Will convert the Hex String stored in the parameter 'hexStr' and create a bit string from it.\n
74  * ex: "A189" will be converted into "1010000110001001"
75  *@param[in] hexStr String containing hex characters '0' - '9', 'A' - 'F'
76  *@param[out] validHexStr returns 'TRUE' if no invalid characters.
77  *@returns BitString with the appropriate bits set to represent the hex number in 'hexStr'.
78  */
79  static
80  BitString FromHexStr (const KKStr& hexStr,
81  bool& validHexStr
82  );
83 
84  /**@brief Returns number of bits set to '1'. */
85  kkuint32 Count () const;
86 
87 
88  ///<summary> Returns true if bit indicated by <paramref name='bitNum'/> is set to 1. </summary>
89  bool Test (kkuint32 bitNum) const;
90 
91 
92  ///<summary>
93  /// Get Bit positions that are set to 1; The parameter <paramref name='setBits'/> will be populated with the list of bits that are set
94  /// to 1 for bit strings that are up to 2^16-1 bits long.
95  ///</summary>
96  ///<code>
97  /// ex: Bit String &quot;001200110011&quot; will produce a vector &lt;2, 3, 6, 7, 10, 11&gt;
98  ///</code>
99  ///<param name='setBits'> Will be populated with all bits that are set to '1', will be cleared first.</param>
100  void ListOfSetBits16 (VectorUint16& setBits) const;
101 
102 
103  ///<summary>
104  /// Get Bit positions that are set to &quot;1&quot;. The parameter <paramref name="setBits"/> will be populated with the list of
105  /// bits that are set to &quot;1&quot; for bit strings that are up to 2^32-1 bits long.
106  ///</summary>
107  ///<example>
108  /// ex: Bit String &quot;001200110011&quot; will produce a vector &lt;2, 3, 6, 7, 10, 11&gt;
109  ///</example>
110  ///<param in="setBits"> Will be populated with all bits that are set to &quot;1&quot;, will be cleared first. </param>
111  void ListOfSetBits32 (VectorUint32& setBits) const;
112 
113  /**
114  *@brief Populates a boolean vector where each element reflects whether the corresponding bit is set.
115  *@param[out] boolVector Vector to be populated reflecting which bits are set to '1'.
116  */
117  void PopulateVectorBool (VectorBool& boolVector) const;
118 
119  void ReSet (); /**< @brief Set all bits to '0'. */
120  void ReSet (kkuint32 bitNum); /**< @brief Set the bit indicated by 'bitNum' to '0'. */
121  void Set (); /**< @brief Set all bits to '1'. */
122  void Set (kkuint32 bitNum); /**< @brief Set the bit indicated by 'bitNum' to '1'. */
123 
124  virtual
125  void ReadXML (XmlStream& s,
126  XmlTagConstPtr tag,
127  VolConstBool& cancelFlag,
128  RunLog& log
129  );
130 
131  virtual
132  void WriteXML (const KKStr& varName,
133  std::ostream& o
134  ) const;
135 
136 
137  /**
138  *@brief Returns a Hex-String representation.
139  *@details ex: "1110 0000 0101 1011" would return "E09B".
140  */
141  KKStr HexStr () const;
142 
143  BitString& operator= (const BitString& right);
144  BitString& operator|= (const BitString& right); /**< @brief Performs a bitwise OR against the left operand. */
145  BitString& operator+= (const BitString& right); /**< @brief Performs a bitwise OR against the left operand. */
146  BitString& operator&= (const BitString& right); /**< @brief Performs a bitwise AND against the left operand. */
147  BitString& operator*= (const BitString& right); /**< @brief Performs a bitwise AND against the left operand. */
148  BitString& operator^= (const BitString& right); /**< @brief Performs a bitwise XOR against the left operand. */
149  BitString operator^ (const BitString& right); /**< @brief Performs a bitwise XOR between two operands returning a new BitString. */
150 
151  bool operator== (const BitString& right) const;
152  bool operator!= (const BitString& right) const;
153  bool operator>= (const BitString& right) const;
154  bool operator> (const BitString& right) const;
155  bool operator<= (const BitString& right) const;
156  bool operator< (const BitString& right) const;
157 
158  private:
159  inline
160  void CalcByteAndBitOffsets (kkuint32 bitNum,
161  kkint32& byteOffset,
162  uchar& bitOffset
163  )
164  const;
165 
166  kkint32 Compare (const BitString& right) const;
167 
168  static uchar bitMasks [8];
169  static uchar bitMasksRev [8];
170  static uchar* bitCounts;
171 
172  /**
173  *@brief Initializes static array 'bitCounts' which maintains the number of bits in the binary representation of 0 - 255.
174  *@details The purpose of 'bitCounts' is to make the computation of total number of bits in a bit string stored in 'str' as fast as possible.
175  */
176  static void BuildBitCounts ();
177 
178  static kkint32 HexCharToInt (uchar hexChar);
179 
180  kkuint32 bitLen; /**< Number of bits to manage; (0 .. bitLen - ) */
181  kkuint32 byteLen; /**< Number of bytes required to manage 'bitLen' bits. */
182  uchar* str; /**< Where the bits will be stored; will be 'byteLen' bytes long.
183  * str[0] will contain bits 0 - 7.
184  */
185  }; /* BitString */
186 
188 
190 } /* namespace KKB */
191 
192 #endif
kkuint32 Count() const
Returns number of bits set to &#39;1&#39;.
Definition: BitString.cpp:142
BitString & operator&=(const BitString &right)
Performs a bitwise AND against the left operand.
Definition: BitString.cpp:484
std::vector< kkuint32 > VectorUint32
Vector of unsigned 32 bit integers.
Definition: KKBaseTypes.h:145
__int32 kkint32
Definition: KKBaseTypes.h:88
void ReSet(kkuint32 bitNum)
Set the bit indicated by &#39;bitNum&#39; to &#39;0&#39;.
Definition: BitString.cpp:215
virtual BitString * Duplicate() const
Definition: BitString.cpp:124
static BitString FromHexStr(const KKStr &hexStr, bool &validHexStr)
Create a bit-string from a Hex String.
Definition: BitString.cpp:399
void Set()
Set all bits to &#39;1&#39;.
Definition: BitString.cpp:173
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
Definition: BitString.cpp:618
KKStr HexStr() const
Returns a Hex-String representation.
Definition: BitString.cpp:361
void ReSet()
Set all bits to &#39;0&#39;.
Definition: BitString.cpp:207
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: BitString.cpp:650
bool operator!=(const BitString &right) const
Definition: BitString.cpp:552
void PopulateVectorBool(VectorBool &boolVector) const
Populates a boolean vector where each element reflects whether the corresponding bit is set...
Definition: BitString.cpp:239
BitString & operator^=(const BitString &right)
Performs a bitwise XOR against the left operand.
Definition: BitString.cpp:580
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
bool operator>(const BitString &right) const
Definition: BitString.cpp:559
BitString & operator|=(const BitString &right)
Performs a bitwise OR against the left operand.
Definition: BitString.cpp:459
XmlElementTemplate< BitString > XmlElementBitString
Definition: BitString.h:189
BitString operator^(const BitString &right)
Performs a bitwise XOR between two operands returning a new BitString.
Definition: BitString.cpp:598
BitString & operator+=(const BitString &right)
Performs a bitwise OR against the left operand.
Definition: BitString.cpp:475
bool operator<=(const BitString &right) const
bool Test(kkuint32 bitNum) const
Definition: BitString.cpp:158
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
std::vector< bool > VectorBool
Definition: KKBaseTypes.h:135
bool operator==(const BitString &right) const
Definition: BitString.cpp:545
Allows you to manage very long bit strings.
Definition: BitString.h:31
BitString * BitStringPtr
Definition: BitString.h:187
KKTHread * KKTHreadPtr
BitString(const BitString &b)
Copy constructor.
Definition: BitString.cpp:78
kkuint32 BitLen() const
Returns the length of the bit-string.
Definition: BitString.h:64
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
BitString & operator=(const BitString &right)
Definition: BitString.cpp:441
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
Base class of all other classes that are meant to be managed by &#39;KKBase&#39;.
Definition: Atom.h:49
void ListOfSetBits16(VectorUint16 &setBits) const
Definition: BitString.cpp:266
void Set(kkuint32 bitNum)
Set the bit indicated by &#39;bitNum&#39; to &#39;1&#39;.
Definition: BitString.cpp:182
BitString()
Instantiates a empty bit-string of length 0; needed for ReadXML.
Definition: BitString.cpp:58
BitString & operator*=(const BitString &right)
Performs a bitwise AND against the left operand.
Definition: BitString.cpp:504
BitString(kkuint32 _bitLen, kkuint16 *_bitNums, kkuint32 _bitNumsLen)
Construct a BitString of length _bitLen with bits indicated by &#39;_bitNums&#39; set to &#39;1&#39;.
Definition: BitString.cpp:89
bool operator<(const BitString &right) const
Definition: BitString.cpp:573
Used for logging messages.
Definition: RunLog.h:49
BitString(kkuint32 _bitLen)
Construct a bit string of length _binLen with all bits set to &#39;0&#39;.
Definition: BitString.cpp:68
std::vector< kkuint16 > VectorUint16
Vector of unsigned 16 bit integers.
Definition: KKBaseTypes.h:141
const char * ClassName() const
Definition: BitString.h:66
void ListOfSetBits32(VectorUint32 &setBits) const
Definition: BitString.cpp:317
bool operator>=(const BitString &right) const
Definition: BitString.cpp:566
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163