-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebHistory.cpp
239 lines (187 loc) · 4.83 KB
/
WebHistory.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "WebHistory.hpp"
WebHistory::WebHistory()
{
// Does nothing.
}
WebHistory::WebHistory(std::string historyText)
{
// history = url timestamp | url timestamp | url timestamp...
// urls are string and timestamps are non-negative integers.
std::string delimiter = " | ";
std::string tabInfo;
std::string url;
std::string timestamp;
size_t pos = 0;
while (true)
{
pos = historyText.find(delimiter);
bool breakTheLoop = (pos == std::string::npos);
tabInfo = historyText.substr(0, pos);
historyText.erase(0, pos + delimiter.length());
pos = tabInfo.find(" ");
url = tabInfo.substr(0, pos);
timestamp = tabInfo.substr(pos + 1, tabInfo.length() - pos);
Node<Tab> *newPage = new Node<Tab>(Tab(url, std::atoi(timestamp.c_str())), NULL, NULL);
insertInOrder(newPage);
if (breakTheLoop)
{
break;
}
}
}
void WebHistory::printHistory()
{
std::cout << "Your web history:" << std::endl;
std::cout << "------------------------------------------------" << std::endl;
std::cout << std::endl;
if (history.getHead()->next == history.getTail())
{
std::cout << "History is empty." << std::endl;
std::cout << std::endl;
}
else
{
Node<Tab> *node = history.getFirstNode();
while (node != history.getTail())
{
std::cout << "Page: " << node->element.getUrl() << std::endl;
std::cout << "Last Visited: " << node->element.getTimestamp() << std::endl;
std::cout << std::endl;
node = node->next;
}
}
std::cout << "------------------------------------------------" << std::endl;
std::cout << std::endl;
}
WebHistory::WebHistory(std::string url, int timestamp)
{
// TODO
Tab newTab(url, timestamp);
history.insertAtTheEnd(newTab);
}
WebHistory::~WebHistory()
{
// TODO
history.~LinkedList<Tab>();
}
void WebHistory::insertInOrder(Node<Tab> *newPage)
{
// TODO
Node<Tab> *head = history.getHead();
Node<Tab> *tail = history.getTail();
int controller = 0;
Node<Tab> *current = head->next;
if(history.isEmpty()){
head->next = newPage;
newPage->prev = head;
tail->prev = newPage;
newPage->next = tail;
controller = 99;
}else{
while(current != tail){
if(newPage->element.getTimestamp() > current->element.getTimestamp()){
newPage->next = current;
newPage->prev = current->prev;
current->prev->next = newPage;
current->prev = newPage;
controller = 99;
break;
}
current = current->next;
}
}
if(controller != 99){
newPage->next = tail;
newPage->prev = tail->prev;
tail->prev->next = newPage;
tail->prev = newPage;
}
}
void WebHistory::goToPage(std::string url, int timestamp)
{
// TODO
Tab newTab(url, timestamp);
Node<Tab> *newPage = new Node<Tab>(newTab, NULL, NULL);
insertInOrder(newPage);
}
void WebHistory::clearHistory()
{
// TODO
history.removeAllNodes();
}
void WebHistory::clearHistory(int timestamp)
{
// TODO
Node<Tab> *head = history.getHead();
Node<Tab> *tail = history.getTail();
Node<Tab> *current = head->next;
while(current != tail){
if(current->element.getTimestamp() == timestamp){
current->next->prev = current->prev;
current->prev->next = current->next;
delete current;
break;
}
current = current->next;
}
}
WebHistory WebHistory::operator+(const WebHistory &rhs) const
{
// TODO
Node<Tab> *rhsHead = rhs.history.getHead();
Node<Tab> *rhsTail = rhs.history.getTail();
Node<Tab> *lhsHead = history.getHead();
Node<Tab> *lhsTail = history.getTail();
WebHistory newWebHistory;
if(rhsHead->next != rhsTail){
Node<Tab> *current = rhsHead->next;
while(current != rhsTail){
Node<Tab> *newPage = new Node<Tab>(current->element, NULL, NULL);
newWebHistory.insertInOrder(newPage);
current = current->next;
}
}
if(lhsHead->next != lhsTail){
Node<Tab> *current = lhsHead->next;
while(current != lhsTail){
Node<Tab> *newPage = new Node<Tab>(current->element, NULL, NULL);
newWebHistory.insertInOrder(newPage);
current = current->next;
}
}
return newWebHistory;
}
int WebHistory::timesVisited(std::string pageName)
{
// BONUS
int counter = 0;
Node<Tab> *head = history.getHead();
Node<Tab> *tail = history.getTail();
Node<Tab> *current = head->next;
while(current != tail){
if(current->element.getWebSite() == pageName){
counter++;
}
current = current->next;
}
return counter;
}
std::string WebHistory::mostVisited()
{
// BONUS
Node<Tab> *head = history.getHead();
Node<Tab> *tail = history.getTail();
Node<Tab> *current = head->next;
int currentWebsiteVisitNumber = 0;
int max=0;
std::string websiteName;
while(current != tail){
currentWebsiteVisitNumber = timesVisited(current->element.getWebSite());
if (max < currentWebsiteVisitNumber){
max = currentWebsiteVisitNumber;
websiteName = current->element.getWebSite();
}
current = current->next;
}
return websiteName;
}