some stupid sorting for testing
This commit is contained in:
parent
2d8b9cb19b
commit
603d808d0e
24
extra/main.c
24
extra/main.c
|
@ -1,11 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <curses.h>
|
||||
|
||||
#include "../src/push_swap_ctx.h"
|
||||
#include "push_swap.h"
|
||||
#include "tui.h"
|
||||
|
||||
typedef struct {
|
||||
|
@ -16,7 +11,16 @@ typedef struct {
|
|||
int stack_min;
|
||||
} t_cb_payload;
|
||||
|
||||
int push_swap_cb(t_stack_view a, t_stack_view b, t_push_swap_op op, void *payload)
|
||||
static void print_stack(t_stack *s)
|
||||
{
|
||||
const t_stack_view view = stack_view(s);
|
||||
|
||||
for (size_t i = 0; i < view.top; i++)
|
||||
printf("%d ", arr_int_get(view.raw, i));
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
static int push_swap_cb(t_stack_view a, t_stack_view b, t_push_swap_op op, void *payload)
|
||||
{
|
||||
t_cb_payload* p = payload;
|
||||
|
||||
|
@ -26,7 +30,7 @@ int push_swap_cb(t_stack_view a, t_stack_view b, t_push_swap_op op, void *payloa
|
|||
wrefresh(p->wop);
|
||||
wrefresh(p->wstack_a);
|
||||
wrefresh(p->wstack_b);
|
||||
usleep(250000);
|
||||
getch();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -63,13 +67,17 @@ 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_cb(stack_view(stack_a), stack_view(ctx->stack_b), ps_op_undefined, &payload);
|
||||
|
||||
push_swap_sort(ctx);
|
||||
getch();
|
||||
|
||||
push_swap_ctx_del(ctx);
|
||||
delwin(wstack_a);
|
||||
delwin(wstack_b);
|
||||
delwin(wopname);
|
||||
endwin();
|
||||
|
||||
print_stack(stack_a);
|
||||
stack_del(stack_a);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "push_swap_ctx.h"
|
||||
#include "push_swap.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
@ -41,12 +40,12 @@ t_push_swap_ctx *push_swap_ctx_new(t_stack *stack_a)
|
|||
ctx->stack_a = stack_a;
|
||||
ctx->cb = default_step_cb;
|
||||
ctx->cb_payload = NULL;
|
||||
ctx->sinfo = NULL;
|
||||
return (ctx);
|
||||
}
|
||||
|
||||
void *push_swap_ctx_del(t_push_swap_ctx *ctx)
|
||||
{
|
||||
stack_del(ctx->stack_a);
|
||||
stack_del(ctx->stack_b);
|
||||
free(ctx);
|
||||
return (NULL);
|
||||
|
|
|
@ -14,10 +14,13 @@
|
|||
|
||||
# include <push_swap.h>
|
||||
|
||||
# include "push_swap_info.h"
|
||||
|
||||
typedef struct s_push_swap_ctx
|
||||
{
|
||||
t_stack *stack_a;
|
||||
t_stack *stack_b;
|
||||
t_stack_info *sinfo;
|
||||
int (*cb)(t_stack_view, t_stack_view, t_push_swap_op, void *);
|
||||
void *cb_payload;
|
||||
} t_push_swap_ctx;
|
||||
|
|
|
@ -19,7 +19,7 @@ t_stackitem_info *stack_info_getitem_bypos(const t_stack_info *sinfo,
|
|||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < stack_height(sinfo->stack))
|
||||
while (i < stack_maxheight(sinfo->stack))
|
||||
{
|
||||
if (sinfo->items[i].pos == pos)
|
||||
return (&sinfo->items[i]);
|
||||
|
@ -34,7 +34,7 @@ t_stackitem_info *stack_info_getitem_bytarget(const t_stack_info *sinfo,
|
|||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < stack_height(sinfo->stack))
|
||||
while (i < stack_maxheight(sinfo->stack))
|
||||
{
|
||||
if (sinfo->items[i].target_pos == pos)
|
||||
return (&sinfo->items[i]);
|
||||
|
@ -49,7 +49,7 @@ t_stackitem_info *stack_info_getitem_byvalue(const t_stack_info *sinfo,
|
|||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < stack_height(sinfo->stack))
|
||||
while (i < stack_maxheight(sinfo->stack))
|
||||
{
|
||||
if (sinfo->items[i].val == val)
|
||||
return (&sinfo->items[i]);
|
||||
|
|
|
@ -58,7 +58,8 @@ static int index_items(t_stack_info *sinfo)
|
|||
while (ctx.i < stack_height(sinfo->stack))
|
||||
{
|
||||
ctx.val = arr_int_get(stack_view(sinfo->stack).raw, ctx.i);
|
||||
if ((ctx.idx == 0 && ctx.val >= ctx.last_min)
|
||||
if ((ctx.idx == 0 && ctx.val >= ctx.last_min
|
||||
&& ctx.val <= ctx.cur_min)
|
||||
|| (ctx.val > ctx.last_min && ctx.val <= ctx.cur_min))
|
||||
{
|
||||
ctx.cur_min = ctx.val;
|
||||
|
|
|
@ -11,22 +11,65 @@
|
|||
/* ************************************************************************** */
|
||||
#include "push_swap_ctx.h"
|
||||
|
||||
#include "push_swap_info.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct s_range
|
||||
{
|
||||
size_t start;
|
||||
size_t end;
|
||||
size_t step;
|
||||
} t_range;
|
||||
|
||||
static void direct_sort(t_push_swap_ctx *ctx);
|
||||
static t_range init_range(const t_stack *s);
|
||||
|
||||
int push_swap_sort(t_push_swap_ctx *ctx)
|
||||
{
|
||||
t_stack_info *sinfo = stack_info_new(ctx->stack_a);
|
||||
t_range range;
|
||||
|
||||
if (!sinfo)
|
||||
ctx->sinfo = stack_info_new(ctx->stack_a);
|
||||
if (!ctx->sinfo)
|
||||
return (-1);
|
||||
range = init_range(ctx->stack_a);
|
||||
while (!stack_int_issorted(ctx->stack_a))
|
||||
{
|
||||
push_swap_exec(ctx, ps_op_ra);
|
||||
push_swap_exec(ctx, ps_op_pb);
|
||||
push_swap_exec(ctx, ps_op_ra);
|
||||
push_swap_exec(ctx, ps_op_pa);
|
||||
if (range.step < 2)
|
||||
{
|
||||
direct_sort(ctx);
|
||||
}
|
||||
}
|
||||
stack_info_del(sinfo);
|
||||
ctx->sinfo = stack_info_del(ctx->sinfo);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void direct_sort(t_push_swap_ctx *ctx)
|
||||
{
|
||||
t_stackitem_info *item;
|
||||
size_t rank;
|
||||
size_t i;
|
||||
|
||||
rank = 0;
|
||||
while (rank < stack_maxheight(ctx->stack_a))
|
||||
{
|
||||
stack_info_update(ctx->sinfo, 0);
|
||||
item = stack_info_getitem_bytarget(ctx->sinfo, rank);
|
||||
i = 0;
|
||||
while (i++ < item->pos)
|
||||
push_swap_exec(ctx, ps_op_rra);
|
||||
push_swap_exec(ctx, ps_op_pb);
|
||||
rank++;
|
||||
}
|
||||
while (rank-- > 0)
|
||||
push_swap_exec(ctx, ps_op_pa);
|
||||
}
|
||||
|
||||
static t_range init_range(const t_stack *s)
|
||||
{
|
||||
t_range r;
|
||||
|
||||
r.start = 0;
|
||||
r.step = stack_height(s) / 10;
|
||||
r.end = r.step;
|
||||
return (r);
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
||||
|
|
Loading…
Reference in New Issue