small changes

This commit is contained in:
2022-05-31 19:15:28 +03:00
parent e9b740183d
commit b48405eae2
9 changed files with 30 additions and 24 deletions

View File

@@ -1,10 +1,10 @@
#include "Autoarr2.hpp"
//
//
//
#include "Autoarr2.hpp"
#define MAX_BLOCK_LENGTH_DEFAULT 64
#define ALLOC_N_BLOCKS 1
template<typename T>
Autoarr2<T>::Autoarr2() {
@@ -21,7 +21,7 @@ Autoarr2<T>::Autoarr2(uint16 _max_block_length) : Autoarr2() {
template<typename T>
T *Autoarr2<T>::GetPtr(uint32 index) {
if(index>=Length) throwcpp(ERR_WRONGINDEX);
if(index>=Length) throwcpp_id(ERR_WRONGINDEX);
return values[index/max_block_length]+index%max_block_length;
}
@@ -38,15 +38,15 @@ void Autoarr2<T>::Set(uint32 index, T value) {
template<typename T>
void Autoarr2<T>::Add(T value) {
if(!values){
values=malloc(ALLOC_N_BLOCKS*sizeof(T*));
values=malloc(sizeof(T*));
goto create_block;
}
else if(block_length==max_block_length){
block_length=0;
create_block:
values=realloc((blocks_count+ALLOC_N_BLOCKS)*sizeof(T*));
blocks_count+=ALLOC_N_BLOCKS;
values=realloc(values,(blocks_count+1)*sizeof(T*));
values[blocks_count]=malloc(max_block_length*sizeof(T));
blocks_count++;
}
values[blocks_count-1][block_length]=value;

View File

@@ -2,7 +2,7 @@
#include "errors.h"
#include "cptr.h"
char* errname(err_t err){
char* errname(ErrorId err){
switch(err){
case SUCCESS: return "SUCCESS";
case ERR_MAXLENGTH: return "ERR_MAXLENGTH";

View File

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

View File

@@ -7,12 +7,12 @@ extern "C" {
#include "std.h"
#include "types.h"
typedef enum err_t {
typedef enum ErrorId {
SUCCESS, // not an error
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
} err_t;
} ErrorId;
char* errname(err_t err);
char* errname(ErrorId err);
char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
@@ -21,8 +21,10 @@ typedef struct Maybe{
Unitype value;
char* errmsg;
} Maybe;
// return it if func doesn't return anything
static const Maybe MaybeNull={.value.type=Null, .value.VoidPtr=NULL,.errmsg=NULL};
// .value .errmsg
static const Maybe MaybeNull={UniNull, NULL};
void Maybe_free(Maybe e);
void printMaybe(Maybe e);

View File

@@ -3,6 +3,6 @@
#ifdef __cplusplus
#include "errors.h"
void throwcpp(err_t eid);
void throwcpp(char* emsg);
void throwcpp_id(ErrorId eid);
void throwcpp_msg(char* emsg);
#endif