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 #include "G4DataVector.hh"
00040 #include <iomanip>
00041
00042 G4DataVector::G4DataVector()
00043 : std::vector<G4double>()
00044 {
00045 }
00046
00047 G4DataVector::G4DataVector(size_t cap)
00048 : std::vector<G4double>(cap, 0.0)
00049 {
00050 }
00051
00052 G4DataVector::G4DataVector(size_t cap, G4double value)
00053 : std::vector<G4double>(cap, value)
00054 {
00055 }
00056
00057 G4DataVector::~G4DataVector()
00058 {
00059 }
00060
00061 G4bool G4DataVector::Store(std::ofstream& fOut, G4bool ascii)
00062 {
00063
00064 if (ascii)
00065 {
00066 fOut << *this;
00067 return true;
00068 }
00069
00070
00071 size_t sizeV = size();
00072 fOut.write((char*)(&sizeV), sizeof sizeV);
00073
00074 G4double* value = new G4double[sizeV];
00075 size_t i=0;
00076 for (const_iterator itr=begin(); itr!=end(); itr++, i++)
00077 {
00078 value[i] = *itr;
00079 }
00080 fOut.write((char*)(value), sizeV*(sizeof (G4double)) );
00081 delete [] value;
00082
00083 return true;
00084 }
00085
00086 G4bool G4DataVector::Retrieve(std::ifstream& fIn, G4bool ascii)
00087 {
00088 clear();
00089 G4int sizeV=0;
00090
00091
00092 if (ascii)
00093 {
00094
00095 fIn >> sizeV;
00096 if (fIn.fail()) { return false; }
00097 if (sizeV<=0)
00098 {
00099 #ifdef G4VERBOSE
00100 G4cerr << "G4DataVector::Retrieve():";
00101 G4cerr << " Invalid vector size: " << sizeV << G4endl;
00102 #endif
00103 return false;
00104 }
00105
00106 reserve(sizeV);
00107 for(G4int i = 0; i < sizeV ; i++)
00108 {
00109 G4double vData=0.0;
00110 fIn >> vData;
00111 if (fIn.fail()) { return false; }
00112 push_back(vData);
00113 }
00114 return true ;
00115 }
00116
00117
00118 fIn.read((char*)(&sizeV), sizeof sizeV);
00119
00120 G4double* value = new G4double[sizeV];
00121 fIn.read((char*)(value), sizeV*(sizeof(G4double)) );
00122 if (G4int(fIn.gcount()) != G4int(sizeV*(sizeof(G4double))) )
00123 {
00124 delete [] value;
00125 return false;
00126 }
00127
00128 reserve(sizeV);
00129 for(G4int i = 0; i < sizeV; i++)
00130 {
00131 push_back(value[i]);
00132 }
00133 delete [] value;
00134 return true;
00135 }
00136
00137 std::ostream& operator<<(std::ostream& out, const G4DataVector& pv)
00138 {
00139 out << pv.size() << std::setprecision(12) << G4endl;
00140 for(size_t i = 0; i < pv.size(); i++)
00141 {
00142 out << pv[i] << G4endl;
00143 }
00144 out << std::setprecision(6);
00145
00146 return out;
00147 }
00148