-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
237 lines (217 loc) · 5.44 KB
/
main.cpp
File metadata and controls
237 lines (217 loc) · 5.44 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <stdio.h>
#include "linked_list.h"
#include <fstream>
#include <thread>
#include <mutex>
#include <chrono>
#include <string>
#include <time.h>
const int tree_array_size = 20;
const int heap_size = 0;
const int INF = 100000;
const int packets_handled = 1;
auto g_lock()
{
static mutex m;
return unique_lock<decltype(m)>(m);
}
void string_to_char(string value, char *t)
{
int n = value.length();
int i;
for (i = 0; i < n; i++)
{
t[i] = value[i];
if (value[i] == ',')
{
if (value[i + 1] == '"' && value[i - 1] == '"')
{
t[i] = value[i];
}
else
{
t[i] = '-';
}
}
}
t[i] = '\0';
}
void swap(int *, int *) noexcept;
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int get_right_child(linked_list &l, int index)
{
if ((((2 * index) + 1) <= l.get_count()) && (index >= 1))
{
return (2 * index) + 1;
}
return -1;
}
int get_left_child(linked_list &l, int index)
{
if (((2 * index) <= l.get_count()) && (index >= 1))
{
return 2 * index;
}
return -1;
}
int get_parent(linked_list &l, int index)
{
if ((index > 1) && (index <= l.get_count()))
{
return index / 2;
}
return -1;
}
void max_heapify(linked_list &A, int index)
{
int left_child_index = get_left_child(A, index);
int right_child_index = get_right_child(A, index);
node *left_ = A.get_node(left_child_index);
node *right_ = A.get_node(right_child_index);
int largest = index;
node *largest_ = A.get_node(largest);
if ((left_child_index <= heap_size) && (left_child_index > 0))
{
if (A[left_child_index] > A[largest])
{
largest = left_child_index;
}
else if (A[left_child_index] == A[largest])
{
if (strcmp(left_->time, largest_->time) < 0)
{
largest = left_child_index;
}
}
}
if ((right_child_index <= heap_size && (right_child_index > 0)))
{
if (A[right_child_index] > A[largest])
{
largest = right_child_index;
}
else if (A[right_child_index] == A[largest])
{
if (strcmp(right_->time, largest_->time) < 0)
{
largest = right_child_index;
}
}
}
if (A[largest] != A[index])
{
exchange(A.get_node(index), A.get_node(largest));
max_heapify(A, largest);
}
}
void build_max_heap(linked_list &A)
{
int i;
for (i = heap_size / 2; i >= 1; i--)
{
max_heapify(A, i);
}
}
int maximum(linked_list &A)
{
return A[1];
}
node *extract_max(linked_list &A)
{
if (A.get_count() == 1)
{
auto save = std::unique_ptr<node>();
save_copy(save, A.get_node(1));
A.delete_last();
return save;
}
else if (A.get_count() > 1)
{
auto save = std::unique_ptr<node>();
save_copy(save, A.get_node(1));
exchange(A.get_node(1), A.get_node(heap_size));
heap_size--;
A.delete_last();
max_heapify(A, 1);
return save;
}
}
void increase_key(linked_list &A, int index, int key)
{
while ((index > 1) && (A[get_parent(A, index)] < A[index]))
{
exchange(A.get_node(index), A.get_node(get_parent(A, index)));
index = get_parent(A, index);
}
}
void insert(linked_list &A, char *sent)
{
heap_size++;
A.insert_end(sent);
increase_key(A, heap_size, A.get_key());
}
void print_heap(linked_list &A)
{
for (int i = 1; i <= heap_size; i++)
{
printf("%d\n", A[i]);
}
printf("\n");
}
void receive_packets()
{
cout << "\n---------------------";
cout << "\n###Starting Router###\n";
cout << "---------------------\n";
std::this_thread::sleep_for(200ms);
string sent;
sent.resize(1000);
string sentence;
ifstream file("1.csv");
int count = 0;
getline(file, sent);
while (getline(file, sent))
{
cout << "Receiving Packet : ";
using namespace std::literals;
std::this_thread::sleep_for(5ms);
string_to_char(sent, sentence);
insert(A, sentence);
cout << "\n\n??????Packets Handled : " << packets_handled++ << endl;
}
}
void send_packets()
{
std::this_thread::sleep_for(400ms);
while (A.get_count() != 0)
{
cout << "\n#########################################CURRENT QUEUE ###"
<< "############################## " << A.get_count() << endl;
cout << "Re-Transmitting Packet : \n";
display(extract_max(A));
using namespace std::literals;
std::this_thread::sleep_for(10ms);
}
}
int main()
{
time_t start;
time_t end;
start = clock();
thread t1(receive_packets);
thread t2(send_packets);
t1.join();
t2.join();
end = clock();
cout << packets_handled << endl;
time_t elapsed = (end - start) / 1000.0;
cout << "\n\nPACKETS HANDLED : " << packets_handled << endl;
cout << "\nTime Elapsed : " << (end - start) / 1000.0 << "seconds; AVERAGE PACKETS HANDLED PER SECOND : " << (float)packets_handled / elapsed << endl;
return 0;
}