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

G4SurfBits provides a simple container of bits, to be used for optimization of tessellated surfaces. The size of the container is automatically extended when a bit number is either set or tested. More...

#include <G4SurfBits.hh>

Public Member Functions

 G4SurfBits (unsigned int nbits=0)
 ~G4SurfBits ()
 G4SurfBits (const G4SurfBits &)
G4SurfBitsoperator= (const 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

Detailed Description

G4SurfBits provides a simple container of bits, to be used for optimization of tessellated surfaces. The size of the container is automatically extended when a bit number is either set or tested.

Definition at line 56 of file G4SurfBits.hh.

Constructor & Destructor Documentation

◆ G4SurfBits() [1/2]

G4SurfBits::G4SurfBits ( unsigned int nbits = 0)

Constructor given the number of bits.

Parameters
[in]nbitsThe number of bits.

Definition at line 35 of file G4SurfBits.cc.

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

Referenced by G4SurfBits(), and operator=().

◆ ~G4SurfBits()

G4SurfBits::~G4SurfBits ( )

Destructor. Clears all allocated bits.

Definition at line 80 of file G4SurfBits.cc.

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

◆ G4SurfBits() [2/2]

G4SurfBits::G4SurfBits ( const G4SurfBits & original)

Copy constructor and assignment operator.

Definition at line 47 of file G4SurfBits.cc.

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

Member Function Documentation

◆ Clear()

void G4SurfBits::Clear ( )

Utilities to clear or reduce the space used.

Definition at line 88 of file G4SurfBits.cc.

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

◆ Compact()

void G4SurfBits::Compact ( )

Definition at line 99 of file G4SurfBits.cc.

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

◆ Get() [1/2]

void G4SurfBits::Get ( char * array) const

Optimized getters. Each of these will replace the contents of the parameter array with the bits in the receiver. The parameter array must be large enough to hold all of the bits in the receiver. Note on semantics: any bits in the parameter array that go beyond the number of the bits in the receiver will have an unspecified value. For example, if calling Get(Int*) with an array of one integer and the G4SurfBits object has less than 32 bits, then the remaining bits in the integer will have an unspecified value.

Definition at line 187 of file G4SurfBits.cc.

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

Referenced by Get().

◆ Get() [2/2]

void G4SurfBits::Get ( G4int * array) const

Definition at line 205 of file G4SurfBits.cc.

206{
207 // Get all the bytes.
208
209 Get((char*)array);
210}
void Get(char *array) const

◆ GetNbits()

unsigned int G4SurfBits::GetNbits ( ) const
inline

Accessors.

Definition at line 121 of file G4SurfBits.hh.

121{ return fNBits; }

◆ GetNbytes()

unsigned int G4SurfBits::GetNbytes ( ) const
inline

Definition at line 122 of file G4SurfBits.hh.

122{ return fNBytes; }

◆ operator=()

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

Definition at line 57 of file G4SurfBits.cc.

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

◆ operator[]()

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

Accessor operator.

◆ Output()

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

Definition at line 122 of file G4SurfBits.cc.

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

◆ Print()

void G4SurfBits::Print ( ) const

Logging functions.

Definition at line 137 of file G4SurfBits.cc.

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

◆ ResetAllBits()

void G4SurfBits::ResetAllBits ( G4bool value = false)

Methods for bit manipulation.

Definition at line 154 of file G4SurfBits.cc.

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

◆ ResetBitNumber()

void G4SurfBits::ResetBitNumber ( unsigned int bitnumber)
inline

◆ set() [1/2]

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

Optimized setters. Each of these will replace the contents of the receiver with the bitvector in the parameter array. The number of bits is changed to 'nbits'. If nbits is smaller than fNBits, the receiver will NOT be compacted.

Definition at line 175 of file G4SurfBits.cc.

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

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

◆ set() [2/2]

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

Definition at line 197 of file G4SurfBits.cc.

198{
199 // set all the bytes.
200
201 set(nBits, (const char*)array);
202}
void set(unsigned int nbits, const char *array)

◆ SetBitNumber()

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

◆ TestBitNumber()

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

Member Data Documentation

◆ fAllBits


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