Geant4 11.1.1
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 original implementation
29// of Root's TBits class by P.Canal
30// --------------------------------------------------------------------
31
32#include "G4SurfBits.hh"
33#include "G4ios.hh"
34
35//______________________________________________________________________________
36G4SurfBits::G4SurfBits(unsigned int nBits) : 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}
46
47//______________________________________________________________________________
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}
56
57//______________________________________________________________________________
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}
79
80//______________________________________________________________________________
82{
83 // G4SurfBits destructor
84
85 delete [] fAllBits;
86}
87
88//______________________________________________________________________________
90{
91 // Clear the value.
92
93 delete [] fAllBits;
94 fAllBits = nullptr;
95 fNBits = 0;
96 fNBytes = 0;
97}
98
99//______________________________________________________________________________
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}
121
122//______________________________________________________________________________
123void G4SurfBits::Output(std::ostream &os) const
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}
136
137//______________________________________________________________________________
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}
153
154//______________________________________________________________________________
156{
157 if (fAllBits != nullptr) std::memset(fAllBits, value ? 0xFF : 0, fNBytes);
158}
159
160//______________________________________________________________________________
161void G4SurfBits::ReserveBytes(unsigned int nbytes)
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}
174
175//______________________________________________________________________________
176void G4SurfBits::set(unsigned int nBits, const char* array)
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}
186
187//______________________________________________________________________________
188void G4SurfBits::Get(char* array) const
189{
190 // Copy all the bytes.
191 std::memcpy(array, fAllBits, (fNBits+7)>>3);
192}
193
194// If we are on a little endian machine, a bitvector represented using
195// any integer type is identical to a bitvector represented using bytes.
196
197//______________________________________________________________________________
198void G4SurfBits::set(unsigned int nBits, const G4int* array)
199{
200 // set all the bytes.
201
202 set(nBits, (const char*)array);
203}
204
205//______________________________________________________________________________
206void G4SurfBits::Get(G4int* array) const
207{
208 // Get all the bytes.
209
210 Get((char*)array);
211}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
unsigned int fNBits
Definition: G4SurfBits.hh:108
void Output(std::ostream &) const
Definition: G4SurfBits.cc:123
G4SurfBits & operator=(const G4SurfBits &)
Definition: G4SurfBits.cc:58
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 Compact()
Definition: G4SurfBits.cc:100
void set(unsigned int nbits, const char *array)
Definition: G4SurfBits.cc:176
void ReserveBytes(unsigned int nbytes)
Definition: G4SurfBits.cc:161
G4SurfBits(unsigned int nbits=0)
Definition: G4SurfBits.cc:36
void Print() const
Definition: G4SurfBits.cc:138
unsigned int fNBytes
Definition: G4SurfBits.hh:109