works with unexpected results…

This commit is contained in:
hsv2 2022-10-01 18:07:24 +02:00
parent 9cd4af7fe6
commit f18beb81e5
10 changed files with 176 additions and 143 deletions

View File

@ -10,9 +10,8 @@ CPPFLAGS = -I$(LFTDIR) \
-MMD \
-MP
LDFLAGS = -L$(LFTDIR) \
-L$(DIST_DIR) \
-lpushswap \
-lftstack \
-L$(DIST_DIR)
LDLIBS = -lftstack \
-lftarray \
-lft
SANITIZE = -fsanitize=address,undefined
@ -39,10 +38,10 @@ STACK_SRC = $(addprefix src/stack/, \
LSTACK = $(addprefix $(DIST_DIR), libftstack.a)
PUSHWAP_SRC = $(addprefix src/pushswap/, \
exec.c \
move.c \
op.c \
op_push_swap.c \
op_rot.c \
op_rrot.c \
presort.c \
push_swap_ctx.c \
push_swap_info.c \
push_swap_info_item.c \
@ -102,15 +101,15 @@ $(LPUSHSWAP): $(PUSHWAP_SRC:.c=.o)
$(TEST_STACK): $(TEST_STACK_SRC:.c=.o) $(LSTACK) $(LARRAY) $(LFT)
mkdir -p $(DIST_DIR)
$(CC) -o $@ $(TEST_PUSHSWAP_SRC:.c=.o) $(CFLAGS) $(LDFLAGS) $(LDLIBS)
$(CC) -o $@ $(TEST_STACK_SRC:.c=.o) $(CFLAGS) $(LDFLAGS) $(LDLIBS)
$(TEST_PUSHSWAP): $(TEST_PUSHSWAP_SRC:.c=.o) $(LPUSHSWAP) $(LSTACK) $(LARRAY) $(LFT)
mkdir -p $(DIST_DIR)
$(CC) -o $@ $(TEST_PUSHSWAP_SRC:.c=.o) $(CFLAGS) $(LDFLAGS) $(LDLIBS)
$(CC) -o $@ $(TEST_PUSHSWAP_SRC:.c=.o) $(CFLAGS) $(LDFLAGS) -lpushswap $(LDLIBS)
$(TUI_MONITOR): $(TUI_MONITOR_SRC:.c=.o) $(LPUSHSWAP) $(LSTACK) $(LARRAY) $(LFT)
mkdir -p $(DIST_DIR)
$(CC) -o $@ $(TUI_MONITOR_SRC:.c=.o) $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(shell pkg-config --libs ncurses)
$(CC) -o $@ $(TUI_MONITOR_SRC:.c=.o) $(CFLAGS) $(LDFLAGS) -lpushswap $(LDLIBS) $(shell pkg-config --libs ncurses)
.PHONY: all clean debug fclean test sanitize;
-include $(DEPENDENCIES)

11
extra/rand_args.bash Executable file
View File

@ -0,0 +1,11 @@
#!/bin/bash
ARGC=$(((${RANDOM} % 10 + 1) * 20))
ARGV=()
for i in `seq 1 $ARGC`
do
ARGV+=($(($RANDOM % 5000)))
done
echo ${ARGV[*]}

View File

@ -43,19 +43,10 @@ void push_swap_step_cb(t_push_swap_ctx *ctx,
int push_swap_sort(t_push_swap_ctx *ctx);
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);
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);
int push_swap_push(t_stack *src, t_stack *dst);
int push_swap_rot(t_stack *s1, t_stack *s2, int direction);
int push_swap_swap(t_stack *s1, t_stack *s2);
void (*push_swap_fn_from_op(t_push_swap_op op))(t_push_swap_ctx*);
const char *push_swap_name_from_op(t_push_swap_op op);
t_push_swap_op push_swap_op_from_name(const char *opname);

View File

@ -9,32 +9,33 @@
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#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);
push_swap_push(ctx->stack_b, ctx->stack_a);
else if (op == ps_op_pb)
push_swap_pb(ctx);
push_swap_push(ctx->stack_a, ctx->stack_b);
else if (op == ps_op_ra)
push_swap_ra(ctx);
push_swap_rot(ctx->stack_a, NULL, 1);
else if (op == ps_op_rb)
push_swap_rb(ctx);
push_swap_rot(ctx->stack_b, NULL, 1);
else if (op == ps_op_rr)
push_swap_rr(ctx);
push_swap_rot(ctx->stack_a, ctx->stack_b, 1);
else if (op == ps_op_rra)
push_swap_rra(ctx);
push_swap_rot(ctx->stack_a, NULL, -1);
else if (op == ps_op_rrb)
push_swap_rrb(ctx);
push_swap_rot(ctx->stack_b, NULL, -1);
else if (op == ps_op_rrr)
push_swap_rrr(ctx);
push_swap_rot(ctx->stack_a, ctx->stack_b, -1);
else if (op == ps_op_sa)
push_swap_sa(ctx);
push_swap_swap(ctx->stack_a, NULL);
else if (op == ps_op_sb)
push_swap_sb(ctx);
push_swap_swap(ctx->stack_b, NULL);
else if (op == ps_op_ss)
push_swap_ss(ctx);
push_swap_swap(ctx->stack_a, ctx->stack_b);
return (ctx->cb(stack_view(ctx->stack_a), stack_view(ctx->stack_b),
op, ctx->cb_payload));
}

View File

@ -9,25 +9,44 @@
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <push_swap.h>
#include "sort.h"
#include "push_swap_ctx.h"
void push_swap_rra(t_push_swap_ctx *ctx)
void move_item(t_push_swap_ctx *ctx, t_stack *s, size_t src, size_t dst)
{
stack_rrotate(ctx->stack_a, 1);
}
t_stackitem_info *item;
void push_swap_rrb(t_push_swap_ctx *ctx)
{
stack_rrotate(ctx->stack_b, 1);
}
if ((src == 1 && dst == 0) || (src == 0 && dst == 1))
{
if (s == ctx->stack_a)
push_swap_exec(ctx, ps_op_sa);
else
push_swap_exec(ctx, ps_op_sb);
return ;
}
void push_swap_rrr(t_push_swap_ctx *ctx)
{
stack_rrotate(ctx->stack_a, 1);
stack_rrotate(ctx->stack_b, 1);
stack_info_update(ctx->sinfo, 0);
item = stack_info_getitem_bypos(ctx->sinfo, src);
if (item->pos > dst)
{
while (item->pos-- > dst)
{
if (s == ctx->stack_a)
push_swap_exec(ctx, ps_op_rra);
else
push_swap_exec(ctx, ps_op_rrb);
}
}
else
{
while (item->pos++ < dst)
{
if (s == ctx->stack_a)
push_swap_exec(ctx, ps_op_rra);
else
push_swap_exec(ctx, ps_op_rrb);
}
}
}
// vi: noet sw=4 ts=4:

View File

@ -13,33 +13,6 @@
#include <libft.h>
void (*push_swap_fn_from_op(t_push_swap_op op))(t_push_swap_ctx*)
{
if (op == ps_op_pa)
return (push_swap_pa);
else if (op == ps_op_pb)
return (push_swap_pb);
else if (op == ps_op_ra)
return (push_swap_ra);
else if (op == ps_op_rb)
return (push_swap_rb);
else if (op == ps_op_rr)
return (push_swap_rr);
else if (op == ps_op_rra)
return (push_swap_rra);
else if (op == ps_op_rrb)
return (push_swap_rrb);
else if (op == ps_op_rrr)
return (push_swap_rrr);
else if (op == ps_op_sa)
return (push_swap_sa);
else if (op == ps_op_sb)
return (push_swap_sb);
else if (op == ps_op_ss)
return (push_swap_ss);
return (NULL);
}
const char *push_swap_name_from_op(t_push_swap_op op)
{
if (op == ps_op_pa)

View File

@ -14,30 +14,40 @@
#include "push_swap_ctx.h"
void push_swap_pa(t_push_swap_ctx *ctx)
int push_swap_push(t_stack *src, t_stack *dst)
{
stack_push(ctx->stack_a, stack_pop(ctx->stack_b).item);
if (src && dst)
{
if (stack_push(dst, stack_pop(src).item).size)
return (0);
}
return (-1);
}
void push_swap_pb(t_push_swap_ctx *ctx)
int push_swap_swap(t_stack *s1, t_stack *s2)
{
stack_push(ctx->stack_b, stack_pop(ctx->stack_a).item);
if (s1)
if (!stack_swap(s1))
return (-1);
if (s2)
if (!stack_swap(s2))
return (-1);
return (0);
}
void push_swap_sa(t_push_swap_ctx *ctx)
int push_swap_rot(t_stack *s1, t_stack *s2, int dir)
{
stack_swap(ctx->stack_a);
}
t_stack *(*rot)(t_stack *, unsigned int);
void push_swap_sb(t_push_swap_ctx *ctx)
{
stack_swap(ctx->stack_b);
rot = stack_lrotate;
if (dir < 0)
rot = stack_rrotate;
if (s1)
if (!rot(s1, 1))
return (-1);
if (s2)
if (!rot(s2, 1))
return (-1);
return (0);
}
void push_swap_ss(t_push_swap_ctx *ctx)
{
stack_swap(ctx->stack_a);
stack_swap(ctx->stack_b);
}
// vi: noet sw=4 ts=4:

73
src/pushswap/presort.c Normal file
View File

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include "ft_array.h"
#include "push_swap.h"
#include "sort.h"
#include <limits.h>
#include "push_swap_ctx.h"
void range_presort(t_push_swap_ctx *ctx, size_t target)
{
t_stackitem_info *item;
int max;
t_stack_view sviewa;
t_stack_view sviewb;
size_t i;
const size_t range = target;
while (target)
{
stack_info_update(ctx->sinfo, 0);
item = stack_info_getitem_bytarget(ctx->sinfo, target);
if (!item)
{
max = INT_MAX;
target = 0;
}
else
{
max = item->val;
target += range;
}
sviewa = stack_view(ctx->stack_a);
sviewb = stack_view(ctx->stack_b);
i = 0;
while (i < sviewa.top)
{
stack_info_update(ctx->sinfo, 0);
item = stack_info_getitem_bypos(ctx->sinfo, i);
if (item->val <= max)
{
move_item(ctx, ctx->stack_a, item->pos, 0);
if (sviewa.top > 1 && sviewb.top > 1
&& arr_int_get(sviewa.raw, sviewa.top - 1) > arr_int_get(sviewa.raw, sviewa.top - 2)
&& arr_int_get(sviewb.raw, sviewb.top - 1) < arr_int_get(sviewb.raw, sviewb.top - 2))
push_swap_exec(ctx, ps_op_ss);
else if (sviewa.top > 1
&& arr_int_get(sviewa.raw, sviewa.top - 1) > arr_int_get(sviewa.raw, sviewa.top - 2))
push_swap_exec(ctx, ps_op_sa);
else if (sviewb.top > 1
&& arr_int_get(sviewb.raw, sviewb.top - 1) < arr_int_get(sviewb.raw, sviewb.top - 2))
push_swap_exec(ctx, ps_op_sb);
push_swap_exec(ctx, ps_op_pb);
sviewa.top--;
sviewb.top++;
i = 0;
continue;
}
i++;
}
}
}
// vi: noet sw=4 ts=4:

View File

@ -14,52 +14,17 @@
#include <limits.h>
#include <stdlib.h>
static void move_item(t_push_swap_ctx *ctx, t_stack *s,
size_t src, size_t dst);
static void range_presort(t_push_swap_ctx *ctx, int range);
#include "sort.h"
int push_swap_sort(t_push_swap_ctx *ctx)
{
ctx->sinfo = stack_info_new(ctx->stack_a);
if (!ctx->sinfo)
return (-1);
range_presort(ctx, 4);
range_presort(ctx, 5);
ctx->sinfo = stack_info_del(ctx->sinfo);
return (0);
}
static void move_item(t_push_swap_ctx *ctx, t_stack *s, size_t src, size_t dst)
{
t_stackitem_info *item;
stack_info_update(ctx->sinfo, 0);
item = stack_info_getitem_bypos(ctx->sinfo, src);
if (item->pos > dst)
{
while (item->pos-- > dst)
{
if (s == ctx->stack_a)
push_swap_exec(ctx, ps_op_rra);
else
push_swap_exec(ctx, ps_op_rrb);
}
}
else
{
while (item->pos++ < dst)
{
if (s == ctx->stack_a)
push_swap_exec(ctx, ps_op_rra);
else
push_swap_exec(ctx, ps_op_rrb);
}
}
}
static void range_presort(t_push_swap_ctx *ctx, int r)
{
t_stackitem_info *item;
stack_info_update(ctx->sinfo, 0);
}
// vi: noet sw=4 ts=4:

View File

@ -9,25 +9,16 @@
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#ifndef SORT_H
# define SORT_H
#include <push_swap.h>
# include <stddef.h>
#include "push_swap_ctx.h"
typedef struct s_push_swap_ctx t_push_swap_ctx;
typedef struct s_stack t_stack;
void push_swap_ra(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_a, 1);
}
void push_swap_rb(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_b, 1);
}
void push_swap_rr(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_a, 1);
stack_lrotate(ctx->stack_b, 1);
}
void range_presort(t_push_swap_ctx *ctx, size_t range);
void move_item(t_push_swap_ctx *ctx, t_stack *s, size_t sidx, size_t didx);
#endif
// vi: noet sw=4 ts=4: