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(Hashlistnode<_Key, _Val> *_curr, Hashtable <_Key, _Val, _KeyInt, _Shift, hash_function, equals> *_table) :
26 /** Override: new operator */
27 void *operator new(size_t size) {
28 return ourmalloc(size);
31 /** Override: delete operator */
32 void operator delete(void *p, size_t size) {
36 /** Override: new[] operator */
37 void *operator new[](size_t size) {
38 return ourmalloc(size);
41 /** Override: delete[] operator */
42 void operator delete[](void *p, size_t size) {
71 Hashlistnode<_Key,_Val> *curr;
72 Hashlistnode<_Key, _Val> *last;
73 Hashtable <_Key, _Val, _KeyInt, _Shift, hash_function, equals> *table;
76 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> >
79 Hashset(unsigned int initialcapacity = 16, double factor = 0.5) :
80 table(new Hashtable<_Key, _Key, _KeyInt, _Shift, hash_function, equals>(initialcapacity, factor))
84 /** @brief Hashset destructor */
89 Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *copy() {
90 Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *copy = new Hashset<_Key, _KeyInt, _Shift, hash_function, equals>(table->getCapacity(), table->getLoadFactor());
91 SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *it = iterator();
93 copy->add(it->next());
102 /** @brief Adds a new key to the hashset. Returns false if the key
103 * is already present. */
105 void addAll(Hashset<_Key, _KeyInt, _Shift, hash_function, equals> *table) {
106 SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *it = iterator();
107 while (it->hasNext())
112 /** @brief Adds a new key to the hashset. Returns false if the key
113 * is already present. */
116 if (!table->contains(key)) {
117 table->put(key, key);
123 /** @brief Gets the original key corresponding to this one from the
124 * hashset. Returns NULL if not present. */
127 return table->get(key);
131 return table->list->key;
134 bool contains(_Key key) {
135 return table->contains(key);
138 bool remove(_Key key) {
139 if (!table->contains(key))
145 unsigned int size() const {
146 return table->size();
149 bool isEmpty() const {
153 SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *iterator() {
154 return new SetIterator<_Key, _Key, _KeyInt, _Shift, hash_function, equals>(table->list, table);
157 /** Override: new operator */
158 void *operator new(size_t size) {
159 return ourmalloc(size);
162 /** Override: delete operator */
163 void operator delete(void *p, size_t size) {
167 /** Override: new[] operator */
168 void *operator new[](size_t size) {
169 return ourmalloc(size);
172 /** Override: delete[] operator */
173 void operator delete[](void *p, size_t size) {
177 Hashtable<_Key, _Key, _KeyInt, _Shift, hash_function, equals> *table;
180 template<typename _Key, typename _Val, typename _KeyInt, int _Shift, unsigned int (*hash_function)(_Key), bool (*equals)(_Key, _Key)>
181 SetIterator<_Key, _Val,_KeyInt, _Shift, hash_function, equals> *getKeyIterator(Hashtable<_Key,_Val,_KeyInt,_Shift,hash_function,equals> *table) {
182 return new SetIterator<_Key, _Val, _KeyInt, _Shift, hash_function, equals>(table->list, table);