-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtablemerger.cpp
168 lines (111 loc) · 3.82 KB
/
tablemerger.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
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <list>
using namespace std;
typedef list<pair<char[7],char[7]>*> Table_t;
bool v = true;
Table_t* read_table( ifstream& intable ){
Table_t* rainbow = new Table_t();
char line[24];
intable.getline(line, 9); // Skip "TABLE #1\n"
while(! intable.eof() ){
intable.getline(line, 24);
if( strlen(line)<6 ) break;
pair<char[7],char[7]>* pp = new pair<char[7],char[7]>();
strncpy( pp->first, line, 6);
pp->first[6] = '\0';
strncpy( pp->second, line+10, 6);
pp->second[6] = '\0';
rainbow->push_back( pp );
}
if(v){
cout << "Table imported succesfully\nIt contains: "
<< rainbow->size() << " chains" << endl;
// ofstream out("whatwasread.txt", ofstream::trunc);
// write_table(rainbow,out);
}
return rainbow;
}
void write_table( Table_t* rainbow, ofstream& outtable ){
outtable << "TABLE 1" << endl;
for( pair<char[7],char[7]>* pp : *rainbow )
outtable << pp->first << " -> " << pp->second << endl;
outtable.put('\n');
}
bool pass_comparator(const pair<char[7],char[7]>* first, const pair<char[7],char[7]>* second){
unsigned int i=0;
while( i<7 ){
if( first->second[i] < second->second[i] )
return true;
else if ( first->second[i] > second->second[i] )
return false;
else
++i;
}
// If endpoints identical, compare based on startpoints!
i=0;
while( i<7 ){
if( first->first[i] < second->first[i] )
return true;
else if ( first->first[i] > second->first[i] )
return false;
else
++i;
}
// If even startpoints identical (<1 in a million) leave them be.
return true;
}
bool pass_predicate(const pair<char[7],char[7]>* pair1, const pair<char[7],char[7]>* pair2){
//replace sole "return" statement with if-clause to deallocate the non-unique pairs found as well
return( !strcmp(pair1->second, pair2->second) );
}
// arg1 is the file which contains all files to be read
// create it with "ls > contents.txt"
// arg2 is the file to output the merged tables
int main( int argc, char** argv ){
if(argc!=3){
cout<<"Usage: "<< argv[0]<<" <contents_file> <output_file>"<<endl;
exit(1);
}
Table_t* full_rainbow = new Table_t();
long m;
char line[128];
ifstream contents, intable;
contents.open(argv[1]);
if(v) cout << "Opened file " << argv[1] << " to read filenames" << endl;
while(! contents.eof() ){
contents.getline(line,128);
if( strlen(line)<6 ) break;
intable.open(line);
if(v) cout << "Opened file " << line << " to import a table" << endl;
Table_t* rainbow = read_table(intable);
full_rainbow->merge(*rainbow,pass_comparator);
m = full_rainbow->size();
cout<<"Full table now contains: "<< m << " chains" << endl;
intable.close();
}
contents.close();
if(v) cout << "Sorting table... " << flush;
full_rainbow -> sort( pass_comparator ); // Sort full table
if(v) cout << "Done!" << endl;
if(v) cout << "Post processing table... " << flush;
full_rainbow -> unique( pass_predicate ); // Full table has Unique endpoints
long perfect = full_rainbow->size();
if(v){
cout << "Done!\n\t" << ( (float)m-(float)perfect )/(float)m * 100
<<"% of chains had same endpoints and were removed\n\tNew m="
<<perfect<<endl;
}
ofstream outtable;
if( ifstream(argv[argc-1]) ){
cout << "Output file \" " << argv[argc-1] << "\" already exists! Overwrite? (y/n): ";
char ans;
cin >> ans;
if(ans != 'y') exit(0);
}
outtable.open(argv[argc-1], ofstream::trunc);
cout << "Opened file " << argv[argc-1] << " to export the full table." << endl;
write_table( full_rainbow, outtable );
outtable.close();
}