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); }
t_lst *sort(t_lst *l_a) { t_lst *l_b; int cnt; int max; l_b = NULL; max = 0; while (l_a) { cnt = 0; while (l_b && l_a->val < l_b->val && cnt < max) { rotate_b(&l_b); cnt++; } pswap_b(&l_a, &l_b); while (cnt-- > 0) rrotate_b(&l_b); max++; } while (l_b) pswap_a(&l_a, &l_b); return (l_a); }
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); }
void decroissant(t_all *i) { while (i->a->len > 2) { push_b(i); if (i->b->len > 1) rotate_b(i); } swap_a(i); while (i->b->len > 0) push_a(i); }
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; }
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; }