-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.hpp
67 lines (57 loc) · 1.96 KB
/
utility.hpp
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
#ifndef UTILITY_HEADER
#define UTILITY_HEADER
#include <unordered_set>
template<typename T>class intersection {
protected:
const std::unordered_set<T>&smaller;
const std::unordered_set<T>&larger;
public:
intersection(const std::unordered_set<T>&left, const std::unordered_set<T>&right) :
smaller((left.size() < right.size()) ? left : right),
larger((left.size() < right.size()) ? right : left) {}
class iterator {
protected:
typename std::unordered_set<T>::const_iterator iterator_over_smaller;
typename std::unordered_set<T>::const_iterator end_of_smaller;
const std::unordered_set<T>&larger;
public:
iterator(typename std::unordered_set<T>::const_iterator iterator_over_smaller, typename std::unordered_set<T>::const_iterator end_of_smaller, const std::unordered_set<T>&larger) :
iterator_over_smaller{iterator_over_smaller},
end_of_smaller{end_of_smaller},
larger{larger} {
while (iterator_over_smaller != end_of_smaller && larger.find(*iterator_over_smaller) == larger.end()) {
++iterator_over_smaller;
}
}
bool operator ==(const iterator&other) const {
return iterator_over_smaller == other.iterator_over_smaller;
}
bool operator !=(const iterator&other) const {
return iterator_over_smaller != other.iterator_over_smaller;
}
const T&operator *() const {
return *iterator_over_smaller;
}
const T*operator ->() const {
return &*iterator_over_smaller;
}
iterator&operator ++() {
do {
++iterator_over_smaller;
} while (iterator_over_smaller != end_of_smaller && larger.find(*iterator_over_smaller) == larger.end());
return *this;
}
iterator&operator ++(int) {
iterator result = *this;
operator ++();
return result;
}
};
iterator begin() const {
return iterator{smaller.begin(), smaller.end(), larger};
}
iterator end() const {
return iterator{smaller.end(), smaller.end(), larger};
}
};
#endif