Update Libft, switch to using IDs for objects
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Wed, 18 Dec 2024 12:14:36 +0000 (13:14 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Wed, 18 Dec 2024 12:14:36 +0000 (13:14 +0100)
The ft_vec_find functions were updated, which necessitated some changes
in the code.
So now the nodes and MOSFETs use unique IDs instead of just their index
in the main vectors. Some hardening was done while switching.
Some minor bugs might also be present in this commit.

12 files changed:
Libft
inc/FET_sim.h
src/build_helper.c
src/c_addfet.c
src/c_addnode.c
src/c_bind.c
src/c_connect.c
src/c_draw.c
src/c_setnode.c
src/colors.c
src/main.c
src/text.c

diff --git a/Libft b/Libft
index 62f583c8ab43754b5265c708243f3f5353ebfa1e..d0985dc4a6e2234daa57f638980ef3769c899767 160000 (submodule)
--- a/Libft
+++ b/Libft
@@ -1 +1 @@
-Subproject commit 62f583c8ab43754b5265c708243f3f5353ebfa1e
+Subproject commit d0985dc4a6e2234daa57f638980ef3769c899767
index 2163e36a7d6cdf3398f20ba619a534b2f3349648..602be8cc9c223465109b01f46f97be93d5597a42 100644 (file)
@@ -26,23 +26,26 @@ typedef enum e_terminal
        gate,
 }      t_terminal;
 
+typedef enum e_type
+{
+       p = 'p',
+       n = 'n',
+}      t_type;
+
+typedef unsigned long t_id;
+
 typedef struct s_node
 {
-       size_t  id;
+       t_id    id;
        int             checked;
        t_state state;
        t_state set_state;
        t_vec   connected;
 }                      t_node;
 
-typedef enum e_type
-{
-       p = 'p',
-       n = 'n',
-}      t_type;
-
 typedef struct s_mosfet
 {
+       t_id    id;
        int             is_opened;
        t_type  type;
        t_node  *gate;
@@ -103,48 +106,51 @@ typedef struct s_windows
        WINDOW  *border_win;
 }                      t_windows;
 
-void   add_node(t_vec *nodes, t_state set_state);
-void   add_mosfet(t_vec *mosfets, t_type type);
-void   bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal);
-t_node *get_node_of_terminal(t_mosfet *mosfet, t_terminal terminal);
-int            transfer_mosfet(t_mosfet *mosfet, t_node *from, t_node *to);
-int            merge_nodes(t_node *node1, t_node *node2);
+t_node         *add_node(t_vec *nodes, t_state set_state);
+t_mosfet       *add_mosfet(t_vec *mosfets, t_type type);
+t_node         *get_node_by_id(t_vec *nodes, t_id id);
+t_mosfet       *get_mosfet_by_id(t_vec *mosfets, t_id id);
+void           bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal);
+t_node         *get_node_of_terminal(t_mosfet *mosfet, t_terminal terminal);
+int                    transfer_mosfet(t_mosfet *mosfet, t_node *from, t_node *to);
+int                    merge_nodes(t_node *node1, t_node *node2);
 
-void   free_node(void *node);
-int            process_input(t_windows *windows, t_vec *nodes, t_vec *mosfets);
-int            get_input(WINDOW *command_win, t_input *input);
+void           free_node(void *node);
+int                    process_input(t_windows *windows, t_vec *nodes, t_vec *mosfets);
+int                    get_input(WINDOW *command_win, t_input *input);
 
 const char     *state_color_escape(t_state state);
-void           draw_single(WINDOW *command_win, t_vec *mosfets, size_t i);
-
-void   update_nodes(t_vec *nodes);
-int            should_open(t_type type, t_state state);
-int            sim_step(t_vec *nodes, t_vec *mosfets);
-
-void   setup_terminal(t_windows *windows);
-void   clean_terminal(t_windows *windows);
-void   print_start(WINDOW *command_win);
-void   command_not_found(WINDOW *command_win, const char *input);
-void   print_help(WINDOW *command_win, t_command c);
-void   print_index_error(WINDOW *command_win, size_t index, size_t size);
-
-int            construct_input(t_input *input, char **split_inp);
-
-void   init_colors(void);
-
-t_state        simple_resolve_state(t_state s1, t_state s2);
-t_state        resolve_state(t_vec *connected_nodes);
-void   apply_state(t_state state, t_vec *connected_nodes);
-t_state        reduce_state(t_state state);
-
-int    c_addfet(WINDOW *command_win, t_input input, t_vec *mosfets);
-int    c_addnode(WINDOW *command_win, t_input input, t_vec *nodes);
-int    c_bind(t_input input, t_vec *nodes, t_vec *mosfets);
-int    c_connect(t_input input, t_vec *nodes, t_vec *mosfets);
-int    c_draw(WINDOW *command_win, t_input input, t_vec *mosfets);
-int    c_help(WINDOW *command_win, t_input input);
-int    c_next(t_input input, t_vec *nodes, t_vec *mosfets);
-int    c_setnode(WINDOW *command_win, t_input input, t_vec *nodes);
-
-int    schema_mode(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets);
+void           draw_single(WINDOW *command_win, const t_mosfet *mosfet);
+
+void           update_nodes(t_vec *nodes);
+int                    should_open(t_type type, t_state state);
+int                    sim_step(t_vec *nodes, t_vec *mosfets);
+
+void           setup_terminal(t_windows *windows);
+void           clean_terminal(t_windows *windows);
+void           print_start(WINDOW *command_win);
+void           command_not_found(WINDOW *command_win, const char *input);
+void           print_help(WINDOW *command_win, t_command c);
+void           print_node_id_error(WINDOW *command_win, t_id id);
+void           print_mosfet_id_error(WINDOW *command_win, t_id id);
+
+int                    construct_input(t_input *input, char **split_inp);
+
+void           init_colors(void);
+
+t_state                simple_resolve_state(t_state s1, t_state s2);
+t_state                resolve_state(t_vec *connected_nodes);
+void           apply_state(t_state state, t_vec *connected_nodes);
+t_state                reduce_state(t_state state);
+
+int                    c_addfet(WINDOW *command_win, t_input input, t_vec *mosfets);
+int                    c_addnode(WINDOW *command_win, t_input input, t_vec *nodes);
+int                    c_bind(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets);
+int                    c_connect(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets);
+int                    c_draw(WINDOW *command_win, t_input input, t_vec *mosfets);
+int                    c_help(WINDOW *command_win, t_input input);
+int                    c_next(t_input input, t_vec *nodes, t_vec *mosfets);
+int                    c_setnode(WINDOW *command_win, t_input input, t_vec *nodes);
+
+int                    schema_mode(WINDOW *schematics_win, t_vec *nodes, t_vec *mosfets);
 #endif //FET_SIM_H
index a48c3ecc94443788aa41da9e70e4b90135e3fdf5..c0ad362e0d07dd86267b24262560964020c12a2c 100644 (file)
@@ -38,6 +38,46 @@ t_node       *get_node_of_terminal(t_mosfet *mosfet, t_terminal terminal)
        return (NULL);
 }
 
+int    mosfet_identity(const void *v_mosfet1, const void *v_mosfet2)
+{
+       const t_mosfet  *mosfet1 = v_mosfet1;
+       const t_mosfet  *mosfet2 = v_mosfet2;
+
+       return (mosfet1->id - mosfet2->id);
+}
+
+t_mosfet       *get_mosfet_by_id(t_vec *mosfets, t_id id)
+{
+       t_mosfet        wanted_dummy;
+
+       wanted_dummy.id = id;
+       return (ft_vec_find(mosfets, &wanted_dummy, mosfet_identity));
+}
+
+int    node_identity(const void *v_node1, const void *v_node2)
+{
+       const t_node    *node1 = v_node1;
+       const t_node    *node2 = v_node2;
+
+       return (node1->id - node2->id);
+}
+
+t_node *get_node_by_id(t_vec *nodes, t_id id)
+{
+       t_node  wanted_dummy;
+
+       wanted_dummy.id = id;
+       return (ft_vec_find(nodes, &wanted_dummy, node_identity));
+}
+
+int    mosfet_identity_indirect(const void *v_mosfet1_ptr, const void *v_mosfet2_ptr)
+{
+       const t_mosfet *const   *mosfet1 = v_mosfet1_ptr;
+       const t_mosfet *const   *mosfet2 = v_mosfet2_ptr;
+
+       return (mosfet_identity(*mosfet1, *mosfet2));
+}
+
 void   bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal)
 {
        t_node  *old;
@@ -60,37 +100,44 @@ void       bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal)
        {
                mosfet->gate = node;
        }
-       if (old && ft_vec_find_index(&old->connected, &mosfet, &index) == success)
+       if (old && ft_vec_find_index(&old->connected, &mosfet, &index, mosfet_identity_indirect) == success)
        {
                ft_vec_forget(&old->connected, index);
        }
        return ;
 }
 
+static t_id    get_new_id(void)
+{
+       static t_id     id = 0;
+
+       return (id++);
+}
+
 // memset is used to initialize struct padding
-void   add_node(t_vec *nodes, t_state set_state)
+t_node *add_node(t_vec *nodes, t_state set_state)
 {
        t_node  node;
 
-       ft_memset(&node, 0, sizeof(node));
-       node.id = nodes->size;
+       node.id = get_new_id();
        node.checked = 0;
        node.state = set_state;
        node.set_state = set_state;
        ft_vec_init(&node.connected, sizeof(t_mosfet *));
        ft_vec_append(nodes, &node);
-       return ;
+       return (ft_vec_access(nodes, nodes->size - 1));
 }
 
-void   add_mosfet(t_vec *mosfets, t_type type)
+t_mosfet       *add_mosfet(t_vec *mosfets, t_type type)
 {
        t_mosfet        mosfet;
 
+       mosfet.id = get_new_id();
        mosfet.is_opened = 0;
        mosfet.type = type;
        mosfet.gate = NULL;
        mosfet.drain = NULL;
        mosfet.source = NULL;
        ft_vec_append(mosfets, &mosfet);
-       return ;
+       return (ft_vec_access(mosfets, mosfets->size - 1));
 }
index 0491f5279abab2d00ad1bd379213745b713e98e2..47cf85dd3094ebd8356b29de6b5c2cbc664a79b7 100644 (file)
@@ -4,23 +4,26 @@
 
 int    c_addfet(WINDOW *command_win, t_input input, t_vec *mosfets)
 {
-       size_t  i;
+       size_t                  num;
+       const t_mosfet  *mosfet;
 
-       if (input.argc == 1 && input.argv[0].type == type)
+       if (input.argc == 1)
        {
-               add_mosfet(mosfets, input.argv[0].val.type);
-               wprintw(command_win, "Index of added FET:\n\t%lu ", mosfets->size - 1);
+               num = 1;
+               wprintw(command_win, "ID of added FET:\n\t");
        }
-       else if (input.argc == 2 && input.argv[0].type == type && input.argv[1].type == num)
+       else
        {
-               i = 0;
-               wprintw(command_win, "Indeces of added FETs:\n\t");
-               while (i < input.argv[1].val.num)
-               {
-                       wprintw(command_win, "%lu ", mosfets->size);
-                       add_mosfet(mosfets, input.argv[0].val.type);
-                       ++i;
-               }
+               num = input.argv[1].val.num;
+               wprintw(command_win, "IDs of added FETs:\n\t");
+       }
+       while (num > 0)
+       {
+               mosfet = add_mosfet(mosfets, input.argv[0].val.type);
+               if (!mosfet)
+                       return (0);
+               wprintw(command_win, "%lu ", mosfet->id);
+               --num;
        }
        wprintw(command_win, "\n");
        /*else
index 09bf13ade7f565bc395fa685d9dab0d9c3ffe470..182cbdb8ed1240a4dea1c7ef23dac0a0afd8a698 100644 (file)
@@ -5,13 +5,17 @@
 static int add_nodes(WINDOW *command_win, t_vec *nodes, size_t num, t_state state)
 {
        size_t  i;
+       t_node  *node;
 
+       if (num == 1)
+               wprintw(command_win, "ID of added node:\n\t");
+       else
+               wprintw(command_win, "IDs of added nodes:\n\t");
        i = 0;
-       wprintw(command_win, "Indexes of added nodes:\n\t");
        while (i < num)
        {
-               wprintw(command_win, "%lu ", nodes->size);
-               add_node(nodes, state);
+               node = add_node(nodes, state);
+               wprintw(command_win, "%lu ", node->id);
                ++i;
        }
        wprintw(command_win, "\n");
@@ -23,21 +27,12 @@ int c_addnode(WINDOW *command_win, t_input input, t_vec *nodes)
        size_t  new_nodes_num;
        t_state set_state;
 
-       /*if (input.argc > 2
-                       || (input.argc == 1
-                               && input.argv[0].type != num && input.argv[0].type != state)
-                       || (input.argc == 2
-                               && (input.argv[0].type != state || input.argv[1].type != num)))
-       {
-               print_wrong_usage(input);
-               return (1);
-       }*/
        new_nodes_num = 1;
        set_state = unknown;
        if (input.argc == 1 && input.argv[0].type == state)
                set_state = input.argv[0].val.state;
        else if (input.argc == 1)
-               new_nodes_num= input.argv[0].val.num;
+               new_nodes_num = input.argv[0].val.num;
        else if (input.argc == 2)
        {
                set_state = input.argv[0].val.state;
index 87e412d51caae4c1d63d265303a817df0164f09b..3ff755258abbdaabf8b5e0f73f03d893cfacfcb3 100644 (file)
@@ -1,7 +1,8 @@
 #include "FET_sim.h"
 #include "libft.h"
+#include <ncurses.h>
 
-int    c_bind(t_input input, t_vec *nodes, t_vec *mosfets)
+int    c_bind(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets)
 {
        t_node          *node;
        t_mosfet        *mosfet;
@@ -13,8 +14,18 @@ int  c_bind(t_input input, t_vec *nodes, t_vec *mosfets)
                print_wrong_usage(input);
                return (1);
        }*/
-       node = ft_vec_access(nodes, input.argv[0].val.num);
-       mosfet = ft_vec_access(mosfets, input.argv[1].val.num);
+       node = get_node_by_id(nodes, input.argv[0].val.num);
+       if (!node)
+       {
+               print_node_id_error(command_win, input.argv[0].val.num);
+               return (1);
+       }
+       mosfet = get_mosfet_by_id(mosfets, input.argv[1].val.num);
+       if (!mosfet)
+       {
+               print_mosfet_id_error(command_win, input.argv[1].val.num);
+               return (1);
+       }
        term = input.argv[2].val.terminal;
        bind_fet_node(mosfet, node, term);
        return (1);
index 4ed338bcb6bc751145b332c418399f6dcfe8cca3..d9361edbd65c9e89c315328930f81260ac334245 100644 (file)
@@ -1,7 +1,7 @@
 #include "FET_sim.h"
 #include "libft.h"
 
-int    c_connect(t_input input, t_vec *nodes, t_vec *mosfets)
+int    c_connect(WINDOW *command_win, t_input input, t_vec *nodes, t_vec *mosfets)
 {
        t_mosfet        *mosfet1;
        t_node          *term_node1;
@@ -9,9 +9,19 @@ int    c_connect(t_input input, t_vec *nodes, t_vec *mosfets)
        t_node          *term_node2;
        t_node          *new_node;
 
-       mosfet1 = ft_vec_access(mosfets, input.argv[0].val.num);
+       mosfet1 = get_mosfet_by_id(mosfets, input.argv[0].val.num);
+       if (!mosfet1)
+       {
+               print_mosfet_id_error(command_win, input.argv[0].val.num);
+               return (1);
+       }
        term_node1 = get_node_of_terminal(mosfet1, input.argv[1].val.terminal);
-       mosfet2 = ft_vec_access(mosfets, input.argv[2].val.num);
+       mosfet2 = get_mosfet_by_id(mosfets, input.argv[2].val.num);
+       if (!mosfet2)
+       {
+               print_mosfet_id_error(command_win, input.argv[2].val.num);
+               return (1);
+       }
        term_node2 = get_node_of_terminal(mosfet2, input.argv[3].val.terminal);
        if (term_node1 && term_node2)
                return (!merge_nodes(term_node1, term_node2));
@@ -21,8 +31,7 @@ int   c_connect(t_input input, t_vec *nodes, t_vec *mosfets)
                bind_fet_node(mosfet1, term_node2, input.argv[1].val.terminal);
        else
        {
-               add_node(nodes, unknown);
-               new_node = ft_vec_access(nodes, nodes->size - 1);
+               new_node = add_node(nodes, unknown);
                bind_fet_node(mosfet1, new_node, input.argv[1].val.terminal);
                bind_fet_node(mosfet2, new_node, input.argv[3].val.terminal);
        }
index 7760d90bdef2faab3e4df3410fe5d103ccd9e1be..2852ee15cfde54dbb6ea585103dbca5d21a146f8 100644 (file)
@@ -3,20 +3,26 @@
 
 int    c_draw(WINDOW *command_win, t_input input, t_vec *mosfets)
 {
-       size_t  i;
+       size_t                  i;
+       const t_mosfet  *mosfet;
 
        if (input.argc == 0)
        {
                i = 0;
                while (i < mosfets->size)
                {
-                       draw_single(command_win, mosfets, i);
+                       mosfet = ft_vec_caccess(mosfets, i);
+                       draw_single(command_win, ft_vec_caccess(mosfets, i));
                        ++i;
                }
        }
        else if (input.argc == 1 && input.argv[0].type == num)
        {
-               draw_single(command_win, mosfets, input.argv[0].val.num);
+               mosfet = get_mosfet_by_id(mosfets, input.argv[0].val.num);
+               if (!mosfet)
+                       print_mosfet_id_error(command_win, input.argv[0].val.num);
+               else
+                       draw_single(command_win, mosfet);
        }
        /*else
        {
index 6bb50b6598001e59902c8806598e7fd24915f7ee..b095f847a9598ae78ef639b1068f0a32e794022f 100644 (file)
@@ -10,10 +10,10 @@ int c_setnode(WINDOW *command_win, t_input input, t_vec *nodes)
                print_wrong_usage(input);
                return (1);
        }*/
-       node = ft_vec_access(nodes, input.argv[0].val.num);
+       node = get_node_by_id(nodes, input.argv[0].val.num);
        if (!node)
        {
-               print_index_error(command_win, input.argv[0].val.num, nodes->size);
+               print_node_id_error(command_win, input.argv[0].val.num);
                return (1);
        }
        node->set_state = input.argv[1].val.state;
index 01bb7ca1fe0db560c85ef44de78e2fda36b0d4e4..99d9ebab9c1b09e99b3b49dbccfa29cc035dd00c 100644 (file)
@@ -43,7 +43,7 @@ static void   print_in_color(WINDOW *command_win, const char *str, t_nc_color colo
        return ;
 }
 
-static void    draw_top(WINDOW *command_win, t_mosfet *mosfet)
+static void    draw_top(WINDOW *command_win, const t_mosfet *mosfet)
 {
        if (mosfet->source)
        {
@@ -58,7 +58,7 @@ static void   draw_top(WINDOW *command_win, t_mosfet *mosfet)
        return ;
 }
 
-static void    draw_switch(WINDOW *command_win, t_mosfet *mosfet)
+static void    draw_switch(WINDOW *command_win, const t_mosfet *mosfet)
 {
        t_nc_color      color;
 
@@ -85,7 +85,7 @@ static void   draw_switch(WINDOW *command_win, t_mosfet *mosfet)
        return ;
 }
 
-static void    draw_middle(WINDOW *command_win, t_mosfet *mosfet)
+static void    draw_middle(WINDOW *command_win, const t_mosfet *mosfet)
 {
        if (mosfet->gate)
        {
@@ -100,7 +100,7 @@ static void draw_middle(WINDOW *command_win, t_mosfet *mosfet)
        return ;
 }
 
-static void    draw_bottom(WINDOW *command_win, t_mosfet *mosfet)
+static void    draw_bottom(WINDOW *command_win, const t_mosfet *mosfet)
 {
        wprintw(command_win, "      ");
        if (mosfet->drain)
@@ -115,12 +115,10 @@ static void       draw_bottom(WINDOW *command_win, t_mosfet *mosfet)
        return ;
 }
 
-void   draw_single(WINDOW *command_win, t_vec *mosfets, size_t i)
+void   draw_single(WINDOW *command_win, const t_mosfet *mosfet)
 {
-       t_mosfet        *mosfet;
-
-       mosfet = ft_vec_access(mosfets, i);
        draw_top(command_win, mosfet);
        draw_middle(command_win, mosfet);
        draw_bottom(command_win, mosfet);
+       return ;
 }
index 97c302b49e8094887eb8989e88b46316bb3e8252..0aeb31ac229c27aca27f021bffcdd1ab068eb995 100644 (file)
@@ -236,9 +236,9 @@ int process_input(t_windows *windows, t_vec *nodes, t_vec *mosfets)
        else if (input.command == addfet)
                res = c_addfet(windows->command_win, input, mosfets);
        else if (input.command == bind)
-               res = c_bind(input, nodes, mosfets);
+               res = c_bind(windows->command_win, input, nodes, mosfets);
        else if (input.command == connect)
-               res = c_connect(input, nodes, mosfets);
+               res = c_connect(windows->command_win, input, nodes, mosfets);
        else if (input.command == help)
                res = c_help(windows->command_win, input);
        else if (input.command == switch_to_schema)
index 9edb023d54343b698c5a3c3ae93a8b87ba081bcf..f5d43343fd4aff5fce8d98a2939a901c3e7c978c 100644 (file)
@@ -140,17 +140,14 @@ void      print_start(WINDOW *command_win)
        return ;
 }
 
-void   print_index_error(WINDOW *command_win, size_t index, size_t size)
+void   print_node_id_error(WINDOW *command_win, t_id id)
 {
-       if (size > 0)
-       {
-               wprintw(command_win, "This action is invalid as given index %lu "
-                       "is larger then the highest index present %lu.\n",
-                       index, size - 1);
-       }
-       else
-       {
-               wprintw(command_win, "This action is invalid as there is no element yet.\n");
-       }
+       wprintw(command_win, "The provided index %lu does not refer to a node.\n", id);
+       return ;
+}
+
+void   print_mosfet_id_error(WINDOW *command_win, t_id id)
+{
+       wprintw(command_win, "The provided index %lu does not refer to a mosfet.\n", id);
        return ;
 }