-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashset.h
219 lines (185 loc) · 5.49 KB
/
hashset.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
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
/* Copyright (c) 2015 Regents of the University of California
*
* Author: Brian Demsky <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/
#ifndef HASH_SET_H
#define HASH_SET_H
#include "hashtable.h"
template<typename _Key>
struct LinkNode {
_Key key;
LinkNode<_Key> *prev;
LinkNode<_Key> *next;
};
template<typename _Key, typename _KeyInt, int _Shift, void * (* _malloc)(size_t), void * (* _calloc)(size_t, size_t), void (*_free)(void *), unsigned int (*hash_function)(_Key), bool (*equals)(_Key, _Key)>
class HashSet;
template<typename _Key, typename _KeyInt, int _Shift, void * (* _malloc)(size_t) = snapshot_malloc, void * (* _calloc)(size_t, size_t) = snapshot_calloc, void (*_free)(void *) = snapshot_free, unsigned int (*hash_function)(_Key) = default_hash_function<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = default_equals<_Key> >
class HSIterator {
public:
HSIterator(LinkNode<_Key> *_curr, HashSet <_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * _set) :
curr(_curr),
set(_set)
{
}
/** Override: new operator */
void * operator new(size_t size) {
return _malloc(size);
}
/** Override: delete operator */
void operator delete(void *p, size_t size) {
_free(p);
}
/** Override: new[] operator */
void * operator new[](size_t size) {
return _malloc(size);
}
/** Override: delete[] operator */
void operator delete[](void *p, size_t size) {
_free(p);
}
bool hasNext() {
return curr!=NULL;
}
_Key next() {
_Key k=curr->key;
last=curr;
curr=curr->next;
return k;
}
_Key currKey() {
return last->key;
}
void remove() {
_Key k=last->key;
set->remove(k);
}
private:
LinkNode<_Key> *curr;
LinkNode<_Key> *last;
HashSet <_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * set;
};
template<typename _Key, typename _KeyInt, int _Shift = 0, void * (*_malloc)(size_t) = snapshot_malloc, void * (*_calloc)(size_t, size_t) = snapshot_calloc, void (*_free)(void *) = snapshot_free, unsigned int (*hash_function)(_Key) = default_hash_function<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = default_equals<_Key> >
class HashSet {
public:
HashSet(unsigned int initialcapacity = 16, double factor = 0.5) :
table(new HashTable<_Key, LinkNode<_Key> *, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals>(initialcapacity, factor)),
list(NULL),
tail(NULL)
{
}
/** @brief Hashset destructor */
~HashSet() {
LinkNode<_Key> *tmp=list;
while(tmp!=NULL) {
LinkNode<_Key> *tmpnext=tmp->next;
_free(tmp);
tmp=tmpnext;
}
delete table;
}
HashSet<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * copy() {
HashSet<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> *copy=new HashSet<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals>(table->getCapacity(), table->getLoadFactor());
HSIterator<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * it=iterator();
while(it->hasNext())
copy->add(it->next());
delete it;
return copy;
}
void reset() {
LinkNode<_Key> *tmp=list;
while(tmp!=NULL) {
LinkNode<_Key> *tmpnext=tmp->next;
_free(tmp);
tmp=tmpnext;
}
list=tail=NULL;
table->reset();
}
/** @brief Adds a new key to the hashset. Returns false if the key
* is already present. */
bool add(_Key key) {
LinkNode<_Key> * val=table->get(key);
if (val==NULL) {
LinkNode<_Key> * newnode=(LinkNode<_Key> *)_malloc(sizeof(struct LinkNode<_Key>));
newnode->prev=tail;
newnode->next=NULL;
newnode->key=key;
if (tail!=NULL)
tail->next=newnode;
else
list=newnode;
tail=newnode;
table->put(key, newnode);
return true;
} else
return false;
}
/** @brief Gets the original key corresponding to this one from the
* hashset. Returns NULL if not present. */
_Key get(_Key key) {
LinkNode<_Key> * val=table->get(key);
if (val!=NULL)
return val->key;
else
return NULL;
}
_Key getFirstKey() {
return list->key;
}
bool contains(_Key key) {
return table->get(key)!=NULL;
}
bool remove(_Key key) {
LinkNode<_Key> * oldlinknode;
oldlinknode=table->get(key);
if (oldlinknode==NULL) {
return false;
}
table->remove(key);
//remove link node from the list
if (oldlinknode->prev==NULL)
list=oldlinknode->next;
else
oldlinknode->prev->next=oldlinknode->next;
if (oldlinknode->next!=NULL)
oldlinknode->next->prev=oldlinknode->prev;
else
tail=oldlinknode->prev;
_free(oldlinknode);
return true;
}
unsigned int getSize() {
return table->getSize();
}
bool isEmpty() {
return getSize()==0;
}
HSIterator<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * iterator() {
return new HSIterator<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals>(list, this);
}
/** Override: new operator */
void * operator new(size_t size) {
return _malloc(size);
}
/** Override: delete operator */
void operator delete(void *p, size_t size) {
_free(p);
}
/** Override: new[] operator */
void * operator new[](size_t size) {
return _malloc(size);
}
/** Override: delete[] operator */
void operator delete[](void *p, size_t size) {
_free(p);
}
private:
HashTable<_Key, LinkNode<_Key>*, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * table;
LinkNode<_Key> *list;
LinkNode<_Key> *tail;
};
#endif