Geant4  10.01.p01
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 //
27 // $Id: G4SurfBits.cc 66356 2012-12-18 09:02:32Z gcosmo $
28 //
29 // --------------------------------------------------------------------
30 // GEANT 4 class source file
31 //
32 // G4SurfBits implementation
33 //
34 // History:
35 // 19.10.12 Marek Gayer, created and adapted from original implementation
36 // of Root's TBits class by P.Canal
37 // --------------------------------------------------------------------
38 
39 #include "G4SurfBits.hh"
40 #include "G4ios.hh"
41 
42 //______________________________________________________________________________
43 G4SurfBits::G4SurfBits(unsigned int nBits) : fNBits(nBits)
44 {
45  // G4SurfBits constructor. All bits set to 0
46 
47  if (fNBits <= 0) fNBits = 0;
48  fNBytes = fNBits ? ((fNBits-1)/8) + 1 : 1;
49  fAllBits = new unsigned char[fNBytes];
50  // this is redundant only with libNew
51  std::memset(fAllBits,0,fNBytes);
52 }
53 
54 //______________________________________________________________________________
55 G4SurfBits::G4SurfBits(const G4SurfBits &original) : fNBits(original.fNBits),
56  fNBytes(original.fNBytes)
57 {
58  // G4SurfBits copy constructor
59 
60  fAllBits = new unsigned char[fNBytes];
61  std::memcpy(fAllBits,original.fAllBits,fNBytes);
62 }
63 
64 //______________________________________________________________________________
66 {
67  // G4SurfBits assignment operator
68  if (this != &rhs) {
69  // TObject::operator=(rhs);
70  fNBits = rhs.fNBits;
71  fNBytes = rhs.fNBytes;
72  delete [] fAllBits;
73  if (fNBytes != 0) {
74  fAllBits = new unsigned char[fNBytes];
75  std::memcpy(fAllBits,rhs.fAllBits,fNBytes);
76  } else {
77  fAllBits = 0;
78  }
79  }
80  return *this;
81 }
82 
83 //______________________________________________________________________________
85 {
86  // G4SurfBits destructor
87 
88  delete [] fAllBits;
89 }
90 
91 //______________________________________________________________________________
93 {
94  // Clear the value.
95 
96  delete [] fAllBits;
97  fAllBits = 0;
98  fNBits = 0;
99  fNBytes = 0;
100 }
101 
102 //______________________________________________________________________________
104 {
105  // Reduce the storage used by the object to a minimun
106 
107  if (!fNBits || !fAllBits) return;
108  unsigned int needed;
109  for(needed=fNBytes-1;
110  needed > 0 && fAllBits[needed]==0; ) { needed--; };
111  needed++;
112 
113  if (needed!=fNBytes) {
114  unsigned char *old_location = fAllBits;
115  fAllBits = new unsigned char[needed];
116 
117  std::memcpy(fAllBits,old_location,needed);
118  delete [] old_location;
119 
120  fNBytes = needed;
121  fNBits = 8*fNBytes;
122  }
123 }
124 
125 //______________________________________________________________________________
126 void G4SurfBits::Output(std::ostream &os) const
127 {
128  // Print the value to the std::ostream
129  for(unsigned int i=0; i<fNBytes; ++i) {
130  unsigned char val = fAllBits[fNBytes - 1 - i];
131  for (unsigned int j=0; j<8; ++j) {
132  os << (G4bool)(val&0x80);
133  val <<= 1;
134  }
135  }
136 }
137 
138 //______________________________________________________________________________
139 void G4SurfBits::Print() const
140 {
141  // Print the list of active bits
142  G4int count = 0;
143  for(unsigned int i=0; i<fNBytes; ++i) {
144  unsigned char val = fAllBits[i];
145  for (unsigned int j=0; j<8; ++j) {
146  if (val & 1) G4cout << " bit:" << count << " = 1" << G4endl;
147  count++;
148  val = val >> 1;
149  }
150  }
151 }
152 
153 //______________________________________________________________________________
155 {
156  if (fAllBits) std::memset(fAllBits, value ? 0xFF : 0,fNBytes);
157 }
158 
159 //______________________________________________________________________________
160 void G4SurfBits::ReserveBytes(unsigned int nbytes)
161 {
162  // Reverse each bytes.
163 
164  if (nbytes > fNBytes) {
165  // do it in this order to remain exception-safe.
166  unsigned char *newBits=new unsigned char[nbytes];
167  delete[] fAllBits;
168  fNBytes=nbytes;
169  fAllBits=newBits;
170  }
171 }
172 
173 //______________________________________________________________________________
174 void G4SurfBits::set(unsigned int nBits, const char *array)
175 {
176  // set all the bytes
177  unsigned int nbytes=(nBits+7)>>3;
178 
179  ReserveBytes(nbytes);
180 
181  fNBits=nBits;
182  std::memcpy(fAllBits, array, nbytes);
183 }
184 
185 //______________________________________________________________________________
186 void G4SurfBits::Get(char *array) const
187 {
188  // Copy all the byes.
189  std::memcpy(array, fAllBits, (fNBits+7)>>3);
190 }
191 
192 // If we are on a little endian machine, a bitvector represented using
193 // any integer type is identical to a bitvector represented using bytes.
194 
195 //______________________________________________________________________________
196 void G4SurfBits::set(unsigned int nBits, const G4int *array)
197 {
198  // set all the bytes.
199 
200  set(nBits, (const char*)array);
201 }
202 
203 //______________________________________________________________________________
204 void G4SurfBits::Get(G4int *array) const
205 {
206  // Get all the bytes.
207 
208  Get((char*)array);
209 }
void Clear()
Definition: G4SurfBits.cc:92
G4SurfBits(unsigned int nbits=0)
Definition: G4SurfBits.cc:43
void Compact()
Definition: G4SurfBits.cc:103
void ReserveBytes(unsigned int nbytes)
Definition: G4SurfBits.cc:160
int G4int
Definition: G4Types.hh:78
G4SurfBits & operator=(const G4SurfBits &)
Definition: G4SurfBits.cc:65
unsigned char * fAllBits
Definition: G4SurfBits.hh:113
void Print() const
Definition: G4SurfBits.cc:139
G4GLOB_DLL std::ostream G4cout
void Output(std::ostream &) const
Definition: G4SurfBits.cc:126
bool G4bool
Definition: G4Types.hh:79
void Get(char *array) const
Definition: G4SurfBits.cc:186
void ResetAllBits(G4bool value=false)
Definition: G4SurfBits.cc:154
void set(unsigned int nbits, const char *array)
Definition: G4SurfBits.cc:174
unsigned int fNBytes
Definition: G4SurfBits.hh:118
#define G4endl
Definition: G4ios.hh:61
unsigned int fNBits
Definition: G4SurfBits.hh:117