diff --git a/src/Autoarr/Autoarr2.hpp b/src/Autoarr/Autoarr2.hpp index e48bea5..6ca15b9 100644 --- a/src/Autoarr/Autoarr2.hpp +++ b/src/Autoarr/Autoarr2.hpp @@ -1,21 +1,24 @@ +// +// +// + #pragma once #include "../base/base.h" - template 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(); }; diff --git a/src/Autoarr/Autoarr2_instances.cpp b/src/Autoarr/Autoarr2_instances.cpp new file mode 100644 index 0000000..44e84b4 --- /dev/null +++ b/src/Autoarr/Autoarr2_instances.cpp @@ -0,0 +1,13 @@ +#include "Autoarr2_methods.hpp" + +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; +template class Autoarr2; diff --git a/src/Autoarr/Autoarr2.cpp b/src/Autoarr/Autoarr2_methods.hpp similarity index 55% rename from src/Autoarr/Autoarr2.cpp rename to src/Autoarr/Autoarr2_methods.hpp index 2402c13..ac7da91 100644 --- a/src/Autoarr/Autoarr2.cpp +++ b/src/Autoarr/Autoarr2_methods.hpp @@ -1,57 +1,57 @@ -// -// -// +#pragma once #include "Autoarr2.hpp" -#define MAX_BLOCK_LENGTH_DEFAULT 64 +#define __AUTOARR2_MAX_BLOCK_LENGTH_DEFAULT 64 template Autoarr2::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 +template Autoarr2::Autoarr2(uint16 _max_block_length) : Autoarr2() { max_block_length=_max_block_length; } template -T *Autoarr2::GetPtr(uint32 index) { - if(index>=Length) throwcpp_id(ERR_WRONGINDEX); +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); +T Autoarr2::get(uint32 index) { + return *getptr(index); } template -void Autoarr2::Set(uint32 index, T value) { - *GetPtr(index)=value; +void Autoarr2::set(uint32 index, T value) { + *getptr(index)=value; } template -void Autoarr2::Add(T value) { +void Autoarr2::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 diff --git a/src/Hashtable/Hashtable2.cpp b/src/Hashtable/Hashtable2.cpp index 46631d4..93486d1 100644 --- a/src/Hashtable/Hashtable2.cpp +++ b/src/Hashtable/Hashtable2.cpp @@ -1,9 +1,5 @@ #include "Hashtable2.hpp" -// -// -// - template Hashtable2::Hashtable2() { diff --git a/src/Hashtable/Hashtable2.hpp b/src/Hashtable/Hashtable2.hpp index a03af78..bfd3eac 100644 --- a/src/Hashtable/Hashtable2.hpp +++ b/src/Hashtable/Hashtable2.hpp @@ -1,3 +1,7 @@ +// +// +// + #pragma once #include "hash.h" diff --git a/src/base/base.h b/src/base/base.h index 2a962b9..f9d56b8 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -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) ({\ diff --git a/src/base/errors.cpp b/src/base/errors.cpp deleted file mode 100644 index 7614c5f..0000000 --- a/src/base/errors.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "errors.hpp" - -void throwcpp_id(ErrorId eid){ - throwcpp_msg(errname(eid)); -} - -void throwcpp_msg(char* emsg){ - __EXIT(emsg); -} \ No newline at end of file diff --git a/src/base/errors.h b/src/base/errors.h index b536038..0b43378 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -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__))); } diff --git a/src/base/errors.hpp b/src/base/errors.hpp deleted file mode 100644 index ad19bd7..0000000 --- a/src/base/errors.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#ifdef __cplusplus -#include "errors.h" - -void throwcpp_id(ErrorId eid); -void throwcpp_msg(char* emsg); -#endif \ No newline at end of file diff --git a/tests/main.c b/tests/main.cpp similarity index 95% rename from tests/main.c rename to tests/main.cpp index 6b3b54b..3ebf36e 100644 --- a/tests/main.c +++ b/tests/main.cpp @@ -7,6 +7,7 @@ void test_all(){ test_searchtree(); test_hashtable(); test_dtsod(); + test_autoarr2(); printf("\e[96m---------------------------------------\e[0m\n"); } diff --git a/tests/test_autoarr.c b/tests/test_autoarr.c index 45f85ba..11b818d 100644 --- a/tests/test_autoarr.c +++ b/tests/test_autoarr.c @@ -20,17 +20,17 @@ static void printautoarr(Autoarr(uint16)* ar){ } static void fillar(Autoarr(uint16)* ar){ - for (uint32 i=0;i* 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_autoarr",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/tests.h b/tests/tests.h index 147ca3f..127f749 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -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(); \ No newline at end of file +void test_safethrow(); +void test_autoarr2(); +#if __cplusplus +} +#endif \ No newline at end of file