From 2e3f16905bbd2ccbe0a940cc30374f7f5e37f096 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 10 Jun 2022 18:47:36 +0300 Subject: [PATCH] working on Hashtable2 --- src/Hashtable2/Hashtable2.hpp | 134 ++++++++++++++++++++++++++-------- src/base/errors.c | 1 + src/base/errors.h | 4 +- 3 files changed, 108 insertions(+), 31 deletions(-) diff --git a/src/Hashtable2/Hashtable2.hpp b/src/Hashtable2/Hashtable2.hpp index cf84e91..e734f0a 100644 --- a/src/Hashtable2/Hashtable2.hpp +++ b/src/Hashtable2/Hashtable2.hpp @@ -3,58 +3,132 @@ #include "../Hashtable/hash.h" #include "../Autoarr2/Autoarr2.hpp" +// amount of rows +typedef uint32 HT_HEIGHT_T; +typedef uint32 HT_HASH_T; +#define HT_HASH(K) hashb_sdbm32(K, sizeof(K)) + +static const HT_HEIGHT_T HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; +#define _HT_HEIN_MIN 0 +#define _HT_HEIN_MAX 6 + +#define _HT_ARR_BC 2 +#define _HT_ARR_BL 8 + + template class Hashtable2{ - uint8 hein; -public: + Autoarr2* hashes; Autoarr2* keys; Autoarr2* values; + uint8 hein; + void expand(); + +public: Hashtable2(); + virtual ~Hashtable2(); + + HT_HEIGHT_T height(){ return HT_HEIGHTS[hein]; } + TVal* getptr(TKey key); TVal get(TKey key); - TVal getptr(TKey key); bool addOrSet(TKey key, TVal); bool remove(TKey); - virtual ~Hashtable2(); }; -// amount of rows -static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; -#define HT_HEIN_MIN 0 -#define HT_HEIN_MAX 6 - -#define ARR_BC 2 -#define ARR_BL 8 - template -Hashtable2::Hashtable2() { - hein=HT_HEIN_MIN; - keys=new Autoarr2; - values=new Autoarr2; +Hashtable2::Hashtable2(){ + hein=_HT_HEIN_MIN; + HT_HEIGHT_T h=height(); + hashes=new Autoarr2[h]; + keys=new Autoarr2[h]; + values=new Autoarr2[h]; + for(HT_HEIGHT_T i=0; i(_HT_ARR_BL); + keys[i]=Autoarr2(_HT_ARR_BL); + values[i]=Autoarr2(_HT_ARR_BL); + } } template -TVal Hashtable2::get(TKey key) { +void Hashtable2::expand(){ + if(hein>=_HT_HEIN_MAX) + throw_id(ERR_MAXLENGTH); + if(hashes->length!=keys->length || hashes->length!=values->length) + throw_id(ERR_DESYNC); + + uint32 oldHeight=height(); + hein++; + uint32 newHeight=height(); + Autoarr2* newHashes=new Autoarr2[newHeight]; + Autoarr2* newKeys=new Autoarr2[newHeight]; + Autoarr2* newValues=new Autoarr2[newHeight]; + for(HT_HEIGHT_T i=0; i(_HT_ARR_BL); + newKeys[i]=Autoarr2(_HT_ARR_BL); + newValues[i]=Autoarr2(_HT_ARR_BL); + } + + for(HT_HEIGHT_T rowN=0; rowN* oldHashesRow= hashes+rowN; + Autoarr2* oldKeysRow = keys +rowN; + Autoarr2* oldValuesRow= values+rowN; + + for(uint32 k=0; k < oldHashesRow->length; k++){ + HT_HASH_T currentHash=oldHashesRow->get(k); + HT_HEIGHT_T newRowN=currentHash%oldHeight; + + Autoarr2* newHashesRow= newHashes+newRowN; + Autoarr2* newKeysRow = newKeys +newRowN; + Autoarr2* newValuesRow= newValues+newRowN; + + newHashesRow->add(currentHash); + newKeysRow->add(oldKeysRow->get(k)); + newValuesRow->add(oldValuesRow->get(k)); + } + //delete oldHashesRow; + //delete oldKeysRow; + //delete oldValuesRow; + } + + delete[] hashes; + delete[] keys; + delete[] values; + hashes=newHashes; + keys=newKeys; + values=newValues; +} + +template +TVal* Hashtable2::getptr(TKey key){ + HT_HASH_T keyHash=HT_HASH(key); + HT_HEIGHT_T rowHeight=keyHash%height(); + Autoarr2* hashesRow=hashes+rowHeight; + uint32 index=hashesRow->search(keyHash); + if(index==Autoarr2_NO_REZULT) + return NULL; + Autoarr2* valuesRow=values+rowHeight; + return valuesRow->getptr(index); +} + +template +TVal Hashtable2::get(TKey key){ } template -TVal Hashtable2::getptr(TKey key) { +bool Hashtable2::addOrSet(TKey key, TVal){ + //expand +} + +template +bool Hashtable2::remove(TKey){ } template -bool Hashtable2::addOrSet(TKey key, TVal) { - -} - -template -bool Hashtable2::remove(TKey) { - -} - -template -Hashtable2::~Hashtable2() { - +Hashtable2::~Hashtable2(){ + delete[] keys; + delete[] values; } diff --git a/src/base/errors.c b/src/base/errors.c index a2a2864..1df7552 100644 --- a/src/base/errors.c +++ b/src/base/errors.c @@ -11,6 +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"; default: return "UNKNOWN_ERROR"; } } diff --git a/src/base/errors.h b/src/base/errors.h index 0b43378..c4c59b5 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -9,7 +9,9 @@ extern "C" { typedef enum ErrorId { SUCCESS, // not an error - ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR + ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, + ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR, + ERR_DESYNC } ErrorId; char* errname(ErrorId err);