-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathData_block.cxx
162 lines (137 loc) · 4.99 KB
/
Data_block.cxx
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include "Data_block.h"
#include "Convert.h"
namespace
{
std::vector<std::string> split_line(std::string& line, const std::string splitter)
{
std::vector<std::string> strings;
// Strip of the comments.
boost::trim(line);
boost::split(strings, line, boost::is_any_of("#"));
// Keep part that is not comment and clear strings.
if (strings.size() >= 2)
line = strings[0];
strings.clear();
// Return if line is empty.
if (line.empty())
return strings;
// Strip of all the whitespace.
boost::trim(line);
// Split string on the given splitter.
boost::split(strings, line, boost::is_any_of(splitter), boost::token_compress_on);
return strings;
}
template<typename T>
T convert_from_string(const std::string& value)
{
std::istringstream ss(value);
T item = Convert::get_item_from_stream<T>(ss);
Convert::check_item<T>(item);
return item;
}
}
Data_block::Data_block(const std::string& file_name)
{
// Read file and throw exception on error.
std::ifstream infile;
infile.open(file_name);
if (!infile.good())
throw std::runtime_error("Illegal file name");
std::string line;
// First, read the header.
int number_of_vectors;
int line_number = 0;
std::vector<std::string> header_items;
while (std::getline(infile, line))
{
++line_number;
std::vector<std::string> strings = split_line(line, " \t");
if (strings.size() == 0)
continue;
number_of_vectors = strings.size();
for (const std::string& s : strings)
{
auto it = std::find(header_items.begin(), header_items.end(), s);
if (it != header_items.end())
throw std::runtime_error("Duplicate name in header");
else
header_items.push_back(s);
}
break;
}
// Second, read the data.
while (std::getline(infile, line))
{
++line_number;
std::vector<std::string> strings = split_line(line, " \t");
if (strings.size() == 0)
continue;
// Check if the number if items match with the header.
if (strings.size() != number_of_vectors)
{
std::string error_string = "Illegal number of items on line ";
error_string += std::to_string(line_number);
throw std::runtime_error(error_string);
}
// Insert data into the vectors.
auto it_s = strings.begin();
for (auto& h : header_items)
{
data_series[h].push_back(*it_s);
++it_s;
}
}
}
template <typename T>
void Data_block::get_vector(std::vector<T>& destination,
const std::string& name,
const size_t length,
const size_t source_start_index,
const size_t destination_start_index)
{
if ( data_series.find(name) == data_series.end() )
{
if ( (destination.size() < destination_start_index + length) )
{
std::string error_string = "Out of bounds write of vector ";
error_string += name;
throw std::runtime_error(error_string);
}
std::generate(destination.begin() + destination_start_index,
destination.begin() + destination_start_index + length,
[]() { return convert_from_string<T>("0"); });
}
else
{
if ( (data_series.at(name).size() < source_start_index + length) )
{
std::string error_string = "Out of bounds read of vector ";
error_string += name;
throw std::runtime_error(error_string);
}
if ( (destination.size() < destination_start_index + length) )
{
std::string error_string = "Out of bounds write of vector ";
error_string += name;
throw std::runtime_error(error_string);
}
{
std::transform(data_series.at(name).begin() + source_start_index,
data_series.at(name).begin() + (source_start_index + length),
destination.begin() + destination_start_index,
[](const std::string value) { return convert_from_string<T>(value); });
}
}
}
template void Data_block::get_vector(std::vector<std::string>&, const std::string&, const size_t, const size_t, const size_t);
template void Data_block::get_vector(std::vector<double>&, const std::string&, const size_t, const size_t, const size_t);
template void Data_block::get_vector(std::vector<float>&, const std::string&, const size_t, const size_t, const size_t);
template void Data_block::get_vector(std::vector<int>&, const std::string&, const size_t, const size_t, const size_t);