From 6bc35c5ca0c00893f383dec89367d6dd4c91e596 Mon Sep 17 00:00:00 2001 From: Richard Brooksby Date: Fri, 21 Feb 2014 11:33:56 +0000 Subject: [PATCH] Generalising splayrotate operations into treerotates. Copied from Perforce Change: 184431 ServerID: perforce.ravenbrook.com --- mps/code/splay.c | 62 ++++-------------------------------------------- mps/code/tree.c | 45 +++++++++++++++++++++++++++++++++++ mps/code/tree.h | 11 +++++---- 3 files changed, 56 insertions(+), 62 deletions(-) diff --git a/mps/code/splay.c b/mps/code/splay.c index 9217e85c981..173824f6fb2 100644 --- a/mps/code/splay.c +++ b/mps/code/splay.c @@ -125,62 +125,6 @@ static void SplayLinkLeft(Tree *topIO, Tree *leftIO) { } -/* SplayRotateLeft -- Rotate right child edge of node - * - * Rotates node, right child of node, and left child of right - * child of node, leftwards in the order stated. - * - * See . - */ - -static void SplayRotateLeft(Tree *nodeIO, SplayTree tree) { - Tree nodeRight; - - AVER(nodeIO != NULL); - AVERT(Tree, *nodeIO); - AVERT(Tree, TreeRight(*nodeIO)); - AVERT(SplayTree, tree); - - nodeRight = TreeRight(*nodeIO); - TreeSetRight(*nodeIO, TreeLeft(nodeRight)); - TreeSetLeft(nodeRight, *nodeIO); - *nodeIO = nodeRight; - - tree->updateNode(tree, TreeLeft(nodeRight)); - /* Don't need to update new root because we know that we will */ - /* do either a link or an assemble next, and that will sort it */ - /* out. */ -} - - -/* SplayRotateRight -- Rotate left child edge of node - * - * Rotates node, left child of node, and right child of left - * child of node, leftwards in the order stated. - * - * See . - */ - -static void SplayRotateRight(Tree *nodeIO, SplayTree tree) { - Tree nodeLeft; - - AVER(nodeIO != NULL); - AVERT(Tree, *nodeIO); - AVERT(Tree, TreeLeft(*nodeIO)); - AVERT(SplayTree, tree); - - nodeLeft = TreeLeft(*nodeIO); - TreeSetLeft(*nodeIO, TreeRight(nodeLeft)); - TreeSetRight(nodeLeft, *nodeIO); - *nodeIO = nodeLeft; - - tree->updateNode(tree, TreeRight(nodeLeft)); - /* Don't need to update new root because we know that we will */ - /* do either a link or an assemble next, and that will sort it */ - /* out. */ -} - - /* SplayAssemble -- Assemble left right and top trees into one * * We do this by moving the children of the top tree to the last and @@ -336,7 +280,8 @@ static Bool SplaySplay(Tree *nodeReturn, SplayTree tree, case CompareLESS: { /* zig-zig */ if (TreeLeft(topLeft) == TreeEMPTY) goto terminalZig; - SplayRotateRight(&top, tree); + TreeRotateRight(&top); + tree->updateNode(tree, TreeRight(top)); SplayLinkRight(&top, &rightFirst); } break; @@ -373,7 +318,8 @@ static Bool SplaySplay(Tree *nodeReturn, SplayTree tree, case CompareGREATER: { /* zag-zag */ if (TreeRight(topRight) == TreeEMPTY) goto terminalZag; - SplayRotateLeft(&top, tree); + TreeRotateLeft(&top); + tree->updateNode(tree, TreeLeft(top)); SplayLinkLeft(&top, &leftLast); } break; diff --git a/mps/code/tree.c b/mps/code/tree.c index 43a3bea100c..4768645b490 100644 --- a/mps/code/tree.c +++ b/mps/code/tree.c @@ -162,6 +162,51 @@ void TreeTraverseMorris(Tree tree, TreeVisitor visit, } +/* TreeRotateLeft -- Rotate right child edge of node + * + * Rotates node, right child of node, and left child of right + * child of node, leftwards in the order stated. + */ + +void TreeRotateLeft(Tree *treeIO) +{ + Tree tree, right; + + AVER(treeIO != NULL); + tree = *treeIO; + AVERT(Tree, tree); + right = TreeRight(tree); + AVERT(Tree, right); + + TreeSetRight(tree, TreeLeft(right)); + TreeSetLeft(right, tree); + + *treeIO = right; +} + + +/* TreeRotateRight -- Rotate left child edge of node + * + * Rotates node, left child of node, and right child of left + * child of node, leftwards in the order stated. + */ + +void TreeRotateRight(Tree *treeIO) { + Tree tree, left; + + AVER(treeIO != NULL); + tree = *treeIO; + AVERT(Tree, tree); + left = TreeLeft(tree); + AVERT(Tree, left); + + TreeSetLeft(*treeIO, TreeRight(left)); + TreeSetRight(left, *treeIO); + + *treeIO = left; +} + + /* C. COPYRIGHT AND LICENSE * * Copyright (C) 2014 Ravenbrook Limited . diff --git a/mps/code/tree.h b/mps/code/tree.h index 53913e88eb7..992ed1febd7 100644 --- a/mps/code/tree.h +++ b/mps/code/tree.h @@ -71,12 +71,15 @@ extern Bool TreeCheckLeaf(Tree tree); extern Compare TreeFind(Tree *treeReturn, Tree root, TreeKey key, TreeCompare compare); -Bool TreeInsert(Tree *treeReturn, Tree root, Tree node, - TreeKey key, TreeCompare compare); +extern Bool TreeInsert(Tree *treeReturn, Tree root, Tree node, + TreeKey key, TreeCompare compare); typedef void TreeVisitor(Tree tree, void *closureP, Size closureS); -void TreeTraverseMorris(Tree tree, TreeVisitor visit, - void *closureP, Size closureS); +extern void TreeTraverseMorris(Tree tree, TreeVisitor visit, + void *closureP, Size closureS); + +extern void TreeRotateLeft(Tree *nodeIO); +extern void TreeRotateRight(Tree *nodeIO); #endif /* tree_h */