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 /** @brief Adds a new key to the hashset. Returns false if the key
121 * is already present. */
123 void addAll(Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *table) {
124 SetIterator<_Key, _KeyInt, _Shift, hash_function, equals> *it = iterator();
125 while (it->hasNext())
130 /** @brief Adds a new key to the hashset. Returns false if the key
131 * is already present. */
134 Linknode<_Key> *val = table->get(key);
136 Linknode<_Key> *newnode = (Linknode<_Key> *)ourmalloc(sizeof(struct Linknode<_Key>));
137 newnode->prev = tail;
138 newnode->next = NULL;
141 tail->next = newnode;
145 table->put(key, newnode);
151 /** @brief Return random key from set. */
153 _Key getRandomElement() {
156 else if (getSize() < 6) {
157 uint count = random() % getSize();
158 Linknode<_Key> *ptr = list;
165 return table->getRandomValue()->key;
168 /** @brief Gets the original key corresponding to this one from the
169 * hashset. Returns NULL if not present. */
172 Linknode<_Key> *val = table->get(key);
183 bool contains(_Key key) {
184 return table->get(key) != NULL;
187 bool remove(_Key key) {
188 Linknode<_Key> *oldlinknode;
189 oldlinknode = table->get(key);
190 if (oldlinknode == NULL) {
195 //remove link node from the list
196 if (oldlinknode->prev == NULL)
197 list = oldlinknode->next;
199 oldlinknode->prev->next = oldlinknode->next;
200 if (oldlinknode->next != NULL)
201 oldlinknode->next->prev = oldlinknode->prev;
203 tail = oldlinknode->prev;
204 ourfree(oldlinknode);
208 unsigned int getSize() {
209 return table->getSize();
213 return getSize() == 0;
216 SetIterator<_Key, _KeyInt, _Shift, hash_function, equals> *iterator() {
217 return new SetIterator<_Key, _KeyInt, _Shift, hash_function, equals>(list, this);
220 /** Override: new operator */
221 void *operator new(size_t size) {
222 return ourmalloc(size);
225 /** Override: delete operator */
226 void operator delete(void *p, size_t size) {
230 /** Override: new[] operator */
231 void *operator new[](size_t size) {
232 return ourmalloc(size);
235 /** Override: delete[] operator */
236 void operator delete[](void *p, size_t size) {
240 Hashtable<_Key, Linknode<_Key> *, _KeyInt, _Shift, hash_function, equals> *table;
241 Linknode<_Key> *list;
242 Linknode<_Key> *tail;