From c2406e5830d5b6e7dbbb1827d2739add4598781f Mon Sep 17 00:00:00 2001 From: Kolomiets Yaroslav Date: Wed, 21 Dec 2016 01:47:55 +0200 Subject: [PATCH] init tor funcitons --- CMakeLists.txt | 91 +++++++++++++++++++++++++++++++++++++++++ inc/fillit.h | 6 +-- src/dancing_links.c | 99 +++++++++++++++++++++++++++++++++++++++++++-- src/get_types.c | 1 + src/main.c | 3 +- 5 files changed, 193 insertions(+), 7 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bd7a5ca --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,91 @@ +cmake_minimum_required(VERSION 3.6) +project(fillit) + +set(CMAKE_CXX_STANDARD 11) + +include_directories(inc) + +set(SOURCE_FILES + inc/fillit.h + inc/libft.h + libft/ft_atoi.c + libft/ft_bzero.c + libft/ft_isalnum.c + libft/ft_isalpha.c + libft/ft_isascii.c + libft/ft_isdigit.c + libft/ft_isprint.c + libft/ft_itoa.c + libft/ft_lst_at.c + libft/ft_lstadd.c + libft/ft_lstadd_back.c + libft/ft_lstdel.c + libft/ft_lstdelone.c + libft/ft_lstfind.c + libft/ft_lstiter.c + libft/ft_lstmap.c + libft/ft_lstnew.c + libft/ft_lststrsplit.c + libft/ft_memalloc.c + libft/ft_memccpy.c + libft/ft_memchr.c + libft/ft_memcmp.c + libft/ft_memcpy.c + libft/ft_memdel.c + libft/ft_memmove.c + libft/ft_memset.c + libft/ft_putchar.c + libft/ft_putchar_fd.c + libft/ft_putendl.c + libft/ft_putendl_fd.c + libft/ft_putnbr.c + libft/ft_putnbr_fd.c + libft/ft_putstr.c + libft/ft_putstr_fd.c + libft/ft_realloc.c + libft/ft_strcat.c + libft/ft_strchr.c + libft/ft_strclr.c + libft/ft_strcmp.c + libft/ft_strcpy.c + libft/ft_strdel.c + libft/ft_strdup.c + libft/ft_strequ.c + libft/ft_striter.c + libft/ft_striteri.c + libft/ft_strjoin.c + libft/ft_strlcat.c + libft/ft_strlen.c + libft/ft_strmap.c + libft/ft_strmapi.c + libft/ft_strncat.c + libft/ft_strncmp.c + libft/ft_strncpy.c + libft/ft_strnequ.c + libft/ft_strnew.c + libft/ft_strnstr.c + libft/ft_strrchr.c + libft/ft_strsplit.c + libft/ft_strstr.c + libft/ft_strsub.c + libft/ft_strtrim.c + libft/ft_tolower.c + libft/ft_toupper.c + libft/libft.h + src/check_raw_string.c + src/dancing_links.c + src/fillit.c + src/get_types.c + src/glue_figure.c + src/main.c + src/move_up_left.c + src/read_file.c + src/string_to_table.c + src/test!_check.c + src/test!_print_one_string.c + src/test!_print_table.c + src/to_letters.c + tests/sample.fillit + tests/sample1.fillit) + +add_executable(fillit ${SOURCE_FILES}) \ No newline at end of file diff --git a/inc/fillit.h b/inc/fillit.h index d8767f0..214d08e 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -32,8 +32,8 @@ typedef struct s_coord typedef struct s_ttrmn { char t[17]; - int x; - int y; + int width; + int height; t_coord c[4]; } t_ttrmn; @@ -80,7 +80,7 @@ int *to_strct_array(char **ttr, t_ttrmn *templates); t_node *add_column(t_node *root); -t_node *add_node(t_node *col, int row); +t_node *add_node(t_node *col, t_coord row); void cover(t_node *to_cover); diff --git a/src/dancing_links.c b/src/dancing_links.c index 2f76f5d..9304787 100644 --- a/src/dancing_links.c +++ b/src/dancing_links.c @@ -10,7 +10,17 @@ /* */ /* ************************************************************************** */ -#include "fillit.h" +#include "../inc/fillit.h" +#include + +t_node *init_root() +{ + t_node *new; + + new = (t_node*)malloc(sizeof(t_node)); + new->left = new->right = new->down = new->up = NULL; + new->column = NULL; +} t_node *add_column(t_node *root) { @@ -26,12 +36,12 @@ t_node *add_column(t_node *root) return (new_col); } -t_node *add_node(t_node *col, int row) +t_node *add_node_with_coord(t_node *col, t_coord coord) { t_node *new_node; new_node = (t_node*)malloc(sizeof(t_node)); - new_node->row = row; + new_node->coord = coord; new_node->up = col->up; new_node->down = col; col->up->down = new_node; @@ -39,6 +49,89 @@ t_node *add_node(t_node *col, int row) return (new_node); } +t_node *add_node(t_node *col +{ + t_node *new_node; + + new_node = (t_node*)malloc(sizeof(t_node)); + new_node->up = col->up; + new_node->down = col; + col->up->down = new_node; + col->up = new_node; + return (new_node); +} + +t_node **add_cols(t_node *root, int number) +{ + t_node **array; + + array = (t_node**)malloc(number * sizeof(t_node*)); + while (number--) + array[number] = add_column(root); + return (array); +} +int *get_coords(t_coord coord, int amount, int type, int size) +{ + int *result; + int i; + t_coord *start; + + result = (int*)malloc(sizeof(int) * 4); + start = g_templates[type].c; + i = 1; + while (i < 5) + result[i++] = coord.x + coord.y * size + start->x + start->y * size + amount; + return (result); +} +void add_row(int *col_numbers, t_node **cols_arr, t_coord coord) +{ + add_node_with_coord(cols_arr[col_numbers[0]], coord); + add_node(cols_arr[col_numbers[1]]); + add_node(cols_arr[col_numbers[2]]); + add_node(cols_arr[col_numbers[3]]); + add_node(cols_arr[col_numbers[4]]); +} + +void link_row(int *col_nums, t_node **cols_arr) +{ + cols_arr[col_nums[0]]->up->left = cols_arr[col_nums[4]]->up; + cols_arr[col_nums[0]]->up->right = cols_arr[col_nums[1]]->up; + cols_arr[col_nums[1]]->up->left = cols_arr[col_nums[0]]->up; + cols_arr[col_nums[1]]->up->right = cols_arr[col_nums[2]]->up; + cols_arr[col_nums[2]]->up->left = cols_arr[col_nums[1]]->up; + cols_arr[col_nums[2]]->up->right = cols_arr[col_nums[3]]->up; + cols_arr[col_nums[3]]->up->left = cols_arr[col_nums[2]]->up; + cols_arr[col_nums[3]]->up->right = cols_arr[col_nums[4]]->up; + cols_arr[col_nums[4]]->up->left = cols_arr[col_nums[3]]->up; + cols_arr[col_nums[4]]->up->right = cols_arr[col_nums[0]]->up; +} + +void add_rows(int *types, int amount, int size, t_node **cols_arr) +{ + int i = 0; + int k = 0; + int l = 0; + int *col_num; + t_coord coord; + + while (i < amount) + { + k = 0; + while (k < size - g_templates[types[i]].width) + { + l = 0; + while (l < size - g_templates[types[i]].height) + { + coord = {k, l}; + col_num = get_coords(coord, amount, types[i], size); + col_num[0] = i; + add_row(col_num, cols_arr, coord); + link_row(col_num, cols_arr); + } + } + } +} + void cover(t_node *to_cover) { t_node *step_vert; diff --git a/src/get_types.c b/src/get_types.c index 157649a..575d5ab 100644 --- a/src/get_types.c +++ b/src/get_types.c @@ -1,4 +1,5 @@ #include "fillit.h" +#include int *get_types(char **ttr) { diff --git a/src/main.c b/src/main.c index 22a95e0..f1b9807 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,7 @@ int main(int argc, char **argv) int i; int *types; int amount; + int size; t_node *r; t_node **cols_arr; t_list *ans; @@ -84,7 +85,7 @@ int main(int argc, char **argv) size = ft_sqrt_ceil(amount * 4); r = init_root(); cols_arr = add_cols(r, (size * size + amount)); - add_rows(r, types, amount, cols_arr); + add_rows(types, amount, cols_arr); ans = dlx(r); print_ans(ans); //printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y);