-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStraightener.h
More file actions
163 lines (146 loc) · 4.58 KB
/
Straightener.h
File metadata and controls
163 lines (146 loc) · 4.58 KB
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
/*
* File: Straightener.h
* Author: hugh
*
* Created on July 25, 2016, 10:51 PM
*/
#ifndef STRAIGHTENER_H
#define STRAIGHTENER_H
#include <set>
#include "Converter.h"
#include "Main.h"
using namespace std;
class Straightener {
private:
map<string, size_t> columnSizes;
size_t total;
size_t maxColumnSize;
map<string, size_t> duplicatesMap;
map<string, size_t> indices;
deque<string> header;
string delimiter;
map<string, deque<string>> embeddedStrings;
public:
Straightener(map<string, deque<string>> embeddedStrings, deque<string> header,
string delimiter)
{
this->embeddedStrings = embeddedStrings;
this->delimiter = delimiter;
this->header = header;
this->getColumnSizes();
this->maxColumnSize = this->getMaxColumnSize();
this->total = this->totalSize();
this->getDuplicates();
}
void getDuplicates()
{
deque<string>::iterator it;
for (it = header.begin(); it != header.end(); ++it)
{
this->duplicatesMap[*it] = (maxColumnSize == this->columnSizes[*it])?
2 : maxColumnSize;
}
}
void getColumnSizes()
{
deque<string>::iterator key;
string filePath;
int number_of_lines;
for (key = header.begin(); key != header.end(); ++key)
{
filePath = Main::getResultsDirectory() + "/" + (*key) + ".txt";
std::string line;
std::ifstream myfile(filePath);
std::getline(myfile, line);
number_of_lines = 1;
if (this->embeddedStrings.find(*key) != this->embeddedStrings.end())
{
number_of_lines = this->embeddedStrings[*key].size();
} else {
while (std::getline(myfile, line))
++number_of_lines;
}
myfile.close();
this->columnSizes[(*key)] = number_of_lines;
}
}
long totalSize()
{
std::set<size_t> m;
int size;
for (deque<string>::iterator it = header.begin(); it != header.end(); ++it)
{
size = this->columnSizes[(*it)];
m.insert(size);
}
long total = 1;
for (std::set<size_t>::iterator it = m.begin(); it != m.end(); ++it){
total *= (*it);
}
return total;
}
/**
* @return
*/
long getMaxColumnSize()
{
size_t max = 0;
for (deque<string>::iterator it = header.begin(); it != header.end(); ++it)
{
if (this->columnSizes[(*it)] > max)
{
max = this->columnSizes[(*it)];
}
}
return max;
}
string oracle(string key, size_t row)
{
string s = this->embeddedStrings[key][this->indices[key]];
if ((row + 1) % this->duplicatesMap[key] == 0)
this->indices[key] = (this->indices[key] + 1) % this->columnSizes[key];
return s;
}
void straighten()
{
string fileName;
string line;
map<string, std::ifstream> files;
size_t row = 0;
deque<string>::iterator key;
//initialization for this method
for (key = this->header.begin(); key != this->header.end(); ++key)
{
this->indices[*key] = 0;
fileName = Main::getResultsDirectory() + "/" + (*key) + ".txt";
files[*key].open(fileName, ios::app);
}
for (size_t blockNum = 0; blockNum < this->total; blockNum++)
{
cout << row << this->delimiter;
for (key = this->header.begin(); key != this->header.end(); ++key)
{
if (this->embeddedStrings.find(*key) != this->embeddedStrings.end())
{
line = this->oracle(*key, row);
} else if (files[*key].eof() || !std::getline(files[*key], line)
|| line.length() == 0)
{
files[*key].clear();
files[*key].seekg(0, ios::beg);
std::getline(files[*key], line);
}
line = (line.length() > 2)? line.substr(1, line.length()-2) : "null";
cout << line << this->delimiter;
}
cout << endl;
row++;
}
for (key = this->header.begin(); key != this->header.end(); ++key)
files[*key].close();
}
~Straightener()
{
}
};
#endif /* STRAIGHTENER_H */