Compare commits

...

2 Commits

Author SHA1 Message Date
hsv2 6f9be636a0 lot of stuff 2022-09-25 18:47:09 +02:00
hsv2 d6dd2ed52f ignore build artefacts 2022-09-25 15:53:26 +02:00
19 changed files with 409 additions and 109 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.o
*.d
*.a
build
dist

62
Makefile Normal file
View File

@ -0,0 +1,62 @@
NAME = push_swap
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
DIST_DIR = dist/
LFTDIR = libft/
LFT = $(addprefix $(LFTDIR), libft.a)
ARRAY_SRC = src/ft_array_func.c \
src/ft_array_func_extra.c \
src/ft_array_life.c
LARRAY = $(addprefix $(DIST_DIR), libftarray.a)
STACK_SRC = src/stack_attr.c \
src/stack_common_ops.c \
src/stack_extra_ops.c \
src/stack_life.c
LSTACK = $(addprefix $(DIST_DIR), libftstack.a)
TEST_STACK_SRC = test/stack.c
TEST_STACK = $(addprefix $(DIST_DIR), test_stack)
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)
ALL_OBJ = $(ALL_SRC:.c=.o)
DEPENDENCIES = $(ALL_SRC:.c=.d)
all: $(TUI_MONITOR);
clean:
$(MAKE) -C $(LFTDIR) clean
$(RM) $(ALL_OBJ) $(ALL_DEPENDENCIES)
fclean: clean
$(MAKE) -C $(LFTDIR) fclean
$(RM) $(ALL_TEST) $(TUI_MONITOR)
$(LFT):
$(MAKE) -C $(LFTDIR)
$(LARRAY): $(ARRAY_SRC:.c=.o)
$(AR) -rs $@ $^
$(LSTACK): $(STACK_SRC:.c=.o)
$(AR) -rs $@ $^
test: CFLAGS += $(DEBUG)
test: | clean $(ALL_TEST)
$(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)
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(shell pkg-config --libs ncurses)
.PHONY: all clean fclean test $(LFT);
-include $(DEPENDENCIES)

24
extra/main.c Normal file
View File

@ -0,0 +1,24 @@
#include <curses.h>
#include <push_swap.h>
#include "tui.h"
int main(void)
{
t_stack *sa = stack_new(10, sizeof (int));
if (!sa)
return -1;
initscr();
cbreak();
refresh();
WINDOW *graph = newwin(25, 80, (LINES - 25) / 2, (COLS - 80) / 2);
box(graph, 0, 0);
wmove(graph, 1, 1);
wprintw(graph, "Hellow");
wrefresh(graph);
getch();
delwin(graph);
endwin();
}

20
extra/tui.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef TUI_H
#define TUI_H
#include <curses.h>
#include <push_swap.h>
static inline float lerp(float a, float b, float t)
{
return a + t * (b - a);
}
static inline float linear(float a, float b, float t)
{
return (t - a) / (b - a);
}
void plot_graph(WINDOW *w, t_push_swap_ctx ctx);
#endif

37
include/ft_array.h Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#ifndef FT_ARRAY_H
# define FT_ARRAY_H
# include <stddef.h>
# include <ft_array_item.h>
# define ARRAY_NOITEM (t_array_item){0, NULL}
typedef struct s_array t_array;
t_array *arr_new(size_t itemsize, size_t itemcount);
void *arr_del(t_array *a);
t_array_item arr_get(const t_array *a, size_t idx);
t_array_item arr_set(t_array *a, size_t idx, void *item);
t_array *arr_rev(t_array *a, size_t start, size_t end);
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);
#endif
// vi: noet sw=4 ts=4:

25
include/ft_array_item.h Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#ifndef FT_ARRAY_ITEM_H
# define FT_ARRAY_ITEM_H
# include <stddef.h>
typedef struct s_array_item
{
size_t size;
void *item;
} t_array_item;
#endif
// vi: noet sw=4 ts=4:

View File

@ -14,30 +14,38 @@
# include <stddef.h>
# include <ft_array.h>
typedef struct s_stack t_stack;
typedef struct s_stack_view
{
const size_t top;
const t_array *const raw;
} t_stack_view;
// Allocate a stack of c elements of size s
t_stack *stack_new(size_t c, size_t s);
t_stack *stack_new(size_t count, size_t size);
// Free a stack and return NULL
void *stack_del(t_stack *s);
void *stack_del(t_stack *s);
// Get the top item of the stack or NULL on error
void *stack_peek(t_stack *s);
t_array_item stack_peek(const t_stack *s);
// Pop from the top of the stack and return popped item or NULL on error
void *stack_pop(t_stack *s);
t_array_item stack_pop(t_stack *s);
// Push to the top of the stack and return pushed item or NULL on error
void *stack_push(t_stack *s, void *item);
t_array_item stack_push(t_stack *s, void *item);
// Left rotate the stack
t_stack *stack_lrotate(t_stack *s, unsigned int step);
t_stack *stack_lrotate(t_stack *s, unsigned int step);
// Right rotate the stack
t_stack *stack_rrotate(t_stack *s, unsigned int step);
t_stack *stack_rrotate(t_stack *s, unsigned int step);
// Swap the two topmost items of the stack
t_stack *stack_swap(t_stack *s);
t_stack *stack_swap(t_stack *s);
size_t stack_height(t_stack *s);
size_t stack_itemsize(t_stack *s);
size_t stack_maxheight(t_stack *s);
size_t stack_height(const t_stack *s);
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);
#endif
// vi: noet sw=4 ts=4:

0
include/ft_stack_print.h Normal file
View File

View File

@ -22,6 +22,9 @@ typedef struct s_push_swap_ctx
t_stack *stack_b;
} t_push_swap_ctx;
int push_swap_main(t_stack *a);
int push_swap_sort(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);

26
src/ft_array_data.h Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#ifndef FT_ARRAY_DATA_H
# define FT_ARRAY_DATA_H
# include <stddef.h>
struct s_array
{
size_t itemcount;
size_t itemsize;
unsigned char *bytes;
};
#endif
// vi: noet sw=4 ts=4:

46
src/ft_array_func.c Normal file
View File

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* 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 <libft.h>
#include "ft_array_data.h"
t_array_item arr_get(const t_array *a, size_t idx)
{
if (idx < a->itemcount)
return ((t_array_item){a->itemsize, &a->bytes[idx * a->itemsize]});
return (ARRAY_NOITEM);
}
t_array_item arr_set(t_array *a, size_t idx, void *item)
{
if (idx < a->itemcount)
return ((t_array_item) {
a->itemsize,
ft_memmove(&a->bytes[idx * a->itemsize], item, a->itemsize),
});
return (ARRAY_NOITEM);
}
size_t arr_size(const t_array *a)
{
return (a->itemcount);
}
size_t arr_itemsize(const t_array *a)
{
return (a->itemsize);
}
// vi: noet sw=4 ts=4:

54
src/ft_array_func_extra.c Normal file
View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* 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 <stdlib.h>
#include <libft.h>
#include "ft_array_data.h"
#include "ft_array_item.h"
t_array *arr_rev(t_array *a, size_t start, size_t end)
{
while (start < end)
{
if (!arr_swap(a, start++, end--))
return (NULL);
}
return (a);
}
t_array *arr_swap(t_array *a, size_t idxa, size_t idxb)
{
unsigned char tmp;
unsigned char *item_a;
unsigned char *item_b;
size_t offset;
item_a = arr_get(a, idxa).item;
item_b = arr_get(a, idxb).item;
if (!item_a || !item_b)
return (NULL);
offset = 0;
while (offset < a->itemsize)
{
tmp = item_a[offset];
item_a[offset] = item_b[offset];
item_b[offset] = tmp;
offset++;
}
return (a);
}
// vi: noet sw=4 ts=4:

40
src/ft_array_life.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* 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 <stdlib.h>
#include "ft_array_data.h"
t_array *arr_new(size_t isize, size_t icount)
{
t_array *const a = malloc(sizeof (t_array));
if (!a)
return (NULL);
a->bytes = malloc(isize * icount);
if (!a->bytes)
return (arr_del(a));
a->itemsize = isize;
a->itemcount = icount;
return (a);
}
void *arr_del(t_array *a)
{
free(a->bytes);
free(a);
return (NULL);
}
// vi: noet sw=4 ts=4:

View File

@ -14,19 +14,24 @@
#include "stack_data.h"
size_t stack_height(t_stack *s)
size_t stack_height(const t_stack *s)
{
return (s->top / s->itemsize);
return (s->top);
}
size_t stack_itemsize(t_stack *s)
size_t stack_itemsize(const t_stack *s)
{
return (s->itemsize);
return (arr_itemsize(s->impl));
}
size_t stack_maxheight(t_stack *s)
size_t stack_maxheight(const t_stack *s)
{
return (s->maxsize / s->itemsize);
return (arr_size(s->impl));
}
t_stack_view stack_view(const t_stack *s)
{
return ((t_stack_view){s->top, s->impl});
}
// vi: noet sw=4 ts=4:

View File

@ -15,28 +15,25 @@
#include "stack_data.h"
void *stack_peek(t_stack *s)
t_array_item stack_peek(const t_stack *s)
{
if (s->top < 1 || s->top > s->maxsize)
return (NULL);
return (&((char *)(s->items))[s->top - 1]);
if (s->top < 1 || s->top > stack_maxheight(s))
return (ARRAY_NOITEM);
return (arr_get(s->impl, s->top - 1));
}
void *stack_pop(t_stack *s)
t_array_item stack_pop(t_stack *s)
{
if (s->top < 1 || s->top > s->maxsize)
return (NULL);
s->top -= s->itemsize;
return (&((char *)(s->items))[s->top]);
if (s->top < 1 || s->top > stack_maxheight(s))
return (ARRAY_NOITEM);
return (arr_get(s->impl, --(s->top)));
}
void *stack_push(t_stack *s, void *item)
t_array_item stack_push(t_stack *s, void *item)
{
if (s->top >= s->maxsize || !item)
return (NULL);
ft_memmove(&((char *)(s->items))[s->top], item, s->itemsize);
s->top += s->itemsize;
return (item);
if (s->top >= stack_maxheight(s) || !item)
return (ARRAY_NOITEM);
return (arr_set(s->impl, s->top++, item));
}
// vi: noet sw=4 ts=4:

View File

@ -14,12 +14,12 @@
# include <stddef.h>
typedef struct s_array t_array;
typedef struct s_stack
{
size_t itemsize;
size_t maxsize;
size_t top;
void *items;
t_array *impl;
} t_stack;
#endif

View File

@ -14,81 +14,29 @@
#include <stdlib.h>
#include "libft.h"
#include "stack_data.h"
static void *reverse_array(void *start, void *end, size_t itemsize);
t_stack *stack_rrotate(t_stack *s, unsigned int step)
{
step = step % (stack_height(s) - 1);
reverse_array(s->items,
&((char *)(s->items))[s->top - s->itemsize],
s->itemsize);
reverse_array(s->items,
&((char *)(s->items))[(step - 1) * s->itemsize],
s->itemsize);
reverse_array(&((char *)(s->items))[step * s->itemsize],
&((char *)(s->items))[s->top - s->itemsize],
s->itemsize);
arr_rev(s->impl, 0, s->top - 1);
arr_rev(s->impl, 0, step - 1);
arr_rev(s->impl, step, s->top - 1);
return (s);
}
t_stack *stack_lrotate(t_stack *s, unsigned int step)
{
step = step % (stack_height(s) - 1);
reverse_array(s->items,
&((char *)(s->items))[(step - 1) * s->itemsize],
s->itemsize);
reverse_array(&((char *)(s->items))[step * s->itemsize],
&((char *)(s->items))[s->top - s->itemsize],
s->itemsize);
reverse_array(s->items,
&((char *)(s->items))[s->top - s->itemsize],
s->itemsize);
return (s);
if (arr_rev(s->impl, 0, step - 1) && arr_rev(s->impl, step, s->top - 1)
&& arr_rev(s->impl, 0, s->top - 1))
return (s);
return (NULL);
}
t_stack *stack_swap(t_stack *s)
{
unsigned char byte;
size_t i;
unsigned char *pos;
if (stack_height(s) < 2)
if (!arr_swap(s->impl, s->top - 1, s->top - 2))
return (NULL);
i = 0;
pos = &((unsigned char *)(s->items))[s->top - s->itemsize];
while (i < s->itemsize)
{
byte = *pos;
*pos = *(pos - s->itemsize);
*(pos - s->itemsize) = byte;
pos++;
i++;
}
return (s);
}
static void *reverse_array(void *start, void *end, size_t itemsize)
{
void *const tmp = malloc(itemsize);
if (!tmp)
return (NULL);
if (start < end)
{
while (((char *)start + itemsize) <= ((char *)end))
{
ft_memcpy(tmp, start, itemsize);
ft_memcpy(start, end, itemsize);
ft_memcpy(end, tmp, itemsize);
start = (char *)start + itemsize;
end = (char *)end - itemsize;
}
}
free(tmp);
return (start);
}
// vi: noet sw=4 ts=4:

View File

@ -23,13 +23,9 @@ t_stack *stack_new(size_t count, size_t size)
s = malloc(sizeof (t_stack));
if (s)
{
s->items = malloc(count * size);
if (s->items)
{
s->itemsize = size;
s->maxsize = count * size;
s->impl = arr_new(size, count);
if (s->impl)
s->top = 0;
}
else
s = stack_del(s);
}
@ -38,7 +34,7 @@ t_stack *stack_new(size_t count, size_t size)
void *stack_del(t_stack *s)
{
free(s->items);
arr_del(s->impl);
free(s);
return (NULL);
}

View File

@ -2,22 +2,26 @@
#include <ft_stack.h>
#include "stack_data.h"
#include "../src/stack_data.h"
//#define printf(...) (void)0
//#define putchar(...) (void)0
#define STACKSIZE 10
static void print_stack(t_stack *s);
int main(void)
{
t_stack *s = stack_new(8, sizeof (long));
t_stack *s = stack_new(STACKSIZE, sizeof (long));
for (long item = 0; item < 10; item++)
for (long item = -(STACKSIZE / 2); item < STACKSIZE / 2; item++)
{
printf("push %ld\n", item);
stack_push(s, &item);
print_stack(s);
}
printf("pop %ld\n", *(long int*)stack_pop(s));
printf("pop %ld\n", *(long int*)stack_pop(s).item);
print_stack(s);
printf("right rotate step 1\n");
@ -33,12 +37,16 @@ int main(void)
stack_swap(s);
print_stack(s);
stack_del(s);
return 0;
}
static void print_stack(t_stack *s)
{
for (size_t i = 0; i < stack_height(s); i++)
printf("%ld ", ((long int*)(s->items))[i]);
const t_stack_view view = stack_view(s);
for (size_t i = 0; i < view.top; i++)
printf("%ld ", *(long*)arr_get(view.raw, i).item);
putchar('\n');
}