-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_integer_files.cpp
215 lines (174 loc) · 5.37 KB
/
sort_integer_files.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
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
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <chrono>
//typedef std::chrono::high_resolution_clock Clock;
enum Exceptions {no_exc = 0, inv_arg_exc, out_of_range_exc};
const int numerical_arg_start_ndx = 2;
using namespace std;
string file_name = "file0";
string file_amend = "-sorted";
string file_format = ".txt";
string in_file_name = "";
ifstream my_infile;
ofstream my_outfile;
int parse_command_arguments(int argc, char* argv[])
{
// if there were additional commands entered
if (argc > 1)
{
// set the base file name
file_name = argv[1];
}
// create the input file name by concatenating file_name and file_format
in_file_name = file_name + file_format;
return no_exc;
}
vector<int> tokenize_line(string curr_line)
{
size_t pos = 0;
string token;
vector<int> values_from_line;
string delimiter = " ";
try
{
int value = -1;
while ((pos = curr_line.find(delimiter)) != string::npos)
{
token = curr_line.substr(0, pos);
//cout << token << ", ";
curr_line.erase(0, pos + delimiter.length());
value = stoi(token);
if (value != -1)
{
values_from_line.push_back(value);
value = -1;
}
}
value = stoi(curr_line);
if (value != -1)
values_from_line.push_back(value);
}
// handle an invalid argument error
catch (const invalid_argument& ia)
{
cout << "Invalid Argument, " << ia.what() << ", was entered.\n";
}
// handle integer range error
catch (const out_of_range& oor)
{
cout << "Input, "<< oor.what()<<", was outside of integer value range.\n";
}
return values_from_line;
/*
*/
}
vector<int> sort_line(vector<int> values_from_line)
{
stable_sort(values_from_line.begin(), values_from_line.end());
return values_from_line;
}
vector<vector<int>> read_file()
{
string curr_line;
vector<vector<int>> lines_from_file;
try
{
while (getline(my_infile, curr_line))
{
vector<int> parsed_ints_from_line = tokenize_line(curr_line);
parsed_ints_from_line = sort_line(parsed_ints_from_line);
lines_from_file.push_back (parsed_ints_from_line);
}
}
catch (ios_base::failure fail)
{
cout << "Unable to get line " << fail.what() << endl;
}
return lines_from_file;
}
int write_file(vector<vector<int>> parsed_lines)
{
for (vector<vector<int>>::iterator main_it = parsed_lines.begin();
main_it != parsed_lines.end();
++main_it)
{
vector<int> line = *main_it;
stringstream line_string;
for (vector<int>::iterator line_it = line.begin();
line_it != line.end();
++line_it)
{
(line_it < line.end()-1) ?
line_string << *line_it << " " : line_string << *line_it;
}
(main_it < parsed_lines.end()-1) ?
my_outfile << line_string.str() << endl : my_outfile << line_string.str();
}
}
vector<chrono::high_resolution_clock::time_point> execute(int argc, char* argv[])
{
vector<chrono::high_resolution_clock::time_point> io_clocks;
int args_parsed = parse_command_arguments(argc, argv);
if (args_parsed == no_exc)
{
// create the file's name
stringstream out_file_name;
out_file_name << file_name << file_amend << file_format;
io_clocks.push_back(chrono::high_resolution_clock::now());
my_infile.open(in_file_name, ios::in);
bool file_read_success = false;
// if it was created/opened correctly,
if (my_infile.is_open())
{
file_read_success = true;
vector<vector<int>> parsed_lines = read_file();
my_infile.close();
io_clocks.push_back(chrono::high_resolution_clock::now());
if (parsed_lines.size() > 0)
{
io_clocks.push_back(chrono::high_resolution_clock::now());
// create the stream for writing to the new file
my_outfile.open(out_file_name.str(), ios::out);
if (my_outfile.is_open())
{
write_file(parsed_lines);
my_outfile.close();
io_clocks.push_back(chrono::high_resolution_clock::now());
}
else cout << "Unable to open file " << out_file_name.str() << endl;
}
}
else cout << "Unable to open file " << in_file_name << endl;
}
return io_clocks;
}
int main (int argc, char* argv[])
{
chrono::high_resolution_clock::time_point start_clock, end_clock;
start_clock = chrono::high_resolution_clock::now();
vector<chrono::high_resolution_clock::time_point> io_clocks = execute(argc, argv);
end_clock = chrono::high_resolution_clock::now();
double diff_input_clocks = chrono::duration_cast<chrono::nanoseconds>(io_clocks[1] - io_clocks[0]).count();
double diff_output_clocks = chrono::duration_cast<chrono::nanoseconds>(io_clocks[3] - io_clocks[2]).count();
double clock_diff = chrono::duration_cast<chrono::nanoseconds>(end_clock - start_clock).count();
double io_time = (diff_input_clocks + diff_output_clocks);
stringstream log_file;
log_file << file_name << ".log";
ofstream times_log(log_file.str(), ios::app);
if (times_log.is_open())
{
times_log << in_file_name << endl;
times_log << "CPU Time: " << clock_diff << " ns"<< endl;
times_log << "IO Time: " << endl <<
" Total: " << io_time << " ns" << endl <<
" Input: " << diff_input_clocks << " ns" << endl <<
" Output: " << diff_output_clocks<< " ns" << endl;
}
return 0;
}