Geant4  10.00.p01
UBits.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * This Software is part of the AIDA Unified Solids Library package *
4 // * See: https://aidasoft.web.cern.ch/USolids *
5 // ********************************************************************
6 //
7 // $Id:$
8 //
9 // --------------------------------------------------------------------
10 //
11 // UBits
12 //
13 // Class description:
14 //
15 // Container of bits
16 //
17 // This class provides a simple container of bits.
18 // Each bit can be set and tested via the functions SetBitNumber and
19 // TestBitNumber.
20 // The default value of all bits is false.
21 // The size of the container is automatically extended when a bit
22 // number is either set or tested. To reduce the memory size of the
23 // container use the Compact function, this will discard the memory
24 // occupied by the upper bits that are 0.
25 //
26 // Created for UTessellatedSolid
27 //
28 // 19.10.12 Marek Gayer
29 // Created from original implementation in ROOT (TBits)
30 // --------------------------------------------------------------------
31 
32 #ifndef UBits_HH
33 #define UBits_HH
34 
35 #include <cstring>
36 #include <ostream>
37 
38 class UBits
39 {
40 
41  public:
42  unsigned char* fAllBits; //[fNBytes] array of UChars
43 
44  protected:
45 
46  unsigned int fNBits; // Highest bit set + 1
47  unsigned int fNBytes; // Number of UChars in fAllBits
48 
49  void ReserveBytes(unsigned int nbytes);
50 
51  /*
52  void DoAndEqual(const UBits& rhs);
53  void DoOrEqual (const UBits& rhs);
54  void DoXorEqual(const UBits& rhs);
55  void DoLeftShift(unsigned int shift);
56  void DoRightShift(unsigned int shift);
57  void DoFlip();
58  */
59 
60  public:
61  UBits(unsigned int nbits = 0);
62  UBits(const UBits&);
63  UBits& operator=(const UBits& rhs);
64  virtual ~UBits();
65 
66  //----- bit manipulation
67  //----- (note the difference with TObject's bit manipulations)
68  void ResetAllBits(bool value = false); // if value=1 set all bits to 1
69  void ResetBitNumber(unsigned int bitnumber);
70  void SetBitNumber(unsigned int bitnumber, bool value = true);
71  bool TestBitNumber(unsigned int bitnumber) const;
72 
73  //----- Accessors and operator
74  bool operator[](unsigned int bitnumber) const;
75 
76  /*
77  UBits& operator&=(const UBits& rhs) { DoAndEqual(rhs); return *this; }
78  UBits& operator|=(const UBits& rhs) { DoOrEqual(rhs); return *this; }
79  UBits& operator^=(const UBits& rhs) { DoXorEqual(rhs); return *this; }
80  UBits& operator<<=(unsigned int rhs) { DoLeftShift(rhs); return *this; }
81  UBits& operator>>=(unsigned int rhs) { DoRightShift(rhs); return *this; }
82  UBits operator<<(unsigned int rhs) { return UBits(*this)<<= rhs; }
83  UBits operator>>(unsigned int rhs) { return UBits(*this)>>= rhs; }
84  UBits operator~() { UBits res(*this); res.DoFlip(); return res; }
85  */
86 
87  //----- Optimized setters
88  // Each of these will replace the contents of the receiver with the bitvector
89  // in the parameter array. The number of bits is changed to nbits. If nbits
90  // is smaller than fNBits, the receiver will NOT be compacted.
91 
92  void Set(unsigned int nbits, const char* array);
93 // void Set(unsigned int nbits, const unsigned char *array) { Set(nbits, (const char*)array); }
94 // void Set(unsigned int nbits, const short *array);
95  //void Set(unsigned int nbits, const unsigned short *array) { Set(nbits, (const short*)array); }
96  void Set(unsigned int nbits, const int* array);
97 // void Set(unsigned int nbits, const unsigned int *array) { Set(nbits, (const int*)array); }
98 
99  //----- Optimized getters
100  // Each of these will replace the contents of the parameter array with the
101  // bits in the receiver. The parameter array must be large enough to hold
102  // all of the bits in the receiver.
103  // Note on semantics: any bits in the parameter array that go beyond the
104  // number of the bits in the receiver will have an unspecified value. For
105  // example, if you call Get(Int*) with an array of one integer and the UBits
106  // object has less than 32 bits, then the remaining bits in the integer will
107  // have an unspecified value.
108  void Get(char* array) const;
109 // void Get(unsigned char *array) const { Get((char*)array); }
110 // void Get(short *array) const;
111 // void Get(unsigned short *array) const { Get((short*)array); }
112  void Get(int* array) const;
113 // void Get(unsigned int *array) const { Get((int*)array); }
114 
115  //----- Utilities
116  void Clear();
117  void Compact(); // Reduce the space used.
118 
119 
120  unsigned int GetNbits() const
121  {
122  return fNBits;
123  }
124  unsigned int GetNbytes() const
125  {
126  return fNBytes;
127  }
128 
129  /*
130  unsigned int CounUBits(unsigned int startBit=0) const ; // return number of bits set to 1
131  unsigned int FirstNullBit(unsigned int startBit=0) const;
132  unsigned int FirstSetBit(unsigned int startBit=0) const;
133  */
134 
135 // bool operator==(const UBits &other) const;
136 // bool operator!=(const UBits &other) const { return !(*this==other); }
137 
138  void Print() const; // to show the list of active bits
139  void Output(std::ostream&) const;
140 };
141 
142 /*
143 inline UBits operator&(const UBits& lhs, const UBits& rhs)
144 {
145  UBits result(lhs);
146  result &= rhs;
147  return result;
148 }
149 
150 inline UBits operator|(const UBits& lhs, const UBits& rhs)
151 {
152  UBits result(lhs);
153  result |= rhs;
154  return result;
155 }
156 
157 inline UBits operator^(const UBits& lhs, const UBits& rhs)
158 {
159  UBits result(lhs);
160  result ^= rhs;
161  return result;
162 }
163 
164 inline std::ostream &operator<<(std::ostream& os, const UBits& rhs)
165 {
166  rhs.Output(os); return os;
167 }
168 */
169 
170 // inline functions...
171 
172 inline void UBits::SetBitNumber(unsigned int bitnumber, bool value)
173 {
174  // Set bit number 'bitnumber' to be value
175  if (bitnumber >= fNBits)
176  {
177  unsigned int new_size = (bitnumber / 8) + 1;
178  if (new_size > fNBytes)
179  {
180  if (new_size < 100 * 1024 * 1024)
181  new_size *= 2;
182  unsigned char* old_location = fAllBits;
183  fAllBits = new unsigned char[new_size];
184  std::memcpy(fAllBits, old_location, fNBytes);
185  std::memset(fAllBits + fNBytes , 0, new_size - fNBytes);
186  fNBytes = new_size;
187  delete [] old_location;
188  }
189  fNBits = bitnumber + 1;
190  }
191  unsigned int loc = bitnumber / 8;
192  unsigned char bit = bitnumber % 8;
193  if (value)
194  fAllBits[loc] |= (1 << bit);
195  else
196  fAllBits[loc] &= (0xFF ^ (1 << bit));
197 }
198 
199 inline bool UBits::TestBitNumber(unsigned int bitnumber) const
200 {
201  // Return the current value of the bit
202 
203  if (bitnumber >= fNBits) return false;
204  unsigned int loc = bitnumber / 8;
205  unsigned char value = fAllBits[loc];
206  unsigned char bit = bitnumber % 8;
207  bool result = (value & (1 << bit)) != 0;
208  return result;
209  // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
210 }
211 
212 inline void UBits::ResetBitNumber(unsigned int bitnumber)
213 {
214  SetBitNumber(bitnumber, false);
215 }
216 
217 inline bool UBits::operator[](unsigned int bitnumber) const
218 {
219  return TestBitNumber(bitnumber);
220 }
221 
222 #endif
unsigned int fNBits
Definition: UBits.hh:46
unsigned int GetNbytes() const
Definition: UBits.hh:124
void Output(std::ostream &) const
Definition: UBits.cc:354
UBits & operator=(const UBits &rhs)
Definition: UBits.cc:44
unsigned int GetNbits() const
Definition: UBits.hh:120
void ResetAllBits(bool value=false)
Definition: UBits.cc:386
unsigned char * fAllBits
Definition: UBits.hh:42
bool operator[](unsigned int bitnumber) const
Definition: UBits.hh:217
void Clear()
Definition: UBits.cc:72
void Set(unsigned int nbits, const char *array)
Definition: UBits.cc:407
void SetBitNumber(unsigned int bitnumber, bool value=true)
Definition: UBits.hh:172
virtual ~UBits()
Definition: UBits.cc:64
void Print() const
Definition: UBits.cc:369
unsigned int fNBytes
Definition: UBits.hh:47
UBits(unsigned int nbits=0)
Definition: UBits.cc:20
bool TestBitNumber(unsigned int bitnumber) const
Definition: UBits.hh:199
void ReserveBytes(unsigned int nbytes)
Definition: UBits.cc:392
void ResetBitNumber(unsigned int bitnumber)
Definition: UBits.hh:212
void Get(char *array) const
Definition: UBits.cc:419
Definition: UBits.hh:38
void Compact()
Definition: UBits.cc:83