Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4SurfBits implementation 26 // G4SurfBits implementation 27 // 27 // 28 // 19.10.12 - Marek Gayer, created and adapted 28 // 19.10.12 - Marek Gayer, created and adapted from original implementation 29 // of Root's TBits cla 29 // of Root's TBits class by P.Canal 30 // ------------------------------------------- 30 // -------------------------------------------------------------------- 31 31 32 #include "G4SurfBits.hh" 32 #include "G4SurfBits.hh" 33 #include "G4ios.hh" 33 #include "G4ios.hh" 34 34 35 //____________________________________________ 35 //______________________________________________________________________________ 36 G4SurfBits::G4SurfBits(unsigned int nBits) : f 36 G4SurfBits::G4SurfBits(unsigned int nBits) : fNBits(nBits) 37 { 37 { 38 // G4SurfBits constructor. All bits set to 38 // G4SurfBits constructor. All bits set to 0 39 39 40 if (fNBits <= 0) fNBits = 0; 40 if (fNBits <= 0) fNBits = 0; 41 fNBytes = fNBits != 0u ? ((fNBits-1)/8) + 1 << 41 fNBytes = fNBits ? ((fNBits-1)/8) + 1 : 1; 42 fAllBits = new unsigned char[fNBytes]; 42 fAllBits = new unsigned char[fNBytes]; 43 // this is redundant only with libNew 43 // this is redundant only with libNew 44 std::memset(fAllBits,0,fNBytes); 44 std::memset(fAllBits,0,fNBytes); 45 } 45 } 46 46 47 //____________________________________________ 47 //______________________________________________________________________________ 48 G4SurfBits::G4SurfBits(const G4SurfBits &origi 48 G4SurfBits::G4SurfBits(const G4SurfBits &original) 49 : fNBits(original.fNBits), fNBytes(original. 49 : fNBits(original.fNBits), fNBytes(original.fNBytes) 50 { 50 { 51 // G4SurfBits copy constructor 51 // G4SurfBits copy constructor 52 52 53 fAllBits = new unsigned char[fNBytes]; 53 fAllBits = new unsigned char[fNBytes]; 54 std::memcpy(fAllBits,original.fAllBits,fNByt 54 std::memcpy(fAllBits,original.fAllBits,fNBytes); 55 } 55 } 56 56 57 //____________________________________________ 57 //______________________________________________________________________________ 58 G4SurfBits& G4SurfBits::operator=(const G4Surf 58 G4SurfBits& G4SurfBits::operator=(const G4SurfBits& rhs) 59 { 59 { 60 // G4SurfBits assignment operator 60 // G4SurfBits assignment operator 61 if (this != &rhs) 61 if (this != &rhs) 62 { 62 { 63 // TObject::operator=(rhs); 63 // TObject::operator=(rhs); 64 fNBits = rhs.fNBits; 64 fNBits = rhs.fNBits; 65 fNBytes = rhs.fNBytes; 65 fNBytes = rhs.fNBytes; 66 delete [] fAllBits; 66 delete [] fAllBits; 67 if (fNBytes != 0) 67 if (fNBytes != 0) 68 { 68 { 69 fAllBits = new unsigned char[fNBytes]; 69 fAllBits = new unsigned char[fNBytes]; 70 std::memcpy(fAllBits,rhs.fAllBits,fNByte 70 std::memcpy(fAllBits,rhs.fAllBits,fNBytes); 71 } 71 } 72 else 72 else 73 { 73 { 74 fAllBits = nullptr; 74 fAllBits = nullptr; 75 } 75 } 76 } 76 } 77 return *this; 77 return *this; 78 } 78 } 79 79 80 //____________________________________________ 80 //______________________________________________________________________________ 81 G4SurfBits::~G4SurfBits() 81 G4SurfBits::~G4SurfBits() 82 { 82 { 83 // G4SurfBits destructor 83 // G4SurfBits destructor 84 84 85 delete [] fAllBits; 85 delete [] fAllBits; 86 } 86 } 87 87 88 //____________________________________________ 88 //______________________________________________________________________________ 89 void G4SurfBits::Clear() 89 void G4SurfBits::Clear() 90 { 90 { 91 // Clear the value. 91 // Clear the value. 92 92 93 delete [] fAllBits; 93 delete [] fAllBits; 94 fAllBits = nullptr; 94 fAllBits = nullptr; 95 fNBits = 0; 95 fNBits = 0; 96 fNBytes = 0; 96 fNBytes = 0; 97 } 97 } 98 98 99 //____________________________________________ 99 //______________________________________________________________________________ 100 void G4SurfBits::Compact() 100 void G4SurfBits::Compact() 101 { 101 { 102 // Reduce the storage used by the object to 102 // Reduce the storage used by the object to a minimun 103 103 104 if ((fNBits == 0u) || (fAllBits == nullptr)) << 104 if (!fNBits || !fAllBits) return; 105 unsigned int needed; 105 unsigned int needed; 106 for(needed=fNBytes-1; needed > 0 && fAllBits 106 for(needed=fNBytes-1; needed > 0 && fAllBits[needed]==0; ) { --needed; } 107 ++needed; 107 ++needed; 108 108 109 if (needed!=fNBytes) 109 if (needed!=fNBytes) 110 { 110 { 111 unsigned char* old_location = fAllBits; 111 unsigned char* old_location = fAllBits; 112 fAllBits = new unsigned char[needed]; 112 fAllBits = new unsigned char[needed]; 113 113 114 std::memcpy(fAllBits,old_location,needed); 114 std::memcpy(fAllBits,old_location,needed); 115 delete [] old_location; 115 delete [] old_location; 116 116 117 fNBytes = needed; 117 fNBytes = needed; 118 fNBits = 8*fNBytes; 118 fNBits = 8*fNBytes; 119 } 119 } 120 } 120 } 121 121 122 //____________________________________________ 122 //______________________________________________________________________________ 123 void G4SurfBits::Output(std::ostream &os) cons 123 void G4SurfBits::Output(std::ostream &os) const 124 { 124 { 125 // Print the value to the std::ostream 125 // Print the value to the std::ostream 126 for(unsigned int i=0; i<fNBytes; ++i) 126 for(unsigned int i=0; i<fNBytes; ++i) 127 { 127 { 128 unsigned char val = fAllBits[fNBytes - 1 - 128 unsigned char val = fAllBits[fNBytes - 1 - i]; 129 for (unsigned int j=0; j<8; ++j) 129 for (unsigned int j=0; j<8; ++j) 130 { 130 { 131 os << (G4bool)(val&0x80); 131 os << (G4bool)(val&0x80); 132 val <<= 1; 132 val <<= 1; 133 } 133 } 134 } 134 } 135 } 135 } 136 136 137 //____________________________________________ 137 //______________________________________________________________________________ 138 void G4SurfBits::Print() const 138 void G4SurfBits::Print() const 139 { 139 { 140 // Print the list of active bits 140 // Print the list of active bits 141 G4int count = 0; 141 G4int count = 0; 142 for(unsigned int i=0; i<fNBytes; ++i) 142 for(unsigned int i=0; i<fNBytes; ++i) 143 { 143 { 144 unsigned char val = fAllBits[i]; 144 unsigned char val = fAllBits[i]; 145 for (unsigned int j=0; j<8; ++j) 145 for (unsigned int j=0; j<8; ++j) 146 { 146 { 147 if ((val & 1) != 0) G4cout << " bit:" << << 147 if (val & 1) G4cout << " bit:" << count << " = 1" << G4endl; 148 ++count; 148 ++count; 149 val = val >> 1; 149 val = val >> 1; 150 } 150 } 151 } 151 } 152 } 152 } 153 153 154 //____________________________________________ 154 //______________________________________________________________________________ 155 void G4SurfBits::ResetAllBits(G4bool value) 155 void G4SurfBits::ResetAllBits(G4bool value) 156 { 156 { 157 if (fAllBits != nullptr) std::memset(fAllBit 157 if (fAllBits != nullptr) std::memset(fAllBits, value ? 0xFF : 0, fNBytes); 158 } 158 } 159 159 160 //____________________________________________ 160 //______________________________________________________________________________ 161 void G4SurfBits::ReserveBytes(unsigned int nby 161 void G4SurfBits::ReserveBytes(unsigned int nbytes) 162 { 162 { 163 // Reverse each bytes. 163 // Reverse each bytes. 164 164 165 if (nbytes > fNBytes) 165 if (nbytes > fNBytes) 166 { 166 { 167 // do it in this order to remain exception 167 // do it in this order to remain exception-safe. 168 auto newBits = new unsigned char[nbytes]; << 168 unsigned char *newBits = new unsigned char[nbytes]; 169 delete [] fAllBits; 169 delete [] fAllBits; 170 fNBytes = nbytes; 170 fNBytes = nbytes; 171 fAllBits = newBits; 171 fAllBits = newBits; 172 } 172 } 173 } 173 } 174 174 175 //____________________________________________ 175 //______________________________________________________________________________ 176 void G4SurfBits::set(unsigned int nBits, const 176 void G4SurfBits::set(unsigned int nBits, const char* array) 177 { 177 { 178 // set all the bytes 178 // set all the bytes 179 unsigned int nbytes=(nBits+7)>>3; 179 unsigned int nbytes=(nBits+7)>>3; 180 180 181 ReserveBytes(nbytes); 181 ReserveBytes(nbytes); 182 182 183 fNBits=nBits; 183 fNBits=nBits; 184 std::memcpy(fAllBits, array, nbytes); 184 std::memcpy(fAllBits, array, nbytes); 185 } 185 } 186 186 187 //____________________________________________ 187 //______________________________________________________________________________ 188 void G4SurfBits::Get(char* array) const 188 void G4SurfBits::Get(char* array) const 189 { 189 { 190 // Copy all the bytes. 190 // Copy all the bytes. 191 std::memcpy(array, fAllBits, (fNBits+7)>>3); 191 std::memcpy(array, fAllBits, (fNBits+7)>>3); 192 } 192 } 193 193 194 // If we are on a little endian machine, a bit 194 // If we are on a little endian machine, a bitvector represented using 195 // any integer type is identical to a bitvecto 195 // any integer type is identical to a bitvector represented using bytes. 196 196 197 //____________________________________________ 197 //______________________________________________________________________________ 198 void G4SurfBits::set(unsigned int nBits, const 198 void G4SurfBits::set(unsigned int nBits, const G4int* array) 199 { 199 { 200 // set all the bytes. 200 // set all the bytes. 201 201 202 set(nBits, (const char*)array); 202 set(nBits, (const char*)array); 203 } 203 } 204 204 205 //____________________________________________ 205 //______________________________________________________________________________ 206 void G4SurfBits::Get(G4int* array) const 206 void G4SurfBits::Get(G4int* array) const 207 { 207 { 208 // Get all the bytes. 208 // Get all the bytes. 209 209 210 Get((char*)array); 210 Get((char*)array); 211 } 211 } 212 212