VERB_code_2.3
drovector-io.hpp
1 //=============================================================================
3 inline double& drovector::operator()(const long& i)
4 {
5 #ifdef CPPL_VERBOSE
6  std::cerr << "# [MARK] drovector::operator()(const long&)"
7  << std::endl;
8 #endif//CPPL_VERBOSE
9 
10 #ifdef CPPL_DEBUG
11  if( i<0 || L<=i ){
12  std::cerr << "[ERROR] drovector::operator()(const long&)"
13  << std::endl
14  << "The required component is out of the vector size."
15  << std::endl
16  << "Your input was (" << i << ")." << std::endl;
17  exit(1);
18  }
19 #endif//CPPL_DEBUG
20 
21  return Array[i];
22 }
23 
24 //=============================================================================
26 inline double drovector::operator()(const long& i) const
27 {
28 #ifdef CPPL_VERBOSE
29  std::cerr << "# [MARK] drovector::operator()(const long&) const"
30  << std::endl;
31 #endif//CPPL_VERBOSE
32 
33 #ifdef CPPL_DEBUG
34  if( i<0 || L<=i ){
35  std::cerr << "[ERROR] drovector::operator()(const long&) const"
36  << std::endl
37  << "The required component is out of the vector size."
38  << std::endl
39  << "Your input was (" << i << ")." << std::endl;
40  exit(1);
41  }
42 #endif//CPPL_DEBUG
43 
44  return Array[i];
45 }
46 
50 
51 //=============================================================================
53 inline void drovector::set(const long& i, const double& v) const
54 {
55 #ifdef CPPL_VERBOSE
56  std::cerr << "# [MARK] drovector::set(const long&, const double&) const"
57  << std::endl;
58 #endif//CPPL_VERBOSE
59 
60 #ifdef CPPL_DEBUG
61  if( i<0 || L<=i ){
62  std::cerr << "[ERROR] drovector::set(const long&, const double&) const"
63  << std::endl
64  << "The required component is out of the vector size."
65  << std::endl
66  << "Your input was (" << i << ")." << std::endl;
67  exit(1);
68  }
69 #endif//CPPL_DEBUG
70 
71  Array[i] =v;
72 }
73 
77 
78 //=============================================================================
79 inline std::ostream& operator<<(std::ostream& s, const drovector& vec)
80 {
81 #ifdef CPPL_VERBOSE
82  std::cerr << "# [MARK] operator<<(std::ostream&, const drovector&)"
83  << std::endl;
84 #endif//CPPL_VERBOSE
85 
86  for(long i=0; i<vec.L; i++){ s << " " << vec.Array[i]; }
87  s << std::endl;
88 
89  return s;
90 }
91 
95 
96 //=============================================================================
97 inline void drovector::write(const char *filename) const
98 {
99 #ifdef CPPL_VERBOSE
100  std::cerr << "# [MARK] drovector::write(const char*) const"
101  << std::endl;
102 #endif//CPPL_VERBOSE
103 
104  std::ofstream s(filename, std::ios::trunc);
105 
106  s << "drovector" << " " << L << std::endl;
107  for(long i=0; i<L; i++){
108  s << operator()(i) << " ";
109  }
110  s << std::endl;
111 
112  s.close();
113 }
114 
115 //=============================================================================
116 inline void drovector::read(const char *filename)
117 {
118 #ifdef CPPL_VERBOSE
119  std::cerr << "# [MARK] drovector::read(const char*)"
120  << std::endl;
121 #endif//CPPL_VERBOSE
122 
123  std::ifstream s( filename );
124  if(!s){
125  std::cerr << "[ERROR] drovector::read(const char*) " << std::endl
126  << "The file \"" << filename << "\" can not be opened."
127  << std::endl;
128  exit(1);
129  }
130 
131  std::string id;
132  s >> id;
133  if( id != "drovector" ){
134  std::cerr << "[ERROR] drovector::read(const char*) " << std::endl
135  << "The type name of the file \"" << filename
136  << "\" is not drovector." << std::endl
137  << "Its type name was " << id << " ." << std::endl;
138  exit(1);
139  }
140 
141  s >> L;
142  resize(L);
143  for(long i=0; i<L; i++) { s >> operator()(i); }
144  if(s.eof()){
145  std::cerr << "[ERROR] drovector::read(const char*) " << std::endl
146  << "There is something is wrong with the file \""
147  << filename << " ." << std::endl
148  << "Most likely, there is not enough data components, "
149  << "or a linefeed code or space code is missing "
150  << "at the end of the last line." << std::endl;
151  exit(1);
152  }
153 
154  s.close();
155 }
double & operator()(const long &)
Definition: drovector-io.hpp:3
Real Double-precision Row Vector Class.
Definition: drovector.hpp:3
void set(const long &, const double &) const
Definition: drovector-io.hpp:53
long L
vector size
Definition: _drovector.hpp:7
double * Array
1D Array to store vector data
Definition: _drovector.hpp:8
void resize(const long &)
Definition: drovector-misc.hpp:93