wip debugging buggy stack info
This commit is contained in:
parent
f18beb81e5
commit
0a8c8fa57a
|
@ -1,11 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
ARGC=$(((${RANDOM} % 10 + 1) * 20))
|
||||
ARGC=$(((${RANDOM} % 10 + 1) * 7))
|
||||
ARGV=()
|
||||
|
||||
for i in `seq 1 $ARGC`
|
||||
do
|
||||
ARGV+=($(($RANDOM % 5000)))
|
||||
ARGV+=($(($RANDOM % 500)))
|
||||
done
|
||||
|
||||
echo ${ARGV[*]}
|
||||
|
|
|
@ -9,44 +9,76 @@
|
|||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "push_swap.h"
|
||||
#include "sort.h"
|
||||
|
||||
#include "push_swap_ctx.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static size_t shortest_rot(size_t stack_size, size_t src, size_t dst,
|
||||
int *direction);
|
||||
|
||||
void move_item(t_push_swap_ctx *ctx, t_stack *s, size_t src, size_t dst)
|
||||
{
|
||||
t_stackitem_info *item;
|
||||
size_t rot_count;
|
||||
int rot_dir;
|
||||
|
||||
if ((src == 1 && dst == 0) || (src == 0 && dst == 1))
|
||||
{
|
||||
if (s == ctx->stack_a)
|
||||
push_swap_exec(ctx, ps_op_sa);
|
||||
push_swap_exec(ctx, ps_op_sa); // TODO: refactor to pass stacks as
|
||||
// parameters and use stack
|
||||
// independent ops.
|
||||
else
|
||||
push_swap_exec(ctx, ps_op_sb);
|
||||
return ;
|
||||
}
|
||||
|
||||
stack_info_update(ctx->sinfo, 0);
|
||||
item = stack_info_getitem_bypos(ctx->sinfo, src);
|
||||
if (item->pos > dst)
|
||||
rot_count = shortest_rot(stack_height(s), src, dst, &rot_dir);
|
||||
printf("doing %lu %crot\n", rot_count, rot_dir < 0 ? 'r' : 'l');
|
||||
while (rot_count-- > 0)
|
||||
{
|
||||
while (item->pos-- > dst)
|
||||
if (rot_dir < 0)
|
||||
{
|
||||
if (s == ctx->stack_a)
|
||||
push_swap_exec(ctx, ps_op_rra);
|
||||
if (ctx->stack_a == s)
|
||||
push_swap_exec(ctx, ps_op_ra);
|
||||
else
|
||||
push_swap_exec(ctx, ps_op_rrb);
|
||||
push_swap_exec(ctx, ps_op_rb);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (item->pos++ < dst)
|
||||
else
|
||||
{
|
||||
if (s == ctx->stack_a)
|
||||
if (ctx->stack_a == s)
|
||||
push_swap_exec(ctx, ps_op_rra);
|
||||
else
|
||||
push_swap_exec(ctx, ps_op_rrb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static size_t shortest_rot(size_t size, size_t src, size_t dst,
|
||||
int *dir)
|
||||
{
|
||||
size_t ldist;
|
||||
size_t rdist;
|
||||
|
||||
if (src < dst)
|
||||
{
|
||||
ldist = dst - src;
|
||||
rdist = src + size - dst;
|
||||
}
|
||||
else
|
||||
{
|
||||
ldist = dst + size - src;
|
||||
rdist = src - dst;
|
||||
}
|
||||
if (ldist < rdist)
|
||||
{
|
||||
*dir = 1;
|
||||
return (ldist);
|
||||
}
|
||||
*dir = -1;
|
||||
return (rdist);
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
||||
|
|
|
@ -11,9 +11,11 @@
|
|||
/* ************************************************************************** */
|
||||
#include "ft_array.h"
|
||||
#include "push_swap.h"
|
||||
#include "push_swap_info.h"
|
||||
#include "sort.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "push_swap_ctx.h"
|
||||
|
||||
|
@ -24,8 +26,17 @@ void range_presort(t_push_swap_ctx *ctx, size_t target)
|
|||
t_stack_view sviewa;
|
||||
t_stack_view sviewb;
|
||||
size_t i;
|
||||
const size_t range = target;
|
||||
size_t range;
|
||||
|
||||
if (target < 4)
|
||||
{
|
||||
target = 1;
|
||||
range = 4;
|
||||
}
|
||||
else
|
||||
range = target;
|
||||
|
||||
printf("presort starting rank %lu\n", target);
|
||||
while (target)
|
||||
{
|
||||
stack_info_update(ctx->sinfo, 0);
|
||||
|
@ -43,23 +54,25 @@ void range_presort(t_push_swap_ctx *ctx, size_t target)
|
|||
sviewa = stack_view(ctx->stack_a);
|
||||
sviewb = stack_view(ctx->stack_b);
|
||||
i = 0;
|
||||
printf("Searching numbers lesser than %d\n", max);
|
||||
while (i < sviewa.top)
|
||||
{
|
||||
stack_info_update(ctx->sinfo, 0);
|
||||
stack_info_update(ctx->sinfo, 1);
|
||||
item = stack_info_getitem_bypos(ctx->sinfo, i);
|
||||
if (item->val <= max)
|
||||
{
|
||||
printf("Found %d at pos %lu, target %lu\n", item->val, item->pos, item->target_pos);
|
||||
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);
|
||||
// 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++;
|
||||
|
|
|
@ -22,9 +22,10 @@ t_stack_info *stack_info_new(const t_stack *s)
|
|||
|
||||
if (!info)
|
||||
return (NULL);
|
||||
info->items = 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));
|
||||
info->items = malloc(sizeof (t_stackitem_info) * stack_height(s));
|
||||
if (!info->items)
|
||||
return (stack_info_del(info));
|
||||
info->stack = s;
|
||||
|
|
|
@ -21,7 +21,7 @@ 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, 5);
|
||||
range_presort(ctx, stack_height(ctx->stack_a) / 10);
|
||||
ctx->sinfo = stack_info_del(ctx->sinfo);
|
||||
return (0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue