Geant4  10.01.p02
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  UBits(unsigned int nbits = 0);
43  UBits(const UBits&);
44  UBits& operator=(const UBits& rhs);
45  virtual ~UBits();
46 
47  //----- bit manipulation
48  //----- (note the difference with TObject's bit manipulations)
49  void ResetAllBits(bool value = false); // if value=1 set all bits to 1
50  void ResetBitNumber(unsigned int bitnumber);
51  void SetBitNumber(unsigned int bitnumber, bool value = true);
52  bool TestBitNumber(unsigned int bitnumber) const;
53 
54  //----- Accessors and operator
55  bool operator[](unsigned int bitnumber) const;
56 
57  /*
58  UBits& operator&=(const UBits& rhs) { DoAndEqual(rhs); return *this; }
59  UBits& operator|=(const UBits& rhs) { DoOrEqual(rhs); return *this; }
60  UBits& operator^=(const UBits& rhs) { DoXorEqual(rhs); return *this; }
61  UBits& operator<<=(unsigned int rhs) { DoLeftShift(rhs); return *this; }
62  UBits& operator>>=(unsigned int rhs) { DoRightShift(rhs); return *this; }
63  UBits operator<<(unsigned int rhs) { return UBits(*this)<<= rhs; }
64  UBits operator>>(unsigned int rhs) { return UBits(*this)>>= rhs; }
65  UBits operator~() { UBits res(*this); res.DoFlip(); return res; }
66  */
67 
68  //----- Optimized setters
69  // Each of these will replace the contents of the receiver with the bitvector
70  // in the parameter array. The number of bits is changed to nbits. If nbits
71  // is smaller than fNBits, the receiver will NOT be compacted.
72 
73  void Set(unsigned int nbits, const char* array);
74 // void Set(unsigned int nbits, const unsigned char *array) { Set(nbits, (const char*)array); }
75 // void Set(unsigned int nbits, const short *array);
76  //void Set(unsigned int nbits, const unsigned short *array) { Set(nbits, (const short*)array); }
77  void Set(unsigned int nbits, const int* array);
78 // void Set(unsigned int nbits, const unsigned int *array) { Set(nbits, (const int*)array); }
79 
80  //----- Optimized getters
81  // Each of these will replace the contents of the parameter array with the
82  // bits in the receiver. The parameter array must be large enough to hold
83  // all of the bits in the receiver.
84  // Note on semantics: any bits in the parameter array that go beyond the
85  // number of the bits in the receiver will have an unspecified value. For
86  // example, if you call Get(Int*) with an array of one integer and the UBits
87  // object has less than 32 bits, then the remaining bits in the integer will
88  // have an unspecified value.
89  void Get(char* array) const;
90 // void Get(unsigned char *array) const { Get((char*)array); }
91 // void Get(short *array) const;
92 // void Get(unsigned short *array) const { Get((short*)array); }
93  void Get(int* array) const;
94 // void Get(unsigned int *array) const { Get((int*)array); }
95 
96  //----- Utilities
97  void Clear();
98  void Compact(); // Reduce the space used.
99 
100 
101  unsigned int GetNbits() const
102  {
103  return fNBits;
104  }
105  unsigned int GetNbytes() const
106  {
107  return fNBytes;
108  }
109 
110  /*
111  unsigned int CounUBits(unsigned int startBit=0) const ; // return number of bits set to 1
112  unsigned int FirstNullBit(unsigned int startBit=0) const;
113  unsigned int FirstSetBit(unsigned int startBit=0) const;
114  */
115 
116 // bool operator==(const UBits &other) const;
117 // bool operator!=(const UBits &other) const { return !(*this==other); }
118 
119  void Print() const; // to show the list of active bits
120  void Output(std::ostream&) const;
121 
122  protected:
123  void ReserveBytes(unsigned int nbytes);
124 
125  /*
126  void DoAndEqual(const UBits& rhs);
127  void DoOrEqual (const UBits& rhs);
128  void DoXorEqual(const UBits& rhs);
129  void DoLeftShift(unsigned int shift);
130  void DoRightShift(unsigned int shift);
131  void DoFlip();
132  */
133 
134  protected:
135  unsigned int fNBits; // Highest bit set + 1
136  unsigned int fNBytes; // Number of UChars in fAllBits
137 
138  public:
139  unsigned char* fAllBits; //[fNBytes] array of UChars
140 
141 };
142 
143 /*
144 inline UBits operator&(const UBits& lhs, const UBits& rhs)
145 {
146  UBits result(lhs);
147  result &= rhs;
148  return result;
149 }
150 
151 inline UBits operator|(const UBits& lhs, const UBits& rhs)
152 {
153  UBits result(lhs);
154  result |= rhs;
155  return result;
156 }
157 
158 inline UBits operator^(const UBits& lhs, const UBits& rhs)
159 {
160  UBits result(lhs);
161  result ^= rhs;
162  return result;
163 }
164 
165 inline std::ostream &operator<<(std::ostream& os, const UBits& rhs)
166 {
167  rhs.Output(os); return os;
168 }
169 */
170 
171 // inline functions...
172 
173 inline void UBits::SetBitNumber(unsigned int bitnumber, bool value)
174 {
175  // Set bit number 'bitnumber' to be value
176  if (bitnumber >= fNBits)
177  {
178  unsigned int new_size = (bitnumber / 8) + 1;
179  if (new_size > fNBytes)
180  {
181  if (new_size < 100 * 1024 * 1024)
182  new_size *= 2;
183  unsigned char* old_location = fAllBits;
184  fAllBits = new unsigned char[new_size];
185  std::memcpy(fAllBits, old_location, fNBytes);
186  std::memset(fAllBits + fNBytes , 0, new_size - fNBytes);
187  fNBytes = new_size;
188  delete [] old_location;
189  }
190  fNBits = bitnumber + 1;
191  }
192  unsigned int loc = bitnumber / 8;
193  unsigned char bit = bitnumber % 8;
194  if (value)
195  fAllBits[loc] |= (1 << bit);
196  else
197  fAllBits[loc] &= (0xFF ^ (1 << bit));
198 }
199 
200 inline bool UBits::TestBitNumber(unsigned int bitnumber) const
201 {
202  // Return the current value of the bit
203 
204  if (bitnumber >= fNBits) return false;
205  unsigned int loc = bitnumber / 8;
206  unsigned char value = fAllBits[loc];
207  unsigned char bit = bitnumber % 8;
208  bool result = (value & (1 << bit)) != 0;
209  return result;
210  // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
211 }
212 
213 inline void UBits::ResetBitNumber(unsigned int bitnumber)
214 {
215  SetBitNumber(bitnumber, false);
216 }
217 
218 inline bool UBits::operator[](unsigned int bitnumber) const
219 {
220  return TestBitNumber(bitnumber);
221 }
222 
223 #endif
unsigned int fNBits
Definition: UBits.hh:135
unsigned int GetNbytes() const
Definition: UBits.hh:105
void Output(std::ostream &) const
Definition: UBits.cc:355
UBits & operator=(const UBits &rhs)
Definition: UBits.cc:45
unsigned int GetNbits() const
Definition: UBits.hh:101
void ResetAllBits(bool value=false)
Definition: UBits.cc:387
unsigned char * fAllBits
Definition: UBits.hh:139
bool operator[](unsigned int bitnumber) const
Definition: UBits.hh:218
void Clear()
Definition: UBits.cc:73
void Set(unsigned int nbits, const char *array)
Definition: UBits.cc:408
void SetBitNumber(unsigned int bitnumber, bool value=true)
Definition: UBits.hh:173
virtual ~UBits()
Definition: UBits.cc:65
void Print() const
Definition: UBits.cc:370
unsigned int fNBytes
Definition: UBits.hh:136
UBits(unsigned int nbits=0)
Definition: UBits.cc:20
bool TestBitNumber(unsigned int bitnumber) const
Definition: UBits.hh:200
void ReserveBytes(unsigned int nbytes)
Definition: UBits.cc:393
void ResetBitNumber(unsigned int bitnumber)
Definition: UBits.hh:213
void Get(char *array) const
Definition: UBits.cc:420
Definition: UBits.hh:38
void Compact()
Definition: UBits.cc:84