kerep-headers
This commit is contained in:
43
kerep-headers/Autoarr/Autoarr.h
Normal file
43
kerep-headers/Autoarr/Autoarr.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "Autoarr_declare.h"
|
||||
#include "Autoarr_define.h"
|
||||
|
||||
Autoarr_declare(Pointer)
|
||||
Autoarr_declare(char)
|
||||
Autoarr_declare(bool)
|
||||
Autoarr_declare(f32)
|
||||
Autoarr_declare(f64)
|
||||
Autoarr_declare(i8)
|
||||
Autoarr_declare(u8)
|
||||
Autoarr_declare(i16)
|
||||
Autoarr_declare(u16)
|
||||
Autoarr_declare(i32)
|
||||
Autoarr_declare(u32)
|
||||
Autoarr_declare(i64)
|
||||
Autoarr_declare(u64)
|
||||
|
||||
Autoarr_declare(Unitype)
|
||||
|
||||
#define Autoarr_foreach(ar, elem, codeblock...) { \
|
||||
if(ar->blocks_count>0) { \
|
||||
typeof(**ar->values) elem; \
|
||||
for(u16 blockI=0;blockI<ar->blocks_count-1;blockI++) \
|
||||
for(u32 elemI=0;elemI<ar->max_block_length;elemI++){ \
|
||||
elem=ar->values[blockI][elemI]; \
|
||||
{ codeblock; } \
|
||||
} \
|
||||
for(u16 elemI=0;elemI<ar->block_length;elemI++){ \
|
||||
elem=ar->values[ar->blocks_count-1][elemI]; \
|
||||
{ codeblock; } \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
74
kerep-headers/Autoarr/Autoarr_declare.h
Normal file
74
kerep-headers/Autoarr/Autoarr_declare.h
Normal file
@@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
#define Autoarr_declare(type) \
|
||||
\
|
||||
struct Autoarr_##type; \
|
||||
\
|
||||
typedef struct __Autoarr_##type##_functions_list_t { \
|
||||
void (*add)(struct Autoarr_##type* ar, type element); \
|
||||
type (*get)(struct Autoarr_##type* ar, u32 index); \
|
||||
type* (*getPtr)(struct Autoarr_##type* ar, u32 index); \
|
||||
void (*set)(struct Autoarr_##type* ar, u32 index, type element); \
|
||||
void (*freeWithMembers)(struct Autoarr_##type* ar, bool freePtr); \
|
||||
void (*freeWithoutMembers)(struct Autoarr_##type* ar, bool freePtr); \
|
||||
type* (*toArray)(struct Autoarr_##type* ar); \
|
||||
} __Autoarr_##type##_functions_list_t; \
|
||||
\
|
||||
extern __Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list; \
|
||||
\
|
||||
STRUCT(Autoarr_##type, \
|
||||
u16 blocks_count; \
|
||||
u16 max_blocks_count; \
|
||||
u16 block_length; \
|
||||
u16 max_block_length; \
|
||||
type** values; \
|
||||
__Autoarr_##type##_functions_list_t* functions; \
|
||||
) \
|
||||
\
|
||||
Autoarr_##type* __Autoarr_##type##_create(u16 max_blocks_count, u16 max_block_length); \
|
||||
void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr); \
|
||||
void ____Autoarr_##type##_freeWithMembers(void* ar);
|
||||
|
||||
#define Autoarr(type) Autoarr_##type
|
||||
|
||||
#define Autoarr_create(type, max_blocks_count, max_block_length) \
|
||||
__Autoarr_##type##_create(max_blocks_count, max_block_length)
|
||||
#define Autoarr_add(autoarr, element) \
|
||||
autoarr->functions->add(autoarr, element)
|
||||
#define Autoarr_get(autoarr, index) \
|
||||
autoarr->functions->get(autoarr,index)
|
||||
#define Autoarr_getPtr(autoarr, index) \
|
||||
autoarr->functions->getPtr(autoarr,index)
|
||||
#define Autoarr_set(autoarr, index, element) \
|
||||
autoarr->functions->set(autoarr, index, element)
|
||||
#define Autoarr_free(autoarr, freePtr) \
|
||||
autoarr->functions->freeWithMembers(autoarr, freePtr)
|
||||
#define Autoarr_freeWithoutMembers(autoarr, freePtr) \
|
||||
autoarr->functions->freeWithoutMembers(autoarr, freePtr)
|
||||
#define Autoarr_toArray(autoarr) \
|
||||
autoarr->functions->toArray(autoarr)
|
||||
|
||||
#define Autoarr_length(autoarr) \
|
||||
(u32)(!autoarr->blocks_count ? 0 : \
|
||||
autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length)
|
||||
#define Autoarr_max_length(autoarr) \
|
||||
(u32)(autoarr->max_block_length*autoarr->max_blocks_count)
|
||||
|
||||
#define Autoarr_pop(AR){ \
|
||||
if(AR->block_length==1){ \
|
||||
AR->blocks_count--; \
|
||||
AR->block_length=AR->max_block_length; \
|
||||
free(AR->values[AR->blocks_count]); \
|
||||
} \
|
||||
else AR->block_length--; \
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
100
kerep-headers/Autoarr/Autoarr_define.h
Normal file
100
kerep-headers/Autoarr/Autoarr_define.h
Normal file
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
#define Autoarr_define(type, TYPE_IS_PTR) \
|
||||
\
|
||||
kt_define(Autoarr_##type, ____Autoarr_##type##_freeWithMembers, NULL); \
|
||||
\
|
||||
void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \
|
||||
if(!ar->values){ \
|
||||
ar->values=malloc(ar->max_blocks_count*sizeof(type*)); \
|
||||
goto create_block; \
|
||||
} \
|
||||
if(ar->block_length==ar->max_block_length){ \
|
||||
if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH); \
|
||||
ar->block_length=0; \
|
||||
create_block: \
|
||||
ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type)); \
|
||||
ar->blocks_count++; \
|
||||
} \
|
||||
ar->values[ar->blocks_count-1][ar->block_length]=element; \
|
||||
ar->block_length++; \
|
||||
} \
|
||||
\
|
||||
type __Autoarr_##type##_get(Autoarr_##type* ar, u32 index){ \
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
|
||||
return ar->values[index/ar->max_block_length][index%ar->max_block_length]; \
|
||||
} \
|
||||
\
|
||||
type* __Autoarr_##type##_getPtr(Autoarr_##type* ar, u32 index){ \
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
|
||||
return ar->values[index/ar->max_block_length]+(index%ar->max_block_length); \
|
||||
} \
|
||||
\
|
||||
void __Autoarr_##type##_set(Autoarr_##type* ar, u32 index, type element){ \
|
||||
if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
|
||||
ar->values[index/ar->max_block_length][index%ar->max_block_length]=element; \
|
||||
} \
|
||||
\
|
||||
void __Autoarr_##type##_freeWithoutMembers(Autoarr_##type* ar, bool freePtr){ \
|
||||
for(u16 i=0; i<ar->blocks_count;i++) \
|
||||
free(ar->values[i]); \
|
||||
free(ar->values); \
|
||||
if(freePtr) free(ar); \
|
||||
} \
|
||||
\
|
||||
void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr){ \
|
||||
if(ktDescriptor_##type.freeMembers!=NULL) { \
|
||||
Autoarr_foreach(ar, el, \
|
||||
void* members_ptr=⪙ \
|
||||
if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \
|
||||
ktDescriptor_##type.freeMembers(members_ptr); \
|
||||
); \
|
||||
} \
|
||||
__Autoarr_##type##_freeWithoutMembers(ar, freePtr);\
|
||||
} \
|
||||
void ____Autoarr_##type##_freeWithMembers(void* ar){ \
|
||||
__Autoarr_##type##_freeWithMembers((Autoarr_##type*)ar, false); \
|
||||
} \
|
||||
\
|
||||
type* __Autoarr_##type##_toArray(Autoarr_##type* ar){ \
|
||||
u32 length=Autoarr_length(ar); \
|
||||
if(length==0) \
|
||||
return NULL; \
|
||||
type* array=malloc(length * sizeof(type)); \
|
||||
for(u32 i=0; i<length; i++) \
|
||||
array[i]=__Autoarr_##type##_get(ar, i); \
|
||||
return array; \
|
||||
} \
|
||||
\
|
||||
__Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list={ \
|
||||
&__Autoarr_##type##_add, \
|
||||
&__Autoarr_##type##_get, \
|
||||
&__Autoarr_##type##_getPtr, \
|
||||
&__Autoarr_##type##_set, \
|
||||
&__Autoarr_##type##_freeWithMembers, \
|
||||
&__Autoarr_##type##_freeWithoutMembers, \
|
||||
&__Autoarr_##type##_toArray \
|
||||
}; \
|
||||
\
|
||||
Autoarr_##type* __Autoarr_##type##_create(u16 max_blocks_count, u16 max_block_length){ \
|
||||
Autoarr_##type* ar=malloc(sizeof(Autoarr_##type)); \
|
||||
*ar=(Autoarr_##type){ \
|
||||
.max_blocks_count=max_blocks_count, \
|
||||
.blocks_count=0, \
|
||||
.max_block_length=max_block_length, \
|
||||
.block_length=0, \
|
||||
.values=NULL, \
|
||||
.functions=&__Autoarr_##type##_functions_list \
|
||||
}; \
|
||||
return ar; \
|
||||
}
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
9
kerep-headers/Autoarr/README.md
Normal file
9
kerep-headers/Autoarr/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Autoarr struct
|
||||
`Autoarr` means Automatically resizing array. It is my implementation of dyynamic array. If you want to use `Autoarr` of some type, it should be declared in header file by macro `Autoarr_declare` and defined in source file by `Autoarr_define`.
|
||||
|
||||
Examples:
|
||||
[Hashtable.h](src/Hashtable/Hashtable.h)
|
||||
[Hashtable.c](src/Hashtable/Hashtable.c)
|
||||
|
||||
### Autoarr_*_exported
|
||||
Contains definitions for functions, exported for using in C# kerep wrapper.
|
||||
Reference in New Issue
Block a user