Use a typedef and interface for part catalog
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Thu, 14 Aug 2025 08:03:44 +0000 (10:03 +0200)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sat, 16 Aug 2025 10:59:40 +0000 (12:59 +0200)
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

index e3c2e2698f447e5291ccf3c594bcc09e8ba1fe85..99c85b3c056a1df25022138709142c6436bd7365 100644 (file)
@@ -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);
 }