This repository was archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColumnVector.cpp
96 lines (85 loc) · 2.77 KB
/
ColumnVector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2017 Ivan Vaccari <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
#include "ColumnVector.h"
#include <list>
ColumnVector::ColumnVector()
{
}
ColumnVector::ColumnVector(const std::vector<float> &values){
data=values;
}
ColumnVector::ColumnVector(int rows){
data.resize(rows,0);
}
int ColumnVector::sizeh(){
return data.size();
}
void ColumnVector::setRawCell(unsigned int row,float number){
if (data.size()<=row)
throw std::string("Can't set raw cell at this row. Vector is smaller.");
data[row]=number;
}
std::vector<float> ColumnVector::toStdVector(){
return data;
}
void ColumnVector::print(){
for(unsigned int i=0;i<data.size();++i){
std::cout<<data[i]<<std::endl;
}
}
bool ColumnVector::loadFromFile(const std::string &fileName, const std::string &vectorName){
bool ret=false;
std::ifstream file(fileName.c_str());
std::string vector_section("[colvector ");
vector_section.append(vectorName);
vector_section.append("]");
bool process=false;
if (file.is_open()){
std::string line;
while (std::getline(file,line)){
while ((line.length()>0) && (line.at(0)==' '))
line=line.substr(1);
while ((line.length()>0) && (line.at(line.length()-1)==' '))
line.resize(line.length()-1);
if (process && line.length()==0){
process=false;
ret=true;
break;
}else if (process){
//removing double spaces
line.append(" ");
std::string::size_type pos=line.find(" ");
while(pos!=std::string::npos){
line.replace(pos,2," ");
pos=line.find(" ");
}
pos=line.find(" ");
std::string::size_type start_pos=0;
if(pos!=std::string::npos){
std::string num=line.substr(start_pos,pos-start_pos);
data.push_back(std::atof(num.c_str()));
}
}else if (line==vector_section){
process=true;
}
}
file.close();
}else{
throw std::string("Unable to open file.");
}
return ret;
}