1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
12 #include "hashtable.h"
14 template<typename _Key>
21 template<typename _Key, typename _KeyInt, int _Shift, unsigned int (*hash_function)(_Key), bool (*equals)(_Key, _Key)>
24 template<typename _Key, typename _KeyInt, int _Shift, unsigned int (*hash_function)(_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = defaultEquals<_Key> >
27 SetIterator(Linknode<_Key> *_curr, Hashset <_Key, _KeyInt, _Shift, hash_function, equals> *_set) :
33 /** Override: new operator */
34 void *operator new(size_t size) {
35 return ourmalloc(size);
38 /** Override: delete operator */
39 void operator delete(void *p, size_t size) {
43 /** Override: new[] operator */
44 void *operator new[](size_t size) {
45 return ourmalloc(size);
48 /** Override: delete[] operator */
49 void operator delete[](void *p, size_t size) {
76 Hashset <_Key, _KeyInt, _Shift, hash_function, equals> *set;
79 template<typename _Key, typename _KeyInt, int _Shift = 0, unsigned int (*hash_function)(_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = defaultEquals<_Key> >
82 Hashset(unsigned int initialcapacity = 16, double factor = 0.5) :
83 table(new Hashtable<_Key, Linknode<_Key> *, _KeyInt, _Shift, hash_function, equals>(initialcapacity, factor)),
89 /** @brief Hashset destructor */
91 Linknode<_Key> *tmp = list;
93 Linknode<_Key> *tmpnext = tmp->next;
100 Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *copy() {
101 Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *copy = new Hashset<_Key, _KeyInt, _Shift, hash_function, equals>(table->getCapacity(), table->getLoadFactor());
102 SetIterator<_Key, _KeyInt, _Shift, hash_function, equals> *it = iterator();
103 while (it->hasNext())
104 copy->add(it->next());
110 Linknode<_Key> *tmp = list;
111 while (tmp != NULL) {
112 Linknode<_Key> *tmpnext = tmp->next;
120 void resetAndDelete() {
121 Linknode<_Key> *tmp = list;
122 while (tmp != NULL) {
123 Linknode<_Key> *tmpnext = tmp->next;
128 table->resetAndDeleteKeys();
131 /** @brief Adds a new key to the hashset. Returns false if the key
132 * is already present. */
134 void addAll(Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *table) {
135 SetIterator<_Key, _KeyInt, _Shift, hash_function, equals> *it = iterator();
136 while (it->hasNext())
141 /** @brief Adds a new key to the hashset. Returns false if the key
142 * is already present. */
145 Linknode<_Key> *val = table->get(key);
147 Linknode<_Key> *newnode = (Linknode<_Key> *)ourmalloc(sizeof(struct Linknode<_Key>));
148 newnode->prev = tail;
149 newnode->next = NULL;
152 tail->next = newnode;
156 table->put(key, newnode);
162 /** @brief Return random key from set. */
164 _Key getRandomElement() {
167 else if (getSize() < 6) {
168 uint count = random() % getSize();
169 Linknode<_Key> *ptr = list;
176 return table->getRandomValue()->key;
179 /** @brief Gets the original key corresponding to this one from the
180 * hashset. Returns NULL if not present. */
183 Linknode<_Key> *val = table->get(key);
194 bool contains(_Key key) {
195 return table->get(key) != NULL;
198 bool remove(_Key key) {
199 Linknode<_Key> *oldlinknode;
200 oldlinknode = table->get(key);
201 if (oldlinknode == NULL) {
206 //remove link node from the list
207 if (oldlinknode->prev == NULL)
208 list = oldlinknode->next;
210 oldlinknode->prev->next = oldlinknode->next;
211 if (oldlinknode->next != NULL)
212 oldlinknode->next->prev = oldlinknode->prev;
214 tail = oldlinknode->prev;
215 ourfree(oldlinknode);
219 unsigned int getSize() const {
220 return table->getSize();
223 bool isEmpty() const {
224 return getSize() == 0;
227 SetIterator<_Key, _KeyInt, _Shift, hash_function, equals> *iterator() {
228 return new SetIterator<_Key, _KeyInt, _Shift, hash_function, equals>(list, this);
231 /** Override: new operator */
232 void *operator new(size_t size) {
233 return ourmalloc(size);
236 /** Override: delete operator */
237 void operator delete(void *p, size_t size) {
241 /** Override: new[] operator */
242 void *operator new[](size_t size) {
243 return ourmalloc(size);
246 /** Override: delete[] operator */
247 void operator delete[](void *p, size_t size) {
251 Hashtable<_Key, Linknode<_Key> *, _KeyInt, _Shift, hash_function, equals> *table;
252 Linknode<_Key> *list;
253 Linknode<_Key> *tail;