Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4SurfBits.cc
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 implementation
27//
28// 19.10.12 - Marek Gayer, created and adapted from Root/TBits class
29// --------------------------------------------------------------------
30
31#include "G4SurfBits.hh"
32#include "G4ios.hh"
33
34//______________________________________________________________________________
35G4SurfBits::G4SurfBits(unsigned int nBits) : 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}
45
46//______________________________________________________________________________
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}
55
56//______________________________________________________________________________
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}
78
79//______________________________________________________________________________
81{
82 // G4SurfBits destructor
83
84 delete [] fAllBits;
85}
86
87//______________________________________________________________________________
89{
90 // Clear the value.
91
92 delete [] fAllBits;
93 fAllBits = nullptr;
94 fNBits = 0;
95 fNBytes = 0;
96}
97
98//______________________________________________________________________________
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}
120
121//______________________________________________________________________________
122void G4SurfBits::Output(std::ostream &os) const
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}
135
136//______________________________________________________________________________
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}
152
153//______________________________________________________________________________
155{
156 if (fAllBits != nullptr) { std::memset(fAllBits, value ? 0xFF : 0, fNBytes); }
157}
158
159//______________________________________________________________________________
160void G4SurfBits::ReserveBytes(unsigned int nbytes)
161{
162 // Reverse each bytes.
163
164 if (nbytes > fNBytes)
165 {
166 // do it in this order to remain exception-safe.
167 auto newBits = new unsigned char[nbytes];
168 delete [] fAllBits;
169 fNBytes = nbytes;
170 fAllBits = newBits;
171 }
172}
173
174//______________________________________________________________________________
175void G4SurfBits::set(unsigned int nBits, const char* array)
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}
185
186//______________________________________________________________________________
187void G4SurfBits::Get(char* array) const
188{
189 // Copy all the bytes.
190 std::memcpy(array, fAllBits, (fNBits+7)>>3);
191}
192
193// If we are on a little endian machine, a bitvector represented using
194// any integer type is identical to a bitvector represented using bytes.
195
196//______________________________________________________________________________
197void G4SurfBits::set(unsigned int nBits, const G4int* array)
198{
199 // set all the bytes.
200
201 set(nBits, (const char*)array);
202}
203
204//______________________________________________________________________________
205void G4SurfBits::Get(G4int* array) const
206{
207 // Get all the bytes.
208
209 Get((char*)array);
210}
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
void Output(std::ostream &) const
G4SurfBits & operator=(const G4SurfBits &)
Definition G4SurfBits.cc:57
unsigned char * fAllBits
void ResetAllBits(G4bool value=false)
void Get(char *array) const
void Clear()
Definition G4SurfBits.cc:88
void Compact()
Definition G4SurfBits.cc:99
void set(unsigned int nbits, const char *array)
G4SurfBits(unsigned int nbits=0)
Definition G4SurfBits.cc:35
void Print() const