removed incomplete c++ code

This commit is contained in:
Timerix22 2022-07-21 03:09:44 +03:00
parent 8dd6e49269
commit b6e47a310b
6 changed files with 0 additions and 452 deletions

View File

@ -1,122 +0,0 @@
#pragma once
#include "../base/base.h"
#define __AUTOARR2_MAX_BLOCK_LENGTH_DEFAULT 64
#define Autoarr2_NO_REZULT (uint32)-1
template<typename T>
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 <value> inclusion
// using <cmpf> to compare values
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);
};
template<typename T>
Autoarr2<T>::Autoarr2(uint16 _max_block_length) {
values = NULL;
block_length = 0;
blocks_count = 0;
length = 0;
max_block_length=_max_block_length;
}
template<typename T>
Autoarr2<T>::Autoarr2() : Autoarr2(__AUTOARR2_MAX_BLOCK_LENGTH_DEFAULT) {}
template<typename T>
Autoarr2<T>::~Autoarr2() {
for (uint16 i=0;i<blocks_count;i++)
delete[] values[i];
delete[] values;
}
template<typename T>
T *Autoarr2<T>::getptr(uint32 index) {
if(index>=length) throw_id(ERR_WRONGINDEX);
return values[index/max_block_length]+index%max_block_length;
}
template<typename T>
T Autoarr2<T>::get(uint32 index) {
return *getptr(index);
}
template<typename T>
void Autoarr2<T>::set(uint32 index, T value) {
*getptr(index)=value;
}
template<typename T>
void Autoarr2<T>::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<blocks_count;i++)
new_values[i]=values[i];
new_values[blocks_count]=new T[max_block_length];
delete[] values;
values=new_values;
blocks_count++;
}
values[blocks_count-1][block_length]=value;
block_length++;
length++;
}
template<typename T>
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)))
return index;
return Autoarr2_NO_REZULT;
}
template<typename T>
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>
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);
}

View File

@ -1,209 +0,0 @@
#pragma once
#include "../HashFunctions/hash.h"
#include "../Autoarr2/Autoarr2.hpp"
#include <functional>
// 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<typename TKey, typename TVal>
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<HT_HASH_T(TKey)>;
using KeyCmpFunc_t=std::function<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;
// 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<typename TKey, typename TVal>
Hashtable2<TKey, TVal>::Hashtable2(HashKeyFunc_t _hashKeyFunc){
hashKeyFunc=_hashKeyFunc;
hein=_HT_HEIN_MIN;
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(HashKeyFunc_t _hashKeyFunc, KeyCmpFunc_t _keyComparFunc) : Hashtable2(_hashKeyFunc) {
keyComparFunc=_keyComparFunc;
}
#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>
Hashtable2<TKey, TVal>::~Hashtable2() { free_rows(); }
template<typename TKey, typename TVal>
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
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<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>
void Hashtable2<TKey, TVal>::remove(TKey key){
throw_id(ERR_NOTIMPLEMENTED);
}

View File

@ -8,8 +8,6 @@ void test_all(){
test_hash_functions(); test_hash_functions();
test_hashtable(); test_hashtable();
test_dtsod(); test_dtsod();
test_autoarr2();
// test_hashtable2();
printf("\e[96m--------------------------------------\e[0m\n"); printf("\e[96m--------------------------------------\e[0m\n");
} }

View File

@ -1,52 +0,0 @@
#include "tests.h"
#include "../src/Autoarr2/Autoarr2.hpp"
#define maxlength 160
void printautoarr2(Autoarr2<uint16>* ar){
printf("\e[94mAutoarr2<uint16>: "
IFWIN("%llu", "%lu")
"\n blocks_count: %u\n"
" max_block_length: %u\n"
" block_length: %u\n"
" length: %u\n",
sizeof(Autoarr2<uint16>),
ar->blocks_count,
ar->max_block_length,
ar->block_length,
ar->length);
}
void fillar2(Autoarr2<uint16>* ar){
for (uint16 i=0;i<maxlength;i++)
ar->add(i);
}
void resetar2(Autoarr2<uint16>* ar){
for (uint16 i=0;i<maxlength;i++)
ar->set(i,maxlength-i-1);
}
void printallval2(Autoarr2<uint16>* ar){
printf("\e[90m");
for (uint16 i=0;i<ar->length;i++)
printf("%u ",ar->get(i));
printf("\n");
}
void test_autoarr2(){
optime("test_autoarr2",1,({
printf("\e[96m------------[test_autoarr2]-----------\n");
Autoarr2<uint16>* ar=new Autoarr2<uint16>(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");
}));
}

View File

@ -1,65 +0,0 @@
#include "tests.h"
#include "../src/Hashtable2/Hashtable2.hpp"
#include <memory>
#define TKey std::shared_ptr<char>
#define TVal uint64
#define HT_TYPE Hashtable2<TKey, TVal>
#define HT_TYPE_NAME "Hashtable2<std::shared_ptr<char*>, 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<char> 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<char>(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");
//}));
}

View File

@ -10,10 +10,8 @@ void test_string();
void test_safethrow(); void test_safethrow();
void test_searchtree(); void test_searchtree();
void test_autoarr(); void test_autoarr();
void test_autoarr2();
void test_hash_functions(); void test_hash_functions();
void test_hashtable(); void test_hashtable();
void test_hashtable2();
void test_dtsod(); void test_dtsod();
#if __cplusplus #if __cplusplus