From e246f30cc269585c93063e39b2a1a123ea5261f4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Thu, 14 Aug 2025 10:03:44 +0200 Subject: [PATCH] Use a typedef and interface for part catalog This may be an example of premature optimization. It however only adds a couple of lines of code. It makes changing the underlying structure easy to swap. And I think it is worth it. It is easy to go back with substitution, but it would not necessarily be easy to introduce this change later on. --- src/c_load.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/c_load.c b/src/c_load.c index e3c2e26..99c85b3 100644 --- a/src/c_load.c +++ b/src/c_load.c @@ -327,8 +327,26 @@ int write_used_part(t_part *part, const t_parse_tree_node *part_reference) free(part_spec.spec.name); return (1); } +typedef t_rbtree t_catalog; -int extract_used_names(t_rbtree *part_catalog, t_parse_tree_node *part_spec_list) +t_ft_stat init_catalog(t_catalog *part_catalog) +{ + return (ft_rbtree_init(part_catalog, sizeof(t_part), cmp_parts_void)); +} + +t_ft_stat insert_into_catalog(t_catalog *part_catalog, t_part *part) +{ + return (ft_rbtree_insert(part_catalog, part) != success); +} + +void free_catalog(t_catalog *part_catalog) +{ + ft_rbtree_free(part_catalog, free_part_void); + return ; +} + + +int extract_used_names(t_catalog *part_catalog, t_parse_tree_node *part_spec_list) { int res; t_part new_part; @@ -350,7 +368,7 @@ int extract_used_names(t_rbtree *part_catalog, t_parse_tree_node *part_spec_list used_parts = ft_cget_node_child(used_parts, 0); } res = res || write_used_part(&new_part, ft_cget_node_child(used_parts, 0)); - res = res || ft_rbtree_insert(part_catalog, &new_part) != success; + res = res || insert_into_catalog(part_catalog, &new_part); if (res) { free_part(&new_part); @@ -363,14 +381,13 @@ int extract_used_names(t_rbtree *part_catalog, t_parse_tree_node *part_spec_list int construct_new_graph(t_parse_tree_node *parse_tree, __attribute__((unused)) t_vec *nodes, __attribute__((unused)) t_vec *mosfets) { - t_rbtree part_catalog; + t_catalog part_catalog; - ft_rbtree_init(&part_catalog, sizeof(t_part), cmp_parts_void); + init_catalog(&part_catalog); extract_used_names(&part_catalog, parse_tree); - //fill_used_parts(&part_catalog); //build_graphs(&part_catalog, parse_tree); //transfer_main(&part_catalog, nodes, mosfets); - ft_rbtree_free(&part_catalog, free_part_void); + free_catalog(&part_catalog); return (0); } -- 2.30.2