+#ifdef SPLAY
+
+#define LINK(node,side,child) (node)->side = (child);if(child) {(child)->parent = (node);};printf("%08x->%s now %08x\n", node, __STRING(side), child);
+
+// rotates segment's left node to the top
+static inline segment_t*rotate_right(actlist_t*a, segment_t*s)
+{
+ /* s n
+ / \
+ n -> s
+ \ /
+ l l
+ */
+ assert(s->leftchild);
+ segment_t*p = s->parent;
+ segment_t*n = s->leftchild;
+ segment_t*l = s->rightchild;
+ LINK(n,rightchild,s);
+ LINK(s,leftchild,l);
+ n->parent = p;
+ if(p) {
+ if(p->leftchild == s)
+ p->leftchild = n;
+ else if(p->rightchild == s)
+ p->rightchild = n;
+ } else {
+ a->root = n;
+ }
+ return n;
+}
+// rotates segment's right node to the top
+static inline segment_t*rotate_left(actlist_t*a, segment_t*s)
+{
+ /* s n
+ \ /
+ n -> s
+ / \
+ r r
+ */
+ assert(s->rightchild);
+ segment_t*p = s->parent;
+ segment_t*n = s->rightchild;
+ segment_t*r = n->leftchild;
+ LINK(n,leftchild,s);
+ LINK(s,rightchild,r);
+ n->parent = p;
+ if(p) {
+ if(p->leftchild == s)
+ p->leftchild = n;
+ else if(p->rightchild == s)
+ p->rightchild = n;
+ } else {
+ a->root = n;
+ }
+ return n;
+}
+static void move_to_root(actlist_t*a, segment_t*s)
+{
+ /* this is a textbook implementation of the three splay operations
+ zig, zig-zig and zig-zag */
+ while(a->root != s) {
+ assert(s->parent);
+ segment_t*p = s->parent;
+ if(p == a->root) {
+ // zig
+ if(a->root->leftchild == s) {
+ rotate_right(a, a->root);
+ } else {
+ rotate_left(a, a->root);
+ }
+ assert(a->root == s);
+ } else {
+ segment_t*pp = p->parent;
+ if(p->leftchild == s && p->parent->leftchild == p->parent) {
+ // zig-zig (left)
+ rotate_right(a, pp);
+ rotate_right(a, pp);
+ } else if(p->rightchild == s && p->parent->rightchild == p->parent) {
+ // zig-zig (right)
+ rotate_left(a, pp);
+ rotate_left(a, pp);
+ } else if(p->leftchild == s && p->parent->rightchild == p->parent) {
+ // zig-zag (left)
+ rotate_right(a, p);
+ rotate_left(a, pp);
+ } else if(p->rightchild == s && p->parent->leftchild == p->parent) {
+ // zig-zag (right)
+ rotate_right(a, p);
+ rotate_left(a, pp);
+ }
+ }
+ }
+}
+
+static int actlist_splay(actlist_t*a, point_t p1, point_t p2)
+{
+ if(!a->list) return;
+
+ segment_t tmp;
+ memset(&tmp, 0, sizeof(tmp));
+ segment_t*left=&tmp,*right=&tmp;
+
+ while(1) {
+ if(cmp(a->root,p1,p2)<0) {
+ /* we're to the left of the root */
+ if(!a->root->leftchild)
+ return -1;
+ if(cmp(a->root->leftchild,p1,p2)<0) {
+ /* we're also to the left of the root's left child
+ -> rotate right, so that the left child is root */
+ segment_t*s = a->root->leftchild;
+ LINK(a->root, leftchild, s->rightchild);
+ LINK(s, rightchild, a->root);
+ a->root = s;
+ if(!a->root->leftchild)
+ return -1;
+ }
+ LINK(right, leftchild, a->root);
+ right = a->root;
+ a->root = a->root->leftchild;
+ } else /* cmp(a->root,p1,p2)>=0 */ {
+ /* we're to the right of the root */
+ if(!a->root->rightchild)
+ return 1;
+ if(cmp(a->root->rightchild,p1,p2)>=0) {
+ /* we're also to the right of the root's right child
+ -> rotate left, so that the right child is root */
+ segment_t*s = a->root->rightchild;
+ LINK(a->root, rightchild, s->leftchild);
+ LINK(s, leftchild, a->root);
+ a->root = s;
+ if(!a->root->rightchild)
+ return 1;
+ }
+ LINK(left, rightchild, a->root);
+ left = a->root;
+ a->root = a->root->rightchild;
+ }
+ }
+ LINK(left, rightchild, a->root->leftchild);
+ LINK(right, leftchild, a->root->rightchild);
+ LINK(a->root, leftchild, tmp.rightchild);
+ LINK(a->root, rightchild, tmp.leftchild);
+}
+
+#endif
+