Fixed major bug in hashtable.h that caused the hashtable to not actually store any data
This commit is contained in:
parent
b879ae1e92
commit
b7566ef777
@ -5,6 +5,7 @@
|
|||||||
#include "linkedlist.h"
|
#include "linkedlist.h"
|
||||||
#include "../memory.h"
|
#include "../memory.h"
|
||||||
#include "maybe.h"
|
#include "maybe.h"
|
||||||
|
#include "../screenstuff.h"
|
||||||
|
|
||||||
namespace xnoe {
|
namespace xnoe {
|
||||||
template <class key, class value>
|
template <class key, class value>
|
||||||
@ -18,36 +19,57 @@ namespace xnoe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void set(key k, value v) {
|
void set(key k, value v) {
|
||||||
xnoe::linkedlist<xnoe::tuple<key, value>> list = table[xnoe::hash<key>(k) % 4096];
|
xnoe::linkedlist<xnoe::tuple<key, value>>* list = &table[xnoe::hash<key>(k) % 4096];
|
||||||
xnoe::linkedlistelem<xnoe::tuple<key, value>>* current = list.start;
|
xnoe::linkedlistelem<xnoe::tuple<key, value>>* current = list->start;
|
||||||
|
|
||||||
bool exists = false;
|
bool exists = false;
|
||||||
|
|
||||||
if (current) {
|
if (current) {
|
||||||
while (current->next) {
|
while (current) {
|
||||||
if (xnoe::get<0>(current->elem) == k) {
|
if (xnoe::get<0>(current->elem) == k) {
|
||||||
exists = true;
|
exists = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exists)
|
if (exists)
|
||||||
current->elem = xnoe::tuple<key, value>(k, v);
|
current->elem = xnoe::tuple<key, value>(k, v);
|
||||||
else
|
else {
|
||||||
list.append(xnoe::tuple<key, value>(k, v));
|
printf("Appended.\n");
|
||||||
|
list->append(xnoe::tuple<key, value>(k, v));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
xnoe::Maybe<value> get(key k) {
|
xnoe::Maybe<value> get(key k) {
|
||||||
xnoe::linkedlist<xnoe::tuple<key, value>> list = table[xnoe::hash<key>(k) % 4096];
|
xnoe::linkedlist<xnoe::tuple<key, value>>* list = &table[xnoe::hash<key>(k) % 4096];
|
||||||
xnoe::linkedlistelem<xnoe::tuple<key, value>>* current = list.start;
|
xnoe::linkedlistelem<xnoe::tuple<key, value>>* current = list->start;
|
||||||
if (current)
|
if (current) {
|
||||||
while (current->next)
|
while (current) {
|
||||||
if (xnoe::get<0>(current->elem) == k)
|
if (xnoe::get<0>(current->elem) == k)
|
||||||
return xnoe::Maybe<value>(xnoe::get<1>(current->elem));
|
return xnoe::Maybe<value>(xnoe::get<1>(current->elem));
|
||||||
|
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return xnoe::Maybe<value>();
|
return xnoe::Maybe<value>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void remove(key k, value v) {
|
||||||
|
xnoe::linkedlist<xnoe::tuple<key, value>>* list = &table[xnoe::hash<key>(k) % 4096];
|
||||||
|
xnoe::linkedlistelem<xnoe::tuple<key, value>>* current = list->start;
|
||||||
|
if (current) {
|
||||||
|
while (current) {
|
||||||
|
if (xnoe::get<0>(current->elem) == k) {
|
||||||
|
list->remove(current);
|
||||||
|
delete current;
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user