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

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

update ti head

File size: 4.0 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.12 2010/11/01 13:55:53 gcosmo Exp $
28// GEANT4 tag $Name: global-V09-03-22 $
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  G4int sizeV=0;
91 
92  // retrieve in ascii mode
93  if (ascii)
94  {
95    // contents
96    fIn >> sizeV;
97    if (fIn.fail())  { return false; }
98    if (sizeV<=0)
99    {
100#ifdef G4VERBOSE 
101      G4cerr << "G4DataVector::Retrieve():";
102      G4cerr << " Invalid vector size: " << sizeV << G4endl;
103#endif
104      return false;
105    }
106
107    reserve(sizeV);
108    for(G4int i = 0; i < sizeV ; i++)
109    {
110      G4double vData=0.0;
111      fIn >> vData;
112      if (fIn.fail())  { return false; }
113      push_back(vData);
114    }
115    return true ;
116  }
117 
118  // retrieve in binary mode
119  fIn.read((char*)(&sizeV), sizeof sizeV); 
120 
121  G4double* value = new G4double[sizeV];
122  fIn.read((char*)(value),  sizeV*(sizeof(G4double)) );
123  if (G4int(fIn.gcount()) != G4int(sizeV*(sizeof(G4double))) )
124  {
125    delete [] value;
126    return false;
127  }
128
129  reserve(sizeV);
130  for(G4int i = 0; i < sizeV; i++) 
131  {
132    push_back(value[i]);
133  }
134  delete [] value;
135  return true;
136}
137   
138std::ostream& operator<<(std::ostream& out, const G4DataVector& pv)
139{
140  out << pv.size() << std::setprecision(12) << G4endl; 
141  for(size_t i = 0; i < pv.size(); i++)
142  {
143    out << pv[i] << G4endl;
144  }
145  out << std::setprecision(6);
146
147  return out;
148}
149
Note: See TracBrowser for help on using the repository browser.