Autoarr2 works now

This commit is contained in:
Timerix22 2022-06-02 20:33:13 +03:00
parent cb64df9218
commit 281befa1d7
13 changed files with 115 additions and 56 deletions

View File

@ -1,21 +1,24 @@
//
//
//
#pragma once
#include "../base/base.h"
template <typename T>
class Autoarr2 {
public:
T** values;
uint16 blocks_count;
uint16 block_length;
uint16 max_block_length;
public:
uint32 Length;
uint32 length;
Autoarr2();
explicit Autoarr2(uint16 _max_block_length);
T* GetPtr(uint32 index);
T Get(uint32 index);
void Set(uint32 index, T value);
void Add(T value);
~Autoarr2();
T* getptr(uint32 index);
T get(uint32 index);
void set(uint32 index, T value);
void add(T value);
virtual ~Autoarr2();
};

View File

@ -0,0 +1,13 @@
#include "Autoarr2_methods.hpp"
template class Autoarr2<uint8>;
template class Autoarr2<int8>;
template class Autoarr2<uint16>;
template class Autoarr2<int16>;
template class Autoarr2<uint32>;
template class Autoarr2<int32>;
template class Autoarr2<uint64>;
template class Autoarr2<int64>;
template class Autoarr2<float>;
template class Autoarr2<double>;
template class Autoarr2<Unitype>;

View File

@ -1,57 +1,57 @@
//
//
//
#pragma once
#include "Autoarr2.hpp"
#define MAX_BLOCK_LENGTH_DEFAULT 64
#define __AUTOARR2_MAX_BLOCK_LENGTH_DEFAULT 64
template<typename T>
Autoarr2<T>::Autoarr2() {
values=NULL;
block_length=0;
blocks_count=0;
max_block_length=MAX_BLOCK_LENGTH_DEFAULT;
length=0;
max_block_length=__AUTOARR2_MAX_BLOCK_LENGTH_DEFAULT;
}
template<typename T>
template<typename T>
Autoarr2<T>::Autoarr2(uint16 _max_block_length) : Autoarr2() {
max_block_length=_max_block_length;
}
template<typename T>
T *Autoarr2<T>::GetPtr(uint32 index) {
if(index>=Length) throwcpp_id(ERR_WRONGINDEX);
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);
T Autoarr2<T>::get(uint32 index) {
return *getptr(index);
}
template<typename T>
void Autoarr2<T>::Set(uint32 index, T value) {
*GetPtr(index)=value;
void Autoarr2<T>::set(uint32 index, T value) {
*getptr(index)=value;
}
template<typename T>
void Autoarr2<T>::Add(T value) {
void Autoarr2<T>::add(T value) {
if(!values){
values=malloc(sizeof(T*));
//values=(T**)malloc(sizeof(T*));
values=new[sizeof(T*)];
goto create_block;
}
else if(block_length==max_block_length){
block_length=0;
create_block:
values=realloc(values,(blocks_count+1)*sizeof(T*));
values[blocks_count]=malloc(max_block_length*sizeof(T));
values=(T**)realloc(values,(blocks_count+1)*sizeof(T*));
values[blocks_count]=new[](max_block_length*sizeof(T));
blocks_count++;
}
values[blocks_count-1][block_length]=value;
block_length++;
Length++;
length++;
}
template<typename T>

View File

@ -1,9 +1,5 @@
#include "Hashtable2.hpp"
//
//
//
template<typename TKey, typename TValue>
Hashtable2<TKey, TValue>::Hashtable2() {

View File

@ -1,3 +1,7 @@
//
//
//
#pragma once
#include "hash.h"

View File

@ -9,10 +9,6 @@ extern "C" {
#include "errors.h"
#include "cptr.h"
#if __cplusplus
#include "errors.hpp"
#endif
// executes codeblock and prints execution time
#ifdef CLOCK_REALTIME // non-standard high-precision clock
#define optime(opname,repeats,codeblock) ({\

View File

@ -1,9 +0,0 @@
#include "errors.hpp"
void throwcpp_id(ErrorId eid){
throwcpp_msg(errname(eid));
}
void throwcpp_msg(char* emsg){
__EXIT(emsg);
}

View File

@ -24,7 +24,7 @@ typedef struct Maybe{
// return it if func doesn't return anything
// .value .errmsg
static const Maybe MaybeNull={UniNull, NULL};
#define MaybeNull (Maybe){UniNull, NULL}
void Maybe_free(Maybe e);
void printMaybe(Maybe e);
@ -46,6 +46,10 @@ char* __unknownErr( );
default: __unknownErr\
)(E)
#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__)))
#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__))); }

View File

@ -1,8 +0,0 @@
#pragma once
#ifdef __cplusplus
#include "errors.h"
void throwcpp_id(ErrorId eid);
void throwcpp_msg(char* emsg);
#endif

View File

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

View File

@ -20,17 +20,17 @@ static void printautoarr(Autoarr(uint16)* ar){
}
static void fillar(Autoarr(uint16)* ar){
for (uint32 i=0;i<Autoarr_max_length(ar);i++)
for (uint16 i=0;i<Autoarr_max_length(ar);i++)
Autoarr_add(ar,i);
}
static void resetar(Autoarr(uint16)* ar){
for (uint32 i=0;i<Autoarr_max_length(ar);i++)
for (uint16 i=0;i<Autoarr_max_length(ar);i++)
Autoarr_set(ar,i,Autoarr_max_length(ar)-i-1);
}
static void printallval(Autoarr(uint16)* ar){
printf("\e[90m");
for (uint32 i=0;i<Autoarr_length(ar);i++)
for (uint16 i=0;i<Autoarr_length(ar);i++)
printf("%u ",Autoarr_get(ar,i));
printf("\n");
}
@ -48,6 +48,6 @@ void test_autoarr(){
printf("\e[92mautoarr values reset\n");
printallval(ar);
Autoarr_free(ar);
printf("\e[92mautoarr cleared\n");
printf("\e[92mautoarr deleted\n");
}));
}

52
tests/test_autoarr2.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "tests.h"
#include "../src/Autoarr/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_autoarr",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

@ -2,9 +2,16 @@
#include "../src/base/base.h"
#if __cplusplus
extern "C" {
#endif
void test_searchtree();
void test_autoarr();
void test_hashtable();
void test_string();
void test_dtsod();
void test_safethrow();
void test_safethrow();
void test_autoarr2();
#if __cplusplus
}
#endif