Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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// G4SurfBits
27//
28// Class description:
29//
30// This class provides a simple container of bits, to be used for
31// optimization of tessellated surfaces (G4TessellatedSolid).
32// Each bit can be set and tested via the functions SetBitNumber and
33// TestBitNumber.
34// The default value of all bits is false.
35// The size of the container is automatically extended when a bit
36// number is either set or tested. To reduce the memory size of the
37// container use the Compact function, this will discard the memory
38// occupied by the upper bits that are 0.
39
40// 19.10.12 - Marek Gayer, created and adapted from original implementation
41// of Root's TBits class by P.Canal
42// --------------------------------------------------------------------
43#ifndef G4SURFBITS_HH
44#define G4SURFBITS_HH
45
46#include <cstring>
47
48#include "G4Types.hh"
49
51{
52 public:
53
54 G4SurfBits(unsigned int nbits = 0);
55 G4SurfBits(const G4SurfBits&);
58
59 //----- Bit manipulation
60 void ResetAllBits(G4bool value = false); // if value=1 set all bits to 1
61 void ResetBitNumber(unsigned int bitnumber);
62 void SetBitNumber(unsigned int bitnumber, G4bool value = true);
63 G4bool TestBitNumber(unsigned int bitnumber) const;
64
65 //----- Accessors and operator
66 G4bool operator[](unsigned int bitnumber) const;
67
68 //----- Optimized setters
69 // Each of these will replace the contents of the receiver with the
70 // bitvector in the parameter array. The number of bits is changed
71 // to nbits. If nbits is smaller than fNBits, the receiver will NOT
72 // be compacted.
73 void set(unsigned int nbits, const char* array);
74 void set(unsigned int nbits, const G4int* array);
75
76 //----- Optimized getters
77 // Each of these will replace the contents of the parameter array with the
78 // bits in the receiver. The parameter array must be large enough to hold
79 // all of the bits in the receiver.
80 // Note on semantics: any bits in the parameter array that go beyond the
81 // number of the bits in the receiver will have an unspecified value. For
82 // example, if you call Get(Int*) with an array of one integer and the
83 // G4SurfBits object has less than 32 bits, then the remaining bits in the
84 // integer will have an unspecified value.
85 void Get(char* array) const;
86 void Get(G4int* array) const;
87
88 //----- Utilities
89 void Clear();
90 void Compact(); // Reduce the space used.
91
92 unsigned int GetNbits() const { return fNBits; }
93 unsigned int GetNbytes() const { return fNBytes; }
94
95 void Print() const; // to show the list of active bits
96 void Output(std::ostream &) const;
97
98 protected:
99
100 void ReserveBytes(unsigned int nbytes);
101
102 public:
103
104 unsigned char* fAllBits = nullptr; // [fNBytes] array of UChars
105
106 protected:
107
108 unsigned int fNBits; // Highest bit set + 1
109 unsigned int fNBytes; // Number of UChars in fAllBits
110};
111
112// inline functions...
113
114inline void G4SurfBits::SetBitNumber(unsigned int bitnumber, G4bool value)
115{
116 // set bit number 'bitnumber' to be value
117
118 if (bitnumber >= fNBits)
119 {
120 unsigned int new_size = (bitnumber/8) + 1;
121 if (new_size > fNBytes)
122 {
123 if (new_size < 100 * 1024 * 1024) new_size *= 2;
124 unsigned char *old_location = fAllBits;
125 fAllBits = new unsigned char[new_size];
126 std::memcpy(fAllBits,old_location,fNBytes);
127 std::memset(fAllBits+fNBytes ,0, new_size-fNBytes);
128 fNBytes = new_size;
129 delete [] old_location;
130 }
131 fNBits = bitnumber+1;
132 }
133 unsigned int loc = bitnumber/8;
134 unsigned char bit = bitnumber%8;
135 if (value)
136 fAllBits[loc] |= (1<<bit);
137 else
138 fAllBits[loc] &= (0xFF ^ (1<<bit));
139}
140
141inline G4bool G4SurfBits::TestBitNumber(unsigned int bitnumber) const
142{
143 // Return the current value of the bit
144
145 if (bitnumber >= fNBits) return false;
146 unsigned int loc = bitnumber/8;
147 unsigned char value = fAllBits[loc];
148 unsigned char bit = bitnumber%8;
149 G4bool result = (value & (1<<bit)) != 0;
150 return result;
151 // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
152}
153
154inline void G4SurfBits::ResetBitNumber(unsigned int bitnumber)
155{
156 SetBitNumber(bitnumber,false);
157}
158
159inline G4bool G4SurfBits::operator[](unsigned int bitnumber) const
160{
161 return TestBitNumber(bitnumber);
162}
163
164#endif
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
unsigned int fNBits
Definition: G4SurfBits.hh:108
unsigned int GetNbits() const
Definition: G4SurfBits.hh:92
void Output(std::ostream &) const
Definition: G4SurfBits.cc:123
G4SurfBits & operator=(const G4SurfBits &)
Definition: G4SurfBits.cc:58
unsigned int GetNbytes() const
Definition: G4SurfBits.hh:93
unsigned char * fAllBits
Definition: G4SurfBits.hh:104
void ResetAllBits(G4bool value=false)
Definition: G4SurfBits.cc:155
void Get(char *array) const
Definition: G4SurfBits.cc:188
void Clear()
Definition: G4SurfBits.cc:89
void ResetBitNumber(unsigned int bitnumber)
Definition: G4SurfBits.hh:154
void Compact()
Definition: G4SurfBits.cc:100
void SetBitNumber(unsigned int bitnumber, G4bool value=true)
Definition: G4SurfBits.hh:114
void set(unsigned int nbits, const char *array)
Definition: G4SurfBits.cc:176
void ReserveBytes(unsigned int nbytes)
Definition: G4SurfBits.cc:161
G4bool operator[](unsigned int bitnumber) const
Definition: G4SurfBits.hh:159
void Print() const
Definition: G4SurfBits.cc:138
G4bool TestBitNumber(unsigned int bitnumber) const
Definition: G4SurfBits.hh:141
unsigned int fNBytes
Definition: G4SurfBits.hh:109