-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresenter.cpp
More file actions
executable file
·116 lines (100 loc) · 3 KB
/
presenter.cpp
File metadata and controls
executable file
·116 lines (100 loc) · 3 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
#include "presenter.h"
#include "tools.h"
int sort_index;
bool is_ascending;
presenter::presenter(int workers_cnt, string order, string sort_field)
{
this->workers_cnt = workers_cnt;
this->order = order;
this->sort_field = sort_field;
sort_index = get_index(sort_field);
is_ascending = this->order == ASCENDING;
}
bool comp(const vector<string> &a,const vector<string> &b)
{
if (isFloat(a[sort_index]))
return is_ascending ? stof(a[sort_index]) < stof(b[sort_index]) : stof(a[sort_index]) > stof(b[sort_index]);
else
return is_ascending ? a[sort_index] < b[sort_index] : a[sort_index] > b[sort_index];
}
void presenter::read_data()
{
char buff[4096];
int workers_done = 0;
vector <vector <string>> recieved;
int fd = open(FIFO_PATHNAME, O_RDONLY);
while(true)
{
memset(buff, '\0', sizeof(buff));
if (read(fd, buff, 4096) <= 0)
continue;
recieved.clear();
string tmp = "";
for (int i = 0; i < strlen(buff); i++)
{
tmp.push_back(buff[i]);
if (buff[i] == '\n')
{
if (tmp == END_OF_THIS_WORKERS_RESULT)
{
workers_done++;
tmp = "";
continue;
}
recieved.push_back(split_line_by_dash(tmp));
tmp = "";
continue;
}
}
if (this->order != NOT_SORTED)
recieved = this->sort_data(recieved);
received_data = this->merge_data(recieved, this->received_data);
if (workers_done >= workers_cnt)
break;
}
close(fd);
std::cout << "Done!\n";
}
vector <vector <string>> presenter::sort_data(vector <vector <string>> data)
{
if (data.size() == 0)
return data;
sort(data.begin(), data.end(), comp);
return data;
}
vector<vector<string>> presenter::merge_data(const vector<vector <string>> &data1, const vector <vector <string>> &data2)
{
vector<vector<string>> result;
if (data1.size() == 0)
return this->received_data;
if (this->order == NOT_SORTED)
{
for (int i = 0; i < data1.size(); i++)
result.push_back(data1[i]);
return result;
}
if (data2.size() == 0)
{
for (int i = 0; i < data1.size(); i++)
result.push_back(data1[i]);
return result;
}
result.resize(data1.size() + data2.size());
std::merge(data1.begin(), data1.end(), data2.begin(), data2.end(), result.begin(), comp);
return result;
}
void presenter::print_data()
{
std::cout << "-------------------------- result --------------------------\n";
for (int i = 0; i < this->received_data.size(); i++)
for (int j = 0; j < this->received_data[i].size(); j++)
{
std::cout << this->received_data[i][j];
if (j != this->received_data[i].size() - 1)
std::cout << " - ";
}
std::cout << endl;
}
presenter::~presenter()
{
}