some visuals of pushswap that doesn't sort

This commit is contained in:
hsv2 2022-09-26 22:59:06 +02:00
parent 4c4b0f7a15
commit e4ef15e189
15 changed files with 361 additions and 108 deletions

View File

@ -3,7 +3,7 @@ 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 -Og -g3 -ggdb
DEBUG = -O0 -g3 -ggdb
DIST_DIR = dist/
LFTDIR = libft/
LFT = $(addprefix $(LFTDIR), libft.a)
@ -18,6 +18,12 @@ STACK_SRC = src/stack_attr.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_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)
@ -27,11 +33,12 @@ TUI_MONITOR_SRC = extra/main.c \
TUI_MONITOR = $(addprefix $(DIST_DIR), pushdemon)
ALL_TEST = $(TEST_STACK)
ALL_SRC = $(ARRAY_SRC) $(STACK_SRC) $(TEST_STACK_SRC) $(TUI_MONITOR_SRC)
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)
all: $(TUI_MONITOR);
all: $(TUI_MONITOR) test;
clean:
$(MAKE) -C $(LFTDIR) clean
@ -41,6 +48,14 @@ fclean: clean
$(MAKE) -C $(LFTDIR) fclean
$(RM) $(ALL_TEST) $(TUI_MONITOR)
debug: CFLAGS += $(DEBUG)
debug: | fclean all
test: $(ALL_TEST)
sanitize: CFLAGS += $(DEBUG) $(SANITIZE)
sanitize: | fclean all
$(LFT):
$(MAKE) -C $(LFTDIR)
@ -50,15 +65,15 @@ $(LARRAY): $(ARRAY_SRC:.c=.o)
$(LSTACK): $(STACK_SRC:.c=.o)
$(AR) -rs $@ $^
test: CFLAGS += $(DEBUG)
test: | clean $(ALL_TEST)
$(LPUSHSWAP): $(PUSHWAP_SRC:.c=.o)
$(AR) -rs $@ $^
$(TEST_STACK): $(TEST_STACK_SRC:.c=.o) $(LSTACK) $(LARRAY) $(LFT)
mkdir -p $(DIST_DIR)
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
$(TUI_MONITOR): $(TUI_MONITOR_SRC:.c=.o) $(LSTACK) $(LARRAY) $(LFT)
$(TUI_MONITOR): $(TUI_MONITOR_SRC:.c=.o) $(LPUSHSWAP) $(LSTACK) $(LARRAY) $(LFT)
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(shell pkg-config --libs ncurses)
.PHONY: all clean fclean test $(LFT);
.PHONY: all clean debug fclean test sanitize;
-include $(DEPENDENCIES)

View File

@ -1,69 +1,74 @@
#include <curses.h>
#include <push_swap.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "ft_stack.h"
#include <curses.h>
#include <push_swap.h>
#include "../src/push_swap_ctx.h"
#include "tui.h"
#define STACK_SIZE 20
static WINDOW *graph_a;
static WINDOW *graph_b;
static int max, min;
t_stack *genstack(void)
{
t_stack *s = stack_new(STACK_SIZE, sizeof (int));
for (int i = 0; i < STACK_SIZE; i++)
{
int val = rand() - rand();
stack_push(s, &val);
}
return s;
}
void plot_graph_a(t_stack_view s)
{
plot_graph(graph_a, s, max, min, 2);
wrefresh(graph_a);
}
void plot_graph_b(t_stack_view s)
{
plot_graph(graph_b, s, max, min, 2);
wrefresh(graph_b);
}
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);
usleep(250000);
return 1;
}
int main(void)
{
t_stack *sa = stack_new(80, sizeof (int));
if (!sa)
return -1;
srand(time(NULL));
t_push_swap_ctx *ctx = NULL;
while (!ctx)
ctx = push_swap_ctx_new(genstack());
stack_int_maxmin(ctx->stack_a, &max, &min);
initscr();
cbreak();
noecho();
refresh();
WINDOW *graph = newwin(0, 0, 0, 0);
box(graph, 0, 0);
int max, min;
for (int val = 0; val < 80; val++)
{
stack_push(sa, &val);
int_stack_maxmin(sa, &max, &min);
plot_graph(graph, stack_view(sa), max, min, 2);
wrefresh(graph);
usleep(50000);
}
for (int i = 0; i < 100; i++)
{
stack_rrotate(sa, i % 3);
plot_graph(graph, stack_view(sa), max, min, 2);
wrefresh(graph);
usleep(50000);
stack_lrotate(sa, i % 13);
plot_graph(graph, stack_view(sa), max, min, 2);
wrefresh(graph);
usleep(50000);
stack_rrotate(sa, i % 5);
plot_graph(graph, stack_view(sa), max, min, 2);
wrefresh(graph);
usleep(50000);
}
stack_del(sa);
delwin(graph);
int h, w;
getmaxyx(stdscr, h, w);
graph_a = newwin(LINES, COLS / 2 - 1, 0, 0);
box(graph_a, 0, 0);
graph_b = newwin(LINES, COLS / 2 - 1, 0, COLS / 2);
box(graph_b, 0, 0);
push_swap_sort_step(ctx, push_swap_cb);
delwin(graph_a);
endwin();
}
/*
void plot_graph(WINDOW *win, const t_stack *s)
{
int w;
int h;
getmaxyx(win, h, w);
for (size_t x = 0; x < (size_t)w - 2 && x < stack_height(s); x++)
{
int *val = arr_get(stack_view(s).raw, x).item;
for (size_t y = 0; val && y < (size_t)h - 2; y++)
{
if (*val >= y)
mvwaddch(win, h - y - 2, x + 1, ACS_BLOCK);
else
mvwaddch(win, h - y - 2, x + 1, ' ');
}
}
}
*/

View File

@ -6,7 +6,7 @@
void plot_graph(WINDOW *win, const t_stack_view s, int max, int min, int offset)
{
if (!s.top)
if (!s.raw)
return;
int h, w;
@ -16,12 +16,13 @@ void plot_graph(WINDOW *win, const t_stack_view s, int max, int min, int offset)
for (int x = 0; x <= w; x++)
{
const int value = arr_get_int(s.raw, lerp(0, s.top - 1, linear(0, w, x)));
const size_t item_n = lerp(0, s.maxheight, linear(0, w, x));
const int value = arr_int_get(s.raw, item_n);
const int bar_h = lerp(0, h, linear(min, max, value));
for (int y = 0; y <= h; y++)
{
mvwaddch(win, h - y + offset, x + offset, ' ');
if (bar_h >= y)
if (item_n < s.top && bar_h >= y)
mvwaddch(win, h - y + offset, x + offset, ACS_BLOCK);
}
}

View File

@ -33,7 +33,8 @@ t_array *arr_swap(t_array *a, size_t idxa, size_t idxb);
size_t arr_size(const t_array *a);
size_t arr_itemsize(const t_array *a);
int arr_get_int(const t_array *a, size_t idx);
int arr_int_get(const t_array *a, size_t idx);
int arr_int_uniq(const t_array *a, size_t start, size_t end);
#endif
// vi: noet sw=4 ts=4:

View File

@ -19,6 +19,7 @@
typedef struct s_stack t_stack;
typedef struct s_stack_view
{
size_t maxheight;
size_t top;
const t_array *raw;
} t_stack_view;
@ -47,8 +48,9 @@ size_t stack_itemsize(const t_stack *s);
size_t stack_maxheight(const t_stack *s);
t_stack_view stack_view(const t_stack *s);
int int_stack_maxmin(const t_stack *s, int *max, int *min);
int int_stack_issorted(const t_stack *s);
int stack_int_maxmin(const t_stack *s, int *max, int *min);
int stack_int_issorted(const t_stack *s);
int stack_int_uniq(const t_stack *s);
#endif
// vi: noet sw=4 ts=4:

View File

@ -15,27 +15,42 @@
# include <ft_stack.h>
typedef struct s_push_swap_ctx
typedef struct s_push_swap_ctx t_push_swap_ctx;
typedef enum e_push_swap_op
{
unsigned int instruction_counter;
t_stack *stack_a;
t_stack *stack_b;
} t_push_swap_ctx;
ps_op_pa,
ps_op_pb,
ps_op_ra,
ps_op_rb,
ps_op_rr,
ps_op_rra,
ps_op_rrb,
ps_op_rrr,
ps_op_sa,
ps_op_sb,
ps_op_ss,
} t_push_swap_op;
int push_swap_main(t_stack *a);
int push_swap_sort(t_push_swap_ctx *ctx);
t_push_swap_ctx *push_swap_ctx_new(t_stack *stack_a);
void *push_swap_ctx_del(t_push_swap_ctx *ctx);
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_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);
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);
#endif

View File

@ -52,11 +52,38 @@ t_array *arr_swap(t_array *a, size_t idxa, size_t idxb)
return (a);
}
int arr_get_int(const t_array *a, size_t idx)
int arr_int_get(const t_array *a, size_t idx)
{
if (a->itemsize != sizeof (int) || idx >= a->itemcount)
return (INT_MIN);
return (*(int *)arr_get(a, idx).item);
}
int arr_int_uniq(const t_array *a, size_t start, size_t end)
{
size_t i;
size_t tmp;
if (a->itemsize != sizeof (int))
return (INT_MIN);
if (start > end)
{
tmp = start;
start = end;
end = tmp;
}
while (start < end - 1)
{
i = start + 1;
while (i < end)
{
if (arr_int_get(a, start) == arr_int_get(a, i))
return (0);
i++;
}
start++;
}
return (1);
}
// vi: noet sw=4 ts=4:

View File

@ -15,7 +15,7 @@
#include <limits.h>
int int_stack_maxmin(const t_stack *s, int *max, int *min)
int stack_int_maxmin(const t_stack *s, int *max, int *min)
{
int value;
t_stack_view view;
@ -27,7 +27,7 @@ int int_stack_maxmin(const t_stack *s, int *max, int *min)
view = stack_view(s);
while (view.top--)
{
value = arr_get_int(view.raw, view.top);
value = arr_int_get(view.raw, view.top);
if (value > *max)
*max = value;
if (value < *min)
@ -36,7 +36,7 @@ int int_stack_maxmin(const t_stack *s, int *max, int *min)
return (0);
}
int int_stack_issorted(const t_stack *s)
int stack_int_issorted(const t_stack *s)
{
int prev;
t_stack_view view;
@ -47,10 +47,15 @@ int int_stack_issorted(const t_stack *s)
prev = INT_MIN;
while (view.top--)
{
if (prev > arr_get_int(view.raw, view.top))
if (prev > arr_int_get(view.raw, view.top))
return (0);
}
return (1);
}
int stack_int_uniq(const t_stack *s)
{
return (arr_int_uniq(stack_view(s).raw, 0, stack_height(s)));
}
// vi: noet sw=4 ts=4:

View File

@ -12,37 +12,32 @@
#include <push_swap.h>
#include "ft_stack.h"
#include "push_swap_ctx.h"
void push_swap_pa(t_push_swap_ctx *ctx)
{
stack_push(ctx->stack_a, stack_pop(ctx->stack_b));
ctx->instruction_counter++;
stack_push(ctx->stack_a, stack_pop(ctx->stack_b).item);
}
void push_swap_pb(t_push_swap_ctx *ctx)
{
stack_push(ctx->stack_b, stack_pop(ctx->stack_a));
ctx->instruction_counter++;
stack_push(ctx->stack_b, stack_pop(ctx->stack_a).item);
}
void push_swap_sa(t_push_swap_ctx *ctx)
{
stack_swap(ctx->stack_a);
ctx->instruction_counter++;
}
void push_swap_sb(t_push_swap_ctx *ctx)
{
stack_swap(ctx->stack_b);
ctx->instruction_counter++;
}
void push_swap_ss(t_push_swap_ctx *ctx)
{
stack_swap(ctx->stack_a);
stack_swap(ctx->stack_b);
ctx->instruction_counter++;
}
// vi: noet sw=4 ts=4:

44
src/push_swap_ctx.c Normal file
View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <push_swap.h>
#include <stdlib.h>
#include <libft.h>
#include "push_swap_ctx.h"
t_push_swap_ctx *push_swap_ctx_new(t_stack *stack_a)
{
t_push_swap_ctx *ctx;
if (!stack_int_uniq(stack_a))
return (NULL);
ctx = ft_calloc(1, sizeof (t_push_swap_ctx));
if (!ctx)
return (NULL);
ctx->stack_b = stack_new(stack_height(stack_a), stack_itemsize(stack_a));
if (!ctx->stack_b)
return (push_swap_ctx_del(ctx));
ctx->stack_a = stack_a;
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);
}
// vi: noet sw=4 ts=4:

24
src/push_swap_ctx.h Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#ifndef PUSH_SWAP_CTX_H
# define PUSH_SWAP_CTX_H
typedef struct s_stack t_stack;
typedef struct s_push_swap_ctx
{
t_stack *stack_a;
t_stack *stack_b;
} t_push_swap_ctx;
#endif
// vi: noet sw=4 ts=4:

125
src/push_swap_sort.c Normal file
View File

@ -0,0 +1,125 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <push_swap.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));
}
int push_swap_sort_step(t_push_swap_ctx *ctx,
int (*cb)(t_stack_view, t_stack_view, t_push_swap_op))
{
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]))
{
push_swap_exec(ctx, ops[i]);
cb(stack_view(ctx->stack_a), stack_view(ctx->stack_b), ops[i]);
i++;
}
return (0);
}
void 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);
}
static inline int default_step_cb(t_stack_view _, t_stack_view __,
t_push_swap_op ___)
{
(void)_;
(void)__;
(void)___;
return (1);
}
// vi: noet sw=4 ts=4:

View File

@ -12,25 +12,22 @@
#include <push_swap.h>
#include "ft_stack.h"
#include "push_swap_ctx.h"
void push_swap_rra(t_push_swap_ctx *ctx)
{
stack_rrotate(ctx->stack_a, 1);
ctx->instruction_counter++;
}
void push_swap_rrb(t_push_swap_ctx *ctx)
{
stack_rrotate(ctx->stack_b, 1);
ctx->instruction_counter++;
}
void push_swap_rrr(t_push_swap_ctx *ctx)
{
stack_rrotate(ctx->stack_a, 1);
stack_rrotate(ctx->stack_b, 1);
ctx->instruction_counter++;
}
// vi: noet sw=4 ts=4:

View File

@ -12,25 +12,22 @@
#include <push_swap.h>
#include "ft_stack.h"
#include "push_swap_ctx.h"
void push_swap_ra(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_a, 1);
ctx->instruction_counter++;
}
void push_swap_rb(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_b, 1);
ctx->instruction_counter++;
}
void push_swap_rr(t_push_swap_ctx *ctx)
{
stack_lrotate(ctx->stack_a, 1);
stack_lrotate(ctx->stack_b, 1);
ctx->instruction_counter++;
}
// vi: noet sw=4 ts=4:

View File

@ -31,7 +31,7 @@ size_t stack_maxheight(const t_stack *s)
t_stack_view stack_view(const t_stack *s)
{
return ((t_stack_view){s->top, s->impl});
return ((t_stack_view){stack_maxheight(s), s->top, s->impl});
}
// vi: noet sw=4 ts=4: