Updated Hashtable to use xnoe::Maybe, fixed many bugs within hashtable, updated hash
This commit is contained in:
parent
f581e6b6fe
commit
b79ae4ec95
@ -8,4 +8,9 @@ namespace xnoe {
|
|||||||
uint32_t hash<void*>(void* t) {
|
uint32_t hash<void*>(void* t) {
|
||||||
return (uint32_t) t;
|
return (uint32_t) t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
uint32_t hash<uint32_t>(uint32_t t) {
|
||||||
|
return t;
|
||||||
|
}
|
||||||
}
|
}
|
@ -9,6 +9,8 @@ namespace xnoe {
|
|||||||
|
|
||||||
template<>
|
template<>
|
||||||
uint32_t hash<void*>(void* t);
|
uint32_t hash<void*>(void* t);
|
||||||
|
template<>
|
||||||
|
uint32_t hash<uint32_t>(uint32_t t);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -4,6 +4,7 @@
|
|||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "linkedlist.h"
|
#include "linkedlist.h"
|
||||||
#include "../memory.h"
|
#include "../memory.h"
|
||||||
|
#include "maybe.h"
|
||||||
|
|
||||||
namespace xnoe {
|
namespace xnoe {
|
||||||
template <class key, class value>
|
template <class key, class value>
|
||||||
@ -17,32 +18,35 @@ namespace xnoe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void set(key k, value v) {
|
void set(key k, value v) {
|
||||||
xnoe::linkedlist<xnoe::tuple<key, value>> list = table[xnoe::hash<k>(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) {
|
||||||
while (current->next) {
|
while (current->next) {
|
||||||
if (xnoe::get<0>(current->elem) == k) {
|
if (xnoe::get<0>(current->elem) == k) {
|
||||||
exists = true;
|
exists = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (exists)
|
if (exists)
|
||||||
current = xnoe::tuple<key, value>(k, v);
|
current->elem = xnoe::tuple<key, value>(k, v);
|
||||||
else
|
else
|
||||||
list.append(xnoe::tuple<key, value>(k, v));
|
list.append(xnoe::tuple<key, value>(k, v));
|
||||||
}
|
}
|
||||||
|
|
||||||
value* get(key k) {
|
xnoe::Maybe<value> get(key k) {
|
||||||
xnoe::linkedlist<xnoe::tuple<key, value>> list = table[xnoe::hash<k>(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)
|
||||||
while (current->next)
|
while (current->next)
|
||||||
if (xnoe::get<0>(current->elem) == k)
|
if (xnoe::get<0>(current->elem) == k)
|
||||||
return &(xnoe::get<1>(current->elem));
|
return xnoe::Maybe<value>(xnoe::get<1>(current->elem));
|
||||||
|
|
||||||
return 0;
|
return xnoe::Maybe<value>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user