global/management/src/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: G4DataVector.cc,v 1.9 2007/11/13 17:35:06 gcosmo Exp $
00028 // GEANT4 tag $Name: geant4-09-01-patch-01 $
00029 //
00030 // 
00031 // --------------------------------------------------------------
00032 //      GEANT 4 class implementation file
00033 //
00034 //  G4DataVector.cc
00035 //
00036 //  History:
00037 //    18 Sep. 2001, H.Kurashige : Structure created based on object model
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   // Ascii mode
00065   if (ascii)
00066   {
00067     fOut << *this;
00068     return true;
00069   } 
00070 
00071   // Binary Mode
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   // retrieve in ascii mode
00093   if (ascii)
00094   {
00095     // contents
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   // retrieve in binary mode
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 

Generated on Fri Apr 11 17:09:57 2008 for Geant4 by  doxygen 1.4.7