Geant4  10.03
G4SurfBits.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4SurfBits.hh 66356 2012-12-18 09:02:32Z gcosmo $
28 //
29 // --------------------------------------------------------------------
30 // GEANT 4 class header file
31 //
32 // G4SurfBits
33 //
34 // Class description:
35 //
36 // This class provides a simple container of bits, to be used for
37 // optimization of tessellated surfaces (G4TessellatedSolid).
38 // Each bit can be set and tested via the functions SetBitNumber and
39 // TestBitNumber.
40 // The default value of all bits is false.
41 // The size of the container is automatically extended when a bit
42 // number is either set or tested. To reduce the memory size of the
43 // container use the Compact function, this will discard the memory
44 // occupied by the upper bits that are 0.
45 //
46 
47 // History:
48 // 19.10.12 Marek Gayer, created and adapted from original implementation
49 // of Root's TBits class by P.Canal
50 // --------------------------------------------------------------------
51 
52 #ifndef G4SurfBits_HH
53 #define G4SurfBits_HH
54 
55 #include <cstring>
56 
57 #include "G4Types.hh"
58 
60 {
61  public:
62 
63  G4SurfBits(unsigned int nbits = 0);
64  G4SurfBits(const G4SurfBits&);
66  ~G4SurfBits();
67 
68  //----- Bit manipulation
69  void ResetAllBits(G4bool value=false); // if value=1 set all bits to 1
70  void ResetBitNumber(unsigned int bitnumber);
71  void SetBitNumber(unsigned int bitnumber, G4bool value = true);
72  G4bool TestBitNumber(unsigned int bitnumber) const;
73 
74  //----- Accessors and operator
75  G4bool operator[](unsigned int bitnumber) const;
76 
77  //----- Optimized setters
78  // Each of these will replace the contents of the receiver with the
79  // bitvector in the parameter array. The number of bits is changed
80  // to nbits. If nbits is smaller than fNBits, the receiver will NOT
81  // be compacted.
82  void set(unsigned int nbits, const char *array);
83  void set(unsigned int nbits, const G4int *array);
84 
85  //----- Optimized getters
86  // Each of these will replace the contents of the parameter array with the
87  // bits in the receiver. The parameter array must be large enough to hold
88  // all of the bits in the receiver.
89  // Note on semantics: any bits in the parameter array that go beyond the
90  // number of the bits in the receiver will have an unspecified value. For
91  // example, if you call Get(Int*) with an array of one integer and the
92  // G4SurfBits object has less than 32 bits, then the remaining bits in the
93  // integer will have an unspecified value.
94  void Get(char *array) const;
95  void Get(G4int *array) const;
96 
97  //----- Utilities
98  void Clear();
99  void Compact(); // Reduce the space used.
100 
101  unsigned int GetNbits() const { return fNBits; }
102  unsigned int GetNbytes() const { return fNBytes; }
103 
104  void Print() const; // to show the list of active bits
105  void Output(std::ostream &) const;
106 
107  protected:
108 
109  void ReserveBytes(unsigned int nbytes);
110 
111  public:
112 
113  unsigned char *fAllBits; // [fNBytes] array of UChars
114 
115  protected:
116 
117  unsigned int fNBits; // Highest bit set + 1
118  unsigned int fNBytes; // Number of UChars in fAllBits
119 };
120 
121 // inline functions...
122 
123 inline void G4SurfBits::SetBitNumber(unsigned int bitnumber, G4bool value)
124 {
125  // set bit number 'bitnumber' to be value
126  if (bitnumber >= fNBits) {
127  unsigned int new_size = (bitnumber/8) + 1;
128  if (new_size > fNBytes) {
129  if (new_size < 100 * 1024 * 1024)
130  new_size *= 2;
131  unsigned char *old_location = fAllBits;
132  fAllBits = new unsigned char[new_size];
133  std::memcpy(fAllBits,old_location,fNBytes);
134  std::memset(fAllBits+fNBytes ,0, new_size-fNBytes);
135  fNBytes = new_size;
136  delete [] old_location;
137  }
138  fNBits = bitnumber+1;
139  }
140  unsigned int loc = bitnumber/8;
141  unsigned char bit = bitnumber%8;
142  if (value)
143  fAllBits[loc] |= (1<<bit);
144  else
145  fAllBits[loc] &= (0xFF ^ (1<<bit));
146 }
147 
148 inline G4bool G4SurfBits::TestBitNumber(unsigned int bitnumber) const
149 {
150  // Return the current value of the bit
151 
152  if (bitnumber >= fNBits) return false;
153  unsigned int loc = bitnumber/8;
154  unsigned char value = fAllBits[loc];
155  unsigned char bit = bitnumber%8;
156  G4bool result = (value & (1<<bit)) != 0;
157  return result;
158  // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
159 }
160 
161 inline void G4SurfBits::ResetBitNumber(unsigned int bitnumber)
162 {
163  SetBitNumber(bitnumber,false);
164 }
165 
166 inline G4bool G4SurfBits::operator[](unsigned int bitnumber) const
167 {
168  return TestBitNumber(bitnumber);
169 }
170 
171 #endif
void ResetBitNumber(unsigned int bitnumber)
Definition: G4SurfBits.hh:161
unsigned int GetNbits() const
Definition: G4SurfBits.hh:101
void Clear()
Definition: G4SurfBits.cc:92
G4SurfBits(unsigned int nbits=0)
Definition: G4SurfBits.cc:43
void Compact()
Definition: G4SurfBits.cc:103
void ReserveBytes(unsigned int nbytes)
Definition: G4SurfBits.cc:160
int G4int
Definition: G4Types.hh:78
G4bool operator[](unsigned int bitnumber) const
Definition: G4SurfBits.hh:166
void SetBitNumber(unsigned int bitnumber, G4bool value=true)
Definition: G4SurfBits.hh:123
G4SurfBits & operator=(const G4SurfBits &)
Definition: G4SurfBits.cc:65
unsigned char * fAllBits
Definition: G4SurfBits.hh:113
void Print() const
Definition: G4SurfBits.cc:139
G4bool TestBitNumber(unsigned int bitnumber) const
Definition: G4SurfBits.hh:148
void Output(std::ostream &) const
Definition: G4SurfBits.cc:126
bool G4bool
Definition: G4Types.hh:79
void Get(char *array) const
Definition: G4SurfBits.cc:186
void ResetAllBits(G4bool value=false)
Definition: G4SurfBits.cc:154
void set(unsigned int nbits, const char *array)
Definition: G4SurfBits.cc:174
unsigned int fNBytes
Definition: G4SurfBits.hh:118
unsigned int fNBits
Definition: G4SurfBits.hh:117
unsigned int GetNbytes() const
Definition: G4SurfBits.hh:102