/* Sort the playlist by file names. */ void plist_sort_fname (struct plist *plist) { struct plist_item *sorted; struct rb_node *x; int n; if (plist_count(plist) == 0) return; sorted = (struct plist_item *)xmalloc (plist_count(plist) * sizeof(struct plist_item)); x = rb_min (plist->search_tree); assert (!rb_is_null(x)); while (plist_deleted(plist, (intptr_t)rb_get_data (x))) x = rb_next (x); sorted[0] = plist->items[(intptr_t)rb_get_data (x)]; rb_set_data (x, NULL); n = 1; while (!rb_is_null(x = rb_next(x))) { if (!plist_deleted(plist, (intptr_t)rb_get_data (x))) { sorted[n] = plist->items[(intptr_t)rb_get_data (x)]; rb_set_data (x, (void *)(intptr_t)n++); } } plist->num = n; plist->not_deleted = n; memcpy (plist->items, sorted, sizeof(struct plist_item) * n); free (sorted); }
/* -------------------------------------- * brief : delete pdata from rb tree * para t: rb tree pointer * para pdata: data pointer * return : success 0, failed -1 * -------------------------------------- */ int rb_delete(RBTree *t, void *pdata) { if (t->rb_count == 0){ return -1; } RBNode *z = rb_find(t, pdata); if (!z) return -1; RBNode *y = z, *x = NULL; RB_NODE_COLOR color = y->color; if (z->child[0] == t->nil) { x = z->child[1]; rb_trans(t, z, x); } else if (z->child[1] == t->nil) { x = z->child[0]; rb_trans(t, z, x); } else { y = rb_min(t, z->child[1]); color = y->color; x = y->child[1]; if (y->p == z) { x->p = y; } else { rb_trans(t, y, x); y->child[1] = z->child[1]; y->child[1]->p = y; } rb_trans(t, z, y); y->child[0] = z->child[0]; y->child[0]->p = y; y->color = z->color; } //free(z); //z = NULL; ((RBLANode*)z)->next = t->p; t->p = (RBLANode*)z; if (color == RB_BLACK) { rb_delete_fix(t, x); } t->rb_count -= 1; return 0; }
struct rb_node *rb_next(struct rb_node *n) { if (!n) { return NULL; } if (n->right) { /* next element is the the smallest element in the right subtree */ n = rb_min(n->right); } else { /* next element is the next largest element upwards. walk up until we go right */ struct rb_node *last = n; n = n->parent; while (n && n->right == last) { last = n; n = n->parent; } } return n; }
struct rb_node *rb_first(struct rb_tree *t) { return rb_min(t->root); }