-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterable_hashset.hpp
107 lines (104 loc) · 3.32 KB
/
iterable_hashset.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
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
#ifndef iterable_hashset_hpp
#define iterable_hashset_hpp
#include "LinkedSet.hpp"
#include "hashfn.hpp"
template <class T, class hasher = hashfn<T>>
class IterableSet {
private:
LinkedSet<T>* table;
int n;
int maxn;
void init(int max) {
maxn = max;
n = 0;
table = new LinkedSet<T>[maxn];
}
class Iterator {
private:
LinkedSet<T>* __setPtr;
typename LinkedSet<T>::Iterator __setIter;
T junk;
public:
Iterator(LinkedSet<T>* __Ptr, T info) {
__setPtr = __Ptr;
__setIter = __Ptr.begin();
while (__setIter != __Ptr->end() && *__setIter != info) __setIter++;
}
Iterator(LinkedSet<T>* __Ptr) {
__setPtr = __Ptr;
while (__setPtr && __setPtr->isEmpty()) {
__setPtr++;
}
if (__setPtr)
__setIter = __setPtr->begin();
}
Iterator() {
}
T& operator*() {
if (__setIter != __setPtr->end())
return *__setIter;
return junk;
}
Iterator operator++() {
if (__setIter != __setPtr->end()) {
++__setIter;
if (__setIter != __setPtr->end()) {
return *this;
}
}
do {
__setPtr++;
} while (__setPtr->isEmpty());
__setIter = __setPtr->begin();
return *this;
}
Iterator operator++(int) {
Iterator it = *this;
++*this;
return it;
}
bool operator==(const Iterator& o) const noexcept {
return __setPtr == o.__setPtr && __setIter == o.__setIter;
}
bool operator!=(const Iterator& o) const noexcept {
return !(*this==o);
}
};
public:
IterableSet(int max = 113) {
init(max);
}
~IterableSet() {
delete [] table;
}
int size() const {
return n;
}
int max_size() const {
return maxn;
}
bool isEmpty() const {
return n == 0;
}
void put(T info) {
unsigned int idx = hasher()(info) % maxn;
table[idx].add(info);
n++;
}
void remove(T info) {
unsigned int idx = hasher()(info) % maxn;
table[idx].remove(info);
n++;
}
Iterator find(T info) {
unsigned int idx = hasher()(info) % maxn;
return table[idx].find(info) == table[idx].end() ? end():Iterator(table+idx, info);
}
Iterator begin() {
return Iterator(table);
}
Iterator end() {
return Iterator(table+maxn);
}
};
#endif