示例#1
0
void	morc_tri_r(t_all *res, int debut, int fin, int base)
{
	int	i;
	int	ind;

	i = debut;
	ind = 0;
	while (i++ <= fin)
		if (res->a->pile[res->a->len - 1] < base)
			push_b(res);
		else
		{
			if (res->a->pile[res->a->len - 1] == base)
			{
				push_b(res);
				rotate_b(res);
			}
			else
			{
				rotate_a(res);
				ind++;
			}
		}
	if ((fin - debut) != (res->a->len + res->b->len - 1))
		while (ind-- > 0)
			reverse_rotate_a(res);
	reverse_rotate_b(res);
}
示例#2
0
文件: push_swap.c 项目: C00kie-/42
t_list_i	*push_swap(t_list_i *list)
{
	t_list_i	*pile_b;
	t_list_i	*last_item;

	pile_b = NULL;
	last_item = NULL;
	while ((test_sort_croissant(list) == 0) || pile_b)
	{
		last_item = can_we_rotate(list);
		if (last_item && list->next && list->next->next)
			list = rotate_a(list);
		if (list->c < list->next->c)
			list = swap_a(list);
		if (list->c > list->next->c && (test_sort_croissant(list) == 0)
			&& list->next && list->next->next != NULL)
			pile_b = push_b(&list, pile_b);
		if (pile_b && pile_b->next && pile_b->c > pile_b->next->c)
			pile_b = swap_b(pile_b);
		if (pile_b && pile_b->next && test_sort_decroissant(pile_b) &&
			test_sort_croissant(list))
			list = push_a(list, &pile_b);
		if (test_sort_croissant(list) && pile_b && pile_b->next == NULL)
			list = push_a(list, &pile_b);
	}
	return (list);
}
示例#3
0
void	rotate_r(t_env *env, t_flags *flag)
{
	rotate_a(env, flag);
	rotate_b(env, flag);
	env->count -= 1;
	ft_putstr("\033[36mrr \033[37m");
	if (flag->v == 1)
		stack_status(env);
}
示例#4
0
static void		check_swap_beg(t_pushswap *data)
{
	int		i;

	if (data->length_a == 2 || data->length_a == 3)
		return ;
	i = 3;
	while (i < data->length_a)
	{
		if (data->pile_a[i - 1] < data->pile_a[i]
				|| data->pile_a[i - 1] > data->pile_a[1])
			return ;
		i++;
	}
	rotate_a(data, 0);
	rotate_a(data, 0);
	swap_a(data, 0);
	if (check_pile(data->pile_a, data->length_a))
		return ;
	reverse_rotate_a(data, 0);
	reverse_rotate_a(data, 0);
}
示例#5
0
pair *map_set(map *m, void *key, void *value) {
	map_node *y, *z; /* Top node to update balance factor, and parent. */
	map_node *p, *q; /* Iterator, and parent. */
	map_node *n;     /* Newly inserted node. */
	map_node *w;     /* New root of rebalanced subtree. */
	unsigned char dir = 0;			/* Direction to descend. */
	unsigned char ds[MAX_HEIGHT];	/* Cached comparison results. */
	int i = 0;						/* Number of cached results. */

	z = (map_node*)&m->x;
	y = m->x;
	for (q = z, p = y; p != NULL; q = p, p = p->children[dir]) {
		int c = m->_cmp(key, p->x->a);
		if (c == 0)
			return p->x;
		if (p->balance != 0) {
			z = q;
			y = p;
			i = 0;
		}
		ds[i++] = dir = c > 0;
	}

	n = q->children[dir] = malloc(sizeof(map_node));
	if (n == NULL)
		return NULL;

	m->size++;
	n->x = create_pair(key, value);
	n->left = n->right = NULL;
	n->balance = 0;
	if (y == NULL)
		return n->x;

	for (p = y, i = 0; p != n; p = p->children[ds[i]], i++) {
		if (ds[i] == 0)
			p->balance--;
		else
			p->balance++;
	}

	if (y->balance == -2) {
		w = (y->left->balance == -1) ? rotate_c(y->left, y) : rotate_a(y->left, y);
	} else if (y->balance == +2) {
		w = (y->right->balance == +1) ? rotate_d(y->right, y) : rotate_b(y->right, y);
	} else
		return n->x;
	z->children[y != z->left] = w;
	return n->x;
}
示例#6
0
static void		put_smallest_last(t_pushswap *data)
{
	int		i;

	i = 0;
	if (data->pos_smallest_a < data->length_a / 2)
	{
		while (data->pos_smallest_a != data->length_a - 1)
			rotate_a(data, 0);
	}
	else
	{
		while (data->pos_smallest_a != data->length_a - 1)
			reverse_rotate_a(data, 0);
	}
}
示例#7
0
pair *map_remove(map *m, void *key) {
	map_node *ns[MAX_HEIGHT];     /* Stack of nodes. */
	unsigned char ds[MAX_HEIGHT]; /* Direction of going down the tree (0 == left, 1 == right). */
	unsigned int i = 0;           /* Stack pointer. */
	map_node *n = m->x;           /* Traverses tree to find node to delete. */
	int c;                        /* Comparison value */
	pair *p;

	if (n == NULL)
		return NULL;

	for (c = -1; c != 0; c = m->_cmp(key, n->x->a)) {
		unsigned char dir = c > 0;
		ns[i] = n;
		ds[i++] = dir;
		if ((n = n->children[dir]) == NULL)
			return NULL;
	}

	p = n->x;

	if (n->right == NULL) {
		ns[i-1]->children[ds[i-1]] = n->left;
	} else {
		map_node *a = n->right;
		if (a->left == NULL) {
			a->left = n->left;
			a->balance = n->balance;
			ns[i-1]->children[ds[i-1]] = a;
			ds[i] = 1;
			ns[i++] = a;
		} else {
			map_node *b;
			int j = i++;
			for (;;) {
				ds[i] = 0;
				ns[i++] = a;
				b = a->left;
				if (b->left == NULL)
					break;
				a = b;
			}
			b->left = n->left;
			a->left = b->right;
			b->right = n->right;
			b->balance = n->balance;
			ns[j-1]->children[ds[j-1]] = b;
			ds[j] = 1;
			ns[j] = b;
		}
	}
	free(n);
	while (--i > 0) {
		map_node *y = ns[i];
		if (ds[i] == 0) {
			y->balance++;
			if (y->balance == +1)
				break;
			else if (y->balance == +2) {
				map_node *x = y->right;
				if (x->balance == -1) {
					ns[i-1]->children[ds[i-1]] = rotate_b(x, y);
				} else {
					y->right = x->left;
					x->left = y;
					ns[i-1]->children[ds[i-1]] = x;
					if (x->balance == 0) {
						x->balance = -1;
						y->balance = +1;
						break;
					} else
						x->balance = y->balance = 0;
				}
			}
		} else {
			y->balance--;
			if (y->balance == -1)
				break;
			else if (y->balance == -2) {
				map_node *x = y->left;
				if (x->balance == +1) {
					ns[i-1]->children[ds[i-1]] = rotate_a(x, y);
				} else {
					y->left = x->right;
					x->right = y;
					ns[i-1]->children[ds[i-1]] = x;
					if (x->balance == 0) {
						x->balance = +1;
						y->balance = -1;
						break;
					} else
						x->balance = y->balance = 0;
				}
			}
		}
	}
	m->size--;
	return p;
}