#pragma once #include "../project/gen.hpp" using namespace gen; 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 gen_array_grow_formula( value ) ( 2 * value + 8 )\n" )); Code get_header = untyped_str( txt( "#define array_get_header( Type, self ) ( (ArrayHeader*)( self ) - 1)\n" )); return def_global_body( args( td_header, header, get_header ) ); }; 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" ) #undef GEN_ASSERT 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 ( * 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 ); bool _reserve ( * self, usize new_capacity ); bool _resize ( * self, usize num ); bool _set_capacity ( * self, usize new_capacity ); _init( AllocatorInfo allocator ) { return _init_reserve( allocator, array_grow_formula( 0 ) ); } _init_reserve( AllocatorInfo allocator, usize capacity ) { ArrayHeader* header = cast(ArrayHeader*, alloc( allocator, sizeof(ArrayHeader) + sizeof() * capacity ) ); if ( header == NULL ) return NULL; header->Allocator = allocator; header->Capacity = capacity; header->Num = 0; return cast( *, header + 1 ); } bool _append( * self, value ) { ArrayHeader* header = get_header( * self ); if ( header->Num == header->Capacity ) { if ( ! _grow( self, header->Capacity)) return false; header = get_header( * self ); } (* self)[ header->Num ] = value; header->Num++; return true; } bool _append_items( * self, * items, usize item_num ) { ArrayHeader* header = get_header( * self ); if ( header->Num + item_num > header->Capacity ) { if ( ! _grow( self, header->Capacity + item_num )) return false; header = get_header( * self ); } mem_copy( (* self) + header->Num, items, sizeof() * item_num ); header->Num += item_num; return true; } bool _append_at( * self, item, usize idx ) { ArrayHeader* header = get_header( * self ); if ( idx >= header->Num ) idx = header->Num - 1; if ( idx < 0 ) idx = 0; if ( header->Capacity < header->Num + 1 ) { if ( ! _grow( self, header->Capacity + 1 ) ) return false; header = get_header( * self ); } target = (* self) + idx; mem_move( target + 1, target, (header->Num - idx) * sizeof() ); header->Num++; return true; } bool _append_items_at( * self, * items, usize item_num, usize idx ) { ArrayHeader* header = get_header( * self ); if ( idx >= header->Num ) { return _append_items( self, items, item_num ); } if ( item_num > header->Capacity ) { if ( ! _grow( self, item_num + header->Capacity ) ) return false; header = 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; } * _back( self ) { ArrayHeader* header = get_header( self ); if ( header->Num == 0 ) return NULL; return self + header->Num - 1; } void _clear( self ) { ArrayHeader* header = get_header( self ); header->Num = 0; } bool _fill( self, usize begin, usize end, value ) { ArrayHeader* header = get_header( self ); if ( begin < 0 || end >= header->Num ) return false; for ( ssize idx = begin; idx < end; idx ++ ) self[ idx ] = value; return true; } void _free( self ) { ArrayHeader* header = get_header( self ); free( header->Allocator, header ); self = NULL; } bool _grow( * self, usize min_capacity ) { ArrayHeader* header = get_header( *self ); usize new_capacity = array_grow_formula( header->Capacity ); if ( new_capacity < min_capacity ) new_capacity = min_capacity; return _set_capacity( self, new_capacity ); } usize _num( self ) { return get_header(self)->Num; } _pop( self ) { ArrayHeader* header = get_header( self ); GEN_ASSERT( header->Num > 0 ); result = self[ header->Num - 1 ]; header->Num--; return result; } void _remove_at( self, usize idx ) { ArrayHeader* header = get_header( self ); GEN_ASSERT( idx < header->Num ); mem_move( self + idx, self + idx + 1, sizeof( ) * ( header->Num - idx - 1 ) ); header->Num--; } bool _reserve( * self, usize new_capacity ) { ArrayHeader* header = get_header( * self ); if ( header->Capacity < new_capacity ) return _set_capacity( self, new_capacity ); return true; } bool _resize( * self, usize num ) { ArrayHeader* header = get_header( * self ); if ( header->Capacity < num ) { if ( ! _grow( self, num ) ) return false; header = get_header( * self ); } header->Num = num; return true; } bool _set_capacity( * self, usize new_capacity ) { ArrayHeader* header = get_header( * self ); if ( new_capacity == header->Capacity ) return true; if ( new_capacity < header->Num ) header->Num = new_capacity; 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 ); free( header->Allocator, & header ); new_header->Capacity = new_capacity; * self = cast( *, new_header + 1 ); return true; } ))); #pragma pop_macro( "GEN_ASSERT" ) return def_global_body( args( def_pragma( to_str( str_fmt_buf( "region %S", array_type ))), fmt_newline, result, fmt_newline, def_pragma( to_str( str_fmt_buf( "endregion %S", array_type ))), fmt_newline )); }; // CodeBody gen_