diff --git a/src/Autoarr2/Autoarr2.hpp b/src/Autoarr2/Autoarr2.hpp deleted file mode 100644 index c3f4790..0000000 --- a/src/Autoarr2/Autoarr2.hpp +++ /dev/null @@ -1,122 +0,0 @@ -#pragma once - -#include "../base/base.h" - -#define __AUTOARR2_MAX_BLOCK_LENGTH_DEFAULT 64 -#define Autoarr2_NO_REZULT (uint32)-1 - - -template -class Autoarr2 { - T** values; -public: - uint16 blocks_count; - uint16 block_length; - uint16 max_block_length; - uint32 length; - - Autoarr2(); - explicit Autoarr2(uint16 _max_block_length); - virtual ~Autoarr2(); - - T* getptr(uint32 index); - T get(uint32 index); - void set(uint32 index, T value); - void add(T value); - void remove(uint32 index); - - // returns index of the first inclusion - // using to compare values - template - uint32 search(T& value, cmp_func_lambda cmpf, uint32 fromIndex, uint32 toIndex); - template - uint32 search(T& value, cmp_func_lambda cmpf, uint32 fromIndex); - template - uint32 search(T& value, cmp_func_lambda cmpf); -}; - -template -Autoarr2::Autoarr2(uint16 _max_block_length) { - values = NULL; - block_length = 0; - blocks_count = 0; - length = 0; - max_block_length=_max_block_length; -} - -template -Autoarr2::Autoarr2() : Autoarr2(__AUTOARR2_MAX_BLOCK_LENGTH_DEFAULT) {} - -template -Autoarr2::~Autoarr2() { - for (uint16 i=0;i -T *Autoarr2::getptr(uint32 index) { - if(index>=length) throw_id(ERR_WRONGINDEX); - return values[index/max_block_length]+index%max_block_length; -} - -template -T Autoarr2::get(uint32 index) { - return *getptr(index); -} - -template -void Autoarr2::set(uint32 index, T value) { - *getptr(index)=value; -} - -template -void Autoarr2::add(T value) { - if(!values){ - values=new T*[1]; - goto create_block; - } - else if(block_length==max_block_length){ - block_length=0; -create_block: - T** new_values=new T*[blocks_count+1]; - for(uint32 i=0;i -template -uint32 Autoarr2::search(T& value, cmp_func_lambda cmpf, uint32 fromIndex, uint32 toIndex){ - uint32 index=fromIndex; - for(; index -template -uint32 Autoarr2::search(T& value, cmp_func_lambda cmpf, uint32 fromIndex){ - return search(value, cmpf, fromIndex, length); -} - -template -template -uint32 Autoarr2::search(T& value, cmp_func_lambda cmpf){ - return search(value, cmpf, 0, length); -} - -template -void Autoarr2::remove(uint32 index){ - throw_id(ERR_NOTIMPLEMENTED); -} \ No newline at end of file diff --git a/src/Hashtable2/Hashtable2.hpp b/src/Hashtable2/Hashtable2.hpp deleted file mode 100644 index 63fd667..0000000 --- a/src/Hashtable2/Hashtable2.hpp +++ /dev/null @@ -1,209 +0,0 @@ -#pragma once - -#include "../HashFunctions/hash.h" -#include "../Autoarr2/Autoarr2.hpp" -#include - -// amount of rows -typedef uint32 HT_HEIGHT_T; -typedef uint32 HT_HASH_T; - -#define STORE_HASHES 0 - -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{ -// internal types - struct KeyValue{ - TKey key; - TVal value; -#if STORE_HASHES - HT_HASH_T hash; -#endif - }; - //using HashKeyFunc_t=HT_HASH_T (*)(TKey); - using HashKeyFunc_t=std::function; - using KeyCmpFunc_t=std::function; - -// fields - Autoarr2** rows; - - HashKeyFunc_t hashKeyFunc; -#if !STORE_HASHES - KeyCmpFunc_t keyComparFunc; -#endif -public: - HT_HEIGHT_T height; -private: - uint8 hein; - -// constructors -#if STORE_HASHES -public: -#endif - Hashtable2(HashKeyFunc_t _hashKeyFunc); -#if !STORE_HASHES -public: - 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(); - TVal* getptr(TKey key); - TVal get(TKey key); - bool tryGet(TKey key, TVal* output); - void add(TKey key, TVal value); - void addOrSet(TKey key, TVal value); - void remove(TKey key); -}; - - -template -Hashtable2::Hashtable2(HashKeyFunc_t _hashKeyFunc){ - hashKeyFunc=_hashKeyFunc; - hein=_HT_HEIN_MIN; - height=HT_HEIGHTS[hein]; - rows=new Autoarr2*[height]; - for(HT_HEIGHT_T i=0; i(_HT_ARR_BL); -} - -#if !STORE_HASHES -template -Hashtable2::Hashtable2(HashKeyFunc_t _hashKeyFunc, KeyCmpFunc_t _keyComparFunc) : Hashtable2(_hashKeyFunc) { - keyComparFunc=_keyComparFunc; -} -#endif - - - -template -void Hashtable2::free_rows(){ - for(uint32 i=0; i< height; i++) - delete rows[i]; - delete[] rows; -} - -template -Hashtable2::~Hashtable2() { free_rows(); } - - -template -TVal* Hashtable2::getptr(TKey key, HT_HASH_T& keyHash, HT_HEIGHT_T& rowN){ - keyHash=hashKeyFunc(key); - rowN=keyHash%height; - KeyValue kv; -#if STORE_HASHES - kv.hash=keyHash; - uint32 index=rows[rowN]->search(kv, [](KeyValue kv0, KeyValue kv1) { return kv0.hash==kv1.hash; }); -#else - 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 &(rows[rowN]->getptr(index))->value; -} - -template -TVal* Hashtable2::getptr(TKey key){ - HT_HASH_T keyHash; - HT_HEIGHT_T rowN; - return getptr(key, keyHash, rowN); -} - -template -TVal Hashtable2::get(TKey key){ - TVal* ptr=getptr(key); - if(!ptr) - throw_id(ERR_KEYNOTFOUND); - return *ptr; -} - -template -bool Hashtable2::tryGet(TKey key, TVal* output){ - TVal* ptr=getptr(key); - if(!ptr) - return false; - *output=*ptr; - return true; -} - -template -void Hashtable2::expand(){ - printf("expand\n"); fflush(stdout); - if(hein>=_HT_HEIN_MAX) - throw_id(ERR_MAXLENGTH); - - uint32 newHeight=HT_HEIGHTS[++hein]; - Autoarr2** newRows=new Autoarr2*[newHeight]; - for(HT_HEIGHT_T i=0; i(_HT_ARR_BL); - - for(HT_HEIGHT_T oldRowN=0; oldRowNlength; 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 -void Hashtable2::add(TKey& key, TVal& value, HT_HASH_T keyHash, HT_HEIGHT_T rowN){ - printf("add\n"); fflush(stdout); - Autoarr2* 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 -void Hashtable2::add(TKey key, TVal value){ - HT_HASH_T keyHash=hashKeyFunc(key); - HT_HEIGHT_T rowN=keyHash%height; - add(key, value, keyHash, rowN); -} - -template -void Hashtable2::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 -void Hashtable2::remove(TKey key){ - throw_id(ERR_NOTIMPLEMENTED); -} diff --git a/tests/main.cpp b/tests/main.cpp index b480be2..10f54f3 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -8,8 +8,6 @@ void test_all(){ test_hash_functions(); test_hashtable(); test_dtsod(); - test_autoarr2(); - // test_hashtable2(); printf("\e[96m--------------------------------------\e[0m\n"); } diff --git a/tests/test_autoarr2.cpp b/tests/test_autoarr2.cpp deleted file mode 100644 index 66af88e..0000000 --- a/tests/test_autoarr2.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "tests.h" -#include "../src/Autoarr2/Autoarr2.hpp" - - -#define maxlength 160 - -void printautoarr2(Autoarr2* ar){ - printf("\e[94mAutoarr2: " - IFWIN("%llu", "%lu") - "\n blocks_count: %u\n" - " max_block_length: %u\n" - " block_length: %u\n" - " length: %u\n", - sizeof(Autoarr2), - ar->blocks_count, - ar->max_block_length, - ar->block_length, - ar->length); -} - -void fillar2(Autoarr2* ar){ - for (uint16 i=0;iadd(i); -} -void resetar2(Autoarr2* ar){ - for (uint16 i=0;iset(i,maxlength-i-1); -} - -void printallval2(Autoarr2* ar){ - printf("\e[90m"); - for (uint16 i=0;ilength;i++) - printf("%u ",ar->get(i)); - printf("\n"); -} - -void test_autoarr2(){ - optime("test_autoarr2",1,({ - printf("\e[96m------------[test_autoarr2]-----------\n"); - Autoarr2* ar=new Autoarr2(16); - printf("\e[92mautoarr2 created\n"); - fillar2(ar); - printf("\e[92mautoarr2 filled up\n"); - printautoarr2(ar); - printallval2(ar); - resetar2(ar); - printf("\e[92mautoarr2 values reset\n"); - printallval2(ar); - delete ar; - printf("\e[92mautoarr2 deleted\n"); - })); -} diff --git a/tests/test_hashtable2.cpp b/tests/test_hashtable2.cpp deleted file mode 100644 index bf306b1..0000000 --- a/tests/test_hashtable2.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "tests.h" -#include "../src/Hashtable2/Hashtable2.hpp" -#include - -#define TKey std::shared_ptr -#define TVal uint64 -#define HT_TYPE Hashtable2 -#define HT_TYPE_NAME "Hashtable2, uint64>" - -void print_hashtable(HT_TYPE* ht){ - printf("\e[94m" HT_TYPE_NAME ": " - IFWIN("%llu", "%lu") - "\n height: %u\n", - sizeof(HT_TYPE), - ht->height); -} - -std::shared_ptr genkey(uint32 i){ - char* key=new char[24]; - IFMSC( - sprintf_s(key,24,"key_%u",i), - sprintf(key,"key_%u",i) - ); - dbg(i); - return std::shared_ptr(key, [](char* s){ delete[] s;}); -} - -void fill(HT_TYPE* ht){ - for(uint32 i=0;i<260;i++) - ht->add(genkey(i), 555666); - print_hashtable(ht); -} - -TVal gett(HT_TYPE* ht){ - TVal u; - for(uint32 i=0;i<1000;i++){ - TKey key=genkey(i); - u=ht->get(key); - } - return u; -} - - -void test_hashtable2(){ - //optime("test_hashtable2",1,({ - printf("\e[96m-----------[test_hashtable2]-----------\n"); -#if STORE_HASHES - HT_TYPE* ht=new HT_TYPE( - [](TKey k) { return hashs(hash_sdbm32,k.get()); }); -#else - HT_TYPE* ht=new HT_TYPE( - [](TKey k) -> HT_HASH_T { return hashs(hash_sdbm32,k.get()); }, - [](TKey k0, TKey k1) -> bool { return cptr_compare(k0.get(), k1.get()); }); -#endif - printf("\e[92mhashtable created\n"); - print_hashtable(ht); - optime("fill",1,fill(ht)); - // TVal r; - // optime("get",1,r=gett(ht)); - // dbg((uint32)r); - // print_hashtable(ht); - delete ht; - printf("\e[92mhashtable deleted\n"); - //})); -} diff --git a/tests/tests.h b/tests/tests.h index fb0be11..d19ea17 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -10,10 +10,8 @@ void test_string(); void test_safethrow(); void test_searchtree(); void test_autoarr(); -void test_autoarr2(); void test_hash_functions(); void test_hashtable(); -void test_hashtable2(); void test_dtsod(); #if __cplusplus