Compare commits
2 Commits
ba9e5c88c0
...
99eef45b90
Author | SHA1 | Date |
---|---|---|
hsv2 | 99eef45b90 | |
hsv2 | 9ddca35605 |
85
Makefile
85
Makefile
|
@ -1,43 +1,50 @@
|
|||
NAME = push_swap
|
||||
CFLAGS = -Ofast -march=native -pipe -Wall -Wextra -Wpedantic
|
||||
CPPFLAGS = -I$(LFTDIR) -Iinclude -MMD -MP
|
||||
LDFLAGS = -L$(LFTDIR) -L$(DIST_DIR)
|
||||
SANITIZE = -fsanitize=address,undefined
|
||||
DEBUG = -O0 -g3 -ggdb
|
||||
DIST_DIR = dist/
|
||||
LFTDIR = libft/
|
||||
LFT = $(addprefix $(LFTDIR), libft.a)
|
||||
NAME = push_swap
|
||||
CFLAGS = -Ofast -march=native -pipe -Wall -Wextra -Wpedantic
|
||||
CPPFLAGS = -I$(LFTDIR) -Iinclude -MMD -MP
|
||||
LDFLAGS = -L$(LFTDIR) -L$(DIST_DIR)
|
||||
SANITIZE = -fsanitize=address,undefined
|
||||
DEBUG = -O0 -g3 -ggdb
|
||||
DIST_DIR = dist/
|
||||
LFTDIR = libft/
|
||||
LFT = $(addprefix $(LFTDIR), libft.a)
|
||||
|
||||
ARRAY_SRC = src/ft_array_func.c \
|
||||
src/ft_array_func_extra.c \
|
||||
src/ft_array_life.c
|
||||
LARRAY = $(addprefix $(DIST_DIR), libftarray.a)
|
||||
STACK_SRC = src/stack_attr.c \
|
||||
src/stack_common_ops.c \
|
||||
src/stack_extra_ops.c \
|
||||
src/stack_life.c \
|
||||
src/int_stack.c
|
||||
LSTACK = $(addprefix $(DIST_DIR), libftstack.a)
|
||||
PUSHWAP_SRC = src/push_swap.c \
|
||||
src/push_swap_ctx.c \
|
||||
src/push_swap_op.c \
|
||||
src/push_swap_sort.c \
|
||||
src/rotate.c \
|
||||
src/reverse_rotate.c
|
||||
LPUSHSWAP = $(addprefix $(DIST_DIR), libpushswap.a)
|
||||
ARRAY_SRC = src/ft_array_func.c \
|
||||
src/ft_array_func_extra.c \
|
||||
src/ft_array_life.c
|
||||
LARRAY = $(addprefix $(DIST_DIR), libftarray.a)
|
||||
STACK_SRC = src/stack_attr.c \
|
||||
src/stack_common_ops.c \
|
||||
src/stack_extra_ops.c \
|
||||
src/stack_life.c \
|
||||
src/int_stack.c
|
||||
LSTACK = $(addprefix $(DIST_DIR), libftstack.a)
|
||||
PUSHWAP_SRC = src/push_swap.c \
|
||||
src/push_swap_ctx.c \
|
||||
src/push_swap_exec.c \
|
||||
src/push_swap_info.c \
|
||||
src/push_swap_info_item.c \
|
||||
src/push_swap_info_update.c \
|
||||
src/push_swap_op.c \
|
||||
src/push_swap_sort.c \
|
||||
src/rotate.c \
|
||||
src/reverse_rotate.c
|
||||
LPUSHSWAP = $(addprefix $(DIST_DIR), libpushswap.a)
|
||||
|
||||
TEST_STACK_SRC = test/stack.c
|
||||
TEST_STACK = $(addprefix $(DIST_DIR), test_stack)
|
||||
TEST_STACK_SRC = test/stack.c
|
||||
TEST_STACK = $(addprefix $(DIST_DIR), test_stack)
|
||||
|
||||
TUI_MONITOR_SRC = extra/main.c \
|
||||
extra/tui.c
|
||||
TUI_MONITOR = $(addprefix $(DIST_DIR), pushdemon)
|
||||
TEST_PUSHSWAP_SRC = test/push_swap.c
|
||||
TEST_PUSHSWAP = $(addprefix $(DIST_DIR), test_push_swap)
|
||||
|
||||
ALL_TEST = $(TEST_STACK)
|
||||
ALL_SRC = $(ARRAY_SRC) $(STACK_SRC) $(TEST_STACK_SRC) \
|
||||
$(TUI_MONITOR_SRC) $(PUSHWAP_SRC)
|
||||
ALL_OBJ = $(ALL_SRC:.c=.o)
|
||||
DEPENDENCIES = $(ALL_SRC:.c=.d)
|
||||
TUI_MONITOR_SRC = extra/main.c \
|
||||
extra/tui.c
|
||||
TUI_MONITOR = $(addprefix $(DIST_DIR), pushdemon)
|
||||
|
||||
ALL_TEST = $(TEST_STACK) $(TEST_PUSHSWAP)
|
||||
ALL_SRC = $(ARRAY_SRC) $(STACK_SRC) $(TEST_STACK_SRC) \
|
||||
$(TUI_MONITOR_SRC) $(PUSHWAP_SRC) $(TEST_PUSHSWAP_SRC)
|
||||
ALL_OBJ = $(ALL_SRC:.c=.o)
|
||||
DEPENDENCIES = $(ALL_SRC:.c=.d)
|
||||
|
||||
all: $(TUI_MONITOR) test;
|
||||
|
||||
|
@ -47,7 +54,7 @@ clean:
|
|||
|
||||
fclean: clean
|
||||
$(MAKE) -C $(LFTDIR) fclean
|
||||
$(RM) $(DIST_DIR)
|
||||
$(RM) -r $(DIST_DIR)
|
||||
|
||||
debug: CFLAGS += $(DEBUG)
|
||||
debug: | fclean all
|
||||
|
@ -76,6 +83,10 @@ $(TEST_STACK): $(TEST_STACK_SRC:.c=.o) $(LSTACK) $(LARRAY) $(LFT)
|
|||
mkdir -p $(DIST_DIR)
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
$(TEST_PUSHSWAP): $(TEST_PUSHSWAP_SRC:.c=.o) $(LPUSHSWAP) $(LSTACK) $(LARRAY) $(LFT)
|
||||
mkdir -p $(DIST_DIR)
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
$(TUI_MONITOR): $(TUI_MONITOR_SRC:.c=.o) $(LPUSHSWAP) $(LSTACK) $(LARRAY) $(LFT)
|
||||
mkdir -p $(DIST_DIR)
|
||||
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(shell pkg-config --libs ncurses)
|
||||
|
|
|
@ -63,7 +63,7 @@ int main(int c, char **v)
|
|||
stack_int_maxmin(ctx->stack_a, &payload.stack_max, &payload.stack_min);
|
||||
push_swap_step_cb(ctx, push_swap_cb, &payload);
|
||||
|
||||
push_swap_sort_step(ctx);
|
||||
push_swap_sort(ctx);
|
||||
|
||||
push_swap_ctx_del(ctx);
|
||||
delwin(wstack_a);
|
||||
|
|
|
@ -41,9 +41,8 @@ void push_swap_step_cb(t_push_swap_ctx *ctx,
|
|||
void *), void *cb_payload);
|
||||
|
||||
int push_swap_sort(t_push_swap_ctx *ctx);
|
||||
int push_swap_sort_step(t_push_swap_ctx *ctx);
|
||||
int push_swap_exec(t_push_swap_ctx *ctx, t_push_swap_op op);
|
||||
|
||||
int push_swap_exec(t_push_swap_ctx *ctx, t_push_swap_op op);
|
||||
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);
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* By: ablanken <ablanken at student dot 42barcelona dot com */
|
||||
/* */
|
||||
/* Created: foo bar by ablanken */
|
||||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "push_swap_ctx.h"
|
||||
|
||||
int push_swap_exec(t_push_swap_ctx *ctx, t_push_swap_op op)
|
||||
{
|
||||
if (op == ps_op_pa)
|
||||
push_swap_pa(ctx);
|
||||
else if (op == ps_op_pb)
|
||||
push_swap_pb(ctx);
|
||||
else if (op == ps_op_ra)
|
||||
push_swap_ra(ctx);
|
||||
else if (op == ps_op_rb)
|
||||
push_swap_rb(ctx);
|
||||
else if (op == ps_op_rr)
|
||||
push_swap_rr(ctx);
|
||||
else if (op == ps_op_rra)
|
||||
push_swap_rra(ctx);
|
||||
else if (op == ps_op_rrb)
|
||||
push_swap_rrb(ctx);
|
||||
else if (op == ps_op_rrr)
|
||||
push_swap_rrr(ctx);
|
||||
else if (op == ps_op_sa)
|
||||
push_swap_sa(ctx);
|
||||
else if (op == ps_op_sb)
|
||||
push_swap_sb(ctx);
|
||||
else if (op == ps_op_ss)
|
||||
push_swap_ss(ctx);
|
||||
return (ctx->cb(stack_view(ctx->stack_a), stack_view(ctx->stack_b),
|
||||
op, ctx->cb_payload));
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* By: ablanken <ablanken at student dot 42barcelona dot com */
|
||||
/* */
|
||||
/* Created: foo bar by ablanken */
|
||||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "push_swap_info.h"
|
||||
#include "ft_stack.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
t_stack_info *stack_info_new(const t_stack *s)
|
||||
{
|
||||
t_stack_info *const info = malloc(sizeof (t_stack_info));
|
||||
|
||||
if (!info)
|
||||
return (NULL);
|
||||
if (stack_int_maxmin(s, &info->max, &info->min) < 0)
|
||||
return (stack_info_del(info));
|
||||
info->items = malloc(sizeof (t_stackitem_info) * stack_maxheight(s));
|
||||
if (!info->items)
|
||||
return (stack_info_del(info));
|
||||
info->stack = s;
|
||||
// TODO: populate that item info thing first, otherwise BOOM
|
||||
stack_info_update(info, 1);
|
||||
return (info);
|
||||
}
|
||||
|
||||
void *stack_info_del(t_stack_info *i)
|
||||
{
|
||||
free(i->items);
|
||||
free(i);
|
||||
return (NULL);
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
|
@ -0,0 +1,47 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* By: ablanken <ablanken at student dot 42barcelona dot com */
|
||||
/* */
|
||||
/* Created: foo bar by ablanken */
|
||||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#ifndef PUSH_SWAP_INFO_H
|
||||
# define PUSH_SWAP_INFO_H
|
||||
|
||||
# include <stddef.h>
|
||||
|
||||
typedef struct s_stack t_stack;
|
||||
|
||||
typedef struct s_stackitem_info
|
||||
{
|
||||
int val;
|
||||
size_t pos;
|
||||
size_t target_pos;
|
||||
} t_stackitem_info;
|
||||
|
||||
typedef struct s_stack_info
|
||||
{
|
||||
t_stackitem_info *items;
|
||||
int max;
|
||||
int min;
|
||||
const t_stack *stack;
|
||||
} t_stack_info;
|
||||
|
||||
t_stack_info *stack_info_new(const t_stack *s);
|
||||
void *stack_info_del(t_stack_info *sinfo);
|
||||
|
||||
t_stack_info *stack_info_update(t_stack_info *sinfo, int reindex);
|
||||
|
||||
t_stackitem_info *stack_info_getitem_bypos(const t_stack_info *sinfo,
|
||||
size_t pos);
|
||||
t_stackitem_info *stack_info_getitem_bytarget(const t_stack_info *sinfo,
|
||||
size_t pos);
|
||||
t_stackitem_info *stack_info_getitem_byvalue(const t_stack_info *sinfo,
|
||||
int value);
|
||||
|
||||
#endif
|
||||
// vi: noet sw=4 ts=4:
|
|
@ -0,0 +1,60 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* By: ablanken <ablanken at student dot 42barcelona dot com */
|
||||
/* */
|
||||
/* Created: foo bar by ablanken */
|
||||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "push_swap_info.h"
|
||||
|
||||
#include <ft_stack.h>
|
||||
|
||||
t_stackitem_info *stack_info_getitem_bypos(const t_stack_info *sinfo,
|
||||
size_t pos)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < stack_height(sinfo->stack))
|
||||
{
|
||||
if (sinfo->items[i].pos == pos)
|
||||
return (&sinfo->items[i]);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_stackitem_info *stack_info_getitem_bytarget(const t_stack_info *sinfo,
|
||||
size_t pos)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < stack_height(sinfo->stack))
|
||||
{
|
||||
if (sinfo->items[i].target_pos == pos)
|
||||
return (&sinfo->items[i]);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_stackitem_info *stack_info_getitem_byvalue(const t_stack_info *sinfo,
|
||||
int val)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < stack_height(sinfo->stack))
|
||||
{
|
||||
if (sinfo->items[i].val == val)
|
||||
return (&sinfo->items[i]);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
|
@ -0,0 +1,72 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* By: ablanken <ablanken at student dot 42barcelona dot com */
|
||||
/* */
|
||||
/* Created: foo bar by ablanken */
|
||||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "push_swap_info.h"
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <ft_stack.h>
|
||||
|
||||
static int index_items(t_stack_info *sinfo);
|
||||
|
||||
t_stack_info *stack_info_update(t_stack_info *sinfo, int reindex)
|
||||
{
|
||||
size_t pos;
|
||||
t_stack_view sview;
|
||||
|
||||
if (reindex)
|
||||
if (index_items(sinfo) < 0)
|
||||
return (NULL);
|
||||
pos = 0;
|
||||
sview = stack_view(sinfo->stack);
|
||||
while (sview.top-- > 0)
|
||||
{
|
||||
stack_info_getitem_byvalue(sinfo, sview.top)->pos = pos;
|
||||
pos++;
|
||||
}
|
||||
return (sinfo);
|
||||
}
|
||||
|
||||
typedef struct s_idx_fn_ctx
|
||||
{
|
||||
int cur_min;
|
||||
int last_min;
|
||||
size_t idx;
|
||||
int val;
|
||||
size_t i;
|
||||
} t_idx_fn_ctx;
|
||||
|
||||
static int index_items(t_stack_info *sinfo)
|
||||
{
|
||||
t_idx_fn_ctx ctx;
|
||||
|
||||
ctx.last_min = INT_MIN;
|
||||
ctx.cur_min = INT_MAX;
|
||||
ctx.idx = 0;
|
||||
while (ctx.idx < stack_height(sinfo->stack))
|
||||
{
|
||||
ctx.i = ctx.idx;
|
||||
while (ctx.i < stack_height(sinfo->stack))
|
||||
{
|
||||
ctx.val = arr_int_get(stack_view(sinfo->stack).raw, ctx.i);
|
||||
if (ctx.val >= ctx.last_min && ctx.val <= ctx.cur_min)
|
||||
{
|
||||
ctx.cur_min = ctx.val;
|
||||
}
|
||||
ctx.i++;
|
||||
}
|
||||
stack_info_getitem_byvalue(sinfo, ctx.cur_min)->target_pos = ctx.idx;
|
||||
ctx.last_min = ctx.cur_min;
|
||||
ctx.idx++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
|
@ -9,18 +9,16 @@
|
|||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include <push_swap.h>
|
||||
|
||||
#include "ft_stack.h"
|
||||
#include "push_swap_ctx.h"
|
||||
|
||||
#include "push_swap_info.h"
|
||||
|
||||
int push_swap_sort(t_push_swap_ctx *ctx)
|
||||
{
|
||||
return (push_swap_sort_step(ctx));
|
||||
}
|
||||
t_stack_info *sinfo = stack_info_new(ctx->stack_a);
|
||||
|
||||
int push_swap_sort_step(t_push_swap_ctx *ctx)
|
||||
{
|
||||
if (!sinfo)
|
||||
return (-1);
|
||||
while (!stack_int_issorted(ctx->stack_a))
|
||||
{
|
||||
push_swap_exec(ctx, ps_op_ra);
|
||||
|
@ -28,34 +26,7 @@ int push_swap_sort_step(t_push_swap_ctx *ctx)
|
|||
push_swap_exec(ctx, ps_op_ra);
|
||||
push_swap_exec(ctx, ps_op_pa);
|
||||
}
|
||||
stack_info_del(sinfo);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int push_swap_exec(t_push_swap_ctx *ctx, t_push_swap_op op)
|
||||
{
|
||||
if (op == ps_op_pa)
|
||||
push_swap_pa(ctx);
|
||||
else if (op == ps_op_pb)
|
||||
push_swap_pb(ctx);
|
||||
else if (op == ps_op_ra)
|
||||
push_swap_ra(ctx);
|
||||
else if (op == ps_op_rb)
|
||||
push_swap_rb(ctx);
|
||||
else if (op == ps_op_rr)
|
||||
push_swap_rr(ctx);
|
||||
else if (op == ps_op_rra)
|
||||
push_swap_rra(ctx);
|
||||
else if (op == ps_op_rrb)
|
||||
push_swap_rrb(ctx);
|
||||
else if (op == ps_op_rrr)
|
||||
push_swap_rrr(ctx);
|
||||
else if (op == ps_op_sa)
|
||||
push_swap_sa(ctx);
|
||||
else if (op == ps_op_sb)
|
||||
push_swap_sb(ctx);
|
||||
else if (op == ps_op_ss)
|
||||
push_swap_ss(ctx);
|
||||
return (ctx->cb(stack_view(ctx->stack_a), stack_view(ctx->stack_b),
|
||||
op, ctx->cb_payload));
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* */
|
||||
/* By: ablanken <ablanken at student dot 42barcelona dot com */
|
||||
/* */
|
||||
/* Created: foo bar by ablanken */
|
||||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include <push_swap.h>
|
||||
|
||||
int main(int c, char **v)
|
||||
{
|
||||
t_stack *stack_a;
|
||||
|
||||
if (c < 2)
|
||||
return (-1);
|
||||
stack_a = push_swap_stack_from_args(&v[1], c - 1);
|
||||
if (!stack_a)
|
||||
return (-2);
|
||||
t_push_swap_ctx *ctx = push_swap_ctx_new(stack_a);
|
||||
if (!ctx)
|
||||
return (-3);
|
||||
push_swap_sort(ctx);
|
||||
push_swap_ctx_del(ctx);
|
||||
stack_del(stack_a);
|
||||
return (0);
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
Loading…
Reference in New Issue