mirror of
https://github.com/Ed94/gencpp.git
synced 2024-11-10 02:54:53 -08:00
HashTable non-parsed implemented.
Improved the text to string macros!
This commit is contained in:
parent
ef790fdda5
commit
4db9aadea7
@ -36,6 +36,7 @@
|
||||
// # define ZPL_MODULE_PARSER
|
||||
#include "zpl.h"
|
||||
|
||||
using zpl::b32;
|
||||
using zpl::s8;
|
||||
using zpl::s16;
|
||||
using zpl::s32;
|
||||
@ -157,9 +158,9 @@ using zpl::str_len;
|
||||
#define scast( Type_, Value_ ) static_cast< Type_ >( Value_ )
|
||||
#define rcast( Type_, Value_ ) reinterpret_cast< Type_ >( Value_ )
|
||||
#define pcast( Type_, Value_ ) ( * (Type_*)( & (Value_) ) )
|
||||
#define txt_impl( Value_ ) #Value_
|
||||
#define txt( Value_ ) txt_impl( Value_ )
|
||||
#define txt_n_len( Value_ ) sizeof( txt_impl( Value_ ) ), txt_impl( Value_ )
|
||||
#define txt_impl( ... ) #__VA_ARGS__
|
||||
#define txt( ... ) txt_impl( __VA_ARGS__ )
|
||||
#define txt_n_len( ... ) sizeof( txt_impl( __VA_ARGS__ ) ), txt_impl( __VA_ARGS__ )
|
||||
#define do_once() \
|
||||
do \
|
||||
{ \
|
||||
@ -513,7 +514,7 @@ char const* Msg_Invalid_Value = "INVALID VALUE PROVIDED";
|
||||
return Data;
|
||||
}
|
||||
|
||||
operator StrC()
|
||||
operator StrC() const
|
||||
{
|
||||
return
|
||||
{
|
||||
|
@ -25,7 +25,6 @@ namespace gen
|
||||
}
|
||||
|
||||
#pragma region Constants
|
||||
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
||||
Code type_ns(auto);
|
||||
Code type_ns(void);
|
||||
Code type_ns(int);
|
||||
@ -33,6 +32,9 @@ namespace gen
|
||||
Code type_ns(char);
|
||||
Code type_ns(wchar_t);
|
||||
|
||||
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
||||
Code type_ns(b32);
|
||||
|
||||
Code type_ns(s8);
|
||||
Code type_ns(s16);
|
||||
Code type_ns(s32);
|
||||
@ -296,6 +298,7 @@ namespace gen
|
||||
break;
|
||||
|
||||
case Untyped:
|
||||
case Execution:
|
||||
result.append( Content );
|
||||
break;
|
||||
|
||||
@ -328,7 +331,6 @@ namespace gen
|
||||
case Access_Private:
|
||||
case Access_Protected:
|
||||
case Access_Public:
|
||||
result.append( indent );
|
||||
result.append( Name );
|
||||
break;
|
||||
|
||||
@ -508,10 +510,6 @@ namespace gen
|
||||
}
|
||||
break;
|
||||
|
||||
case Execution:
|
||||
result.append( Content );
|
||||
break;
|
||||
|
||||
case Export_Body:
|
||||
{
|
||||
result.append_fmt( "%sexport\n%s{\n", indent_str, indent_str );
|
||||
@ -1107,6 +1105,8 @@ namespace gen
|
||||
def_constant_code_type( wchar_t );
|
||||
|
||||
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
||||
type_ns(b32) = def_type( name(b32) );
|
||||
|
||||
def_constant_code_type( s8 );
|
||||
def_constant_code_type( s16 );
|
||||
def_constant_code_type( s32 );
|
||||
@ -1129,21 +1129,21 @@ namespace gen
|
||||
access_private_write = ccast( Code, access_private );
|
||||
access_private_write = make_code();
|
||||
access_private_write->Type = ECode::Access_Private;
|
||||
access_private_write->Name = get_cached_string( StrC::from("private") );
|
||||
access_private_write->Name = get_cached_string( StrC::from("private:") );
|
||||
access_private_write.lock();
|
||||
|
||||
Code&
|
||||
access_protected_write = ccast( Code, access_protected );
|
||||
access_protected_write = make_code();
|
||||
access_protected_write->Type = ECode::Access_Protected;
|
||||
access_protected_write->Name = get_cached_string( StrC::from("protected") );
|
||||
access_protected_write->Name = get_cached_string( StrC::from("protected:") );
|
||||
access_protected_write.lock();
|
||||
|
||||
Code&
|
||||
access_public_write = ccast( Code, access_public );
|
||||
access_public_write = make_code();
|
||||
access_public_write->Type = ECode::Access_Public;
|
||||
access_public_write->Name = get_cached_string( StrC::from("public") );
|
||||
access_public_write->Name = get_cached_string( StrC::from("public:") );
|
||||
access_public_write.lock();
|
||||
|
||||
module_global_fragment = make_code();
|
||||
@ -2088,12 +2088,13 @@ namespace gen
|
||||
switch ( body->Type )
|
||||
{
|
||||
case Function_Body:
|
||||
case Execution:
|
||||
case Untyped:
|
||||
break;
|
||||
|
||||
default:
|
||||
{
|
||||
log_failure("gen::def_function: body must be either of Function_Body or Untyped type. %s", body->debug_str());
|
||||
log_failure("gen::def_function: body must be either of Function_Body, Execution, or Untyped type. %s", body->debug_str());
|
||||
return Code::Invalid;
|
||||
}
|
||||
}
|
||||
@ -2193,12 +2194,6 @@ namespace gen
|
||||
{
|
||||
using namespace ECode;
|
||||
|
||||
if ( body && body->Type != Function_Body && body->Type != Untyped )
|
||||
{
|
||||
log_failure( "gen::def_operator: Body was provided but its not of function body type: %s", body->debug_str() );
|
||||
return Code::Invalid;
|
||||
}
|
||||
|
||||
if ( attributes && attributes->Type != Attributes )
|
||||
{
|
||||
log_failure( "gen::def_operator: Attributes was provided but its not of attributes type: %s", attributes->debug_str() );
|
||||
@ -2227,6 +2222,20 @@ namespace gen
|
||||
|
||||
if ( body )
|
||||
{
|
||||
switch ( body->Type )
|
||||
{
|
||||
case Function_Body:
|
||||
case Execution:
|
||||
case Untyped:
|
||||
break;
|
||||
|
||||
default:
|
||||
{
|
||||
log_failure("gen::def_operator: body must be either of Function_Body, Execution, or Untyped type. %s", body->debug_str());
|
||||
return Code::Invalid;
|
||||
}
|
||||
}
|
||||
|
||||
result->Type = check_result == OpValidateResult::Global ?
|
||||
Operator : Operator_Member;
|
||||
|
||||
@ -5270,7 +5279,10 @@ namespace gen
|
||||
|
||||
tokmap_clear( & tok_map );
|
||||
|
||||
return buf_size - remaining;
|
||||
sw result = buf_size - remaining;
|
||||
// buf[ result ] = '\0';
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Code untyped_str( StrC content )
|
||||
|
@ -866,6 +866,7 @@ namespace gen
|
||||
{
|
||||
local_persist thread_local
|
||||
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
|
||||
mem_set( buf, 0, ZPL_PRINTF_MAXLEN );
|
||||
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
@ -1014,11 +1015,7 @@ namespace gen
|
||||
# define name( Id_ ) { txt_n_len( Id_ ) }
|
||||
|
||||
// Same as name just used to indicate intention of literal for code instead of names.
|
||||
# define code( Code_ ) { txt_n_len( Code_ ) }
|
||||
|
||||
# define code_args( num, ... ) num, (Code[num]){ __VA_ARGS__ }
|
||||
|
||||
# define enum_entry( id ) "\t" #id ",\n"
|
||||
# define code( ... ) { txt_n_len( __VA_ARGS__ ) }
|
||||
#pragma endregion Macros
|
||||
|
||||
#pragma region Constants
|
||||
@ -1027,6 +1024,8 @@ namespace gen
|
||||
{
|
||||
// Predefined typename codes. Are set to readonly and are setup during gen::init()
|
||||
|
||||
extern Code type_ns( b32 );
|
||||
|
||||
extern Code type_ns( s8 );
|
||||
extern Code type_ns( s16 );
|
||||
extern Code type_ns( s32 );
|
||||
|
@ -20,7 +20,7 @@ Code gen__array_base()
|
||||
, def_specifiers( 2, ESpecifier::Static_Member, ESpecifier::Inline )
|
||||
);
|
||||
|
||||
return def_global_body( code_args( 2, header, grow_formula ) );
|
||||
return def_global_body( 2, header, grow_formula );
|
||||
}
|
||||
|
||||
Code gen__array( StrC type, sw type_size )
|
||||
@ -49,6 +49,10 @@ Code gen__array( StrC type, sw type_size )
|
||||
Code t_type_ptr = def_type( type, __, spec_ptr );
|
||||
Code t_type_ref = def_type( type, __, spec_ref );
|
||||
|
||||
Code t_alias = def_type( name(Type) );
|
||||
Code t_alias_ptr = def_type( name(Type), __, spec_ptr );
|
||||
Code t_alias_ref = def_type( name(Type), __, spec_ref );
|
||||
|
||||
Code t_header = def_type( name(Header) );
|
||||
Code t_header_ptr = def_type( name(Header), __, spec_ptr );
|
||||
Code t_header_ref = def_type( name(Header), __, spec_ref );
|
||||
@ -56,7 +60,7 @@ Code gen__array( StrC type, sw type_size )
|
||||
Code array;
|
||||
{
|
||||
Code using_type = def_using( name(Type), t_type );
|
||||
Code data = def_variable( t_type_ptr, name(Data) );
|
||||
Code data = def_variable( t_alias_ptr, name(Data) );
|
||||
|
||||
Code init;
|
||||
{
|
||||
@ -93,13 +97,13 @@ Code gen__array( StrC type, sw type_size )
|
||||
|
||||
Code append;
|
||||
{
|
||||
Code param_type;
|
||||
if ( type_size <= 8 )
|
||||
param_type = t_type;
|
||||
else
|
||||
param_type = t_type_ref;
|
||||
// Code param_type;
|
||||
// if ( type_size <= 64 )
|
||||
// param_type = t_type;
|
||||
// else
|
||||
// param_type = t_type_ref;
|
||||
|
||||
append = def_function( name(append), def_param(param_type, name(value)), t_bool
|
||||
append = def_function( name(append), def_param(t_alias, name(value)), t_bool
|
||||
, untyped_str( code(
|
||||
Header& header = get_header();
|
||||
|
||||
@ -124,7 +128,7 @@ Code gen__array( StrC type, sw type_size )
|
||||
return Data[ header.Num - 1 ];
|
||||
));
|
||||
|
||||
back = def_function( name(back), __, t_type_ref, body, spec_inline );
|
||||
back = def_function( name(back), __, t_alias_ref, body, spec_inline );
|
||||
}
|
||||
|
||||
Code clear = def_function( name(clear), __, t_void
|
||||
@ -138,9 +142,9 @@ Code gen__array( StrC type, sw type_size )
|
||||
Code fill;
|
||||
{
|
||||
Code params = def_params( 3
|
||||
, def_param( t_uw, name(begin) )
|
||||
, def_param( t_uw, name(end) )
|
||||
, def_param( t_type, name(value) )
|
||||
, def_param( t_uw, name(begin) )
|
||||
, def_param( t_uw, name(end) )
|
||||
, def_param( t_alias, name(value) )
|
||||
);
|
||||
|
||||
Code body = untyped_str( code(
|
||||
@ -185,6 +189,13 @@ Code gen__array( StrC type, sw type_size )
|
||||
))
|
||||
);
|
||||
|
||||
Code num = def_function( name(num), __, t_uw
|
||||
, untyped_str( code(
|
||||
return get_header().Num;
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code pop;
|
||||
{
|
||||
Code body = untyped_str( code(
|
||||
@ -197,6 +208,17 @@ Code gen__array( StrC type, sw type_size )
|
||||
pop = def_function( name(pop), __, t_bool, body, spec_inline );
|
||||
}
|
||||
|
||||
Code remove_at = def_function( name(remove_at), def_param( t_uw, name(idx)), t_void
|
||||
, def_execution( code(
|
||||
Header* header = & get_header();
|
||||
ZPL_ASSERT( idx < header->Num );
|
||||
|
||||
mem_move( header + idx, header + idx + 1, sizeof( Type ) * ( header->Num - idx - 1 ) );
|
||||
header->Num--;
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code reserve = def_function( name(reserve), def_param( t_uw, name(new_capacity)), t_bool
|
||||
, untyped_str( code(
|
||||
Header& header = get_header();
|
||||
@ -248,7 +270,7 @@ Code gen__array( StrC type, sw type_size )
|
||||
|
||||
zpl::free( header.Allocator, & header );
|
||||
|
||||
Data = (u8*) new_header + 1;
|
||||
Data = (Type*) new_header + 1;
|
||||
|
||||
return true;
|
||||
));
|
||||
@ -256,7 +278,14 @@ Code gen__array( StrC type, sw type_size )
|
||||
set_capacity = def_function( name(set_capacity), def_param( t_uw, name(new_capacity)), t_bool, body );
|
||||
}
|
||||
|
||||
Code body = def_struct_body( 17
|
||||
Code op_ptr = untyped_str( code(
|
||||
operator Type*()
|
||||
{
|
||||
return Data;
|
||||
}
|
||||
));
|
||||
|
||||
Code body = def_struct_body( 20
|
||||
, using_header
|
||||
, using_type
|
||||
, ct_grow_formula
|
||||
@ -271,11 +300,15 @@ Code gen__array( StrC type, sw type_size )
|
||||
, free
|
||||
, get_header
|
||||
, grow
|
||||
, num
|
||||
, pop
|
||||
, remove_at
|
||||
, reserve
|
||||
, resize
|
||||
, set_capacity
|
||||
|
||||
, op_ptr
|
||||
|
||||
, data
|
||||
);
|
||||
array = def_struct( name, body );
|
||||
|
@ -15,7 +15,7 @@ Code gen__buffer_base()
|
||||
, def_variable( t_uw, name(Num) )
|
||||
));
|
||||
|
||||
return def_global_body( code_args( 1, header ) );
|
||||
return def_global_body( 1, header );
|
||||
}
|
||||
|
||||
Code gen__buffer( StrC type, sw type_size )
|
||||
@ -97,13 +97,13 @@ Code gen__buffer( StrC type, sw type_size )
|
||||
|
||||
Code append;
|
||||
{
|
||||
Code param_type;
|
||||
if ( type_size <= 8 )
|
||||
param_type = t_type;
|
||||
else
|
||||
param_type = t_type_ref;
|
||||
// Code param_type;
|
||||
// if ( type_size <= 64 )
|
||||
// param_type = t_type;
|
||||
// else
|
||||
// param_type = t_type_ref;
|
||||
|
||||
append = def_function( name(append), def_param( param_type, name(value)), t_void
|
||||
append = def_function( name(append), def_param( t_type, name(value)), t_void
|
||||
, untyped_str( code(
|
||||
Header& header = get_header();
|
||||
Data[ header.Num ] = value;
|
||||
|
476
test/NonParsed/HashTable.NonParsed.hpp
Normal file
476
test/NonParsed/HashTable.NonParsed.hpp
Normal file
@ -0,0 +1,476 @@
|
||||
#pragma once
|
||||
|
||||
#if gen_time
|
||||
#include "gen.hpp"
|
||||
#include "Array.NonParsed.hpp"
|
||||
|
||||
using namespace gen;
|
||||
|
||||
Code gen__hashtable_base()
|
||||
{
|
||||
Code hashIndex = def_variable( t_sw, name(HashIndex) );
|
||||
Code entry_prev = def_variable( t_sw, name(PrevIndex) );
|
||||
Code entry_index = def_variable( t_sw, name(EntryIndex) );
|
||||
|
||||
Code find_result = def_struct( name(HashTableFindResult), def_struct_body( 3
|
||||
, hashIndex
|
||||
, entry_prev
|
||||
, entry_index
|
||||
));
|
||||
|
||||
return find_result;
|
||||
}
|
||||
|
||||
Code gen__hashtable( StrC type, sw type_size )
|
||||
{
|
||||
static Code t_allocator_info = def_type( name(AllocatorInfo) );
|
||||
|
||||
Code t_find_result = def_type( name(HashTableFindResult) );
|
||||
|
||||
StringCached name;
|
||||
{
|
||||
char const* name_str = str_fmt_buf( "HashTable_%s", type.Ptr );
|
||||
s32 len = str_len( name_str );
|
||||
|
||||
name = get_cached_string({ len, name_str });
|
||||
}
|
||||
|
||||
Code t_ht_type = def_type( name );
|
||||
|
||||
Code t_type = def_type( type );
|
||||
Code t_type_ptr = def_type( type, __, spec_ptr );
|
||||
Code t_type_ref = def_type( type, __, spec_ref );
|
||||
|
||||
// Hash table depends on array container for its entry structure.
|
||||
Code t_ht_entry, ht_entry, array_ht_entry, t_array_ht_entry;
|
||||
{
|
||||
char const* name_str = str_fmt_buf( "HashTable_%s_Entry", type.Ptr );
|
||||
s32 len = str_len( name_str );
|
||||
|
||||
StringCached ht_entry_name = get_cached_string({ len, name_str });
|
||||
sw const entry_size = sizeof( u64 ) + sizeof( sw ) + type_size;
|
||||
|
||||
t_ht_entry = def_type( ht_entry_name );
|
||||
ht_entry = def_struct( ht_entry_name, def_struct_body( 3
|
||||
, def_variable( t_u64, name(Key))
|
||||
, def_variable( t_sw, name(Next))
|
||||
, def_variable( t_type, name(Value))
|
||||
));
|
||||
|
||||
array_ht_entry = gen__array( ht_entry_name, entry_size );
|
||||
t_array_ht_entry = def_type( array_ht_entry->Name );
|
||||
}
|
||||
|
||||
Code hashtable;
|
||||
{
|
||||
Code using_entry = def_using( name(Entry), t_ht_entry );
|
||||
Code using_array_entry = def_using( name(Array_Entry), t_array_ht_entry );
|
||||
Code using_find_result = def_using( name(FindResult), t_find_result );
|
||||
|
||||
Code t_array_sw = def_type( name(Array_sw) );
|
||||
Code t_array_entry = def_type( name(Array_Entry) );
|
||||
|
||||
Code hashes = def_variable( t_array_sw, name(Hashes) );
|
||||
Code entries = def_variable( t_array_entry, name(Entries));
|
||||
|
||||
Code init;
|
||||
{
|
||||
char const* tmpl = txt(
|
||||
<type> result = { 0 };
|
||||
|
||||
result.Hashes = Array_sw ::init( allocator );
|
||||
result.Entries = Array_Entry::init( allocator );
|
||||
|
||||
return result;
|
||||
);
|
||||
Code body = def_execution( token_fmt( tmpl, 1, "type", name ) );
|
||||
|
||||
init = def_function( name(init), def_param( t_allocator_info, name(allocator)), t_ht_type, body, spec_static_member );
|
||||
}
|
||||
|
||||
Code clear = def_function( name(clear), __, t_void
|
||||
, def_execution( code(
|
||||
if ( s32 idx = 0; idx < Hashes.num(), idx++ )
|
||||
Hashes[ idx ] = -1;
|
||||
|
||||
Entries.clear();
|
||||
))
|
||||
);
|
||||
|
||||
Code destroy = def_function( name(destroy), __, t_void
|
||||
, def_execution( code(
|
||||
if ( Hashes )
|
||||
Hashes .free();
|
||||
if ( Entries )
|
||||
Entries.free();
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code get = def_function( name(get), def_param( t_u64, name(key)), t_type_ptr
|
||||
, def_execution( code(
|
||||
sw idx = find( key ).EntryIndex;
|
||||
if ( idx > 0 )
|
||||
return & Entries[ idx ].Value;
|
||||
|
||||
return nullptr;
|
||||
))
|
||||
);
|
||||
|
||||
Code using_map_proc;
|
||||
{
|
||||
char const* tmpl = txt(
|
||||
void (*) ( u64 key, <type> value )
|
||||
);
|
||||
Code value = untyped_str( token_fmt( tmpl, 1, "type", t_type.to_string() ) );
|
||||
|
||||
using_map_proc = def_using ( name(MapProc), value);
|
||||
}
|
||||
|
||||
Code map;
|
||||
{
|
||||
Code t_map_proc = def_type( name(MapProc) );
|
||||
|
||||
Code body = def_execution( code(
|
||||
ZPL_ASSERT_NOT_NULL( map_proc );
|
||||
|
||||
for ( sw idx = 0; idx < Entries.num(); idx++ )
|
||||
{
|
||||
map_proc( Entries[ idx ].Key, Entries[ idx ].Value );
|
||||
}
|
||||
));
|
||||
|
||||
map = def_function( name(map), def_param( t_map_proc, name(map_proc) ), t_void, body );
|
||||
}
|
||||
|
||||
Code using_map_mut_proc;
|
||||
{
|
||||
char const* tmpl = txt(
|
||||
void (*) ( u64 key, <type> value )
|
||||
);
|
||||
Code value = untyped_str( token_fmt( tmpl, 1, "type", t_type_ptr.to_string() ) );
|
||||
|
||||
using_map_mut_proc = def_using ( name(MapMutProc), value);
|
||||
}
|
||||
|
||||
Code map_mut;
|
||||
{
|
||||
Code t_map_mut_proc = def_type( name(MapMutProc));
|
||||
|
||||
Code body = def_execution( code(
|
||||
ZPL_ASSERT_NOT_NULL( map_proc );
|
||||
|
||||
for ( sw idx = 0; idx < Entries.num(); idx++ )
|
||||
{
|
||||
map_proc( Entries[ idx ].Key, & Entries[ idx ].Value );
|
||||
}
|
||||
));
|
||||
|
||||
map_mut = def_function( name(map_mut), def_param( t_map_mut_proc, name(map_proc)), t_void, body );
|
||||
}
|
||||
|
||||
Code grow = def_function( name(grow), __, t_void
|
||||
, def_execution( code(
|
||||
sw new_num = array_grow_formula( Entries.num() );
|
||||
rehash( new_num );
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code rehash;
|
||||
{
|
||||
char const* tmpl = txt(
|
||||
sw idx;
|
||||
sw last_added_index;
|
||||
|
||||
<type> new_ht = <type>::init( Hashes.get_header().Allocator );
|
||||
|
||||
new_ht.Hashes.resize( new_num );
|
||||
new_ht.Entries.reserve( new_ht.Hashes.num() );
|
||||
|
||||
for ( idx = 0; idx < new_ht.Hashes.num(); ++idx )
|
||||
new_ht.Hashes[ idx ] = -1;
|
||||
|
||||
for ( idx = 0; idx < Entries.num(); ++idx )
|
||||
{
|
||||
Entry& entry = Entries[ idx ];
|
||||
|
||||
FindResult find_result;
|
||||
|
||||
if ( new_ht.Hashes.num() == 0 )
|
||||
new_ht.grow();
|
||||
|
||||
entry = Entries[ idx ];
|
||||
find_result = new_ht.find( entry.Key );
|
||||
last_added_index = new_ht.add_entry( entry.Key );
|
||||
|
||||
if ( find_result.PrevIndex < 0 )
|
||||
new_ht.Hashes[ find_result.HashIndex ] = last_added_index;
|
||||
|
||||
else
|
||||
new_ht.Entries[ find_result.PrevIndex ].Next = last_added_index;
|
||||
|
||||
new_ht.Entries[ last_added_index ].Next = find_result.EntryIndex;
|
||||
new_ht.Entries[ last_added_index ].Value = entry.Value;
|
||||
}
|
||||
|
||||
// <type>* old_ht = this;
|
||||
// *this = new_ht;
|
||||
|
||||
// old_ht.destroy();
|
||||
|
||||
destroy();
|
||||
Hashes = new_ht.Hashes;
|
||||
Entries = new_ht.Entries;
|
||||
);
|
||||
Code body = def_execution( token_fmt( tmpl, 1, "type", name ) );
|
||||
|
||||
rehash = def_function( name(rehash), def_param( t_sw, name(new_num)), t_void, body, spec_inline);
|
||||
}
|
||||
|
||||
Code rehash_fast;
|
||||
{
|
||||
char const* tmpl = txt(
|
||||
sw idx;
|
||||
|
||||
for ( idx = 0; idx < Entries.num(); idx++ )
|
||||
Entries[ idx ].Next = -1;
|
||||
|
||||
for ( idx = 0; idx < Hashes.num(); idx++ )
|
||||
Hashes[ idx ] = -1;
|
||||
|
||||
for ( idx = 0; idx < Entries.num(); idx++ )
|
||||
{
|
||||
Entry* entry;
|
||||
|
||||
FindResult find_result;
|
||||
}
|
||||
);
|
||||
Code body = def_execution( token_fmt( tmpl, 1, "type", name ) );
|
||||
|
||||
rehash_fast = def_function( name(rehash_fast), __, t_void, body );
|
||||
}
|
||||
|
||||
Code remove = def_function( name(remove), def_param( t_u64, name(key)), t_void
|
||||
, def_execution( code(
|
||||
FindResult find_result = find( key);
|
||||
|
||||
if ( find_result.EntryIndex >= 0 )
|
||||
{
|
||||
Entries.remove_at( find_result.EntryIndex );
|
||||
rehash_fast();
|
||||
}
|
||||
))
|
||||
);
|
||||
|
||||
Code remove_entry = def_function( name(remove_entry), def_param( t_sw, name(idx)), t_void
|
||||
, def_execution( code(
|
||||
Entries.remove_at( idx );
|
||||
))
|
||||
);
|
||||
|
||||
Code set;
|
||||
{
|
||||
Code params = def_params( 2
|
||||
, def_param( t_u64, name(key))
|
||||
, def_param( t_type, name(value))
|
||||
);
|
||||
|
||||
Code body = def_execution( code(
|
||||
sw idx;
|
||||
FindResult find_result;
|
||||
|
||||
if ( Hashes.num() == 0 )
|
||||
grow();
|
||||
|
||||
find_result = find( key );
|
||||
|
||||
if ( find_result.EntryIndex >= 0 )
|
||||
{
|
||||
idx = find_result.EntryIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
idx = add_entry( key );
|
||||
|
||||
if ( find_result.PrevIndex >= 0 )
|
||||
{
|
||||
Entries[ find_result.PrevIndex ].Next = idx;
|
||||
}
|
||||
else
|
||||
{
|
||||
Hashes[ find_result.HashIndex ] = idx;
|
||||
}
|
||||
}
|
||||
|
||||
Entries[ idx ].Value = value;
|
||||
|
||||
if ( full() )
|
||||
grow();
|
||||
));
|
||||
|
||||
set = def_function( name(set), params, t_void, body );
|
||||
}
|
||||
|
||||
Code slot = def_function( name(slot), def_param( t_u64, name(key)), t_sw
|
||||
, def_execution( code(
|
||||
for ( sw idx = 0; idx < Hashes.num(); ++idx )
|
||||
if ( Hashes[ idx ] == key )
|
||||
return idx;
|
||||
|
||||
return -1;
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code add_entry = def_function( name(add_entry), def_param( t_u64, name(key)), t_sw
|
||||
, def_execution( code(
|
||||
sw idx;
|
||||
Entry entry = { key, -1 };
|
||||
|
||||
idx = Entries.num();
|
||||
Entries.append( entry );
|
||||
return idx;
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code find = def_function( name(find), def_param( t_u64, name(key)), t_find_result
|
||||
, def_execution( code(
|
||||
FindResult result = { -1, -1, -1 };
|
||||
|
||||
if ( Hashes.num() > 0 )
|
||||
{
|
||||
result.HashIndex = key % Hashes.num();
|
||||
result.EntryIndex = Hashes[ result.HashIndex ];
|
||||
|
||||
while ( result.EntryIndex >= 0 )
|
||||
{
|
||||
if ( Entries[ result.EntryIndex ].Key == key )
|
||||
break;
|
||||
|
||||
result.PrevIndex = result.EntryIndex;
|
||||
result.EntryIndex = Entries[ result.EntryIndex ].Next;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
))
|
||||
);
|
||||
|
||||
Code full = def_function( name(full), __, t_b32
|
||||
, def_execution( code(
|
||||
return 0.75f * Hashes.num() < Entries.num();
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
hashtable = def_struct( name, def_struct_body( 24
|
||||
, using_entry
|
||||
, using_array_entry
|
||||
, using_find_result
|
||||
, using_map_proc
|
||||
, using_map_mut_proc
|
||||
|
||||
, init
|
||||
|
||||
, clear
|
||||
, destroy
|
||||
, get
|
||||
, grow
|
||||
, map
|
||||
, map_mut
|
||||
, rehash
|
||||
, rehash_fast
|
||||
, remove
|
||||
, remove_entry
|
||||
, set
|
||||
, slot
|
||||
|
||||
, hashes
|
||||
, entries
|
||||
|
||||
, access_protected
|
||||
, add_entry
|
||||
, find
|
||||
, full
|
||||
));
|
||||
}
|
||||
|
||||
return def_global_body( 3, ht_entry, array_ht_entry, hashtable );
|
||||
}
|
||||
|
||||
struct GenHashTableRequest
|
||||
{
|
||||
StrC Dependency;
|
||||
StrC Type;
|
||||
sw TypeSize;
|
||||
};
|
||||
Array(GenHashTableRequest) GenHashTableRequests;
|
||||
|
||||
void gen__hashtable_request( StrC type, sw size, StrC dep = {} )
|
||||
{
|
||||
do_once_start
|
||||
array_init( GenHashTableRequests, g_allocator );
|
||||
|
||||
gen_array( sw );
|
||||
do_once_end
|
||||
|
||||
// Make sure we don't already have a request for the type.
|
||||
for ( sw idx = 0; idx < array_count( GenHashTableRequests ); ++idx )
|
||||
{
|
||||
StrC const reqest_type = GenHashTableRequests[ idx ].Type;
|
||||
|
||||
if ( reqest_type.Len != type.Len )
|
||||
continue;
|
||||
|
||||
if ( str_compare( reqest_type.Ptr, type.Ptr, reqest_type.Len ) == 0 )
|
||||
return;
|
||||
}
|
||||
|
||||
GenHashTableRequest request = { dep, type, size};
|
||||
array_append( GenHashTableRequests, request );
|
||||
}
|
||||
#define gen_hashtable( type ) gen__hashtable_request( { txt_n_len(type) }, sizeof( type ))
|
||||
|
||||
u32 gen_hashtable_file()
|
||||
{
|
||||
Builder
|
||||
gen_buffer_file;
|
||||
gen_buffer_file.open( "hashtable.gen.hpp" );
|
||||
|
||||
gen_buffer_file.print( def_include( StrC::from("Bloat.hpp")) );
|
||||
gen_buffer_file.print( def_include( StrC::from("Array.NonParsed.hpp")) );
|
||||
gen_buffer_file.print( def_include( StrC::from("array.gen.hpp")) );
|
||||
|
||||
gen_buffer_file.print( gen__hashtable_base());
|
||||
|
||||
GenHashTableRequest* current = GenHashTableRequests;
|
||||
s32 left = array_count( GenHashTableRequests );
|
||||
while (left--)
|
||||
{
|
||||
GenHashTableRequest const& request = * current;
|
||||
|
||||
Code generated_buffer = gen__hashtable( current->Type, current->TypeSize );
|
||||
|
||||
if ( request.Dependency )
|
||||
{
|
||||
char const* cmt_str = str_fmt_buf( "// Dependency for %s type", request.Type );
|
||||
s32 cmt_len = str_len( cmt_str );
|
||||
|
||||
Code cmt = def_comment( { cmt_len, cmt_str } );
|
||||
Code include = def_include( request.Dependency );
|
||||
|
||||
gen_buffer_file.print( cmt );
|
||||
gen_buffer_file.print( include );
|
||||
}
|
||||
|
||||
gen_buffer_file.print( generated_buffer );
|
||||
current++;
|
||||
}
|
||||
|
||||
gen_buffer_file.write();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // gen_time
|
@ -35,6 +35,8 @@ Code gen__ring( StrC type, sw type_size )
|
||||
|
||||
Code ring;
|
||||
{
|
||||
Code using_type = def_using( name(Type), t_type );
|
||||
|
||||
Code backing = def_variable( t_allocator_info, name(Backing) );
|
||||
Code capacity = def_variable( t_uw, name(Capacity) );
|
||||
Code head = def_variable( t_uw, name(Head) );
|
||||
@ -70,9 +72,84 @@ Code gen__ring( StrC type, sw type_size )
|
||||
init = def_function( name(init), params, t_ring_type, body, spec_static_member );
|
||||
}
|
||||
|
||||
ring = def_struct( name, def_struct_body( 6,
|
||||
Code append = def_function( name(append), def_param( t_type, name(value)), t_void
|
||||
, untyped_str( code(
|
||||
Buffer[ Head ] = value;
|
||||
Head = ( Head + 1 ) % Capacity;
|
||||
|
||||
if ( Head == Tail )
|
||||
Tail = ( Tail + 1 ) % Capacity;
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code appendv;
|
||||
{
|
||||
Code params = def_params( 2
|
||||
, def_param( t_type_ptr, name(values))
|
||||
, def_param( t_sw, name(num))
|
||||
);
|
||||
|
||||
Code body = untyped_str( code(
|
||||
for ( sw idx = 0; idx < num; idx++ )
|
||||
append( values[ idx ] );
|
||||
));
|
||||
|
||||
appendv = def_function( name(append), params, t_void, body, spec_inline );
|
||||
}
|
||||
|
||||
Code empty = def_function( name(empty), __, t_bool
|
||||
, def_execution( code(
|
||||
return Head == Tail;
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code free = def_function( name(free), __, t_void
|
||||
, def_execution( code(
|
||||
Buffer.free();
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code full = def_function( name(full), __, t_bool
|
||||
, def_execution( code(
|
||||
return (Head + 1) % Capacity == Tail;
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
Code get = def_function( name(get), __, t_type_ref
|
||||
, def_execution( code(
|
||||
Type& data = Buffer[ Tail ];
|
||||
Tail = ( Tail + 1 ) % Capacity;
|
||||
|
||||
return data;
|
||||
))
|
||||
);
|
||||
|
||||
Code wipe = def_function( name(wipe), __, t_void
|
||||
, def_execution( code(
|
||||
Head = 0;
|
||||
Tail = 0;
|
||||
Buffer.wipe();
|
||||
))
|
||||
, spec_inline
|
||||
);
|
||||
|
||||
ring = def_struct( name, def_struct_body( 14,
|
||||
using_type,
|
||||
|
||||
init,
|
||||
|
||||
append,
|
||||
appendv,
|
||||
empty,
|
||||
free,
|
||||
full,
|
||||
get,
|
||||
wipe,
|
||||
|
||||
backing,
|
||||
capacity,
|
||||
head,
|
||||
@ -110,7 +187,7 @@ void gen__ring_request( StrC type, sw size, StrC dep = {} )
|
||||
return;
|
||||
}
|
||||
|
||||
// Ring definition depends on a buffer definition.
|
||||
// Ring definition depends on a array and buffer definition.
|
||||
gen__buffer_request( type, size, dep );
|
||||
|
||||
GenRingRequest request = { dep, type, size};
|
||||
|
@ -53,10 +53,10 @@ u32 gen_sanity()
|
||||
Code fwd = def_enum( name(ETestEnum), NoCode, t_u8 );
|
||||
Code def;
|
||||
{
|
||||
Code body = untyped_str( StrC::from(
|
||||
enum_entry( A )
|
||||
enum_entry( B )
|
||||
enum_entry( C )
|
||||
Code body = untyped_str( code(
|
||||
A,
|
||||
B,
|
||||
C
|
||||
));
|
||||
|
||||
def = def_enum( name(ETestEnum), body, t_u8 );
|
||||
@ -162,10 +162,10 @@ u32 gen_sanity()
|
||||
|
||||
Code bitflagtest;
|
||||
{
|
||||
Code body = def_enum_body( 1, untyped_str( StrC::from(
|
||||
enum_entry( A = 1 << 0 )
|
||||
enum_entry( B = 1 << 1 )
|
||||
enum_entry( C = 1 << 2 )
|
||||
Code body = def_enum_body( 1, untyped_str( code(
|
||||
A = 1 << 0,
|
||||
B = 1 << 1,
|
||||
C = 1 << 2
|
||||
)));
|
||||
bitflagtest = def_enum( name(EBitFlagtest), body, t_u8, EnumClass );
|
||||
}
|
||||
@ -173,10 +173,10 @@ u32 gen_sanity()
|
||||
|
||||
Code op_fwd, op_or;
|
||||
{
|
||||
Code params = def_params( code_args( 2,
|
||||
Code params = def_params( 2,
|
||||
def_param( t_bitflag, name(a) ),
|
||||
def_param( t_bitflag, name(b) )
|
||||
));
|
||||
);
|
||||
|
||||
op_fwd = def_operator( EOperator::BOr, params, t_bitflag );
|
||||
op_or = def_operator( EOperator::BOr, params, t_bitflag, untyped_str( code(
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "Bloat.cpp"
|
||||
#include "NonParsed\Array.NonParsed.hpp"
|
||||
#include "NonParsed\Buffer.NonParsed.hpp"
|
||||
#include "NonParsed\HashTable.NonParsed.hpp"
|
||||
#include "NonParsed\Ring.NonParsed.hpp"
|
||||
#include "NonParsed\Sanity.hpp"
|
||||
|
||||
@ -23,10 +24,13 @@ int gen_main()
|
||||
|
||||
gen_buffer( u8 );
|
||||
|
||||
gen_hashtable( u32 );
|
||||
|
||||
gen_ring( s16 );
|
||||
|
||||
gen_array_file();
|
||||
gen_buffer_file();
|
||||
gen_hashtable_file();
|
||||
gen_ring_file();
|
||||
|
||||
gen::deinit();
|
||||
|
Loading…
Reference in New Issue
Block a user