-
Notifications
You must be signed in to change notification settings - Fork 0
/
DotFileReader.hpp
executable file
·205 lines (155 loc) · 5.77 KB
/
DotFileReader.hpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef ROSSB83_DOT_FILE_READER_HPP
#define ROSSB83_DOT_FILE_READER_HPP
#include <unordered_set>
#include <set>
#include <string>
#include <list>
#include <iostream>
#include <vector>
#include <memory>
#include <fstream>
#include <sstream>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <regex>
#include <functional>
#include <algorithm>
#include <numeric>
#include <limits>
#include <math.h>
#include <complex>
#include <cmath>
// #include <regex> -> this doesn't seem to work, TODO: investigate https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53631
#include "kdtree.hpp"
// ben's namespace
namespace rossb83 {
/*
* class that deserializes a dot file persisted on disk to an in-memory kdtree
*/
template <typename T>
class DotFileReader {
typedef std::pair<Point<T>,std::size_t> PointDimPair;
public:
/*
* ctor open file with filename
* input filename - file to be read from
*/
DotFileReader(const std::string& filename) {
file_.open(filename);
}
/*
* dtor closes file
*/
~DotFileReader() {file_.close();}
/*
* begin iterator
*/
auto begin() {return DotFileIterator<PointDimPair>(file_);}
/*
* end iterator
*/
auto end() {return DotFileIterator<PointDimPair>();}
/*
* const begin iterator
*/
auto begin() const {return DotFileIterator<PointDimPair>(file_);}
/*
* const end iterator
*/
auto end() const {return DotFileIterator<PointDimPair>();}
/*
* iteraters through dotfile extracting the points
*/
template <typename PointDimPair = std::pair<Point<T>,int>>
class DotFileIterator {
public:
typedef typename std::string::value_type char_type;
typedef typename std::string::traits_type traits_type;
typedef std::basic_istream<char_type, traits_type> istream_type;
/**
* returns empty iterator signaling end iterator
*/
DotFileIterator() : is_(nullptr) {}
/*
* returns initialized values in iterator signaling begin iterator
* input root - node to begin preorder traversal
*/
DotFileIterator(istream_type& is) : is_(&is) {++*this;}
/*
* overloaded deref operator that returns current point/dim given iterator
*/
const PointDimPair& operator*() const {return pointDimPair_; }
/*
* iterate to next point in DOT file
*/
DotFileIterator<PointDimPair>& operator++() {
std::string line = "";
while (is_ && !containsPoint(line)) { // continue to next line until point is found
if (is_ && !getline(*is_, line)) { // check if end of file is reached
is_ = nullptr; // no point found
}
}
if (is_ != nullptr) { // ensure end of file is not reached yet
pointDimPair_ = extractPoint(line);
}
return *this;
}
/*
* called upon every iteration to ensure iteration is not complete
*/
bool operator!=(const DotFileIterator<PointDimPair>& other) const {return (is_ != other.is_);}
private:
/*
* istream iterator to read file line by line
*/
istream_type* is_;
/*
* point/dimension read from input file
*/
PointDimPair pointDimPair_;
/*
* helper function to determine if line from input file contains a point
* TODO: refactor to use c++11 regexes
* input line - line of text from input file
* output true if line contains a point and false otherwise
*/
static bool containsPoint(const std::string& line) {return (line.find("];") != std::string::npos);}
/*
* given a line of input text, return point and dimension
* TODO: refactor to use c++11 regexes
* input line - line of text from input file guaranteed to contain point
* output deserialized point and dimension
*/
static PointDimPair extractPoint(const std::string& line) {
// this entire function should be refactored to use regex c++11 extension
std::size_t pointstart = 0; // starting position of point data
// regex "\"" indicates this line contains a null node
if ((pointstart = line.find("\"")) == std::string::npos) {
// empty point interpreted as null node
return std::make_pair<Point<T>, std::size_t>(Point<T>(),0);
} else {
// very ugly code to extract point and dimension for line of text in DOT file
std::string label = line.substr(line.find("*") + 1,pointstart - line.find("*") - 11);
std::istringstream point(line.substr(pointstart + 2, line.find("@") - pointstart - 3));
std::istringstream dim(line.substr(line.find("@") + 1, line.length() - line.find("@") - 4));
// output point/dimension pair
Point<T> p(std::count(line.begin(),line.end(),',') + 1); // input dimensions by counting commas in line
std::size_t d;
point >> p; // deserialize point
p.label(std::move(label)); // add label to point
dim >> d; // deserialize dimension
return std::make_pair<Point<T>, std::size_t>(static_cast<Point<T>>(p),static_cast<size_t>(d));
}
}
}; // class iterator
private:
/*
* file stream to be read
*/
std::ifstream file_;
}; // class DotFileReader
} // namespace rossb83
#endif // ROSSB83_DOT_FILE_READER_HPP