diff --git a/Makefile b/Makefile index 8794ca8..77dc8ef 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/02/23 14:30:57 by tmaze #+# #+# # -# Updated: 2019/03/15 16:39:08 by tmaze ### ########.fr # +# Updated: 2019/03/16 17:21:14 by tmaze ### ########.fr # # # #******************************************************************************# @@ -37,12 +37,16 @@ OBJDIR = objs INCDIR = includes libft # Source files -SRC = ps_push.c \ +SRC = ps_clean.c \ + ps_push.c \ ps_rot.c \ ps_rerot.c \ ps_swap.c \ ps_stktools.c \ - ps_stktools2.c + ps_stktools2.c \ + ps_sort.c \ + ps_sorttools.c \ + ps_specsort.c MAIN1 = checker.c MAIN2 = push_swap.c @@ -66,41 +70,41 @@ DRULE = all # Main rules default : - @echo -e "$(PUR)===> $(GRE)$(NAME) : $(PUR) START RULE : $(DRULE) <===$(DEF)" + @echo -e "$(PUR)===> $(GRE)$(NAME2) : $(PUR) START RULE : $(DRULE) <===$(DEF)" @make $(DRULE) - @echo -e "$(PUR)===> $(GRE)$(NAME) : $(PUR) END RULE : $(DRULE) <===$(DEF)" + @echo -e "$(PUR)===> $(GRE)$(NAME2) : $(PUR) END RULE : $(DRULE) <===$(DEF)" all : $(NAME1) $(NAME2) # Compilation rules -$(OBJDIR)/%.o : $(SRCDIR)/%.c $(OBJDIR) +$(OBJDIR)/%.o : $(SRCDIR)/%.c $(OBJDIR) includes/push_swap.h $(CC) $(FLAGS) -c -o $@ $< $(INCP) $(OBJDIR) : - @echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Directory Creation <===$(DEF)" + @echo -e "$(YEL)===> $(GRE)$(NAME2) : $(YEL) Directory Creation <===$(DEF)" mkdir -p $(OBJDIR) $(LIBDIR)/$(LIBFILE) : - @echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Librairy Compilation <===$(DEF)" + @echo -e "$(YEL)===> $(GRE)$(NAME2) : $(YEL) Librairy Compilation <===$(DEF)" $(MAKE) -C $(LIBDIR) all $(NAME1) : $(OBJP1) $(OBJP) $(LIBDIR)/$(LIBFILE) - @echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Binary Compilation <===$(DEF)" + @echo -e "$(YEL)===> $(GRE)$(NAME1) : $(YEL) Binary Compilation <===$(DEF)" $(CC) $(FLAGS) -o $@ $^ $(INCP) -L$(LIBDIR) -l$(LIB) $(NAME2) : $(OBJP2) $(OBJP) $(LIBDIR)/$(LIBFILE) - @echo -e "$(YEL)===> $(GRE)$(NAME) : $(YEL) Binary Compilation <===$(DEF)" + @echo -e "$(YEL)===> $(GRE)$(NAME2) : $(YEL) Binary Compilation <===$(DEF)" $(CC) $(FLAGS) -o $@ $^ $(INCP) -L$(LIBDIR) -l$(LIB) # Cleaner rules clean : @echo -e "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Object Files <===$(DEF)" - @rm -rf $(OBJDIR) + @$(RM) -rf $(OBJDIR) fclean : clean @echo -e "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Binary File <===$(DEF)" - @rm -f $(NAME1) $(NAME2) + @$(RM) -f $(NAME1) $(NAME2) re : fclean default diff --git a/includes/push_swap.h b/includes/push_swap.h index 6ff1330..5d78d7a 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/21 14:13:53 by tmaze #+# #+# */ -/* Updated: 2019/03/15 15:33:18 by tmaze ### ########.fr */ +/* Updated: 2019/03/16 13:32:59 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,4 +53,17 @@ t_list *add_op(t_psdata *data, char *op); int get_params(t_psdata *data, int ac, char **av, int viz); int check_input(char *in, int *ret); +void mark_groups(t_psdata *data); +t_stack *get_last_a(t_psdata *data); +int get_size_firsts(t_psdata *data); +int get_size_last(t_psdata *data); +int get_nb_groups(t_psdata *data); + +void sortfor3(t_psdata *data); +void sortfor5(t_psdata *data); + +void clean_op(t_psdata *data); + +int sort(t_psdata *data); + #endif diff --git a/libft b/libft index d49f964..acf5431 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit d49f9640459ae0351636e1d8da41e12264f2b518 +Subproject commit acf5431d1458dc07ea37302d134d7125f15aebf0 diff --git a/srcs/push_swap.c b/srcs/push_swap.c index 1f86dc6..48e8c61 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -6,295 +6,12 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/03 11:41:49 by tmaze #+# #+# */ -/* Updated: 2019/03/15 16:17:37 by tmaze ### ########.fr */ +/* Updated: 2019/03/16 13:23:53 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -void mark_groups(t_psdata *data) -{ - t_stack *ptr; - int inds; - - ptr = data->a; - inds = 1; - while (ptr) - { - ptr->ind = inds; - if (ptr->next && ptr->nb > ptr->next->nb) - inds++; - ptr = ptr->next; - } -} - -void get_min_max(t_psdata *data, int *min_max, int *ind_min_max) -{ - t_stack *ptr; - int i; - - min_max[0] = FT_INT_MAX; - min_max[1] = FT_INT_MIN; - ptr = data->a; - i = 0; - while (ptr) - { - if (ptr->nb < min_max[0] && (ind_min_max[0] = i) == i) - min_max[0] = ptr->nb; - if (ptr->nb > min_max[1] && (ind_min_max[1] = i) == i) - min_max[1] = ptr->nb; - i++; - ptr = ptr->next; - } -} - -void sortfor3(t_psdata *data) -{ - int min_max[2]; - int ind_min_max[2]; - - get_min_max(data, min_max, ind_min_max); - while (ind_min_max[0] != 0 || ind_min_max[1] != 2) - { - if (ind_min_max[0] == ind_min_max[1] - 1) - ft_printf("sa\n"); - if (ind_min_max[0] == ind_min_max[1] - 1) - ps_swap(data, 'a'); - if (ind_min_max[1] == 0) - ft_printf("ra\n"); - if (ind_min_max[1] == 0) - ps_rot(data, 'a'); - if (ind_min_max[0] == 2 && ind_min_max[1] == 1) - ft_printf("rra\n"); - if (ind_min_max[0] == 2 && ind_min_max[1] == 1) - ps_rerot(data, 'a'); - get_min_max(data, min_max, ind_min_max); - } -} - -void sortfor5(t_psdata *data) -{ - ft_printf("pb\n"); - ps_push(data, 'b'); - ft_printf("pb\n"); - ps_push(data, 'b'); - if (data->b->nb > data->b->next->nb) - ft_printf("sb\n"); - if (data->b->nb > data->b->next->nb) - ps_swap(data, 'b'); - sortfor3(data); - data->b->ind = 1; - data->b->next->ind = 1; - data->a->ind = 2; - data->a->next->ind = 2; - data->a->next->next->ind = 2; - while (data->a->ind == 2 || data->size_b > 0) - { - if (data->a->ind != 2 || (data->b && data->b->nb < data->a->nb)) - ft_printf("pa\n"); - if (data->a->ind != 2 || (data->b && data->b->nb < data->a->nb)) - ps_push(data, 'a'); - data->a->ind = 3; - ft_printf("ra\n"); - ps_rot(data, 'a'); - } -} - -t_stack *get_last_a(t_psdata *data) -{ - t_stack *ptr; - - ptr = data->a; - while (ptr && ptr->next) - ptr = ptr->next; - return (ptr); -} - -int get_size_firsts(t_psdata *data) -{ - t_stack *ptr; - int inds; - int i; - - inds = data->a->ind; - i = 0; - ptr = data->a; - while (ptr && ptr->ind == inds && (i += 1)) - ptr = ptr->next; - if (ptr) - { - inds = ptr->ind; - while (ptr && ptr->ind == inds && (i += 1)) - ptr = ptr->next; - } - return (i); -} - -int get_size_last(t_psdata *data) -{ - t_stack *ptr; - int inds; - int i; - - i = 0; - ptr = data->a; - while (ptr && ptr->next) - ptr = ptr->next; - inds = ptr->ind; - ptr = data->a; - while (ptr) - { - i += (ptr->ind == inds) ? 1 : 0; - ptr = ptr->next; - } - return (i); -} - -int get_nb_groups(t_psdata *data) -{ - t_stack *ptr; - int ind; - int ret; - - ret = 0; - ptr = data->a; - ind = ptr->ind; - while (ptr) - { - if (ret == 0 || ptr->ind != ind) - { - ret++; - ind = ptr->ind; - } - ptr = ptr->next; - } - return (ret); -} - -int join_lists(t_psdata *data, t_stack **last, int inds, int inds2) -{ - while ((*last)->ind == inds2 || data->size_b > 0) - if (((*last)->ind != inds2 && data->size_b > 0) - || (data->size_b > 0 && data->b->nb > (*last)->nb)) - { - if (add_op(data, "pa\0") == NULL) - return (0); - ps_push(data, 'a'); - data->a->ind = inds; - } - else if (data->size_b == 0 || data->b->nb < (*last)->nb) - { - if (add_op(data, "rra") == NULL) - return (0); - ps_rerot(data, 'a'); - data->a->ind = inds; - (*last) = get_last_a(data); - } - return (1); -} - -int rotate_list(t_psdata *data, t_stack **last) -{ - t_list *tmp; - - if (get_nb_groups(data) > 2) - { - if (get_nb_groups(data) != 2 - && data->a->ind != (*last)->ind && add_op(data, "rra") == NULL) - return (0); - else if (get_nb_groups(data) != 2 && data->a->ind != (*last)->ind) - { - ps_rerot(data, 'a'); - (*last) = get_last_a(data); - while ((*last)->ind == data->a->ind - && (tmp = add_op(data, "rra")) != NULL) - { - ps_rerot(data, 'a'); - (*last) = get_last_a(data); - } - } - } - return (1); -} - -int sort(t_psdata *data) -{ - t_list *tmp; - t_stack *last; - int inds; - int inds2; - - last = get_last_a(data); - inds = last->ind + 1; - while (data->a->ind != last->ind) - { - inds2 = data->a->ind; - while (data->a && data->a->ind == inds2 - && (tmp = add_op(data, "pb\0")) != NULL) - ps_push(data, 'b'); - if (tmp == NULL) - return (0); - inds2 = last->ind; - if (join_lists(data, &last, inds, inds2) == 0) - return (0); - if (rotate_list(data, &last) == 0) - return (0); - inds++; - } - return (1); -} - -void clean_op(t_psdata *data) -{ - t_list *it; - t_list **prec; - char *ct1; - char *ct2; - char *ct3; - - it = data->op; - prec = &(data->op); - while (it && it->next) - { - ct1 = (char*)(it->content); - ct2 = (char*)(it->next->content); - if (ct1[0] == 'p' && ct2[0] == 'p' && ct1[1] != ct2[1]) - { - *prec = it->next->next; - ft_memdel(&(it->next->content)); - ft_memdel((void**)(&(it->next))); - ft_memdel(&(it->content)); - ft_memdel((void**)(&it)); - it = data->op; - prec = &(data->op); - } - else if (it->next->next) - { - ct3 = (char*)(it->next->next->content); - if (ct1[0] == 'p' && ct1[1] == 'b' && ct3[0] == 'p' - && ct3[1] == 'a' && ft_strcmp(ct2, "rra") == 0) - { - *prec = it->next; - ft_strcpy((char*)(it->next->next->content), "sa"); - ft_memdel(&(it->content)); - ft_memdel((void**)&it); - it = data->op; - prec = &(data->op); - } - else - { - prec = &(it->next); - it = it->next; - } - } - else - { - prec = &(it->next); - it = it->next; - } - } -} - void print_ops(t_psdata *data) { t_list *it;