#pragma once #include "../project/gen.hpp" using namespace gen; // Used to know what slot the array will be for generic selection global s32 Array_DefinitionCounter = 0; CodeBody gen_array_base() { CodeTypedef td_header = parse_typedef( code( typedef struct ArrayHeader ArrayHeader; )); CodeStruct header = parse_struct( code( struct ArrayHeader { AllocatorInfo Allocator; usize Capacity; usize Num; }; )); Code grow_formula = untyped_str( txt( "#define array_grow_formula( value ) ( 2 * value + 8 )\n" )); Code get_header = untyped_str( txt( "#define array_get_header( self ) ( (ArrayHeader*)( self ) - 1)\n" )); Code type_define = untyped_str( txt( "#define Array(Type) Array_##Type\n")); return def_global_body( args( fmt_newline, td_header, header, type_define, grow_formula, get_header, fmt_newline ) ); }; CodeBody gen_array( StrC type, StrC array_name ) { String array_type = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr ); String fn = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr ); // str_to_lower(fn.Data); #pragma push_macro( "GEN_ASSERT" ) #pragma push_macro( "rcast" ) #pragma push_macro( "cast" ) #pragma push_macro( "typeof" ) #pragma push_macro( "forceinline" ) #undef GEN_ASSERT #undef rcast #undef cast #undef typeof #undef forceinline CodeBody result = parse_global_body( token_fmt( "array_type", (StrC)array_type, "fn", (StrC)fn, "type", (StrC)type , stringize( typedef * ; _init ( AllocatorInfo allocator ); _init_reserve ( AllocatorInfo allocator, usize capacity ); bool _append_array ( * self, other ); bool _append ( * self, value ); bool _append_items ( * self, * items, usize item_num ); bool _append_at ( * self, item, usize idx ); bool _append_items_at( * self, * items, usize item_num, usize idx ); * _back ( self ); void _clear ( self ); bool _fill ( self, usize begin, usize end, value ); void _free ( * self ); bool _grow ( * self, usize min_capacity ); usize _num ( self ); _pop ( self ); void _remove_at ( self, usize idx ); bool _reserve ( * self, usize new_capacity ); bool _resize ( * self, usize num ); bool _set_capacity ( * self, usize new_capacity ); forceinline _init( AllocatorInfo allocator ) { size_t initial_size = array_grow_formula(0); return array_init_reserve( , allocator, initial_size ); } inline _init_reserve( AllocatorInfo allocator, usize capacity ) { GEN_ASSERT(capacity > 0); ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof() * capacity)); if (header == nullptr) return nullptr; header->Allocator = allocator; header->Capacity = capacity; header->Num = 0; return rcast(*, header + 1); } forceinline bool _append_array( * self, other ) { return array_append_items( * self, ()other, _num(other)); } inline bool _append( * self, value ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); ArrayHeader* header = array_get_header( * self ); if ( header->Num == header->Capacity ) { if ( ! array_grow( self, header->Capacity)) return false; header = array_get_header( * self ); } (* self)[ header->Num ] = value; header->Num++; return true; } inline bool _append_items( * self, * items, usize item_num ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); GEN_ASSERT(items != nullptr); GEN_ASSERT(item_num > 0); ArrayHeader* header = array_get_header( * self ); if ( header->Num + item_num > header->Capacity ) { if ( ! array_grow( self, header->Capacity + item_num )) return false; header = array_get_header( * self ); } mem_copy( (* self) + header->Num, items, sizeof() * item_num ); header->Num += item_num; return true; } inline bool _append_at( * self, item, usize idx ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); ArrayHeader* header = array_get_header( * self ); if ( idx >= header->Num ) idx = header->Num - 1; if ( idx < 0 ) idx = 0; if ( header->Capacity < header->Num + 1 ) { if ( ! array_grow( self, header->Capacity + 1 ) ) return false; header = array_get_header( * self ); } target = (* self) + idx; mem_move( target + 1, target, (header->Num - idx) * sizeof() ); header->Num++; return true; } inline bool _append_items_at( * self, * items, usize item_num, usize idx ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); ArrayHeader* header = array_get_header( * self ); if ( idx >= header->Num ) { return array_append_items( * self, items, item_num ); } if ( item_num > header->Capacity ) { if ( ! array_grow( self, item_num + header->Capacity ) ) return false; header = array_get_header( * self ); } * target = (* self) + idx + item_num; * src = (* self) + idx; mem_move( target, src, (header->Num - idx) * sizeof() ); mem_copy( src, items, item_num * sizeof() ); header->Num += item_num; return true; } inline * _back( self ) { GEN_ASSERT(self != nullptr); ArrayHeader* header = array_get_header( self ); if ( header->Num == 0 ) return NULL; return self + header->Num - 1; } inline void _clear( self ) { GEN_ASSERT(self != nullptr); ArrayHeader* header = array_get_header( self ); header->Num = 0; } inline bool _fill( self, usize begin, usize end, value ) { GEN_ASSERT(self != nullptr); GEN_ASSERT(begin <= end); ArrayHeader* header = array_get_header( self ); if ( begin < 0 || end >= header->Num ) return false; for ( ssize idx = begin; idx < end; idx ++ ) self[ idx ] = value; return true; } inline void _free( * self ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); ArrayHeader* header = array_get_header( * self ); allocator_free( header->Allocator, header ); self = NULL; } inline bool _grow( * self, usize min_capacity ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); GEN_ASSERT( min_capacity > 0 ); ArrayHeader* header = array_get_header( *self ); usize new_capacity = array_grow_formula( header->Capacity ); if ( new_capacity < min_capacity ) new_capacity = min_capacity; return array_set_capacity( self, new_capacity ); } forceinline usize _num( self ) { GEN_ASSERT( self != nullptr); return array_get_header(self)->Num; } inline _pop( self ) { GEN_ASSERT( self != nullptr); ArrayHeader* header = array_get_header( self ); GEN_ASSERT( header->Num > 0 ); result = self[ header->Num - 1 ]; header->Num--; return result; } forceinline void _remove_at( self, usize idx ) { GEN_ASSERT( self != nullptr); ArrayHeader* header = array_get_header( self ); GEN_ASSERT( idx < header->Num ); mem_move( self + idx, self + idx + 1, sizeof( ) * ( header->Num - idx - 1 ) ); header->Num--; } inline bool _reserve( * self, usize new_capacity ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); GEN_ASSERT(new_capacity > 0); ArrayHeader* header = array_get_header( * self ); if ( header->Capacity < new_capacity ) return array_set_capacity( self, new_capacity ); return true; } inline bool _resize( * self, usize num ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); GEN_ASSERT(num > 0); ArrayHeader* header = array_get_header( * self ); if ( header->Capacity < num ) { if ( ! array_grow( self, num ) ) return false; header = array_get_header( * self ); } header->Num = num; return true; } inline bool _set_capacity( * self, usize new_capacity ) { GEN_ASSERT( self != nullptr); GEN_ASSERT(* self != nullptr); GEN_ASSERT( new_capacity > 0 ); ArrayHeader* header = array_get_header( * self ); if ( new_capacity == header->Capacity ) return true; if ( new_capacity < header->Num ) { header->Num = new_capacity; return true; } usize size = sizeof( ArrayHeader ) + sizeof( ) * new_capacity; ArrayHeader* new_header = cast( ArrayHeader*, alloc( header->Allocator, size )); if ( new_header == NULL ) return false; mem_move( new_header, header, sizeof( ArrayHeader ) + sizeof( ) * header->Num ); new_header->Capacity = new_capacity; allocator_free( header->Allocator, & header ); * self = cast( *, new_header + 1 ); return true; } ))); #pragma pop_macro( "GEN_ASSERT" ) #pragma pop_macro( "rcast" ) #pragma pop_macro( "cast" ) #pragma pop_macro( "typeof" ) #pragma pop_macro( "forceinline" ) ++ Array_DefinitionCounter; StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", Array_DefinitionCounter).to_strc(); Code generic_interface_slot = untyped_str(token_fmt( "type", type, "array_type", (StrC)array_type, "slot", (StrC)slot_str, R"(#define GENERIC_SLOT___array_init , _init #define GENERIC_SLOT___array_init_reserve , _init_reserve #define GENERIC_SLOT___array_append , _append #define GENERIC_SLOT___array_append_items , _append_items #define GENERIC_SLOT___array_append_at , _append_at #define GENERIC_SLOT___array_append_items_at , _append_items_at #define GENERIC_SLOT___array_back , _back #define GENERIC_SLOT___array_clear , _clear #define GENERIC_SLOT___array_fill , _fill #define GENERIC_SLOT___array_free , _free #define GENERIC_SLOT___array_grow *, _grow #define GENERIC_SLOT___array_num , _num #define GENERIC_SLOT___array_pop , _pop #define GENERIC_SLOT___array_remove_at , _remove_at #define GENERIC_SLOT___array_reserve , _reserve #define GENERIC_SLOT___array_resize , _resize #define GENERIC_SLOT___array_set_capacity *, _set_capacity )" )); return def_global_body( args( def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "region %S", array_type ))), fmt_newline, generic_interface_slot, fmt_newline, result, fmt_newline, def_pragma( string_to_strc(string_fmt_buf( GlobalAllocator, "endregion %S", array_type ))), fmt_newline )); }; CodeBody gen_array_generic_selection_interface() { CodeBody interface_defines = def_body(CT_Global_Body); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_init"), GenericSel_Direct_Type )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_init_reserve"), GenericSel_Direct_Type )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_append"), GenericSel_By_Ref )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_append_at"), GenericSel_By_Ref )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_append_items"), GenericSel_By_Ref )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_append_items_at"), GenericSel_By_Ref )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_back"), GenericSel_Default, GenericSel_One_Arg )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_clear"), GenericSel_Default, GenericSel_One_Arg )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_fill")) ); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_free"), GenericSel_By_Ref, GenericSel_One_Arg ) ); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_grow")) ); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_num"), GenericSel_Default, GenericSel_One_Arg )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_pop"), GenericSel_Default, GenericSel_One_Arg )); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_remove_at")) ); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_reserve"), GenericSel_By_Ref) ); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_resize"), GenericSel_By_Ref) ); interface_defines.append( gen_generic_selection_function_macro( Array_DefinitionCounter, txt("array_set_capacity")) ); return interface_defines; }