From c37e30beca72f9e54b71e148682a54ce33a38cbe Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 20 Jun 2024 17:50:54 +0200 Subject: [PATCH] Add a stack structure Add a classic FILO stack with push, top, pop and some other functions. It is basically just a wrapper for t_vec. It is added into a new directory ft_struct. I should have made it instead of the ft_arr dir. I may later move t_vec and t_mat to ft_struct. --- Makefile | 9 ++++++++- ft_struct/ft_stack_free.c | 20 ++++++++++++++++++++ ft_struct/ft_stack_init.c | 20 ++++++++++++++++++++ ft_struct/ft_stack_pop.c | 24 ++++++++++++++++++++++++ ft_struct/ft_stack_pop_forget.c | 24 ++++++++++++++++++++++++ ft_struct/ft_stack_push.c | 19 +++++++++++++++++++ ft_struct/ft_stack_top.c | 22 ++++++++++++++++++++++ inc/ft_struct.h | 31 +++++++++++++++++++++++++++++++ inc/libft.h | 3 ++- 9 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 ft_struct/ft_stack_free.c create mode 100644 ft_struct/ft_stack_init.c create mode 100644 ft_struct/ft_stack_pop.c create mode 100644 ft_struct/ft_stack_pop_forget.c create mode 100644 ft_struct/ft_stack_push.c create mode 100644 ft_struct/ft_stack_top.c create mode 100644 inc/ft_struct.h diff --git a/Makefile b/Makefile index b71a204..a33caea 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,14 @@ RM := rm -f INCDIR = ./inc INCLUDE := $(addprefix -I, $(INCDIR)) -SRCDIR := ft_gen ft_math ft_str ft_mem ft_io ft_check ft_conv ft_lst ft_arr ft_parse +SRCDIR := ft_gen ft_math ft_str ft_mem ft_io ft_check ft_conv ft_lst ft_arr ft_parse ft_struct + +SRCstruct:= ft_stack_free.c \ + ft_stack_init.c \ + ft_stack_pop.c \ + ft_stack_pop_forget.c \ + ft_stack_push.c \ + ft_stack_top.c \ SRCparse:= ft_parse.c \ ft_parsing_table_init.c \ diff --git a/ft_struct/ft_stack_free.c b/ft_struct/ft_stack_free.c new file mode 100644 index 0000000..66148f5 --- /dev/null +++ b/ft_struct/ft_stack_free.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:16:56 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:18:38 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" + +void ft_stack_free(t_stack *stack, void (*free_el)(void *)) +{ + ft_vec_free(&stack->vec, free_el); + return ; +} diff --git a/ft_struct/ft_stack_init.c b/ft_struct/ft_stack_init.c new file mode 100644 index 0000000..6e8e0d5 --- /dev/null +++ b/ft_struct/ft_stack_init.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:04:10 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:47:14 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" +#include + +t_ft_stat ft_stack_init(t_stack *stack, size_t el_size) +{ + return (ft_vec_init(&stack->vec, el_size)); +} diff --git a/ft_struct/ft_stack_pop.c b/ft_struct/ft_stack_pop.c new file mode 100644 index 0000000..1fd7db6 --- /dev/null +++ b/ft_struct/ft_stack_pop.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_pop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:06:43 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:47:27 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" + +void *ft_stack_pop(t_stack *stack, void (*free_el)(void *)) +{ + void *res; + + res = ft_stack_top(stack); + if (res) + ft_vec_erase(&stack->vec, stack->vec.size - 1, free_el); + return (res); +} diff --git a/ft_struct/ft_stack_pop_forget.c b/ft_struct/ft_stack_pop_forget.c new file mode 100644 index 0000000..b0b0095 --- /dev/null +++ b/ft_struct/ft_stack_pop_forget.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_pop_forget.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:15:41 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:47:35 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" + +void *ft_stack_pop_forget(t_stack *stack) +{ + void *res; + + res = ft_stack_top(stack); + if (res) + ft_vec_forget(&stack->vec, stack->vec.size - 1); + return (res); +} diff --git a/ft_struct/ft_stack_push.c b/ft_struct/ft_stack_push.c new file mode 100644 index 0000000..9ad3319 --- /dev/null +++ b/ft_struct/ft_stack_push.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:05:28 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:47:51 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" + +t_ft_stat ft_stack_push(t_stack *stack, void *element) +{ + return (ft_vec_append(&stack->vec, element)); +} diff --git a/ft_struct/ft_stack_top.c b/ft_struct/ft_stack_top.c new file mode 100644 index 0000000..36dd701 --- /dev/null +++ b/ft_struct/ft_stack_top.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_stack_top.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:11:34 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:13:19 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_struct.h" +#include "ft_arr.h" +#include + +void *ft_stack_top(t_stack *stack) +{ + if (stack->vec.size == 0) + return (NULL); + return (ft_vec_access(&stack->vec, stack->vec.size - 1)); +} diff --git a/inc/ft_struct.h b/inc/ft_struct.h new file mode 100644 index 0000000..53555bc --- /dev/null +++ b/inc/ft_struct.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_struct.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 16:59:43 by ljiriste #+# #+# */ +/* Updated: 2024/06/20 17:46:28 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STRUCT_H +# define FT_STRUCT_H + +#include "ft_arr.h" +#include + +typedef struct s_stack +{ + t_vec vec; +} t_stack; + +t_ft_stat ft_stack_init(t_stack *stack, size_t el_size); +t_ft_stat ft_stack_push(t_stack *stack, void *element); +void *ft_stack_top(t_stack *stack); +void *ft_stack_pop(t_stack *stack, void (*free_el)(void *)); +void *ft_stack_pop_forget(t_stack *stack); +void ft_stack_free(t_stack *stack, void (*free_el)(void *)); + +#endif //FT_STRUCT_H diff --git a/inc/libft.h b/inc/libft.h index 179b729..283f702 100644 --- a/inc/libft.h +++ b/inc/libft.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ -/* Updated: 2024/06/20 12:45:07 by ljiriste ### ########.fr */ +/* Updated: 2024/06/20 17:21:06 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ # include "ft_io.h" # include "ft_lst.h" # include "ft_arr.h" +# include "ft_struct.h" # include "ft_parse.h" # include "ft_stat.h" -- 2.30.2