Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4DataVector class implementation << 27 // 23 // 28 // Author: H.Kurashige, 18 September 2001 << 24 // $Id: G4DataVector.cc,v 1.6 2003/06/06 16:17:16 gcosmo Exp $ 29 // ------------------------------------------- << 25 // GEANT4 tag $Name: geant4-07-00-cand-01 $ >> 26 // >> 27 // >> 28 // -------------------------------------------------------------- >> 29 // GEANT 4 class implementation file >> 30 // >> 31 // G4DataVector.cc >> 32 // >> 33 // History: >> 34 // 18 Sep. 2001, H.Kurashige : Structure created based on object model >> 35 // -------------------------------------------------------------- 30 36 31 #include "G4DataVector.hh" 37 #include "G4DataVector.hh" 32 #include <iomanip> 38 #include <iomanip> 33 39 34 // ------------------------------------------- << 40 G4DataVector::G4DataVector() 35 G4DataVector::G4DataVector(std::size_t cap) << 41 : std::vector<G4double>() 36 : std::vector<G4double>(cap, 0.0) << 42 { 37 {} << 43 } 38 << 44 39 // ------------------------------------------- << 45 G4DataVector::G4DataVector(size_t capacity) 40 G4DataVector::G4DataVector(std::size_t cap, G4 << 46 : std::vector<G4double>(capacity, 0.0) 41 : std::vector<G4double>(cap, value) << 47 { 42 {} << 48 } >> 49 >> 50 G4DataVector::G4DataVector(size_t capacity, G4double value) >> 51 : std::vector<G4double>(capacity, value) >> 52 { >> 53 } >> 54 >> 55 G4DataVector::~G4DataVector() >> 56 { >> 57 } 43 58 44 // ------------------------------------------- << 45 G4bool G4DataVector::Store(std::ofstream& fOut 59 G4bool G4DataVector::Store(std::ofstream& fOut, G4bool ascii) 46 { 60 { 47 // Ascii mode 61 // Ascii mode 48 if(ascii) << 62 if (ascii) { 49 { << 50 fOut << *this; 63 fOut << *this; 51 return true; 64 return true; 52 } << 65 } 53 66 54 // Binary Mode 67 // Binary Mode 55 auto sizeV = G4int(size()); << 68 size_t sizeV = size(); 56 fOut.write((char*) (&sizeV), sizeof sizeV); << 69 fOut.write((char*)(&sizeV), sizeof sizeV); 57 << 58 auto value = new G4double[sizeV]; << 59 std::size_t i = 0; << 60 for(auto itr = cbegin(); itr != cend(); ++it << 61 { << 62 value[i] = *itr; << 63 } << 64 fOut.write((char*) (value), sizeV * (sizeof( << 65 delete[] value; << 66 70 >> 71 G4double* value = new G4double[sizeV]; >> 72 size_t i=0; >> 73 for (const_iterator itr=begin(); itr!=end(); itr++, i++){ >> 74 value[i] = *itr; >> 75 } >> 76 fOut.write((char*)(value), sizeV*(sizeof (G4double)) ); >> 77 delete [] value; >> 78 67 return true; 79 return true; 68 } 80 } 69 81 70 // ------------------------------------------- << 71 G4bool G4DataVector::Retrieve(std::ifstream& f 82 G4bool G4DataVector::Retrieve(std::ifstream& fIn, G4bool ascii) 72 { 83 { 73 clear(); 84 clear(); 74 G4int sizeV = 0; << 85 size_t sizeV; 75 << 86 76 // retrieve in ascii mode 87 // retrieve in ascii mode 77 if(ascii) << 88 if (ascii) { 78 { << 79 // contents 89 // contents 80 fIn >> sizeV; 90 fIn >> sizeV; 81 if(fIn.fail()) << 91 if (fIn.fail()) return false; 82 { << 92 83 return false; << 84 } << 85 if(sizeV <= 0) << 86 { << 87 #ifdef G4VERBOSE << 88 G4cerr << "G4DataVector::Retrieve():"; << 89 G4cerr << " Invalid vector size: " << si << 90 #endif << 91 return false; << 92 } << 93 << 94 reserve(sizeV); 93 reserve(sizeV); 95 for(G4int i = 0; i < sizeV; ++i) << 94 for(size_t i = 0; i < sizeV ; i++) { 96 { << 95 G4double vData; 97 G4double vData = 0.0; << 98 fIn >> vData; 96 fIn >> vData; 99 if(fIn.fail()) << 97 if (fIn.fail()) return false; 100 { << 101 return false; << 102 } << 103 push_back(vData); 98 push_back(vData); 104 } 99 } 105 return true; << 100 return true ; 106 } 101 } 107 << 102 108 // retrieve in binary mode 103 // retrieve in binary mode 109 fIn.read((char*) (&sizeV), sizeof sizeV); << 104 fIn.read((char*)(&sizeV), sizeof sizeV); 110 << 105 111 auto value = new G4double[sizeV]; << 106 G4double* value = new G4double[sizeV]; 112 fIn.read((char*) (value), sizeV * (sizeof(G4 << 107 fIn.read((char*)(value), sizeV*(sizeof(G4double)) ); 113 if(G4int(fIn.gcount()) != G4int(sizeV * (siz << 108 if (G4int(fIn.gcount()) != G4int(sizeV*(sizeof(G4double))) ){ 114 { << 109 delete [] value; 115 delete[] value; << 116 return false; 110 return false; 117 } 111 } 118 112 119 reserve(sizeV); 113 reserve(sizeV); 120 for(G4int i = 0; i < sizeV; ++i) << 114 for(size_t i = 0; i < sizeV; i++) { 121 { << 122 push_back(value[i]); 115 push_back(value[i]); 123 } 116 } 124 delete[] value; << 117 delete [] value; 125 return true; 118 return true; 126 } 119 } 127 << 120 128 // ------------------------------------------- << 129 std::ostream& operator<<(std::ostream& out, co 121 std::ostream& operator<<(std::ostream& out, const G4DataVector& pv) 130 { 122 { 131 out << pv.size() << std::setprecision(12) << << 123 size_t i; 132 for(std::size_t i = 0; i < pv.size(); ++i) << 124 out << pv.size() << G4endl; 133 { << 125 for(i = 0; i < pv.size(); i++) { 134 out << pv[i] << G4endl; << 126 out << std::setprecision(12) << pv[i] << G4endl; 135 } 127 } 136 out << std::setprecision(6); << 137 128 138 return out; 129 return out; 139 } 130 } >> 131 140 132