Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 // G4SurfBits implementation 27 // 28 // 19.10.12 - Marek Gayer, created and adapted 29 // of Root's TBits cla 30 // ------------------------------------------- 31 32 #include "G4SurfBits.hh" 33 #include "G4ios.hh" 34 35 //____________________________________________ 36 G4SurfBits::G4SurfBits(unsigned int nBits) : f 37 { 38 // G4SurfBits constructor. All bits set to 39 40 if (fNBits <= 0) fNBits = 0; 41 fNBytes = fNBits != 0u ? ((fNBits-1)/8) + 1 42 fAllBits = new unsigned char[fNBytes]; 43 // this is redundant only with libNew 44 std::memset(fAllBits,0,fNBytes); 45 } 46 47 //____________________________________________ 48 G4SurfBits::G4SurfBits(const G4SurfBits &origi 49 : fNBits(original.fNBits), fNBytes(original. 50 { 51 // G4SurfBits copy constructor 52 53 fAllBits = new unsigned char[fNBytes]; 54 std::memcpy(fAllBits,original.fAllBits,fNByt 55 } 56 57 //____________________________________________ 58 G4SurfBits& G4SurfBits::operator=(const G4Surf 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,fNByte 71 } 72 else 73 { 74 fAllBits = nullptr; 75 } 76 } 77 return *this; 78 } 79 80 //____________________________________________ 81 G4SurfBits::~G4SurfBits() 82 { 83 // G4SurfBits destructor 84 85 delete [] fAllBits; 86 } 87 88 //____________________________________________ 89 void G4SurfBits::Clear() 90 { 91 // Clear the value. 92 93 delete [] fAllBits; 94 fAllBits = nullptr; 95 fNBits = 0; 96 fNBytes = 0; 97 } 98 99 //____________________________________________ 100 void G4SurfBits::Compact() 101 { 102 // Reduce the storage used by the object to 103 104 if ((fNBits == 0u) || (fAllBits == nullptr)) 105 unsigned int needed; 106 for(needed=fNBytes-1; needed > 0 && fAllBits 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 //____________________________________________ 123 void G4SurfBits::Output(std::ostream &os) cons 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 - 129 for (unsigned int j=0; j<8; ++j) 130 { 131 os << (G4bool)(val&0x80); 132 val <<= 1; 133 } 134 } 135 } 136 137 //____________________________________________ 138 void G4SurfBits::Print() const 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) != 0) G4cout << " bit:" << 148 ++count; 149 val = val >> 1; 150 } 151 } 152 } 153 154 //____________________________________________ 155 void G4SurfBits::ResetAllBits(G4bool value) 156 { 157 if (fAllBits != nullptr) std::memset(fAllBit 158 } 159 160 //____________________________________________ 161 void G4SurfBits::ReserveBytes(unsigned int nby 162 { 163 // Reverse each bytes. 164 165 if (nbytes > fNBytes) 166 { 167 // do it in this order to remain exception 168 auto newBits = new unsigned char[nbytes]; 169 delete [] fAllBits; 170 fNBytes = nbytes; 171 fAllBits = newBits; 172 } 173 } 174 175 //____________________________________________ 176 void G4SurfBits::set(unsigned int nBits, const 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 //____________________________________________ 188 void 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 bit 195 // any integer type is identical to a bitvecto 196 197 //____________________________________________ 198 void G4SurfBits::set(unsigned int nBits, const 199 { 200 // set all the bytes. 201 202 set(nBits, (const char*)array); 203 } 204 205 //____________________________________________ 206 void G4SurfBits::Get(G4int* array) const 207 { 208 // Get all the bytes. 209 210 Get((char*)array); 211 } 212