Add a stack structure
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 20 Jun 2024 15:50:54 +0000 (17:50 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 20 Jun 2024 15:50:54 +0000 (17:50 +0200)
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
ft_struct/ft_stack_free.c [new file with mode: 0644]
ft_struct/ft_stack_init.c [new file with mode: 0644]
ft_struct/ft_stack_pop.c [new file with mode: 0644]
ft_struct/ft_stack_pop_forget.c [new file with mode: 0644]
ft_struct/ft_stack_push.c [new file with mode: 0644]
ft_struct/ft_stack_top.c [new file with mode: 0644]
inc/ft_struct.h [new file with mode: 0644]
inc/libft.h

index b71a204a755331e16e23935cf179957ba714aad7..a33caea5c911099c930d38801b27c6b3e8df6e7b 100644 (file)
--- 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 (file)
index 0000000..66148f5
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_stack_free.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 (file)
index 0000000..6e8e0d5
--- /dev/null
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_stack_init.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 <sys/types.h>
+
+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 (file)
index 0000000..1fd7db6
--- /dev/null
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_stack_pop.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 (file)
index 0000000..b0b0095
--- /dev/null
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_stack_pop_forget.c                              :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 (file)
index 0000000..9ad3319
--- /dev/null
@@ -0,0 +1,19 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_stack_push.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 (file)
index 0000000..36dd701
--- /dev/null
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_stack_top.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 <stddef.h>
+
+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 (file)
index 0000000..53555bc
--- /dev/null
@@ -0,0 +1,31 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_struct.h                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 <sys/types.h>
+
+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
index 179b7295b25c26ebc2503dd4063e9c7bc2a25b05..283f70211b230b3f5d507bbcbbdf381e2aacc6e8 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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"