-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlQueue.h
More file actions
115 lines (74 loc) · 2.71 KB
/
Copy pathUrlQueue.h
File metadata and controls
115 lines (74 loc) · 2.71 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
#pragma once
#include <queue>
#include <vector>
#include "../ranker/StaticRanker.h"
#include <cf/ParsedUrl.h>
// Data Structure that abstracts random K access to a queue of URLs
class UrlQueue {
private:
std::vector<string> urls;
//? CAN THIS BE A VECTOR ?
//* YUH BECAUSE ORDERING DOESN:T MATTER IF ITS ALREADY IIN THE POOL
std::priority_queue<string, std::vector<string>, StaticRanker> urlPool;
static constexpr size_t MAX_POOL_CANDIDATES = 20000;
static constexpr size_t MAX_POOL_SIZE = 5000;
void fillUrlPool() {
// select random K urls from urls and add them to urlPool
const size_t k = std::min(urls.size(), MAX_POOL_SIZE);
const size_t N = std::min(urls.size(), MAX_POOL_CANDIDATES);
unsigned int count = 0;
while (count < N) {
const unsigned int randomIndex = rand() % urls.size();
const string& selectedUrl = urls[randomIndex];
string curr = ParsedUrl(selectedUrl).Host;
count++;
urlPool.push(selectedUrl);
// swap the selected url with the last url in the vector to efficiently remote it
std::swap(urls[randomIndex], urls[urls.size() - 1]);
urls.pop_back();
}
for (int i = 0; i < (N - k); i++) {
string top = urlPool.top();
urls.push_back(top);
urlPool.pop();
}
// remaining 5000 urls are sorted in reverse
}
public:
std::vector<string> *getUrls() {
return &urls;
}
UrlQueue() = default;
void addUrl(const string &url) {
urls.push_back(url);
}
string getNextUrl() {
if (urlPool.empty() and urls.empty()) {
throw std::runtime_error("No URLs available");
}
if (urlPool.empty()) {
fillUrlPool();
}
string nextUrl, curr;
nextUrl = urlPool.top();
urlPool.pop();
curr = ParsedUrl(nextUrl).Host;
return nextUrl;
}
inline bool empty() const {
return urls.empty() and urlPool.empty();
}
inline bool vecempty() const {
return urls.empty();
}
inline int size() const {
return (urls.size() + urlPool.size());
}
string &at(int i) {
return urls[i];
}
inline void erase(int i) {
std::swap(urls[urls.size() - 1], urls[i]);
urls.pop_back();
}
};