From 8bb2bc7b1b0c4673ef2807cf9ddeab5957d14feb Mon Sep 17 00:00:00 2001 From: Ed_ Date: Thu, 5 Dec 2024 17:48:24 -0500 Subject: [PATCH] fixes on containers (compiles but still verifying parity with c++ templates I'm going to have to change some c++ templates to match the init interfaces as they must not be in the return type --- gen_c_library/c_library.cpp | 3 +- gen_c_library/components/containers.array.hpp | 53 ++++++++++--------- .../components/containers.hashtable.hpp | 53 +++++++++---------- project/bootstrap.cpp | 2 + project/dependencies/containers.hpp | 6 +-- project/dependencies/parsing.cpp | 5 +- .../helpers/pop_container_defines.inline.hpp | 4 +- 7 files changed, 66 insertions(+), 60 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 5d52b9d..162bb3d 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -9,7 +9,9 @@ #include "helpers/helper.hpp" GEN_NS_BEGIN +#include "helpers/push_container_defines.inline.hpp" #include "dependencies/parsing.cpp" +#include "helpers/pop_container_defines.inline.hpp" GEN_NS_END #include "auxillary/builder.hpp" @@ -373,7 +375,6 @@ int gen_main() { CodeBody ht = gen_hashtable(txt("StrC"), name_string_table); strings.append(ht); - strings.append(td); break; } strings.append(td); diff --git a/gen_c_library/components/containers.array.hpp b/gen_c_library/components/containers.array.hpp index 5661a80..6cb5040 100644 --- a/gen_c_library/components/containers.array.hpp +++ b/gen_c_library/components/containers.array.hpp @@ -41,23 +41,24 @@ CodeBody gen_array( StrC type, StrC array_name ) , stringize( typedef * ; - void _init ( * self, AllocatorInfo allocator ); - void _init_reserve ( * self, 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 ); - bool _reserve ( * self, usize new_capacity ); - bool _resize ( * self, usize num ); - bool _set_capacity ( * self, usize new_capacity ); + void _init ( * self, AllocatorInfo allocator ); + void _init_reserve ( * self, 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 ); void _init( * self, AllocatorInfo allocator ) { @@ -202,9 +203,9 @@ CodeBody gen_array( StrC type, StrC array_name ) return true; } - void _free( self ) + void _free( * self ) { - ArrayHeader* header = array_get_header( self ); + ArrayHeader* header = array_get_header( * self ); allocator_free( header->Allocator, header ); self = NULL; } @@ -217,7 +218,7 @@ CodeBody gen_array( StrC type, StrC array_name ) if ( new_capacity < min_capacity ) new_capacity = min_capacity; - return array_set_capacity( * self, new_capacity ); + return array_set_capacity( self, new_capacity ); } usize _num( self ) @@ -249,7 +250,7 @@ CodeBody gen_array( StrC type, StrC array_name ) ArrayHeader* header = array_get_header( * self ); if ( header->Capacity < new_capacity ) - return array_set_capacity( * self, new_capacity ); + return array_set_capacity( self, new_capacity ); return true; } @@ -311,12 +312,14 @@ R"(#define GENERIC_SLOT___array_init , __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 +#define GENERIC_SLOT___array_set_capacity *, _set_capacity )" )); @@ -387,12 +390,12 @@ CodeBody gen_array_generic_selection_interface() interface_defines.append( gen_array_generic_selection_function_macro(txt("array_back")) ); interface_defines.append( gen_array_generic_selection_function_macro(txt("array_clear")) ); interface_defines.append( gen_array_generic_selection_function_macro(txt("array_fill")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_free")) ); + interface_defines.append( gen_array_generic_selection_function_macro(txt("array_free"), array_by_ref) ); interface_defines.append( gen_array_generic_selection_function_macro(txt("array_grow")) ); interface_defines.append( gen_array_generic_selection_function_macro(txt("array_num")) ); interface_defines.append( gen_array_generic_selection_function_macro(txt("arary_pop")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("arary_remove_at")) ); + interface_defines.append( gen_array_generic_selection_function_macro(txt("array_remove_at")) ); interface_defines.append( gen_array_generic_selection_function_macro(txt("arary_reserve"), array_by_ref) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_set_capacity"), array_by_ref) ); + interface_defines.append( gen_array_generic_selection_function_macro(txt("array_set_capacity")) ); return interface_defines; } diff --git a/gen_c_library/components/containers.hashtable.hpp b/gen_c_library/components/containers.hashtable.hpp index 3b45dce..1704e81 100644 --- a/gen_c_library/components/containers.hashtable.hpp +++ b/gen_c_library/components/containers.hashtable.hpp @@ -44,7 +44,7 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) "tbl_name", (StrC) hashtable_name, "tbl_type", (StrC) tbl_type, stringize( - typedef struct ; + typedef struct HashTable_ ; typedef struct HTE_ HTE_; struct HTE_ { @@ -72,8 +72,7 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) "array_entry", (StrC) entry_array_name, "fn_array", (StrC) entry_array_fn_ns, stringize( - typedef struct ; - struct + struct HashTable_ { Array_ssize Hashes; Entries; @@ -120,19 +119,19 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) void _clear( self ) { - for ( ssize idx = 0; idx < array_header( self.Hashes )->Num; idx++ ) + for ( ssize idx = 0; idx < array_get_header( self.Hashes )->Num; idx++ ) self.Hashes[idx] = -1; - array_ssize_clear( self.Hashes ); - _clear( self.Entries ); + array_clear( self.Hashes ); + array_clear( self.Entries ); } void _destroy( self ) { if ( self.Hashes && self.Entries ) { - array_ssize_free( self.Hashes ); - _free( self.Entries ); + array_free( self.Hashes ); + array_free( self.Entries ); } } @@ -149,7 +148,7 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) { GEN_ASSERT_NOT_NULL( map_proc ); - for ( ssize idx = 0; idx < array_header( self.Entries )->Num; idx++ ) + for ( ssize idx = 0; idx < array_get_header( self.Entries )->Num; idx++ ) { map_proc( self, self.Entries[idx].Key, self.Entries[idx].Value ); } @@ -159,7 +158,7 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) { GEN_ASSERT_NOT_NULL( map_proc ); - for ( ssize idx = 0; idx < array_header( self.Entries )->Num; idx++ ) + for ( ssize idx = 0; idx < array_get_header( self.Entries )->Num; idx++ ) { map_proc( self, self.Entries[idx].Key, & self.Entries[idx].Value ); } @@ -167,7 +166,7 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) void _grow( * self ) { - ssize new_num = array_grow_formula( array_header( self->Entries )->Num ); + ssize new_num = array_grow_formula( array_get_header( self->Entries )->Num ); _rehash( self, new_num ); } @@ -176,12 +175,12 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) ssize idx; ssize last_added_index; - ArrayHeader* old_hash_header = array_header( self->Hashes ); - ArrayHeader* old_entries_header = array_header( self->Entries ); + ArrayHeader* old_hash_header = array_get_header( self->Hashes ); + ArrayHeader* old_entries_header = array_get_header( self->Entries ); new_tbl = _make_reserve( old_hash_header->Allocator, old_hash_header->Num ); - ArrayHeader* new_hash_header = array_header( new_tbl.Hashes ); + ArrayHeader* new_hash_header = array_get_header( new_tbl.Hashes ); for ( idx = 0; idx < new_hash_header->Num; idx++ ) new_tbl.Hashes[idx] = -1; @@ -215,13 +214,13 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) { ssize idx; - for ( idx = 0; idx < array_header( self.Entries )->Num; idx++ ) + for ( idx = 0; idx < array_get_header( self.Entries )->Num; idx++ ) self.Entries[ idx ].Next = -1; - for ( idx = 0; idx < array_header( self.Hashes )->Num; idx++ ) + for ( idx = 0; idx < array_get_header( self.Hashes )->Num; idx++ ) self.Hashes[ idx ] = -1; - for ( idx = 0; idx < array_header( self.Entries )->Num; idx++ ) + for ( idx = 0; idx < array_get_header( self.Entries )->Num; idx++ ) { * entry; HT_FindResult find_result; @@ -242,22 +241,22 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) if ( find_result.EntryIndex >= 0 ) { - _remove_at( self.Entries, find_result.EntryIndex ); + array_remove_at( self.Entries, find_result.EntryIndex ); _rehash_fast( self ); } } void _remove_entry( self, ssize idx ) { - _remove_at( self.Entries, idx ); + array_remove_at( self.Entries, idx ); } void _set( * self, u64 key, value ) { - ssize idx; + ssize idx; HT_FindResult find_result; - if ( array_header( self->Hashes )->Num == 0 ) + if ( array_get_header( self->Hashes )->Num == 0 ) _grow( self ); find_result = __find( * self, key ); @@ -288,7 +287,7 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) ssize _slot( self, u64 key ) { - for ( ssize idx = 0; idx < array_header( self.Hashes )->Num; ++idx ) + for ( ssize idx = 0; idx < array_get_header( self.Hashes )->Num; ++idx ) if ( self.Hashes[ idx ] == key ) return idx; @@ -300,8 +299,8 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) ssize idx; entry = { key, -1 }; - idx = array_header( self.Entries )->Num; - _append( & self.Entries, entry ); + idx = array_get_header( self.Entries )->Num; + array_append( self.Entries, entry ); return idx; } @@ -309,7 +308,7 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) { HT_FindResult result = { -1, -1, -1 }; - ArrayHeader* hash_header = array_header( self.Hashes ); + ArrayHeader* hash_header = array_get_header( self.Hashes ); if ( hash_header->Num > 0 ) { @@ -331,8 +330,8 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) b32 __full( self ) { - ArrayHeader* hash_header = array_header( self.Hashes ); - ArrayHeader* entries_header = array_header( self.Entries ); + ArrayHeader* hash_header = array_get_header( self.Hashes ); + ArrayHeader* entries_header = array_get_header( self.Entries ); return 0.75f * hash_header->Num < entries_header->Num; } diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index c9b55ce..5ed2011 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -9,7 +9,9 @@ #include "helpers/helper.hpp" GEN_NS_BEGIN +#include "helpers/push_container_defines.inline.hpp" #include "dependencies/parsing.cpp" +#include "helpers/pop_container_defines.inline.hpp" GEN_NS_END #include "auxillary/builder.hpp" diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 309a3dd..506e938 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -73,11 +73,11 @@ struct Array forceinline static Array init_reserve(AllocatorInfo allocator, ssize capacity) { return GEN_NS array_init_reserve(allocator, capacity); } forceinline static usize grow_formula(ssize value) { return GEN_NS array_grow_formula(value); } - forceinline bool append(Array other) { return GEN_NS array_append(this, other); } + forceinline bool append(Array other) { return GEN_NS array_append_array(this, other); } forceinline bool append(Type value) { return GEN_NS array_append(this, value); } - forceinline bool append(Type* items, usize item_num) { return GEN_NS array_append(this, items, item_num); } + forceinline bool append(Type* items, usize item_num) { return GEN_NS array_append_items(this, items, item_num); } forceinline bool append_at(Type item, usize idx) { return GEN_NS array_append_at(this, item, idx); } - forceinline bool append_at(Type* items, usize item_num, usize idx) { return GEN_NS array_append_at(this, items, item_num, idx); } + forceinline bool append_at(Type* items, usize item_num, usize idx) { return GEN_NS array_append_items_at(this, items, item_num, idx); } forceinline Type* back() { return GEN_NS array_back(* this); } forceinline void clear() { GEN_NS array_clear(* this); } forceinline bool fill(usize begin, usize end, Type value) { return GEN_NS array_fill(* this, begin, end, value); } diff --git a/project/dependencies/parsing.cpp b/project/dependencies/parsing.cpp index 1698cff..bc1fbf3 100644 --- a/project/dependencies/parsing.cpp +++ b/project/dependencies/parsing.cpp @@ -1,5 +1,6 @@ #ifdef GEN_INTELLISENSE_DIRECTIVES # pragma once +# include "parsing.hpp" #endif #pragma region ADT @@ -42,7 +43,7 @@ u8 adt_destroy_branch( ADT_Node* node ) adt_destroy_branch( node->nodes + i ); } - array_free(& node->nodes); + array_free(node->nodes); } return 0; } @@ -288,7 +289,7 @@ ADT_Node* adt_alloc_at( ADT_Node* parent, ssize index ) ADT_Node o = { 0 }; o.parent = parent; - if ( ! array_append_at( & parent->nodes, o, index ) ) + if ( ! array_append_at( parent->nodes, o, index ) ) return NULL; ADT_Node* node = & parent->nodes[index]; diff --git a/project/helpers/pop_container_defines.inline.hpp b/project/helpers/pop_container_defines.inline.hpp index 5c6c38d..cb6f8e1 100644 --- a/project/helpers/pop_container_defines.inline.hpp +++ b/project/helpers/pop_container_defines.inline.hpp @@ -2,10 +2,10 @@ #undef array_init #undef array_init_reserve #undef array_append_array -#undef array_append_value +#undef array_append #undef array_append_items #undef array_append_at -#undef array_append_at +#undef array_append_items_at #undef array_back #undef array_clear #undef array_fill