From ba33cb63c868ad6f37e15bc4882e1ee257429576 Mon Sep 17 00:00:00 2001 From: hsv2 Date: Sat, 24 Sep 2022 23:07:27 +0200 Subject: [PATCH] push swap specific func wip --- include/push_swap.h | 39 +++++++++++++++++++++++++++++++++++ src/push_swap.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ src/reverse_rotate.c | 36 +++++++++++++++++++++++++++++++++ src/rotate.c | 36 +++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 include/push_swap.h create mode 100644 src/push_swap.c create mode 100644 src/reverse_rotate.c create mode 100644 src/rotate.c diff --git a/include/push_swap.h b/include/push_swap.h new file mode 100644 index 0000000..aad3b95 --- /dev/null +++ b/include/push_swap.h @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* */ +/* */ +/* */ +/* By: ablanken + +typedef struct s_push_swap_ctx +{ + unsigned int instruction_counter; + t_stack *stack_a; + t_stack *stack_b; +} t_push_swap_ctx; + +void push_swap_pa(t_push_swap_ctx *ctx); +void push_swap_pb(t_push_swap_ctx *ctx); +void push_swap_ra(t_push_swap_ctx *ctx); +void push_swap_rb(t_push_swap_ctx *ctx); +void push_swap_rr(t_push_swap_ctx *ctx); +void push_swap_rra(t_push_swap_ctx *ctx); +void push_swap_rrb(t_push_swap_ctx *ctx); +void push_swap_rrr(t_push_swap_ctx *ctx); +void push_swap_sa(t_push_swap_ctx *ctx); +void push_swap_sb(t_push_swap_ctx *ctx); +void push_swap_ss(t_push_swap_ctx *ctx); + +#endif + +// vi: noet sw=4 ts=4: diff --git a/src/push_swap.c b/src/push_swap.c new file mode 100644 index 0000000..8449890 --- /dev/null +++ b/src/push_swap.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* */ +/* */ +/* */ +/* By: ablanken + +#include "ft_stack.h" + +void push_swap_pa(t_push_swap_ctx *ctx) +{ + stack_push(ctx->stack_a, stack_pop(ctx->stack_b)); + ctx->instruction_counter++; +} + +void push_swap_pb(t_push_swap_ctx *ctx) +{ + stack_push(ctx->stack_b, stack_pop(ctx->stack_a)); + ctx->instruction_counter++; +} + +void push_swap_sa(t_push_swap_ctx *ctx) +{ + stack_swap(ctx->stack_a); + ctx->instruction_counter++; +} + +void push_swap_sb(t_push_swap_ctx *ctx) +{ + stack_swap(ctx->stack_b); + ctx->instruction_counter++; +} + +void push_swap_ss(t_push_swap_ctx *ctx) +{ + stack_swap(ctx->stack_a); + stack_swap(ctx->stack_b); + ctx->instruction_counter++; +} + +// vi: noet sw=4 ts=4: diff --git a/src/reverse_rotate.c b/src/reverse_rotate.c new file mode 100644 index 0000000..e4a6f54 --- /dev/null +++ b/src/reverse_rotate.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* */ +/* */ +/* */ +/* By: ablanken + +#include "ft_stack.h" + +void push_swap_rra(t_push_swap_ctx *ctx) +{ + stack_rrotate(ctx->stack_a, 1); + ctx->instruction_counter++; +} + +void push_swap_rrb(t_push_swap_ctx *ctx) +{ + stack_rrotate(ctx->stack_b, 1); + ctx->instruction_counter++; +} + +void push_swap_rrr(t_push_swap_ctx *ctx) +{ + stack_rrotate(ctx->stack_a, 1); + stack_rrotate(ctx->stack_b, 1); + ctx->instruction_counter++; +} + +// vi: noet sw=4 ts=4: diff --git a/src/rotate.c b/src/rotate.c new file mode 100644 index 0000000..b092cea --- /dev/null +++ b/src/rotate.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* */ +/* */ +/* */ +/* By: ablanken + +#include "ft_stack.h" + +void push_swap_ra(t_push_swap_ctx *ctx) +{ + stack_lrotate(ctx->stack_a, 1); + ctx->instruction_counter++; +} + +void push_swap_rb(t_push_swap_ctx *ctx) +{ + stack_lrotate(ctx->stack_b, 1); + ctx->instruction_counter++; +} + +void push_swap_rr(t_push_swap_ctx *ctx) +{ + stack_lrotate(ctx->stack_a, 1); + stack_lrotate(ctx->stack_b, 1); + ctx->instruction_counter++; +} + +// vi: noet sw=4 ts=4: