various search tree fixes

This commit is contained in:
Joel Dice 2008-04-07 18:14:59 -06:00
parent 3fb90fdaa4
commit ebffc5852c

View File

@ -14,13 +14,6 @@ using namespace vm;
namespace { namespace {
inline object
cloneTreeNode(Thread* t, object n)
{
return makeTreeNode
(t, treeNodeValue(t, n), treeNodeLeft(t, n), treeNodeRight(t, n));
}
inline object inline object
getTreeNodeValue(Thread*, object n) getTreeNodeValue(Thread*, object n)
{ {
@ -45,9 +38,22 @@ treeNodeRed(Thread*, object n)
} }
inline void inline void
setTreeNodeRed(Thread*, object n, intptr_t red) setTreeNodeRed(Thread*, object n, bool red)
{ {
cast<intptr_t>(n, TreeNodeValue) |= red; if (red) {
cast<intptr_t>(n, TreeNodeValue) |= 1;
} else {
cast<intptr_t>(n, TreeNodeValue) &= PointerMask;
}
}
inline object
cloneTreeNode(Thread* t, object n)
{
object newNode = makeTreeNode
(t, getTreeNodeValue(t, n), treeNodeLeft(t, n), treeNodeRight(t, n));
setTreeNodeRed(t, newNode, treeNodeRed(t, n));
return newNode;
} }
object object
@ -124,7 +130,7 @@ treeAdd(Thread* t, object path)
PROTECT(t, ancestors); PROTECT(t, ancestors);
// rebalance // rebalance
setTreeNodeRed(t, new_, 1); setTreeNodeRed(t, new_, true);
while (ancestors != 0 and treeNodeRed(t, pairFirst(t, ancestors))) { while (ancestors != 0 and treeNodeRed(t, pairFirst(t, ancestors))) {
if (pairFirst(t, ancestors) if (pairFirst(t, ancestors)
== treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors)))) == treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors))))
@ -132,7 +138,7 @@ treeAdd(Thread* t, object path)
if (treeNodeRed if (treeNodeRed
(t, treeNodeRight(t, pairFirst(t, pairSecond(t, ancestors))))) (t, treeNodeRight(t, pairFirst(t, pairSecond(t, ancestors)))))
{ {
setTreeNodeRed(t, pairFirst(t, ancestors), 1); setTreeNodeRed(t, pairFirst(t, ancestors), true);
object n = cloneTreeNode object n = cloneTreeNode
(t, treeNodeRight(t, pairFirst(t, pairSecond(t, ancestors)))); (t, treeNodeRight(t, pairFirst(t, pairSecond(t, ancestors))));
@ -141,9 +147,9 @@ treeAdd(Thread* t, object path)
setTreeNodeRed setTreeNodeRed
(t, treeNodeRight (t, treeNodeRight
(t, pairFirst(t, pairSecond(t, ancestors))), 0); (t, pairFirst(t, pairSecond(t, ancestors))), false);
setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), 0); setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), false);
new_ = pairFirst(t, pairSecond(t, ancestors)); new_ = pairFirst(t, pairSecond(t, ancestors));
ancestors = pairSecond(t, pairSecond(t, ancestors)); ancestors = pairSecond(t, pairSecond(t, ancestors));
@ -161,8 +167,8 @@ treeAdd(Thread* t, object path)
} }
ancestors = makePair(t, n, ancestors); ancestors = makePair(t, n, ancestors);
} }
setTreeNodeRed(t, pairFirst(t, ancestors), 0); setTreeNodeRed(t, pairFirst(t, ancestors), false);
setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), 1); setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), true);
object n = rightRotate(t, pairFirst(t, pairSecond(t, ancestors))); object n = rightRotate(t, pairFirst(t, pairSecond(t, ancestors)));
if (pairSecond(t, pairSecond(t, ancestors)) == 0) { if (pairSecond(t, pairSecond(t, ancestors)) == 0) {
@ -184,7 +190,7 @@ treeAdd(Thread* t, object path)
if (treeNodeRed if (treeNodeRed
(t, treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors))))) (t, treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors)))))
{ {
setTreeNodeRed(t, pairFirst(t, ancestors), 1); setTreeNodeRed(t, pairFirst(t, ancestors), true);
object n = cloneTreeNode object n = cloneTreeNode
(t, treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors)))); (t, treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors))));
@ -193,9 +199,9 @@ treeAdd(Thread* t, object path)
setTreeNodeRed setTreeNodeRed
(t, treeNodeLeft (t, treeNodeLeft
(t, pairFirst(t, pairSecond(t, ancestors))), 0); (t, pairFirst(t, pairSecond(t, ancestors))), false);
setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), 0); setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), false);
new_ = pairFirst(t, pairSecond(t, ancestors)); new_ = pairFirst(t, pairSecond(t, ancestors));
ancestors = pairSecond(t, pairSecond(t, ancestors)); ancestors = pairSecond(t, pairSecond(t, ancestors));
@ -213,8 +219,8 @@ treeAdd(Thread* t, object path)
} }
ancestors = makePair(t, n, ancestors); ancestors = makePair(t, n, ancestors);
} }
setTreeNodeRed(t, pairFirst(t, ancestors), 0); setTreeNodeRed(t, pairFirst(t, ancestors), false);
setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), 1); setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), true);
object n = leftRotate(t, pairFirst(t, pairSecond(t, ancestors))); object n = leftRotate(t, pairFirst(t, pairSecond(t, ancestors)));
if (pairSecond(t, pairSecond(t, ancestors)) == 0) { if (pairSecond(t, pairSecond(t, ancestors)) == 0) {
@ -234,7 +240,7 @@ treeAdd(Thread* t, object path)
} }
} }
setTreeNodeRed(t, newRoot, 0); setTreeNodeRed(t, newRoot, false);
return newRoot; return newRoot;
} }