-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_reader.h
174 lines (145 loc) · 4.25 KB
/
csv_reader.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
//
// Created by Marek Kolman on 31.12.2022.
//
// sada dvou funkci pro cteni CSV do formatu vector nebo matice, tj. vector<vector>>
//
// vector<vector<double>> mat = readcsv_matrix<double>(R"(c:\temp\test\data.csv)", true);
// vector<double> res = readcsv_vector<double>(R"(c:\temp\test\data.csv)", false);
//
#include <vector>
#include <string>
#include <type_traits>
#include <fstream>
using namespace std;
class CSVRow
{
public:
std::string operator[](std::size_t index) const
{
return std::string(&m_line[m_data[index] + 1], m_data[index + 1] - (m_data[index] + 1));
}
std::size_t size() const
{
return m_data.size() - 1;
}
void readNextRow(std::istream& str)
{
std::getline(str, m_line);
m_data.clear();
m_data.emplace_back(-1);
std::string::size_type pos = 0;
while ((pos = m_line.find(',', pos)) != std::string::npos)
{
m_data.emplace_back(pos);
++pos;
}
// This checks for a trailing comma with no data after it.
pos = m_line.size();
m_data.emplace_back(pos);
}
private:
std::string m_line;
std::vector<int> m_data;
};
std::istream& operator>>(std::istream& str, CSVRow& data)
{
data.readNextRow(str);
return str;
}
class CSVIterator
{
public:
typedef std::input_iterator_tag iterator_category;
typedef CSVRow value_type;
typedef std::size_t difference_type;
typedef CSVRow* pointer;
typedef CSVRow& reference;
CSVIterator(std::istream& str) :m_str(str.good() ? &str : nullptr) { ++(*this); }
CSVIterator() :m_str(nullptr) {}
// Pre Increment
CSVIterator& operator++() { if (m_str) { if (!((*m_str) >> m_row)) { m_str = nullptr; } }return *this; }
// Post increment
CSVIterator operator++(int) { CSVIterator tmp(*this);++(*this);return tmp; }
CSVRow const& operator*() const { return m_row; }
CSVRow const* operator->() const { return &m_row; }
bool operator==(CSVIterator const& rhs) { return ((this == &rhs) || ((this->m_str == nullptr) && (rhs.m_str == nullptr))); }
bool operator!=(CSVIterator const& rhs) { return !((*this) == rhs); }
private:
std::istream* m_str;
CSVRow m_row;
};
class CSVRange
{
std::istream& stream;
public:
CSVRange(std::istream& str)
: stream(str)
{}
CSVIterator begin() const { return CSVIterator{ stream }; }
CSVIterator end() const { return CSVIterator{}; }
};
template<typename T>
vector<vector<T>> readcsv_matrix(string filepath, bool skip_header = true) {
/*
Funkce precte csv numerickymi daty a vrati matici, tedy vector<vector<>>
Matice muze byt dat typu int nebo double
By default je preskocen header, tedy prvni radek
volame napr takto: vector<vector<double>> mat = readcsv_matrix<double>(R"(c:\temp\test\data.csv)", true);
*/
std::ifstream file(filepath);
vector<vector<T>> mat;
string s;
int j = 0;
for (auto& row : CSVRange(file))
{
if ((j == 0) && skip_header) {}
else
{
vector<T> v;
for (int i = 0; i < row.size();i++) {
s = row[i];
if (is_floating_point_v<T>)
{
v.push_back(stod(s));
}
else
{
v.push_back(stoi(s));
}
}
mat.push_back(v);
}
j++;
}
return mat;
}
template<typename T>
vector<T> readcsv_vector(string filepath, bool skip_header = false) {
/*
Funkce precte csv s jednim sloupcem a vrati vektor typu int nebo double
Volame napr takto: vector<double> res = readcsv_vector<double>(R"(c:\temp\test\data.csv)", false);
*/
std::ifstream file(filepath);
vector<T> v;
string s;
int j = 0;
for (auto& row : CSVRange(file))
{
if ((j == 0) && skip_header) {}
else
{
s = row[0];
if (is_floating_point_v<T>)
{
v.push_back(stod(s));
}
else
{
v.push_back(stoi(s));
}
}
j++;
}
return v;
}