From ebffc5852c801a3bef98d4dd817871991515f079 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Mon, 7 Apr 2008 18:14:59 -0600 Subject: [PATCH] various search tree fixes --- src/util.cpp | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 31de5bfac0..4b6db26fd0 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -14,13 +14,6 @@ using namespace vm; namespace { -inline object -cloneTreeNode(Thread* t, object n) -{ - return makeTreeNode - (t, treeNodeValue(t, n), treeNodeLeft(t, n), treeNodeRight(t, n)); -} - inline object getTreeNodeValue(Thread*, object n) { @@ -45,9 +38,22 @@ treeNodeRed(Thread*, object n) } inline void -setTreeNodeRed(Thread*, object n, intptr_t red) +setTreeNodeRed(Thread*, object n, bool red) { - cast(n, TreeNodeValue) |= red; + if (red) { + cast(n, TreeNodeValue) |= 1; + } else { + cast(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 @@ -124,7 +130,7 @@ treeAdd(Thread* t, object path) PROTECT(t, ancestors); // rebalance - setTreeNodeRed(t, new_, 1); + setTreeNodeRed(t, new_, true); while (ancestors != 0 and treeNodeRed(t, pairFirst(t, ancestors))) { if (pairFirst(t, ancestors) == treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors)))) @@ -132,7 +138,7 @@ treeAdd(Thread* t, object path) if (treeNodeRed (t, treeNodeRight(t, pairFirst(t, pairSecond(t, ancestors))))) { - setTreeNodeRed(t, pairFirst(t, ancestors), 1); + setTreeNodeRed(t, pairFirst(t, ancestors), true); object n = cloneTreeNode (t, treeNodeRight(t, pairFirst(t, pairSecond(t, ancestors)))); @@ -141,9 +147,9 @@ treeAdd(Thread* t, object path) setTreeNodeRed (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)); ancestors = pairSecond(t, pairSecond(t, ancestors)); @@ -161,8 +167,8 @@ treeAdd(Thread* t, object path) } ancestors = makePair(t, n, ancestors); } - setTreeNodeRed(t, pairFirst(t, ancestors), 0); - setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), 1); + setTreeNodeRed(t, pairFirst(t, ancestors), false); + setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), true); object n = rightRotate(t, pairFirst(t, pairSecond(t, ancestors))); if (pairSecond(t, pairSecond(t, ancestors)) == 0) { @@ -184,7 +190,7 @@ treeAdd(Thread* t, object path) if (treeNodeRed (t, treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors))))) { - setTreeNodeRed(t, pairFirst(t, ancestors), 1); + setTreeNodeRed(t, pairFirst(t, ancestors), true); object n = cloneTreeNode (t, treeNodeLeft(t, pairFirst(t, pairSecond(t, ancestors)))); @@ -193,9 +199,9 @@ treeAdd(Thread* t, object path) setTreeNodeRed (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)); ancestors = pairSecond(t, pairSecond(t, ancestors)); @@ -213,8 +219,8 @@ treeAdd(Thread* t, object path) } ancestors = makePair(t, n, ancestors); } - setTreeNodeRed(t, pairFirst(t, ancestors), 0); - setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), 1); + setTreeNodeRed(t, pairFirst(t, ancestors), false); + setTreeNodeRed(t, pairFirst(t, pairSecond(t, ancestors)), true); object n = leftRotate(t, pairFirst(t, pairSecond(t, ancestors))); 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; }