CPPLapack
 All Classes Files Functions Variables Friends Pages
dcovector-io.hpp
Go to the documentation of this file.
1 //=============================================================================
2 /*! operator() for non-const object */
3 inline double& dcovector::operator()(const CPPL_INT& i)
4 {CPPL_VERBOSE_REPORT;
5 #ifdef CPPL_DEBUG
6  if( i<0 || l<=i ){
7  ERROR_REPORT;
8  std::cerr << "The required component is out of the vector size." << std::endl
9  << "Your input is (" << i << "), whereas the vector size is " << l << "." << std::endl;
10  exit(1);
11  }
12 #endif//CPPL_DEBUG
13 
14  return array[i];
15 }
16 
17 //=============================================================================
18 /*! operator() for const object */
19 inline double dcovector::operator()(const CPPL_INT& i) const
20 {CPPL_VERBOSE_REPORT;
21 #ifdef CPPL_DEBUG
22  if( i<0 || l<=i ){
23  ERROR_REPORT;
24  std::cerr << "The required component is out of the vector size." << std::endl
25  << "Your input is (" << i << "), whereas the vector size is " << l << "." << std::endl;
26  exit(1);
27  }
28 #endif//CPPL_DEBUG
29 
30  return array[i];
31 }
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////////
36 
37 //=============================================================================
38 /*! set value for const object */
39 inline dcovector& dcovector::set(const CPPL_INT& i, const double& v) //const
40 {CPPL_VERBOSE_REPORT;
41 #ifdef CPPL_DEBUG
42  if( i<0 || l<=i ){
43  ERROR_REPORT;
44  std::cerr << "The required component is out of the vector size." << std::endl
45  << "Your input is (" << i << "), whereas the vector size is " << l << "." << std::endl;
46  exit(1);
47  }
48 #endif//CPPL_DEBUG
49 
50  array[i] =v;
51  return *this;
52 }
53 
54 ///////////////////////////////////////////////////////////////////////////////
55 ///////////////////////////////////////////////////////////////////////////////
56 ///////////////////////////////////////////////////////////////////////////////
57 
58 //=============================================================================
59 inline std::ostream& operator<<(std::ostream& s, const dcovector& vec)
60 {CPPL_VERBOSE_REPORT;
61  for(CPPL_INT i=0; i<vec.l; i++){
62  s << " " << vec.array[i] << std::endl;
63  }
64 
65  return s;
66 }
67 
68 ///////////////////////////////////////////////////////////////////////////////
69 ///////////////////////////////////////////////////////////////////////////////
70 ///////////////////////////////////////////////////////////////////////////////
71 
72 //=============================================================================
73 inline void dcovector::write(const char *filename) const
74 {CPPL_VERBOSE_REPORT;
75  std::ofstream ofs(filename, std::ios::trunc);
76  ofs.setf(std::cout.flags());
77  ofs.precision(std::cout.precision());
78  ofs.width(std::cout.width());
79  ofs.fill(std::cout.fill());
80 
81  ofs << "#dcovector" << " " << l << std::endl;
82  for(CPPL_INT i=0; i<l; i++){
83  ofs << operator()(i) << std::endl;
84  }
85 
86  ofs.close();
87 }
88 
89 //=============================================================================
90 inline void dcovector::read(const char *filename)
91 {CPPL_VERBOSE_REPORT;
92  std::ifstream s(filename);
93  if(!s){
94  ERROR_REPORT;
95  std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
96  exit(1);
97  }
98 
99  std::string id;
100  s >> id;
101  if( id != "dcovector" && id != "#dcovector" ){
102  ERROR_REPORT;
103  std::cerr << "The type name of the file \"" << filename << "\" is not dcovector." << std::endl
104  << "Its type name was " << id << " ." << std::endl;
105  exit(1);
106  }
107 
108  s >> l;
109  resize(l);
110  for(CPPL_INT i=0; i<l; i++) { s >> operator()(i); }
111  if(s.eof()){
112  ERROR_REPORT;
113  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
114  << "Most likely, there is a lack of data components, or a linefeed code or space code is missing at the end of the last line." << std::endl;
115  exit(1);
116  }
117 
118  s >> id;
119  if(!s.eof()){
120  ERROR_REPORT;
121  std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
122  << "Most likely, there are extra data components." << std::endl;
123  exit(1);
124  }
125 
126 
127  s.close();
128 }
void write(const char *) const
CPPL_INT l
vector size
Definition: dcovector.hpp:9
_dgematrix i(const _dgbmatrix &mat)
dcovector & resize(const CPPL_INT &, const CPPL_INT=0)
double * array
1D array to store vector data
Definition: dcovector.hpp:11
void read(const char *)
Real Double-precision Column Vector Class.
Definition: dcovector.hpp:3
double & operator()(const CPPL_INT &)
Definition: dcovector-io.hpp:3
dcovector & set(const CPPL_INT &, const double &)
std::ostream & operator<<(std::ostream &s, const dcovector &vec)