Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4SurfBits Class Reference

#include <G4SurfBits.hh>

Public Member Functions

 G4SurfBits (unsigned int nbits=0)
 
 G4SurfBits (const G4SurfBits &)
 
G4SurfBitsoperator= (const G4SurfBits &)
 
 ~G4SurfBits ()
 
void ResetAllBits (G4bool value=false)
 
void ResetBitNumber (unsigned int bitnumber)
 
void SetBitNumber (unsigned int bitnumber, G4bool value=true)
 
G4bool TestBitNumber (unsigned int bitnumber) const
 
G4bool operator[] (unsigned int bitnumber) const
 
void set (unsigned int nbits, const char *array)
 
void set (unsigned int nbits, const G4int *array)
 
void Get (char *array) const
 
void Get (G4int *array) const
 
void Clear ()
 
void Compact ()
 
unsigned int GetNbits () const
 
unsigned int GetNbytes () const
 
void Print () const
 
void Output (std::ostream &) const
 

Public Attributes

unsigned char * fAllBits = nullptr
 

Protected Member Functions

void ReserveBytes (unsigned int nbytes)
 

Protected Attributes

unsigned int fNBits
 
unsigned int fNBytes
 

Detailed Description

Definition at line 50 of file G4SurfBits.hh.

Constructor & Destructor Documentation

◆ G4SurfBits() [1/2]

G4SurfBits::G4SurfBits ( unsigned int  nbits = 0)

Definition at line 36 of file G4SurfBits.cc.

36 : fNBits(nBits)
37{
38 // G4SurfBits constructor. All bits set to 0
39
40 if (fNBits <= 0) fNBits = 0;
41 fNBytes = fNBits ? ((fNBits-1)/8) + 1 : 1;
42 fAllBits = new unsigned char[fNBytes];
43 // this is redundant only with libNew
44 std::memset(fAllBits,0,fNBytes);
45}
unsigned int fNBits
Definition: G4SurfBits.hh:108
unsigned char * fAllBits
Definition: G4SurfBits.hh:104
unsigned int fNBytes
Definition: G4SurfBits.hh:109

◆ G4SurfBits() [2/2]

G4SurfBits::G4SurfBits ( const G4SurfBits original)

Definition at line 48 of file G4SurfBits.cc.

49 : fNBits(original.fNBits), fNBytes(original.fNBytes)
50{
51 // G4SurfBits copy constructor
52
53 fAllBits = new unsigned char[fNBytes];
54 std::memcpy(fAllBits,original.fAllBits,fNBytes);
55}

◆ ~G4SurfBits()

G4SurfBits::~G4SurfBits ( )

Definition at line 81 of file G4SurfBits.cc.

82{
83 // G4SurfBits destructor
84
85 delete [] fAllBits;
86}

Member Function Documentation

◆ Clear()

void G4SurfBits::Clear ( )

Definition at line 89 of file G4SurfBits.cc.

90{
91 // Clear the value.
92
93 delete [] fAllBits;
94 fAllBits = nullptr;
95 fNBits = 0;
96 fNBytes = 0;
97}

Referenced by G4Voxelizer::Voxelize().

◆ Compact()

void G4SurfBits::Compact ( )

Definition at line 100 of file G4SurfBits.cc.

101{
102 // Reduce the storage used by the object to a minimun
103
104 if (!fNBits || !fAllBits) return;
105 unsigned int needed;
106 for(needed=fNBytes-1; needed > 0 && fAllBits[needed]==0; ) { --needed; }
107 ++needed;
108
109 if (needed!=fNBytes)
110 {
111 unsigned char* old_location = fAllBits;
112 fAllBits = new unsigned char[needed];
113
114 std::memcpy(fAllBits,old_location,needed);
115 delete [] old_location;
116
117 fNBytes = needed;
118 fNBits = 8*fNBytes;
119 }
120}

◆ Get() [1/2]

void G4SurfBits::Get ( char *  array) const

Definition at line 188 of file G4SurfBits.cc.

189{
190 // Copy all the bytes.
191 std::memcpy(array, fAllBits, (fNBits+7)>>3);
192}

Referenced by Get().

◆ Get() [2/2]

void G4SurfBits::Get ( G4int array) const

Definition at line 206 of file G4SurfBits.cc.

207{
208 // Get all the bytes.
209
210 Get((char*)array);
211}
void Get(char *array) const
Definition: G4SurfBits.cc:188

◆ GetNbits()

unsigned int G4SurfBits::GetNbits ( ) const
inline

Definition at line 92 of file G4SurfBits.hh.

92{ return fNBits; }

Referenced by G4TessellatedSolid::SafetyFromOutside().

◆ GetNbytes()

unsigned int G4SurfBits::GetNbytes ( ) const
inline

Definition at line 93 of file G4SurfBits.hh.

93{ return fNBytes; }

Referenced by G4TessellatedSolid::AllocatedMemory(), and G4Voxelizer::AllocatedMemory().

◆ operator=()

G4SurfBits & G4SurfBits::operator= ( const G4SurfBits rhs)

Definition at line 58 of file G4SurfBits.cc.

59{
60 // G4SurfBits assignment operator
61 if (this != &rhs)
62 {
63 // TObject::operator=(rhs);
64 fNBits = rhs.fNBits;
65 fNBytes = rhs.fNBytes;
66 delete [] fAllBits;
67 if (fNBytes != 0)
68 {
69 fAllBits = new unsigned char[fNBytes];
70 std::memcpy(fAllBits,rhs.fAllBits,fNBytes);
71 }
72 else
73 {
74 fAllBits = nullptr;
75 }
76 }
77 return *this;
78}

◆ operator[]()

G4bool G4SurfBits::operator[] ( unsigned int  bitnumber) const
inline

Definition at line 159 of file G4SurfBits.hh.

160{
161 return TestBitNumber(bitnumber);
162}
G4bool TestBitNumber(unsigned int bitnumber) const
Definition: G4SurfBits.hh:141

◆ Output()

void G4SurfBits::Output ( std::ostream &  os) const

Definition at line 123 of file G4SurfBits.cc.

124{
125 // Print the value to the std::ostream
126 for(unsigned int i=0; i<fNBytes; ++i)
127 {
128 unsigned char val = fAllBits[fNBytes - 1 - i];
129 for (unsigned int j=0; j<8; ++j)
130 {
131 os << (G4bool)(val&0x80);
132 val <<= 1;
133 }
134 }
135}
bool G4bool
Definition: G4Types.hh:86

◆ Print()

void G4SurfBits::Print ( ) const

Definition at line 138 of file G4SurfBits.cc.

139{
140 // Print the list of active bits
141 G4int count = 0;
142 for(unsigned int i=0; i<fNBytes; ++i)
143 {
144 unsigned char val = fAllBits[i];
145 for (unsigned int j=0; j<8; ++j)
146 {
147 if (val & 1) G4cout << " bit:" << count << " = 1" << G4endl;
148 ++count;
149 val = val >> 1;
150 }
151 }
152}
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout

◆ ReserveBytes()

void G4SurfBits::ReserveBytes ( unsigned int  nbytes)
protected

Definition at line 161 of file G4SurfBits.cc.

162{
163 // Reverse each bytes.
164
165 if (nbytes > fNBytes)
166 {
167 // do it in this order to remain exception-safe.
168 unsigned char *newBits = new unsigned char[nbytes];
169 delete [] fAllBits;
170 fNBytes = nbytes;
171 fAllBits = newBits;
172 }
173}

Referenced by set().

◆ ResetAllBits()

void G4SurfBits::ResetAllBits ( G4bool  value = false)

Definition at line 155 of file G4SurfBits.cc.

156{
157 if (fAllBits != nullptr) std::memset(fAllBits, value ? 0xFF : 0, fNBytes);
158}

◆ ResetBitNumber()

void G4SurfBits::ResetBitNumber ( unsigned int  bitnumber)
inline

Definition at line 154 of file G4SurfBits.hh.

155{
156 SetBitNumber(bitnumber,false);
157}
void SetBitNumber(unsigned int bitnumber, G4bool value=true)
Definition: G4SurfBits.hh:114

Referenced by G4MultiUnion::DistanceToOutVoxels().

◆ set() [1/2]

void G4SurfBits::set ( unsigned int  nbits,
const char *  array 
)

Definition at line 176 of file G4SurfBits.cc.

177{
178 // set all the bytes
179 unsigned int nbytes=(nBits+7)>>3;
180
181 ReserveBytes(nbytes);
182
183 fNBits=nBits;
184 std::memcpy(fAllBits, array, nbytes);
185}
void ReserveBytes(unsigned int nbytes)
Definition: G4SurfBits.cc:161

Referenced by G4Voxelizer::DisplayListNodes(), and set().

◆ set() [2/2]

void G4SurfBits::set ( unsigned int  nbits,
const G4int array 
)

Definition at line 198 of file G4SurfBits.cc.

199{
200 // set all the bytes.
201
202 set(nBits, (const char*)array);
203}
void set(unsigned int nbits, const char *array)
Definition: G4SurfBits.cc:176

◆ SetBitNumber()

void G4SurfBits::SetBitNumber ( unsigned int  bitnumber,
G4bool  value = true 
)
inline

Definition at line 114 of file G4SurfBits.hh.

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}

Referenced by G4MultiUnion::DistanceToOutVoxels(), and ResetBitNumber().

◆ TestBitNumber()

G4bool G4SurfBits::TestBitNumber ( unsigned int  bitnumber) const
inline

Definition at line 141 of file G4SurfBits.hh.

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}

Referenced by operator[]().

Member Data Documentation

◆ fAllBits

◆ fNBits

unsigned int G4SurfBits::fNBits
protected

◆ fNBytes

unsigned int G4SurfBits::fNBytes
protected

The documentation for this class was generated from the following files: