change step cb to include payload
This commit is contained in:
parent
996e003d2a
commit
ba9e5c88c0
2
Makefile
2
Makefile
|
@ -43,7 +43,7 @@ all: $(TUI_MONITOR) test;
|
|||
|
||||
clean:
|
||||
$(MAKE) -C $(LFTDIR) clean
|
||||
$(RM) $(ALL_OBJ) $(ALL_DEPENDENCIES)
|
||||
$(RM) $(ALL_OBJ) $(DEPENDENCIES)
|
||||
|
||||
fclean: clean
|
||||
$(MAKE) -C $(LFTDIR) fclean
|
||||
|
|
90
extra/main.c
90
extra/main.c
|
@ -1,77 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <curses.h>
|
||||
|
||||
#include "../src/push_swap_ctx.h"
|
||||
#include "push_swap.h"
|
||||
#include "tui.h"
|
||||
|
||||
static WINDOW *wgraph_a;
|
||||
static WINDOW *wgraph_b;
|
||||
static WINDOW *wopname;
|
||||
typedef struct {
|
||||
WINDOW *wstack_a;
|
||||
WINDOW *wstack_b;
|
||||
WINDOW *wop;
|
||||
int stack_max;
|
||||
int stack_min;
|
||||
} t_cb_payload;
|
||||
|
||||
static int max, min;
|
||||
|
||||
void plot_graph_a(t_stack_view s)
|
||||
int push_swap_cb(t_stack_view a, t_stack_view b, t_push_swap_op op, void *payload)
|
||||
{
|
||||
plot_graph(wgraph_a, s, max, min, 2);
|
||||
}
|
||||
t_cb_payload* p = payload;
|
||||
|
||||
void plot_graph_b(t_stack_view s)
|
||||
{
|
||||
plot_graph(wgraph_b, s, max, min, 2);
|
||||
}
|
||||
|
||||
void display_op(t_push_swap_op op)
|
||||
{
|
||||
print_op(wopname, op);
|
||||
}
|
||||
|
||||
void refresh_windows()
|
||||
{
|
||||
wrefresh(wgraph_a);
|
||||
wrefresh(wgraph_b);
|
||||
wrefresh(wopname);
|
||||
}
|
||||
|
||||
int push_swap_cb(t_stack_view a, t_stack_view b, t_push_swap_op op)
|
||||
{
|
||||
plot_graph_a(a);
|
||||
plot_graph_b(b);
|
||||
display_op(op);
|
||||
refresh_windows();
|
||||
plot_graph(p->wstack_a, a, p->stack_max, p->stack_min);
|
||||
plot_graph(p->wstack_b, b, p->stack_max, p->stack_min);
|
||||
print_op(p->wop, op);
|
||||
wrefresh(p->wop);
|
||||
wrefresh(p->wstack_a);
|
||||
wrefresh(p->wstack_b);
|
||||
usleep(250000);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int c, char **v)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
if (c < 2)
|
||||
return (-1);
|
||||
return -1;
|
||||
t_stack *stack_a = push_swap_stack_from_args(&v[1], c - 1);
|
||||
if (!stack_a)
|
||||
return (-2);
|
||||
return -2;
|
||||
t_push_swap_ctx *ctx = push_swap_ctx_new(stack_a);
|
||||
if (!ctx)
|
||||
return (-3);
|
||||
stack_int_maxmin(ctx->stack_a, &max, &min);
|
||||
return -3;
|
||||
|
||||
WINDOW* wstack_a;
|
||||
WINDOW* wstack_b;
|
||||
WINDOW* wopname;
|
||||
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
refresh();
|
||||
wgraph_a = newwin(LINES - 3, COLS / 2 - 1, 0, 0);
|
||||
wgraph_b = newwin(LINES - 3, COLS / 2 - 1, 0, COLS / 2);
|
||||
box(wgraph_b, 0, 0);
|
||||
// refresh();
|
||||
wstack_a = newwin(LINES - 3, COLS / 2 - 1, 0, 0);
|
||||
wstack_b = newwin(LINES - 3, COLS / 2 - 1, 0, COLS / 2);
|
||||
wopname = newwin(3, COLS, LINES - 3, 0);
|
||||
box(wgraph_b, 0, 0);
|
||||
push_swap_sort_step(ctx, push_swap_cb);
|
||||
|
||||
t_cb_payload payload = {
|
||||
wstack_a,
|
||||
wstack_b,
|
||||
wopname,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
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_ctx_del(ctx);
|
||||
delwin(wgraph_a);
|
||||
delwin(wstack_a);
|
||||
delwin(wstack_b);
|
||||
delwin(wopname);
|
||||
endwin();
|
||||
return (0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@
|
|||
|
||||
#include <ncurses.h>
|
||||
|
||||
void plot_graph(WINDOW *win, t_stack_view s, int max, int min, int offset)
|
||||
void plot_graph(WINDOW *win, t_stack_view s, int max, int min)
|
||||
{
|
||||
const int offset = 2;
|
||||
|
||||
if (!s.raw)
|
||||
return;
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ static inline float linear(float a, float b, float t)
|
|||
return (t - a) / (b - a);
|
||||
}
|
||||
|
||||
void plot_graph(WINDOW *w, t_stack_view s, int max, int min, int offset);
|
||||
void plot_graph(WINDOW *w, t_stack_view s, int max, int min);
|
||||
void print_op(WINDOW *w, t_push_swap_op);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -36,11 +36,13 @@ typedef enum e_push_swap_op
|
|||
t_push_swap_ctx *push_swap_ctx_new(t_stack *stack_a);
|
||||
void *push_swap_ctx_del(t_push_swap_ctx *ctx);
|
||||
t_stack *push_swap_stack_from_args(char **args, int count);
|
||||
void push_swap_step_cb(t_push_swap_ctx *ctx,
|
||||
int (*cb)(t_stack_view, t_stack_view, t_push_swap_op,
|
||||
void *), void *cb_payload);
|
||||
|
||||
int push_swap_sort(t_push_swap_ctx *ctx);
|
||||
int push_swap_sort_step(t_push_swap_ctx *ctx,
|
||||
int (*step_cb)(t_stack_view, t_stack_view, t_push_swap_op));
|
||||
void push_swap_exec(t_push_swap_ctx *ctx, t_push_swap_op op);
|
||||
int push_swap_sort_step(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);
|
||||
|
|
|
@ -9,15 +9,22 @@
|
|||
/* Updated: foo bar by Andrea Blanke */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <push_swap.h>
|
||||
#include "push_swap_ctx.h"
|
||||
#include "push_swap.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <libft.h>
|
||||
|
||||
#include "ft_stack.h"
|
||||
#include "push_swap_ctx.h"
|
||||
static inline int default_step_cb(t_stack_view _, t_stack_view __,
|
||||
t_push_swap_op ___, void *____)
|
||||
{
|
||||
(void)_;
|
||||
(void)__;
|
||||
(void)___;
|
||||
(void)____;
|
||||
return (1);
|
||||
}
|
||||
|
||||
t_push_swap_ctx *push_swap_ctx_new(t_stack *stack_a)
|
||||
{
|
||||
|
@ -32,6 +39,8 @@ t_push_swap_ctx *push_swap_ctx_new(t_stack *stack_a)
|
|||
if (!ctx->stack_b)
|
||||
return (push_swap_ctx_del(ctx));
|
||||
ctx->stack_a = stack_a;
|
||||
ctx->cb = default_step_cb;
|
||||
ctx->cb_payload = NULL;
|
||||
return (ctx);
|
||||
}
|
||||
|
||||
|
@ -58,4 +67,17 @@ t_stack *push_swap_stack_from_args(char **args, int count)
|
|||
}
|
||||
return (s);
|
||||
}
|
||||
|
||||
void push_swap_step_cb(t_push_swap_ctx *ctx,
|
||||
int (*cb)(t_stack_view, t_stack_view, t_push_swap_op, void *),
|
||||
void *payload)
|
||||
{
|
||||
if (!cb)
|
||||
{
|
||||
ctx->cb = default_step_cb;
|
||||
return ;
|
||||
}
|
||||
ctx->cb = cb;
|
||||
ctx->cb_payload = payload;
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
||||
|
|
|
@ -12,12 +12,14 @@
|
|||
#ifndef PUSH_SWAP_CTX_H
|
||||
# define PUSH_SWAP_CTX_H
|
||||
|
||||
typedef struct s_stack t_stack;
|
||||
# include <push_swap.h>
|
||||
|
||||
typedef struct s_push_swap_ctx
|
||||
{
|
||||
t_stack *stack_a;
|
||||
t_stack *stack_b;
|
||||
int (*cb)(t_stack_view, t_stack_view, t_push_swap_op, void *);
|
||||
void *cb_payload;
|
||||
} t_push_swap_ctx;
|
||||
|
||||
#endif
|
||||
|
|
|
@ -11,84 +11,27 @@
|
|||
/* ************************************************************************** */
|
||||
#include <push_swap.h>
|
||||
|
||||
#include "ft_stack.h"
|
||||
#include "push_swap_ctx.h"
|
||||
|
||||
static inline int default_step_cb(t_stack_view _, t_stack_view __,
|
||||
t_push_swap_op ___);
|
||||
|
||||
int push_swap_sort(t_push_swap_ctx *ctx)
|
||||
{
|
||||
return (push_swap_sort_step(ctx, default_step_cb));
|
||||
return (push_swap_sort_step(ctx));
|
||||
}
|
||||
|
||||
int push_swap_sort_step(t_push_swap_ctx *ctx,
|
||||
int (*cb)(t_stack_view, t_stack_view, t_push_swap_op))
|
||||
int push_swap_sort_step(t_push_swap_ctx *ctx)
|
||||
{
|
||||
size_t i;
|
||||
const t_push_swap_op ops[] = {
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_rr,
|
||||
ps_op_ss,
|
||||
ps_op_pa,
|
||||
ps_op_pa,
|
||||
ps_op_pa,
|
||||
ps_op_pa,
|
||||
ps_op_pa,
|
||||
ps_op_sa,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_rr,
|
||||
ps_op_pa,
|
||||
ps_op_sa,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_rrr,
|
||||
ps_op_pb,
|
||||
ps_op_pa,
|
||||
ps_op_pa,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pb,
|
||||
ps_op_pa,
|
||||
ps_op_sa,
|
||||
ps_op_pa,
|
||||
ps_op_rr,
|
||||
ps_op_rr,
|
||||
ps_op_sa,
|
||||
ps_op_sa,
|
||||
ps_op_pa,
|
||||
ps_op_pa,
|
||||
ps_op_pa,
|
||||
ps_op_rr,
|
||||
ps_op_ss,
|
||||
ps_op_pb,
|
||||
ps_op_rrr,
|
||||
};
|
||||
|
||||
i = 0;
|
||||
while (i < sizeof (ops) / sizeof (ops[0]))
|
||||
while (!stack_int_issorted(ctx->stack_a))
|
||||
{
|
||||
push_swap_exec(ctx, ops[i]);
|
||||
cb(stack_view(ctx->stack_a), stack_view(ctx->stack_b), ops[i]);
|
||||
i++;
|
||||
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);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void 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)
|
||||
{
|
||||
if (op == ps_op_pa)
|
||||
push_swap_pa(ctx);
|
||||
|
@ -112,14 +55,7 @@ void push_swap_exec(t_push_swap_ctx *ctx, t_push_swap_op op)
|
|||
push_swap_sb(ctx);
|
||||
else if (op == ps_op_ss)
|
||||
push_swap_ss(ctx);
|
||||
}
|
||||
|
||||
static inline int default_step_cb(t_stack_view _, t_stack_view __,
|
||||
t_push_swap_op ___)
|
||||
{
|
||||
(void)_;
|
||||
(void)__;
|
||||
(void)___;
|
||||
return (1);
|
||||
return (ctx->cb(stack_view(ctx->stack_a), stack_view(ctx->stack_b),
|
||||
op, ctx->cb_payload));
|
||||
}
|
||||
// vi: noet sw=4 ts=4:
|
||||
|
|
Loading…
Reference in New Issue