00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "G4DataVector.hh"
00041 #include <iomanip>
00042
00043 G4DataVector::G4DataVector()
00044 : std::vector<G4double>()
00045 {
00046 }
00047
00048 G4DataVector::G4DataVector(size_t cap)
00049 : std::vector<G4double>(cap, 0.0)
00050 {
00051 }
00052
00053 G4DataVector::G4DataVector(size_t cap, G4double value)
00054 : std::vector<G4double>(cap, value)
00055 {
00056 }
00057
00058 G4DataVector::~G4DataVector()
00059 {
00060 }
00061
00062 G4bool G4DataVector::Store(std::ofstream& fOut, G4bool ascii)
00063 {
00064
00065 if (ascii)
00066 {
00067 fOut << *this;
00068 return true;
00069 }
00070
00071
00072 size_t sizeV = size();
00073 fOut.write((char*)(&sizeV), sizeof sizeV);
00074
00075 G4double* value = new G4double[sizeV];
00076 size_t i=0;
00077 for (const_iterator itr=begin(); itr!=end(); itr++, i++)
00078 {
00079 value[i] = *itr;
00080 }
00081 fOut.write((char*)(value), sizeV*(sizeof (G4double)) );
00082 delete [] value;
00083
00084 return true;
00085 }
00086
00087 G4bool G4DataVector::Retrieve(std::ifstream& fIn, G4bool ascii)
00088 {
00089 clear();
00090 size_t sizeV=0;
00091
00092
00093 if (ascii)
00094 {
00095
00096 fIn >> sizeV;
00097 if (fIn.fail()) { return false; }
00098
00099 reserve(sizeV);
00100 for(size_t i = 0; i < sizeV ; i++)
00101 {
00102 G4double vData=0.0;
00103 fIn >> vData;
00104 if (fIn.fail()) { return false; }
00105 push_back(vData);
00106 }
00107 return true ;
00108 }
00109
00110
00111 fIn.read((char*)(&sizeV), sizeof sizeV);
00112
00113 G4double* value = new G4double[sizeV];
00114 fIn.read((char*)(value), sizeV*(sizeof(G4double)) );
00115 if (G4int(fIn.gcount()) != G4int(sizeV*(sizeof(G4double))) )
00116 {
00117 delete [] value;
00118 return false;
00119 }
00120
00121 reserve(sizeV);
00122 for(size_t i = 0; i < sizeV; i++)
00123 {
00124 push_back(value[i]);
00125 }
00126 delete [] value;
00127 return true;
00128 }
00129
00130 std::ostream& operator<<(std::ostream& out, const G4DataVector& pv)
00131 {
00132 out << pv.size() << G4endl;
00133 for(size_t i = 0; i < pv.size(); i++)
00134 {
00135 out << std::setprecision(12) << pv[i] << G4endl;
00136 }
00137
00138 return out;
00139 }
00140