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