Finished array test (parsed/nonparsed)

In the future I will have tests in a parsed and non-parsed variants.
This commit is contained in:
Edward R. Gonzalez 2023-05-06 18:35:11 -04:00
parent 75588f5227
commit 74c5736f59
2 changed files with 207 additions and 10 deletions

View File

@ -130,8 +130,31 @@ Code gen__array( StrC type, sw type_size )
back = def_function( name(back), __, t_type_ref, body ); back = def_function( name(back), __, t_type_ref, body );
} }
Code clear; Code clear = def_function( name(clear), __, t_void, untyped_str( code(
Header& header = get_header();
header.Num = 0;
)));
Code fill; Code fill;
{
Code params = def_params( 3, t_uw, txt_n_len(begin), t_uw, txt_n_len(end), t_type, txt_n_len(value) );
Code body = untyped_str( code(
Header& header = get_header();
if ( begin < 0 || end >= header.Num )
return false;
for ( sw idx = begin; idx < end; idx++ )
{
data[ idx ] = value;
}
return true;
));
fill = def_function( name(fill), params, t_bool, body );
}
Code free; Code free;
{ {
@ -143,12 +166,110 @@ Code gen__array( StrC type, sw type_size )
free = def_function( name(free), __, t_void, body, spec_inline ); free = def_function( name(free), __, t_void, body, spec_inline );
} }
Code get_header; Code get_header = def_function( name(get_header), __, t_header_ref, untyped_str( code(
return pcast( Header, Data - 1 );
)));
Code grow; Code grow;
{
Code param = def_param( t_uw, name(min_capacity) );
Code body = untyped_str( code(
Header& header = get_header();
t_uw new_capacity = grow_formula( header.Capacity );
if ( new_capacity < min_capacity )
new_capacity = 8;
return set_capacity( new_capacity );
));
grow = def_function( name(grow), param, t_bool, body );
}
Code pop; Code pop;
{
Code body = untyped_str( code(
Header& header = get_header();
assert_crash( header.Num > 0 );
header.Num--;
));
pop = def_function( name(pop), __, t_bool, body );
}
Code reserve; Code reserve;
{
Code param = def_param( t_uw, name(new_capacity) );
Code body = untyped_str( code(
Header& header = get_header();
if ( header.Capacity < new_capacity )
return set_capacity( capacity );
return true;
));
reserve = def_function( name(reserve), param, t_bool, body );
}
Code resize; Code resize;
{
Code param = def_param( t_uw, name( num ));
Code body = untyped_str( code(
Header& header = get_header();
if ( num > header.Capacity )
{
if ( ! grow( header.Allocator ))
return false;
}
header.Num = num;
return true;
));
resize = def_function( name(resize), param, t_bool, body );
}
Code set_capacity; Code set_capacity;
{
Code param = def_param( t_uw, name(new_capacity) );
Code body = untyped_str( code(
Header& header = get_header();
if ( new_capacity == header.Capacity )
return true;
if ( new_capacity < header.Num )
header.Num = new_capacity;
sw size = sizeof(Header) + sizeof(Type) * new_capacity;
Header* new_header = rcast( Header*, alloc( header.Allocator, size ));
if ( new_header == nullptr )
return false;
mem_move( new_header, & header, sizeof( Header ) + sizeof(Type) * header.Num );
new_header->Allocator = header.Allocator;
new_header->Num = header.Num;
new_header->Capacity = new_capacity;
::free( header );
*Data = new_header + 1;
return true;
));
set_capacity = def_function( name(set_capacity), param, t_bool, body );
}
Code body = def_struct_body( 17 Code body = def_struct_body( 17
, using_type , using_type

View File

@ -81,9 +81,24 @@ Code gen__array( s32 length, char const* type_str, sw type_size )
return data[ header.Num - 1 ]; return data[ header.Num - 1 ];
} }
void clear(); void clear()
{
Header& header = get_header();
header.Num = 0;
}
bool fill(); bool fill( uw begin, uw end, Type const& value )
{
Header& header = get_header();
if ( begin < 0 || end >= header.Num )
return false;
for ( uw idx = begin; idx < end; idx++ )
data[ idx ] = value;
return true;
}
void free() void free()
{ {
@ -93,18 +108,79 @@ Code gen__array( s32 length, char const* type_str, sw type_size )
Header& get_header() Header& get_header()
{ {
return rcast( Header*, Data ) - 1; return pcast( Header, Data - 1 );
} }
bool grow(); bool grow( uw min_capacity )
{
Header& header = get_header();
void pop(); uw new_capacity = grow_formula( header.Capacity );
bool reserve( uw num ); if ( new_capacity < min_capacity )
new_capacity = min_capacity;
bool resize( uw num ); return set_capacity( new_capacity );
}
bool set_capacity( uw capacity ); void pop()
{
Header& header = get_header();
assert_crash( header.Num > 0 );
header.Num--;
}
bool reserve( uw new_capacity )
{
Header& header = get_header();
if ( header.Capacity < new_capacity )
return set_capacity( new_capacity );
return true;
}
bool resize( uw num )
{
Header& header = get_header();
if ( header.Capacity < num )
if ( ! grow( num ))
return false;
header.Num = num;
return true;
}
bool set_capacity( uw new_capacity )
{
Header& header = get_header();
if ( new_capacity == header.Capacity )
return true;
if ( new_capacity < header.Num )
header.Num = new_capacity;
sw size = sizeof(Header) + sizeof(Type) * new_capacity;
Header* new_header = rcast( Header*, alloc( header.Allocator, size ));
if ( new_header == nullptr )
return false;
mem_move( new_header, & header, sizeof( Header ) + sizeof(Type) * header.Num );
new_header->Allocator = header.Allocator;
new_header->Num = header.Num;
new_header->Capacity = new_capacity;
::free( header );
*Data = new_header + 1;
return true;
}
} }
); );