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