Updated Hashtable to use xnoe::Maybe, fixed many bugs within hashtable, updated hash

This commit is contained in:
Xnoe 2021-11-21 16:40:16 +00:00
parent f581e6b6fe
commit b79ae4ec95
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
3 changed files with 25 additions and 14 deletions

View File

@ -8,4 +8,9 @@ namespace xnoe {
uint32_t hash<void*>(void* t) {
return (uint32_t) t;
}
template<>
uint32_t hash<uint32_t>(uint32_t t) {
return t;
}
}

View File

@ -9,6 +9,8 @@ namespace xnoe {
template<>
uint32_t hash<void*>(void* t);
template<>
uint32_t hash<uint32_t>(uint32_t t);
}
#endif

View File

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