-
Notifications
You must be signed in to change notification settings - Fork 5
/
uqueue.h
76 lines (63 loc) · 1.01 KB
/
uqueue.h
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
// queue with unique elements.
#ifndef uqueue_h
#define uqueue_h
#include <queue>
#include <unordered_set>
#include "rpc/marshall.h"
template <typename T>
class uqueue {
private:
std::queue<T> q;
std::unordered_set<T> h;
public:
bool push(const T &x) {
if (h.count(x)) {
return false;
}
q.push(x);
h.insert(x);
return true;
}
T front() const {
return q.front();
}
void pop() {
T r = q.front();
h.erase(r);
q.pop();
}
bool empty() const {
return q.empty();
}
std::queue<T> get() const {
return q;
}
};
template <typename T>
inline marshall &
operator<<(marshall &m, uqueue<T> q)
{
std::queue<T> _q = q.get();
m << (int) _q.size();
while (!_q.empty()) {
T elem = _q.front();
_q.pop();
m << elem;
}
return m;
}
template <typename T>
inline unmarshall &
operator>>(unmarshall &u, uqueue<T> &q)
{
int size;
u >> size;
while (size > 0) {
size -= 1;
T elem;
u >> elem;
q.push(elem);
}
return u;
}
#endif