Hashtable2 with many bugs
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#define Autoarr2_NO_REZULT -1
|
||||
#define Autoarr2_NO_REZULT (uint32)-1
|
||||
|
||||
|
||||
template<typename T>
|
||||
@@ -20,13 +20,16 @@ public:
|
||||
T get(uint32 index);
|
||||
void set(uint32 index, T value);
|
||||
void add(T value);
|
||||
void remove(uint32 index);
|
||||
|
||||
// returns index of the first <value> inclusion
|
||||
// using <cmpf> to compare values
|
||||
using cmp_func_t=bool (*)(T, T);
|
||||
uint32 search(T& value, cmp_func_t cmpf, uint32 fromIndex, uint32 toIndex);
|
||||
uint32 search(T& value, cmp_func_t cmpf, uint32 fromIndex);
|
||||
uint32 search(T& value, cmp_func_t cmpf);
|
||||
template<class cmp_func_lambda>
|
||||
uint32 search(T& value, cmp_func_lambda cmpf, uint32 fromIndex, uint32 toIndex);
|
||||
template<class cmp_func_lambda>
|
||||
uint32 search(T& value, cmp_func_lambda cmpf, uint32 fromIndex);
|
||||
template<class cmp_func_lambda>
|
||||
uint32 search(T& value, cmp_func_lambda cmpf);
|
||||
};
|
||||
|
||||
|
||||
@@ -49,7 +52,7 @@ Autoarr2<T>::Autoarr2(uint16 _max_block_length) : Autoarr2() {
|
||||
template<typename T>
|
||||
Autoarr2<T>::~Autoarr2() {
|
||||
for (uint16 i=0;i<blocks_count;i++)
|
||||
delete[] values[i];
|
||||
delete[] values[i];
|
||||
delete[] values;
|
||||
}
|
||||
|
||||
@@ -73,7 +76,6 @@ void Autoarr2<T>::set(uint32 index, T value) {
|
||||
template<typename T>
|
||||
void Autoarr2<T>::add(T value) {
|
||||
if(!values){
|
||||
//values=(T**)malloc(sizeof(T*));
|
||||
values=new T*[1];
|
||||
goto create_block;
|
||||
}
|
||||
@@ -83,9 +85,9 @@ create_block:
|
||||
T** new_values=new T*[blocks_count+1];
|
||||
for(uint32 i=0;i<blocks_count;i++)
|
||||
new_values[i]=values[i];
|
||||
new_values[blocks_count]=new T[max_block_length];
|
||||
delete[] values;
|
||||
values=new_values;
|
||||
values[blocks_count]=new T[max_block_length];
|
||||
blocks_count++;
|
||||
}
|
||||
|
||||
@@ -95,7 +97,8 @@ create_block:
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
uint32 Autoarr2<T>::search(T& value, cmp_func_t cmpf, uint32 fromIndex, uint32 toIndex){
|
||||
template<class cmp_func_lambda>
|
||||
uint32 Autoarr2<T>::search(T& value, cmp_func_lambda cmpf, uint32 fromIndex, uint32 toIndex){
|
||||
uint32 index=fromIndex;
|
||||
for(; index<toIndex; index++)
|
||||
if(cmpf(value,get(index)))
|
||||
@@ -104,11 +107,18 @@ uint32 Autoarr2<T>::search(T& value, cmp_func_t cmpf, uint32 fromIndex, uint32 t
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
uint32 Autoarr2<T>::search(T& value, cmp_func_t cmpf, uint32 fromIndex){
|
||||
template<class cmp_func_lambda>
|
||||
uint32 Autoarr2<T>::search(T& value, cmp_func_lambda cmpf, uint32 fromIndex){
|
||||
return search(value, cmpf, fromIndex, length);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
uint32 Autoarr2<T>::search(T& value, cmp_func_t cmpf){
|
||||
template<class cmp_func_lambda>
|
||||
uint32 Autoarr2<T>::search(T& value, cmp_func_lambda cmpf){
|
||||
return search(value, cmpf, 0, length);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Autoarr2<T>::remove(uint32 index){
|
||||
throw_id(ERR_NOTIMPLEMENTED);
|
||||
}
|
||||
@@ -4,11 +4,10 @@
|
||||
#include "../Autoarr2/Autoarr2.hpp"
|
||||
|
||||
// amount of rows
|
||||
typedef uint16 HT_HEIGHT_T;
|
||||
typedef uint32 HT_HEIGHT_T;
|
||||
typedef uint32 HT_HASH_T;
|
||||
#define HT_HASH(K) hashb_sdbm32(K, sizeof(K))
|
||||
|
||||
#define STORE_HASHES 1
|
||||
#define STORE_HASHES 0
|
||||
|
||||
static const HT_HEIGHT_T HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
||||
#define _HT_HEIN_MIN 0
|
||||
@@ -20,162 +19,187 @@ static const HT_HEIGHT_T HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
class Hashtable2{
|
||||
using cmp_func_t=bool (*)(TKey, TKey);
|
||||
|
||||
// internal types
|
||||
struct KeyValue{
|
||||
TKey key;
|
||||
TVal value;
|
||||
#if STORE_HASHES
|
||||
Autoarr2<HT_HASH_T>** hashes;
|
||||
|
||||
HT_HASH_T hash;
|
||||
#endif
|
||||
cmp_func_t keyComparFunc;
|
||||
Autoarr2<TKey>** keys;
|
||||
Autoarr2<TVal>** values;
|
||||
};
|
||||
using HashKeyFunc_t=HT_HASH_T (*)(TKey);
|
||||
using KeyCmpFunc_t=bool (*)(TKey, TKey);
|
||||
|
||||
// fields
|
||||
Autoarr2<KeyValue>** rows;
|
||||
|
||||
HashKeyFunc_t hashKeyFunc;
|
||||
#if !STORE_HASHES
|
||||
KeyCmpFunc_t keyComparFunc;
|
||||
#endif
|
||||
public:
|
||||
HT_HEIGHT_T height;
|
||||
private:
|
||||
uint8 hein;
|
||||
HT_HEIGHT_T _height;
|
||||
void expand();
|
||||
bool uint32_compare(uint32& a, uint32& b) { return a==b; }
|
||||
|
||||
// constructors
|
||||
#if STORE_HASHES
|
||||
public:
|
||||
#endif
|
||||
Hashtable2();
|
||||
Hashtable2(HashKeyFunc_t _hashKeyFunc);
|
||||
#if !STORE_HASHES
|
||||
public:
|
||||
explicit Hashtable2(cmp_func_t _keyComparFunc);
|
||||
explicit Hashtable2(HashKeyFunc_t _hashKeyFunc, KeyCmpFunc_t _keyComparFunc);
|
||||
#endif
|
||||
|
||||
|
||||
// methods
|
||||
private:
|
||||
void free_rows();
|
||||
TVal* getptr(TKey key, HT_HASH_T& keyHash, HT_HEIGHT_T& rowN);
|
||||
void expand();
|
||||
void add(TKey& key, TVal& value, HT_HASH_T keyHash, HT_HEIGHT_T rowN);
|
||||
|
||||
public:
|
||||
virtual ~Hashtable2();
|
||||
inline HT_HEIGHT_T height(){ return _height; }
|
||||
TVal* getptr(TKey key);
|
||||
TVal get(TKey key);
|
||||
bool addOrSet(TKey key, TVal);
|
||||
bool remove(TKey);
|
||||
bool tryGet(TKey key, TVal* output);
|
||||
void add(TKey key, TVal value);
|
||||
void addOrSet(TKey key, TVal value);
|
||||
void remove(TKey key);
|
||||
};
|
||||
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
Hashtable2<TKey, TVal>::Hashtable2(){
|
||||
Hashtable2<TKey, TVal>::Hashtable2(HashKeyFunc_t _hashKeyFunc){
|
||||
hashKeyFunc=_hashKeyFunc;
|
||||
hein=_HT_HEIN_MIN;
|
||||
_height=HT_HEIGHTS[hein];
|
||||
#if STORE_HASHES
|
||||
hashes=new Autoarr2<HT_HASH_T>*[_height];
|
||||
#endif
|
||||
keys=new Autoarr2<TKey>*[_height];
|
||||
values=new Autoarr2<TVal>*[_height];
|
||||
for(HT_HEIGHT_T i=0; i<_height; i++){
|
||||
#if STORE_HASHES
|
||||
hashes[i]=new Autoarr2<HT_HASH_T>(_HT_ARR_BL);
|
||||
keyComparFunc=NULL;
|
||||
#endif
|
||||
keys[i]=new Autoarr2<TKey>(_HT_ARR_BL);
|
||||
values[i]=new Autoarr2<TVal>(_HT_ARR_BL);
|
||||
}
|
||||
height=HT_HEIGHTS[hein];
|
||||
rows=new Autoarr2<KeyValue>*[height];
|
||||
for(HT_HEIGHT_T i=0; i<height; i++)
|
||||
rows[i]=new Autoarr2<KeyValue>(_HT_ARR_BL);
|
||||
}
|
||||
|
||||
#if !STORE_HASHES
|
||||
template<typename TKey, typename TVal>
|
||||
Hashtable2<TKey, TVal>::Hashtable2(cmp_func_t _keyComparFunc) : Hashtable2() {
|
||||
Hashtable2<TKey, TVal>::Hashtable2(HashKeyFunc_t _hashKeyFunc, KeyCmpFunc_t _keyComparFunc) : Hashtable2(_hashKeyFunc) {
|
||||
keyComparFunc=_keyComparFunc;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
Hashtable2<TKey, TVal>::~Hashtable2(){
|
||||
for(uint32 i=0; i< _height; i++){
|
||||
delete keys[i];
|
||||
delete values[i];
|
||||
#if STORE_HASHES
|
||||
delete hashes[i];
|
||||
#endif
|
||||
}
|
||||
|
||||
delete keys;
|
||||
delete values;
|
||||
#if STORE_HASHES
|
||||
delete hashes;
|
||||
#endif
|
||||
template<typename TKey, typename TVal>
|
||||
void Hashtable2<TKey, TVal>::free_rows(){
|
||||
for(uint32 i=0; i< height; i++)
|
||||
delete rows[i];
|
||||
delete[] rows;
|
||||
}
|
||||
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
void Hashtable2<TKey, TVal>::expand(){
|
||||
if(hein>=_HT_HEIN_MAX)
|
||||
throw_id(ERR_MAXLENGTH);
|
||||
uint32 newHeight=HT_HEIGHTS[++hein];
|
||||
Autoarr2<TKey>** newKeys=new Autoarr2<TKey>*[newHeight];
|
||||
Autoarr2<TVal>** newValues=new Autoarr2<TVal>*[newHeight];
|
||||
#if STORE_HASHES
|
||||
Autoarr2<HT_HASH_T>** newHashes=new Autoarr2<HT_HASH_T>*[newHeight];
|
||||
#endif
|
||||
|
||||
for(HT_HEIGHT_T i=0; i<newHeight; i++){
|
||||
newKeys[i]=new Autoarr2<TKey>(_HT_ARR_BL);
|
||||
newValues[i]=new Autoarr2<TVal>(_HT_ARR_BL);
|
||||
#if STORE_HASHES
|
||||
newHashes[i]=new Autoarr2<HT_HASH_T>(_HT_ARR_BL);
|
||||
#endif
|
||||
}
|
||||
|
||||
for(HT_HEIGHT_T rowN=0; rowN<_height; rowN++){
|
||||
Autoarr2<TKey>* oldKeysRow=keys[rowN];
|
||||
Autoarr2<TVal>* oldValuesRow=values[rowN];
|
||||
#if STORE_HASHES
|
||||
Autoarr2<HT_HASH_T>* oldHashesRow=hashes[rowN];
|
||||
#endif
|
||||
|
||||
for(uint32 k=0; k < oldKeysRow->length; k++){
|
||||
TKey currentKey=oldKeysRow->get(k);
|
||||
#if STORE_HASHES
|
||||
HT_HASH_T currentHash=oldHashesRow->get(k);
|
||||
#else
|
||||
HT_HASH_T currentHash=HT_HASH(currentKey);
|
||||
#endif
|
||||
HT_HEIGHT_T newRowN=currentHash%newHeight;
|
||||
#if STORE_HASHES
|
||||
newHashes[newRowN]->add(currentHash);
|
||||
#endif
|
||||
newKeys[newRowN]->add(currentKey);
|
||||
newValues[newRowN]->add(values[rowN]->get(k));
|
||||
}
|
||||
}
|
||||
|
||||
~Hashtable2();
|
||||
_height=newHeight;
|
||||
keys=newKeys;
|
||||
values=newValues;
|
||||
#if STORE_HASHES
|
||||
hashes=newHashes;
|
||||
#endif
|
||||
}
|
||||
Hashtable2<TKey, TVal>::~Hashtable2() { free_rows(); }
|
||||
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
TVal* Hashtable2<TKey, TVal>::getptr(TKey key){
|
||||
HT_HASH_T keyHash=HT_HASH(key);
|
||||
HT_HEIGHT_T rowN=keyHash%_height;
|
||||
TVal* Hashtable2<TKey, TVal>::getptr(TKey key, HT_HASH_T& keyHash, HT_HEIGHT_T& rowN){
|
||||
keyHash=hashKeyFunc(key);
|
||||
rowN=keyHash%height;
|
||||
KeyValue kv;
|
||||
#if STORE_HASHES
|
||||
uint32 index=hashes[rowN]->search(keyHash, uint32_compare);
|
||||
kv.hash=keyHash;
|
||||
uint32 index=rows[rowN]->search(kv, [](KeyValue kv0, KeyValue kv1) { return kv0.hash==kv1.hash; });
|
||||
#else
|
||||
uint32 index=keys[rowN]->search(key, keyComparFunc);
|
||||
kv.key=key;
|
||||
uint32 index=rows[rowN]->search(kv, [this](KeyValue kv0, KeyValue kv1) { return this->keyComparFunc(kv0.key, kv1.key); });
|
||||
#endif
|
||||
if(index==Autoarr2_NO_REZULT)
|
||||
return NULL;
|
||||
return values[rowN]->getptr(index);
|
||||
return &(rows[rowN]->getptr(index))->value;
|
||||
}
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
TVal* Hashtable2<TKey, TVal>::getptr(TKey key){
|
||||
HT_HASH_T keyHash;
|
||||
HT_HEIGHT_T rowN;
|
||||
return getptr(key, keyHash, rowN);
|
||||
}
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
TVal Hashtable2<TKey, TVal>::get(TKey key){
|
||||
|
||||
TVal* ptr=getptr(key);
|
||||
if(!ptr)
|
||||
throw_id(ERR_KEYNOTFOUND);
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
bool Hashtable2<TKey, TVal>::tryGet(TKey key, TVal* output){
|
||||
TVal* ptr=getptr(key);
|
||||
if(!ptr)
|
||||
return false;
|
||||
*output=*ptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
void Hashtable2<TKey, TVal>::expand(){ printf("expand\n"); fflush(stdout);
|
||||
if(hein>=_HT_HEIN_MAX)
|
||||
throw_id(ERR_MAXLENGTH);
|
||||
|
||||
uint32 newHeight=HT_HEIGHTS[++hein];
|
||||
Autoarr2<KeyValue>** newRows=new Autoarr2<KeyValue>*[newHeight];
|
||||
for(HT_HEIGHT_T i=0; i<newHeight; i++)
|
||||
newRows[i]=new Autoarr2<KeyValue>(_HT_ARR_BL);
|
||||
|
||||
for(HT_HEIGHT_T oldRowN=0; oldRowN<height; oldRowN++)
|
||||
for(uint32 k=0; k < rows[oldRowN]->length; k++){
|
||||
KeyValue kv=rows[oldRowN]->get(k);
|
||||
#if STORE_HASHES
|
||||
HT_HEIGHT_T newRowN=kv.hash%newHeight;
|
||||
#else
|
||||
HT_HEIGHT_T newRowN=hashKeyFunc(kv.key)%newHeight;
|
||||
#endif
|
||||
newRows[newRowN]->add(kv);
|
||||
}
|
||||
|
||||
free_rows();
|
||||
height=newHeight;
|
||||
rows=newRows;
|
||||
}
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
void Hashtable2<TKey, TVal>::add(TKey& key, TVal& value, HT_HASH_T keyHash, HT_HEIGHT_T rowN){ printf("add\n"); fflush(stdout);
|
||||
Autoarr2<KeyValue>* row=rows[rowN];
|
||||
if(row->length == _HT_ARR_BC*_HT_ARR_BL)
|
||||
expand();
|
||||
KeyValue kv;
|
||||
kv.key=key;
|
||||
kv.value=value;
|
||||
#if STORE_HASHES
|
||||
kv.hash=keyHash;
|
||||
#endif
|
||||
row->add(kv);
|
||||
}
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
void Hashtable2<TKey, TVal>::add(TKey key, TVal value){
|
||||
HT_HASH_T keyHash=hashKeyFunc(key);
|
||||
HT_HEIGHT_T rowN=keyHash%height;
|
||||
add(key, value, keyHash, rowN);
|
||||
}
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
void Hashtable2<TKey, TVal>::addOrSet(TKey key, TVal value){
|
||||
HT_HASH_T keyHash;
|
||||
HT_HEIGHT_T rowN;
|
||||
TVal* valptr=getptr(key, keyHash, rowN);
|
||||
if(valptr) *valptr=value;
|
||||
else add(key, value, keyHash, rowN);
|
||||
}
|
||||
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
bool Hashtable2<TKey, TVal>::addOrSet(TKey key, TVal){
|
||||
//expand
|
||||
}
|
||||
|
||||
|
||||
template<typename TKey, typename TVal>
|
||||
bool Hashtable2<TKey, TVal>::remove(TKey){
|
||||
|
||||
void Hashtable2<TKey, TVal>::remove(TKey key){
|
||||
throw_id(ERR_NOTIMPLEMENTED);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ char* errname(ErrorId err){
|
||||
case ERR_NOTIMPLEMENTED: return "ERR_NOTIMPLEMENTED";
|
||||
case ERR_NULLPTR: return "ERR_NULLPTR";
|
||||
case ERR_ENDOFSTR: return "ERR_ENDOFSTR";
|
||||
case ERR_DESYNC: return "ERR_DESYNC";
|
||||
case ERR_KEYNOTFOUND: return "ERR_KEYNOTFOUND";
|
||||
default: return "UNKNOWN_ERROR";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ typedef enum ErrorId {
|
||||
SUCCESS, // not an error
|
||||
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX,
|
||||
ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR,
|
||||
ERR_DESYNC
|
||||
ERR_KEYNOTFOUND
|
||||
} ErrorId;
|
||||
|
||||
char* errname(ErrorId err);
|
||||
@@ -51,10 +51,10 @@ char* __unknownErr( );
|
||||
#if __cplusplus
|
||||
#define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)))
|
||||
#define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)))
|
||||
#else
|
||||
#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
|
||||
#define safethrow(E, FREEMEM) { FREEMEM; __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); }
|
||||
#endif
|
||||
#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
|
||||
#define safethrow(E, FREEMEM) { FREEMEM; __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); }
|
||||
|
||||
|
||||
#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
||||
freeMem;\
|
||||
|
||||
Reference in New Issue
Block a user