source: trunk/source/global/management/src/G4DataVector.cc @ 1250

Last change on this file since 1250 was 1228, checked in by garnier, 14 years ago

update geant4.9.3 tag

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