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, void * (* _malloc)(size_t), void * (* _calloc)(size_t, size_t), void (*_free)(void *), unsigned int (*hash_function)(_Key), bool (*equals)(_Key, _Key)>
24 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> >
27 HSIterator(LinkNode<_Key> *_curr, HashSet <_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * _set) :
33 /** Override: new operator */
34 void * operator new(size_t 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) {
48 /** Override: delete[] operator */
49 void operator delete[](void *p, size_t size) {
76 HashSet <_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * set;
79 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> >
82 HashSet(unsigned int initialcapacity = 16, double factor = 0.5) :
83 table(new HashTable<_Key, LinkNode<_Key> *, _KeyInt, _Shift, _malloc, _calloc, _free, 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, _malloc, _calloc, _free, hash_function, equals> * copy() {
101 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());
102 HSIterator<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * it=iterator();
104 copy->add(it->next());
110 LinkNode<_Key> *tmp=list;
112 LinkNode<_Key> *tmpnext=tmp->next;
120 /** @brief Adds a new key to the hashset. Returns false if the key
121 * is already present. */
124 LinkNode<_Key> * val=table->get(key);
126 LinkNode<_Key> * newnode=(LinkNode<_Key> *)_malloc(sizeof(struct LinkNode<_Key>));
135 table->put(key, newnode);
141 /** @brief Gets the original key corresponding to this one from the
142 * hashset. Returns NULL if not present. */
145 LinkNode<_Key> * val=table->get(key);
156 bool contains(_Key key) {
157 return table->get(key)!=NULL;
160 bool remove(_Key key) {
161 LinkNode<_Key> * oldlinknode;
162 oldlinknode=table->get(key);
163 if (oldlinknode==NULL) {
168 //remove link node from the list
169 if (oldlinknode->prev==NULL)
170 list=oldlinknode->next;
172 oldlinknode->prev->next=oldlinknode->next;
173 if (oldlinknode->next!=NULL)
174 oldlinknode->next->prev=oldlinknode->prev;
176 tail=oldlinknode->prev;
181 unsigned int getSize() {
182 return table->getSize();
191 HSIterator<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * iterator() {
192 return new HSIterator<_Key, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals>(list, this);
195 /** Override: new operator */
196 void * operator new(size_t size) {
197 return _malloc(size);
200 /** Override: delete operator */
201 void operator delete(void *p, size_t size) {
205 /** Override: new[] operator */
206 void * operator new[](size_t size) {
207 return _malloc(size);
210 /** Override: delete[] operator */
211 void operator delete[](void *p, size_t size) {
215 HashTable<_Key, LinkNode<_Key>*, _KeyInt, _Shift, _malloc, _calloc, _free, hash_function, equals> * table;
216 LinkNode<_Key> *list;
217 LinkNode<_Key> *tail;