13 return "No Item found with that key\n";
19 return "No Bucket found with that key\n";
26 template<
typename K,
typename V>
30 hashnode(
const K& key,
const V& value) : key(key), value(value) {}
63 return static_cast<size_t>(key) % Size;
74 template<
typename K,
typename V,
typename F = HashFunc<K>>
80 hashmap(
int startSize = 8) : maxSize(startSize) {
91 unsigned int index = hashFunc(key, maxSize);
92 auto entry = buf[index];
95 while (entry !=
nullptr) {
96 if (entry->getKey() == key) {
97 return entry->getValue();
99 entry = entry->getNext();
107 void put(
const K& key,
const V& value) {
111 if (size >= maxSize) {
116 int index = hashFunc(key, maxSize);
118 auto entry = buf[index];
121 while (entry !=
nullptr && entry->getKey() != key) {
126 if (entry ==
nullptr) {
128 if (prev ==
nullptr) {
135 entry->setValue(value);
145 int index = hashFunc(key, maxSize);
147 auto entry = buf[index];
150 while(entry !=
nullptr && entry->getKey() != key) {
155 if (entry ==
nullptr) {
158 if (prev ==
nullptr) {
160 buf[index] = entry->getNext();
176 for (
int i=0; i<maxSize; i++) {
178 auto oldEntry = buf[i];
181 while (oldEntry !=
nullptr) {
184 int index = hashFunc(oldEntry->getKey(), newSize);
186 auto newEntry = temp[index];
190 temp[index] =
new hashnode<K, V>(oldEntry->getKey(), oldEntry->getValue());
193 auto prev = newEntry->getNext();
196 while (prev !=
nullptr) {
197 prev = prev->getNext();
201 prev->setNext(
new hashnode<K, V>(oldEntry->getKey(), oldEntry->getValue()));
206 oldEntry = oldEntry->getNext();
220 for (
int i=0; i<maxSize; i++) {
226 while (entry !=
nullptr) {
231 entry = entry->getNext();
249 hashnode<K, V>** buf;
Definition application.h:13
const char * what()
Definition hashmap.h:12
const char * what()
Definition hashmap.h:18
hashnode * getNext() const
Definition hashmap.h:41
V getValue() const
Definition hashmap.h:37
void setValue(V newVal)
Definition hashmap.h:45
K getKey() const
Definition hashmap.h:33
void setNext(hashnode *newNext)
Definition hashmap.h:49
hashnode(const K &key, const V &value)
Definition hashmap.h:30
size_t operator()(const K &key, int Size) const
Definition hashmap.h:62
void rehash(int newSize)
Creates a new table of the given size and rehash all of the previous items.
Definition hashmap.h:170
~hashmap()
Definition hashmap.h:84
void remove(const K &key)
Removes the item.
Definition hashmap.h:140
V get(const K &key)
Gets the value based on the key given.
Definition hashmap.h:89
void put(const K &key, const V &value)
puts an item in the table.
Definition hashmap.h:107
hashmap(int startSize=8)
Basic constructor with a startSize of 8.
Definition hashmap.h:80