Rb_node rb_insert_b(Rb_node n, char *key, char *val) { Rb_node newleft, newright, newnode, p; if (ishead(n)) { if (n->p.root == n) { /* Tree is empty */ mk_new_ext(newnode, key, val); insert(newnode, n); n->p.root = newnode; newnode->p.parent = n; setroot(newnode); return newnode; } else { mk_new_ext(newright, key, val); insert(newright, n); newleft = newright->c.list.blink; setnormal(newleft); mk_new_int(newleft, newright, newleft->p.parent, isleft(newleft)); p = rprev(newright); if (!ishead(p)) p->k.lext = newright; return newright; } } else { mk_new_ext(newleft, key, val); insert(newleft, n); setnormal(n); mk_new_int(newleft, n, n->p.parent, isleft(n)); p = lprev(newleft); if (!ishead(p)) p->v.rext = newleft; return newleft; } }
static JRB jrb_insert_b(JRB n, Jval key, Jval val) { JRB newleft, newright, newnode, p; if (ishead(n)) { if (n->parent == n) { /* Tree is empty */ mk_new_ext(newnode, key, val); insert(newnode, n); n->parent = newnode; newnode->parent = n; setroot(newnode); return newnode; } else { mk_new_ext(newright, key, val); insert(newright, n); newleft = newright->blink; setnormal(newleft); mk_new_int(newleft, newright, newleft->parent, isleft(newleft)); p = rprev(newright); if (!ishead(p)) setlext(p, newright); return newright; } } else { mk_new_ext(newleft, key, val); insert(newleft, n); setnormal(n); mk_new_int(newleft, n, n->parent, isleft(n)); p = lprev(newleft); if (!ishead(p)) setrext(p, newleft); return newleft; } }
// Recursive Delaunay Triangulation Procedure // Contains modifications for axis-switching division. void CDelaunay::build(int lo, int hi, EdgePointer *le, EdgePointer *re, int rows) { EdgePointer a, b, c, ldo, rdi, ldi, rdo, maxx, minx; int split, lowrows; int low, high; SitePointer s1, s2, s3; low = lo; high = hi; if ( low < (high-2) ) { // more than three elements; do recursion minx = sp[low]; maxx = sp[high]; if (rows == 1) { // time to switch axis of division spsorty( sp, low, high); rows = 65536; } lowrows = rows/2; split = low - 1 + (int) (0.5 + ((double)(high-low+1) * ((double)lowrows / (double)rows))); build( low, split, &ldo, &ldi, lowrows ); build( split+1, high, &rdi, &rdo, (rows-lowrows) ); doMerge(&ldo, ldi, rdi, &rdo); while (orig(ldo) != minx) { ldo = rprev(ldo); } while (orig(rdo) != maxx) { rdo = (SitePointer) lprev(rdo); } *le = ldo; *re = rdo; } else if (low >= (high - 1)) { // two or one points a = makeEdge(sp[low], sp[high]); *le = a; *re = (EdgePointer) sym(a); } else { // three points // 3 cases: triangles of 2 orientations, and 3 points on a line a = makeEdge((s1 = sp[low]), (s2 = sp[low+1])); b = makeEdge(s2, (s3 = sp[high])); splice((EdgePointer) sym(a), b); if (ccw(s1, s3, s2)) { c = connectLeft(b, a); *le = (EdgePointer) sym(c); *re = c; } else { *le = a; *re = (EdgePointer) sym(b); if (ccw(s1, s2, s3)) { // not colinear c = connectLeft(b, a); } } } }
void jrb_delete_node(JRB n) { JRB s, p, gp; char ir; if (isint(n)) { fprintf(stderr, "Cannot delete an internal node: 0x%p\n", (void *)n); exit(1); } if (ishead(n)) { fprintf(stderr, "Cannot delete the head of an jrb_tree: 0x%p\n", (void *)n); exit(1); } delete_item(n); /* Delete it from the list */ p = n->parent; /* The only node */ if (isroot(n)) { p->parent = p; free(n); return; } s = sibling(n); /* The only node after deletion */ if (isroot(p)) { s->parent = p->parent; s->parent->parent = s; setroot(s); free(p); free(n); return; } gp = p->parent; /* Set parent to sibling */ s->parent = gp; if (isleft(p)) { gp->flink = s; setleft(s); } else { gp->blink = s; setright(s); } ir = isred(p); free(p); free(n); if (isext(s)) { /* Update proper rext and lext values */ p = lprev(s); if (!ishead(p)) setrext(p, s); p = rprev(s); if (!ishead(p)) setlext(p, s); } else if (isblack(s)) { fprintf(stderr, "DELETION PROB -- sib is black, internal\n"); exit(1); } else { p = lprev(s); if (!ishead(p)) setrext(p, s->flink); p = rprev(s); if (!ishead(p)) setlext(p, s->blink); setblack(s); return; } if (ir) return; /* Recolor */ n = s; p = n->parent; s = sibling(n); while(isblack(p) && isblack(s) && isint(s) && isblack(s->flink) && isblack(s->blink)) { setred(s); n = p; if (isroot(n)) return; p = n->parent; s = sibling(n); } if (isblack(p) && isred(s)) { /* Rotation 2.3b */ single_rotate(p, isright(n)); setred(p); setblack(s); s = sibling(n); } { JRB x, z; char il; if (isext(s)) { fprintf(stderr, "DELETION ERROR: sibling not internal\n"); exit(1); } il = isleft(n); x = il ? s->flink : s->blink ; z = sibling(x); if (isred(z)) { /* Rotation 2.3f */ single_rotate(p, !il); setblack(z); if (isred(p)) setred(s); else setblack(s); setblack(p); } else if (isblack(x)) { /* Recoloring only (2.3c) */ if (isred(s) || isblack(p)) { fprintf(stderr, "DELETION ERROR: 2.3c not quite right\n"); exit(1); } setblack(p); setred(s); return; } else if (isred(p)) { /* 2.3d */ single_rotate(s, il); single_rotate(p, !il); setblack(x); setred(s); return; } else { /* 2.3e */ single_rotate(s, il); single_rotate(p, !il); setblack(x); return; } } }