G4DataVector.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // --------------------------------------------------------------
00031 //      GEANT 4 class implementation file
00032 //
00033 //  G4DataVector.cc
00034 //
00035 //  History:
00036 //    18 Sep. 2001, H.Kurashige : Structure created based on object model
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   // Ascii mode
00064   if (ascii)
00065   {
00066     fOut << *this;
00067     return true;
00068   } 
00069 
00070   // Binary Mode
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   // retrieve in ascii mode
00092   if (ascii)
00093   {
00094     // contents
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   // retrieve in binary mode
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 

Generated on Mon May 27 17:47:59 2013 for Geant4 by  doxygen 1.4.7