コード例 #1
0
Res SplayTreeDelete(SplayTree tree, SplayNode node, void *key) {
    SplayNode rightHalf, del, leftLast;
    Bool found;

    AVERT(SplayTree, tree);
    AVERT(SplayNode, node);

    found = SplaySplay(&del, tree, key, tree->compare);
    AVER(!found || del == node);

    if (!found) {
        return ResFAIL;
    } else if (SplayNodeLeftChild(node) == NULL) {
        SplayTreeSetRoot(tree, SplayNodeRightChild(node));
    } else if (SplayNodeRightChild(node) == NULL) {
        SplayTreeSetRoot(tree, SplayNodeLeftChild(node));
    } else {
        rightHalf = SplayNodeRightChild(node);
        SplayTreeSetRoot(tree, SplayNodeLeftChild(node));
        if (SplaySplay(&leftLast, tree, key, tree->compare)) {
            return ResFAIL;
        } else {
            AVER(SplayNodeRightChild(leftLast) == NULL);
            SplayNodeSetRightChild(leftLast, rightHalf);
            if (tree->updateNode != NULL) {
                SplayNodeUpdate(tree, leftLast);
            }
        }
    }

    SplayNodeFinish(node);

    return ResOK;
}
コード例 #2
0
ファイル: splay.c プロジェクト: aseaday/mps-temporary
Bool SplayFindFirst(Tree *nodeReturn, SplayTree splay,
                    SplayTestNodeFunction testNode,
                    SplayTestTreeFunction testTree,
                    void *closureP, Size closureS)
{
  SplayFindClosureStruct closureStruct;
  Bool found;

  AVER(nodeReturn != NULL);
  AVERT(SplayTree, splay);
  AVER(FUNCHECK(testNode));
  AVER(FUNCHECK(testTree));

  if (SplayTreeIsEmpty(splay) ||
      !testTree(splay, SplayTreeRoot(splay), closureP, closureS))
    return FALSE; /* no suitable nodes in tree */

  closureStruct.p = closureP;
  closureStruct.s = closureS;
  closureStruct.testNode = testNode;
  closureStruct.testTree = testTree;
  closureStruct.splay = splay;
  closureStruct.found = FALSE;

  found = SplaySplay(splay, &closureStruct,
                     SplayFindFirstCompare) == CompareEQUAL &&
          closureStruct.found;

  while (!found) {
    Tree oldRoot, newRoot;
    
    /* FIXME: Rename to "seen" and "not yet seen" or something. */
    oldRoot = SplayTreeRoot(splay);
    newRoot = TreeRight(oldRoot);

    if (newRoot == TreeEMPTY || !(*testTree)(splay, newRoot, closureP, closureS))
      return FALSE; /* no suitable nodes in the rest of the tree */
  
    /* Temporarily chop off the left half-tree, inclusive of root,
       so that the search excludes any nodes we've seen already. */
    SplayTreeSetRoot(splay, newRoot);
    TreeSetRight(oldRoot, TreeEMPTY);

    found = SplaySplay(splay, &closureStruct,
                       SplayFindFirstCompare) == CompareEQUAL &&
            closureStruct.found;

    /* Restore the left tree, then rotate left so that the node we
       just splayed is at the root.  Update both. */
    newRoot = SplayTreeRoot(splay);
    TreeSetRight(oldRoot, newRoot);
    SplayTreeSetRoot(splay, oldRoot);
    TreeRotateLeft(&splay->root);
    splay->updateNode(splay, oldRoot);
    splay->updateNode(splay, newRoot);
  }

  *nodeReturn = SplayTreeRoot(splay);
  return TRUE;
}
コード例 #3
0
ファイル: splay.c プロジェクト: Ravenbrook/mps
Bool SplayFindLast(Tree *nodeReturn, SplayTree splay,
                   SplayTestNodeFunction testNode,
                   SplayTestTreeFunction testTree,
                   void *testClosure)
{
  SplayFindClosureStruct closureStruct;
  Bool found;

  AVER_CRITICAL(nodeReturn != NULL);
  AVERT_CRITICAL(SplayTree, splay);
  AVER_CRITICAL(FUNCHECK(testNode));
  AVER_CRITICAL(FUNCHECK(testTree));

  if (SplayTreeIsEmpty(splay) ||
      !testTree(splay, SplayTreeRoot(splay), testClosure))
    return FALSE; /* no suitable nodes in tree */

  closureStruct.testClosure = testClosure;
  closureStruct.testNode = testNode;
  closureStruct.testTree = testTree;
  closureStruct.splay = splay;

  found = SplaySplay(splay, &closureStruct,
                     SplayFindLastCompare) == CompareEQUAL &&
          closureStruct.found;

  while (!found) {
    Tree oldRoot, newRoot;
    
    oldRoot = SplayTreeRoot(splay);
    newRoot = TreeLeft(oldRoot);

    if (newRoot == TreeEMPTY || !(*testTree)(splay, newRoot, testClosure))
      return FALSE; /* no suitable nodes in the rest of the tree */
  
    /* Temporarily chop off the right half-tree, inclusive of root,
       so that the search excludes any nodes we've seen already. */
    SplayTreeSetRoot(splay, newRoot);
    TreeSetLeft(oldRoot, TreeEMPTY);

    found = SplaySplay(splay, &closureStruct,
                       SplayFindLastCompare) == CompareEQUAL &&
            closureStruct.found;

    /* Restore the right tree, then rotate right so that the node we
       just splayed is at the root.  Update both. */
    newRoot = SplayTreeRoot(splay);
    TreeSetLeft(oldRoot, newRoot);
    SplayTreeSetRoot(splay, oldRoot);
    TreeRotateRight(&splay->root);
    splay->updateNode(splay, oldRoot);
    splay->updateNode(splay, newRoot);
  }

  *nodeReturn = SplayTreeRoot(splay);
  return TRUE;
}
コード例 #4
0
ファイル: splay.c プロジェクト: aseaday/mps-temporary
static Compare SplaySplay(SplayTree splay, TreeKey key,
                          TreeCompareFunction compare)
{
  Compare cmp;
  SplayStateStruct stateStruct;

#ifdef SPLAY_DEBUG
  Count count = SplayDebugCount(splay);
#endif

  /* Short-circuit common cases.  Splay trees often bring recently
     acccessed nodes to the root. */
  if (SplayTreeIsEmpty(splay) ||
      compare(SplayTreeRoot(splay), key) == CompareEQUAL)
    return CompareEQUAL;

  if (SplayHasUpdate(splay)) {
    cmp = SplaySplitRev(&stateStruct, splay, key, compare);
    SplayAssembleRev(splay, &stateStruct);
  } else {
    cmp = SplaySplitDown(&stateStruct, splay, key, compare);
    SplayAssembleDown(splay, &stateStruct);
  }

  SplayTreeSetRoot(splay, stateStruct.middle);

#ifdef SPLAY_DEBUG
  AVER(count == SplayDebugCount(splay));
#endif

  return cmp;
}
コード例 #5
0
ファイル: splay.c プロジェクト: Ravenbrook/mps
static Tree SplayTreeSuccessor(SplayTree splay)
{
  Tree oldRoot, newRoot;

  AVERT(SplayTree, splay);
  AVER(!SplayTreeIsEmpty(splay));

  oldRoot = SplayTreeRoot(splay);

  if (!TreeHasRight(oldRoot))
    return TreeEMPTY; /* No successor */

  /* temporarily chop off the left half-tree, inclusive of root */
  SplayTreeSetRoot(splay, TreeRight(oldRoot));
  TreeSetRight(oldRoot, TreeEMPTY);
  (void)SplaySplay(splay, NULL, compareLess);
  newRoot = SplayTreeRoot(splay);
  AVER(newRoot != TreeEMPTY);
  AVER(TreeLeft(newRoot) == TreeEMPTY);
  TreeSetLeft(newRoot, oldRoot);
  splay->updateNode(splay, oldRoot);
  splay->updateNode(splay, newRoot);

  return newRoot;
}
コード例 #6
0
static SplayNode SplayTreeSuccessor(SplayTree tree, void *key) {
    SplayNode oldRoot, newRoot;

    AVERT(SplayTree, tree);

    oldRoot = SplayTreeRoot(tree);
    AVERT(SplayNode, oldRoot);

    if (SplayNodeRightChild(oldRoot) == NULL) {
        newRoot = NULL; /* No successor */
    } else {
        /* temporarily chop off the left half-tree, inclusive of root */
        SplayTreeSetRoot(tree, SplayNodeRightChild(oldRoot));
        SplayNodeSetRightChild(oldRoot, NULL);
        if (SplaySplay(&newRoot, tree, key, tree->compare)) {
            NOTREACHED; /* Another matching node found */
        } else {
            AVER(SplayNodeLeftChild(newRoot) == NULL);
            SplayNodeSetLeftChild(newRoot, oldRoot);
        }

        if (tree->updateNode != NULL) {
            SplayNodeUpdate(tree, oldRoot);
            SplayNodeUpdate(tree, newRoot);
        }
    }

    return newRoot;
}
コード例 #7
0
Res SplayTreeInsert(SplayTree tree, SplayNode node, void *key) {
    SplayNode neighbour;

    AVERT(SplayTree, tree);
    AVERT(SplayNode, node);
    AVER(SplayNodeLeftChild(node) == NULL);
    AVER(SplayNodeRightChild(node) == NULL);

    if (SplayTreeRoot(tree) == NULL) {
        SplayTreeSetRoot(tree, node);
    } else if (SplaySplay(&neighbour, tree, key, tree->compare)) {
        return ResFAIL;
    } else {
        AVER(SplayTreeRoot(tree) == neighbour);
        switch(SplayCompare(tree, key, neighbour)) {

        case CompareGREATER: { /* left neighbour */
            SplayTreeSetRoot(tree, node);
            SplayNodeSetRightChild(node, SplayNodeRightChild(neighbour));
            SplayNodeSetLeftChild(node, neighbour);
            SplayNodeSetRightChild(neighbour, NULL);
        }
        break;

        case CompareLESS: { /* right neighbour */
            SplayTreeSetRoot(tree, node);
            SplayNodeSetLeftChild(node, SplayNodeLeftChild(neighbour));
            SplayNodeSetRightChild(node, neighbour);
            SplayNodeSetLeftChild(neighbour, NULL);
        }
        break;

        case CompareEQUAL:
        default: {
            NOTREACHED;
        }
        break;
        }

        if (tree->updateNode != NULL) {
            SplayNodeUpdate(tree, neighbour);
            SplayNodeUpdate(tree, node);
        }
    }

    return ResOK;
}
コード例 #8
0
ファイル: splay.c プロジェクト: aseaday/mps-temporary
void SplayTreeFinish(SplayTree splay)
{
  AVERT(SplayTree, splay);
  splay->sig = SigInvalid;
  SplayTreeSetRoot(splay, TreeEMPTY);
  splay->compare = NULL;
  splay->nodeKey = NULL;
  splay->updateNode = NULL;
}
コード例 #9
0
ファイル: splay.c プロジェクト: Ravenbrook/mps
Bool SplayTreeInsert(SplayTree splay, Tree node)
{
  Tree neighbour;

  AVERT(SplayTree, splay);
  AVERT(Tree, node);
  AVER(TreeLeft(node) == TreeEMPTY);
  AVER(TreeRight(node) == TreeEMPTY);

  if (SplayTreeIsEmpty(splay)) {
    SplayTreeSetRoot(splay, node);
    return TRUE;
  }
  
  switch (SplaySplay(splay, splay->nodeKey(node), splay->compare)) {
  default:
    NOTREACHED;
    /* fall through */
  case CompareEQUAL: /* duplicate node */
    return FALSE;
    
  case CompareGREATER: /* left neighbour is at root */
    neighbour = SplayTreeRoot(splay);
    SplayTreeSetRoot(splay, node);
    TreeSetRight(node, TreeRight(neighbour));
    TreeSetLeft(node, neighbour);
    TreeSetRight(neighbour, TreeEMPTY);
    break;

  case CompareLESS: /* right neighbour is at root */
    neighbour = SplayTreeRoot(splay);
    SplayTreeSetRoot(splay, node);
    TreeSetLeft(node, TreeLeft(neighbour));
    TreeSetRight(node, neighbour);
    TreeSetLeft(neighbour, TreeEMPTY);
    break;
  }

  splay->updateNode(splay, neighbour);
  splay->updateNode(splay, node);
  return TRUE;
}
コード例 #10
0
ファイル: splay.c プロジェクト: aseaday/mps-temporary
Bool SplayTreeNeighbours(Tree *leftReturn, Tree *rightReturn,
                         SplayTree splay, TreeKey key)
{
  SplayStateStruct stateStruct;
  Bool found;
  Compare cmp;
#ifdef SPLAY_DEBUG
  Count count = SplayDebugCount(splay);
#endif


  AVERT(SplayTree, splay);
  AVER(leftReturn != NULL);
  AVER(rightReturn != NULL);

  if (SplayTreeIsEmpty(splay)) {
    *leftReturn = *rightReturn = TreeEMPTY;
    return TRUE;
  }

  cmp = SplaySplit(&stateStruct, splay, key, splay->compare);

  switch (cmp) {
  default:
    NOTREACHED;
    /* defensive fall-through */
  case CompareEQUAL:
    found = FALSE;
    break;

  case CompareLESS:
    AVER(!TreeHasLeft(stateStruct.middle));
    *rightReturn = stateStruct.middle;
    *leftReturn = stateStruct.leftLast;
    found = TRUE;
    break;

  case CompareGREATER:
    AVER(!TreeHasRight(stateStruct.middle));
    *leftReturn = stateStruct.middle;
    *rightReturn = stateStruct.rightFirst;
    found = TRUE;
    break;
  }

  SplayAssemble(splay, &stateStruct);
  SplayTreeSetRoot(splay, stateStruct.middle);

#ifdef SPLAY_DEBUG
  AVER(count == SplayDebugCount(splay));
#endif

  return found;
}
コード例 #11
0
ファイル: splay.c プロジェクト: Ravenbrook/mps
Bool SplayTreeDelete(SplayTree splay, Tree node)
{
  Tree leftLast;
  Compare cmp;

  AVERT(SplayTree, splay);
  AVERT(Tree, node);

  if (SplayTreeIsEmpty(splay))
    return FALSE;

  cmp = SplaySplay(splay, splay->nodeKey(node), splay->compare);
  AVER(cmp != CompareEQUAL || SplayTreeRoot(splay) == node);

  if (cmp != CompareEQUAL) {
    return FALSE;
  } else if (!TreeHasLeft(node)) {
    SplayTreeSetRoot(splay, TreeRight(node));
    TreeClearRight(node);
  } else if (!TreeHasRight(node)) {
    SplayTreeSetRoot(splay, TreeLeft(node));
    TreeClearLeft(node);
  } else {
    Tree rightHalf = TreeRight(node);
    TreeClearRight(node);
    SplayTreeSetRoot(splay, TreeLeft(node));
    TreeClearLeft(node);
    (void)SplaySplay(splay, NULL, compareGreater);
    leftLast = SplayTreeRoot(splay);
    AVER(leftLast != TreeEMPTY);
    AVER(!TreeHasRight(leftLast));
    TreeSetRight(leftLast, rightHalf);
    splay->updateNode(splay, leftLast);
  }

  TreeFinish(node);

  return TRUE;
}
コード例 #12
0
void SplayTreeInit(SplayTree tree, SplayCompareMethod compare,
                   SplayUpdateNodeMethod updateNode)
{
    AVER(tree != NULL);
    AVER(FUNCHECK(compare));
    AVER(updateNode == NULL || FUNCHECK(updateNode));

    tree->compare = compare;
    tree->updateNode = updateNode;
    SplayTreeSetRoot(tree, NULL);

    AVERT(SplayTree, tree);
}
コード例 #13
0
ファイル: splay.c プロジェクト: aseaday/mps-temporary
void SplayTreeInit(SplayTree splay,
                   TreeCompareFunction compare,
                   TreeKeyFunction nodeKey,
                   SplayUpdateNodeFunction updateNode)
{
  AVER(splay != NULL);
  AVER(FUNCHECK(compare));
  AVER(FUNCHECK(nodeKey));
  AVER(FUNCHECK(updateNode));

  splay->compare = compare;
  splay->nodeKey = nodeKey;
  splay->updateNode = updateNode;
  SplayTreeSetRoot(splay, TreeEMPTY);
  splay->sig = SplayTreeSig;

  AVERT(SplayTree, splay);
}
コード例 #14
0
void SplayTreeFinish(SplayTree tree)
{
    AVERT(SplayTree, tree);
    SplayTreeSetRoot(tree, NULL);
    tree->compare = NULL;
}
コード例 #15
0
static Bool SplaySplay(SplayNode *nodeReturn, SplayTree tree,
                       void *key, SplayCompareMethod compareMethod) {
    /* The sides structure avoids a boundary case in SplayLink* */
    SplayNodeStruct sides; /* rightTop and leftTop */
    SplayNode top, leftLast, rightFirst;
    Bool found;
    Compare compareTop;

    AVERT(SplayTree, tree);
    AVER(nodeReturn != NULL);
    AVER(FUNCHECK(compareMethod));

    top = SplayTreeRoot(tree); /* will be copied back at end */

    if (top == NULL) {
        *nodeReturn = NULL;
        return FALSE;
    }

    /* short-circuit case where node is already top */
    compareTop = (*compareMethod)(key, top);
    if (compareTop == CompareEQUAL) {
        *nodeReturn = top;
        return TRUE;
    }

    SplayNodeInit(&sides); /* left and right trees now NULL */
    leftLast = &sides;
    rightFirst = &sides;

    while(TRUE) {
        /* compareTop is already initialised above. */
        switch(compareTop) {

        case CompareLESS: {
            SplayNode topLeft = SplayNodeLeftChild(top);
            if (topLeft == NULL) {
                found = FALSE;
                goto assemble;
            } else {
                Compare compareTopLeft = (*compareMethod)(key, topLeft);

                switch(compareTopLeft) {

                case CompareEQUAL: {                 /* zig */
                    SplayLinkRight(&top, &rightFirst);
                    found = TRUE;
                    goto assemble;
                    } /* break; */

                case CompareLESS: {                  /* zig-zig */
                    if (SplayNodeLeftChild(topLeft) == NULL)
                        goto terminalZig;
                    SplayRotateRight(&top, tree);
                    SplayLinkRight(&top, &rightFirst);
                }
                break;

                case CompareGREATER: {               /* zig-zag */
                    if (SplayNodeRightChild(topLeft) == NULL)
                        goto terminalZig;
                    SplayLinkRight(&top, &rightFirst);
                    SplayLinkLeft(&top, &leftLast);
                }
                break;

                default: {
                    NOTREACHED;
                }
                break;
                }
            }
        }
        break;

        case CompareGREATER: {
            SplayNode topRight = SplayNodeRightChild(top);
            if (topRight == NULL) {
                found = FALSE;
                goto assemble;
            } else {
                Compare compareTopRight = (*compareMethod)(key, topRight);

                switch(compareTopRight) {

                case CompareEQUAL: {                 /* zag */
                    SplayLinkLeft(&top, &leftLast);
                    found = TRUE;
                    goto assemble;
                    } /* break; */

                case CompareGREATER: {               /* zag-zag */
                    if (SplayNodeRightChild(topRight) == NULL)
                        goto terminalZag;
                    SplayRotateLeft(&top, tree);
                    SplayLinkLeft(&top, &leftLast);
                }
                break;

                case CompareLESS: {                  /* zag-zig */
                    if (SplayNodeLeftChild(topRight) == NULL)
                        goto terminalZag;
                    SplayLinkLeft(&top, &leftLast);
                    SplayLinkRight(&top, &rightFirst);
                }
                break;

                default: {
                    NOTREACHED;
                }
                break;
                }
            }
        }
        break;

        case CompareEQUAL: {
            found = TRUE;
            goto assemble;
            } /* break; */

        default: {
            NOTREACHED;
        }
        break;
        }
        compareTop = (*compareMethod)(key, top);
    } /* end while(TRUE) */

terminalZig:
    SplayLinkRight(&top, &rightFirst);
    found = FALSE;
    goto assemble;

terminalZag:
    SplayLinkLeft(&top, &leftLast);
    found = FALSE;
    goto assemble;

assemble:
    SplayAssemble(tree, top,
                  SplayNodeRightChild(&sides), leftLast,
                  SplayNodeLeftChild(&sides), rightFirst);

    SplayTreeSetRoot(tree, top);
    *nodeReturn = top;

    return found;
}