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 "../memory.h"
|
||||
#include "maybe.h"
|
||||
#include "../screenstuff.h"
|
||||
|
||||
namespace xnoe {
|
||||
template <class key, class value>
|
||||
@ -18,36 +19,57 @@ namespace xnoe {
|
||||
}
|
||||
|
||||
void set(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;
|
||||
xnoe::linkedlist<xnoe::tuple<key, value>>* list = &table[xnoe::hash<key>(k) % 4096];
|
||||
xnoe::linkedlistelem<xnoe::tuple<key, value>>* current = list->start;
|
||||
|
||||
bool exists = false;
|
||||
|
||||
if (current) {
|
||||
while (current->next) {
|
||||
while (current) {
|
||||
if (xnoe::get<0>(current->elem) == k) {
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
if (exists)
|
||||
current->elem = xnoe::tuple<key, value>(k, v);
|
||||
else
|
||||
list.append(xnoe::tuple<key, value>(k, v));
|
||||
else {
|
||||
printf("Appended.\n");
|
||||
list->append(xnoe::tuple<key, value>(k, v));
|
||||
}
|
||||
}
|
||||
|
||||
xnoe::Maybe<value> get(key k) {
|
||||
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->next)
|
||||
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)
|
||||
return xnoe::Maybe<value>(xnoe::get<1>(current->elem));
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
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