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, typename _KeyInt, int _Shift, unsigned int (*hash_function)(_Key), bool (*equals)(_Key, _Key)>
17 template<typename _Key, typename _Val, typename _KeyInt = uintptr_t, int _Shift = 0, unsigned int (*hash_function)(_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = defaultEquals<_Key> >
20 SetIterator(Hashtable <_Key, _Val, _KeyInt, _Shift, hash_function, equals> *_table) :
27 /** Override: new operator */
28 void *operator new(size_t size) {
29 return ourmalloc(size);
32 /** Override: delete operator */
33 void operator delete(void *p, size_t size) {
37 /** Override: new[] operator */
38 void *operator new[](size_t size) {
39 return ourmalloc(size);
42 /** Override: delete[] operator */
43 void operator delete[](void *p, size_t size) {
48 if (currptr > nextptr) {
54 if (table->zero && table->zero->val)
59 for(;nextptr < (table->capacity + 1);nextptr++) {
60 struct Hashlistnode<_Key, _Val> *ptr = & table->table[nextptr-1];
61 if (ptr->key && ptr->val) {
70 return table->zero->val;
72 return table->table[currptr-1].val;
77 return table->zero->key;
79 return table->table[currptr-1].key;
88 table->remove(currKey());
94 Hashtable <_Key, _Val, _KeyInt, _Shift, hash_function, equals> *table;
97 template<typename _Key, typename _KeyInt = uintptr_t, int _Shift = 0, unsigned int (*hash_function)(_Key) = defaultHashFunction<_Key, _Shift, _KeyInt>, bool (*equals)(_Key, _Key) = defaultEquals<_Key> >
100 Hashset(unsigned int initialcapacity = 16, double factor = 0.5) :
101 table(new Hashtable<_Key, _Key, _KeyInt, _Shift, hash_function, equals>(initialcapacity, factor))
105 /** @brief Hashset destructor */
110 Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *copy() {
111 Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *copy = new Hashset<_Key, _KeyInt, _Shift, hash_function, equals>(table->getCapacity(), table->getLoadFactor());
112 SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *it = iterator();
113 while (it->hasNext())
114 copy->add(it->next());
123 /** @brief Adds a new key to the hashset. Returns false if the key
124 * is already present. */
126 void addAll(Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *table) {
127 SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *it = iterator();
128 while (it->hasNext())
133 /** @brief Adds a new key to the hashset. Returns false if the key
134 * is already present. */
137 if (!table->contains(key)) {
138 table->put(key, key);
144 /** @brief Gets the original key corresponding to this one from the
145 * hashset. Returns NULL if not present. */
148 return table->get(key);
152 return table->list->key;
155 bool contains(_Key key) {
156 return table->contains(key);
159 bool remove(_Key key) {
160 if (!table->contains(key))
166 unsigned int size() const {
167 return table->size();
170 bool isEmpty() const {
174 SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *iterator() {
175 return new SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals>(table);
178 /** Override: new operator */
179 void *operator new(size_t size) {
180 return ourmalloc(size);
183 /** Override: delete operator */
184 void operator delete(void *p, size_t size) {
188 /** Override: new[] operator */
189 void *operator new[](size_t size) {
190 return ourmalloc(size);
193 /** Override: delete[] operator */
194 void operator delete[](void *p, size_t size) {
198 Hashtable<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *table;
201 template<typename _Key, typename _Val, typename _KeyInt, int _Shift, unsigned int (*hash_function)(_Key), bool (*equals)(_Key, _Key)>
202 SetIterator<_Key, _Val,_KeyInt, _Shift, hash_function, equals> *getKeyIterator(Hashtable<_Key,_Val,_KeyInt,_Shift,hash_function,equals> *table) {
203 return new SetIterator<_Key, _Val, _KeyInt, _Shift, hash_function, equals>(table);