diff --git a/src/Array/Array.h b/src/Array/Array.h new file mode 100644 index 0000000..ac9dabc --- /dev/null +++ b/src/Array/Array.h @@ -0,0 +1,40 @@ +#include "Array_declare.h" + +Array_declare(char) +Array_declare(bool) +Array_declare(float32) +Array_declare(float64) +Array_declare(int8) +Array_declare(uint8) +Array_declare(int16) +Array_declare(uint16) +Array_declare(int32) +Array_declare(uint32) +Array_declare(int64) +Array_declare(uint64) + +ktId_declare(ArrayChar); +ktId_declare(ArrayBool); +ktId_declare(ArrayFloat32); +ktId_declare(ArrayFloat64); +ktId_declare(ArrayInt8); +ktId_declare(ArrayUInt8); +ktId_declare(ArrayInt16); +ktId_declare(ArrayUInt16); +ktId_declare(ArrayInt32); +ktId_declare(ArrayUInt32); +ktId_declare(ArrayInt64); +ktId_declare(ArrayUInt64); + +ktId_declare(ArrayCharPtr); +ktId_declare(ArrayBoolPtr); +ktId_declare(ArrayFloat32Ptr); +ktId_declare(ArrayFloat64Ptr); +ktId_declare(ArrayInt8Ptr); +ktId_declare(ArrayUInt8Ptr); +ktId_declare(ArrayInt16Ptr); +ktId_declare(ArrayUInt16Ptr); +ktId_declare(ArrayInt32Ptr); +ktId_declare(ArrayUInt32Ptr); +ktId_declare(ArrayInt64Ptr); +ktId_declare(ArrayUInt64Ptr); diff --git a/src/Array/Array_declare.h b/src/Array/Array_declare.h new file mode 100644 index 0000000..4f50f53 --- /dev/null +++ b/src/Array/Array_declare.h @@ -0,0 +1,40 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/base.h" + +#define Array_declare(type)\ +typedef struct Array_##type{\ + type* values;\ + uint32 length;\ + bool allocatedOnHeap;\ +} Array_##type;\ +\ +\ +static inline Array_##type Array_##type##_allocValues(uint32 length){\ + return (Array_##type) {\ + .values=malloc(sizeof(type)*length),\ + .length=length,\ + .allocatedOnHeap=true\ + };\ +}\ +\ +static inline Array_##type Array_##type##_fromBuffer(buffer, bufferLength, allocatedOnHeap){\ + return (Array_##type) {\ + .values=buffer,\ + .length=bufferLength,\ + .allocatedOnHeap=allocatedOnHeap\ + };\ +}\ +\ +static inline void Array_##type##_freeValues(Array_##type* array){\ + if(array->allocatedOnHeap)\ + free(array->values);\ +} + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Array/README.md b/src/Array/README.md new file mode 100644 index 0000000..931ff09 --- /dev/null +++ b/src/Array/README.md @@ -0,0 +1,2 @@ +# Array struct +This struct stores array pointer and length. If you want to use `Array` of some type, it should be declared in header file by macro `Array_declare`. It uses `static inline` functions, so all definitions will be generated in header. diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 91f592e..729da9f 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -1,4 +1,5 @@ #include "../base.h" +#include "../../Array/Array.h" #include "../../Autoarr/Autoarr.h" #include "../../SearchTree/SearchTree.h" #include "../../Hashtable/Hashtable.h" @@ -39,6 +40,34 @@ void ktDescriptors_initKerepTypes(){ kt_register(ktDescriptor, ktId_ktDescriptor, NULL, NULL); kt_register(ktDescriptor*, ktId_ktDescriptorPtr, NULL, NULL); + + // base type arrays + kt_register(Array_char, ktId_ArrayChar, Array_char_freeValues, NULL); + kt_register(Array_bool, ktId_ArrayBool, Array_bool_freeValues, NULL); + kt_register(Array_float32, ktId_ArrayFloat32, Array_float32_freeValues, NULL); + kt_register(Array_float64, ktId_ArrayFloat64, Array_float64_freeValues, NULL); + kt_register(Array_int8, ktId_ArrayInt8, Array_int8_freeValues, NULL); + kt_register(Array_uint8, ktId_ArrayUInt8, Array_uint8_freeValues, NULL); + kt_register(Array_int16, ktId_ArrayInt16, Array_int16_freeValues, NULL); + kt_register(Array_uint16, ktId_ArrayUInt16, Array_uint16_freeValues, NULL); + kt_register(Array_int32, ktId_ArrayInt32, Array_int32_freeValues, NULL); + kt_register(Array_uint32, ktId_ArrayUInt32, Array_uint32_freeValues, NULL); + kt_register(Array_int64, ktId_ArrayInt64, Array_int64_freeValues, NULL); + kt_register(Array_uint64, ktId_ArrayUInt64, Array_uint64_freeValues, NULL); + // base type array pointers + kt_register(Array_char*, ktId_ArrayCharPtr, Array_char_freeValues, NULL); + kt_register(Array_bool*, ktId_ArrayBoolPtr, Array_bool_freeValues, NULL); + kt_register(Array_float32*, ktId_ArrayFloat32Ptr, Array_float32_freeValues, NULL); + kt_register(Array_float64*, ktId_ArrayFloat64Ptr, Array_float64_freeValues, NULL); + kt_register(Array_int8*, ktId_ArrayInt8Ptr, Array_int8_freeValues, NULL); + kt_register(Array_uint8*, ktId_ArrayUInt8Ptr, Array_uint8_freeValues, NULL); + kt_register(Array_int16*, ktId_ArrayInt16Ptr, Array_int16_freeValues, NULL); + kt_register(Array_uint16*, ktId_ArrayUInt16Ptr, Array_uint16_freeValues, NULL); + kt_register(Array_int32*, ktId_ArrayInt32Ptr, Array_int32_freeValues, NULL); + kt_register(Array_uint32*, ktId_ArrayUInt32Ptr, Array_uint32_freeValues, NULL); + kt_register(Array_int64*, ktId_ArrayInt64Ptr, Array_int64_freeValues, NULL); + kt_register(Array_uint64*, ktId_ArrayUInt64Ptr, Array_uint64_freeValues, NULL); + // base type autoarrs kt_register(Autoarr_char, ktId_AutoarrChar, ____Autoarr_free_char, NULL); kt_register(Autoarr_bool, ktId_AutoarrBool, ____Autoarr_free_bool, NULL);