/* -->
<h2>B-tree Implementation notes</h2>
This implements B-trees with several refinements. Most of them can be found
in Knuth Vol 3, but some were developed to adapt to restrictions imposed
by C++. First, a restatement of Knuth's properties that a B-tree must
satisfy, assuming we make the enhancement he suggests in the paragraph
at the bottom of page 476. Instead of storing null pointers to non-existent
nodes (which Knuth calls the leaves) we utilize the space to store keys.
Therefore, what Knuth calls level (l-1) is the bottom of our tree, and
we call the nodes at this level LeafNodes. Other nodes are called InnerNodes.
The other enhancement we have adopted is in the paragraph at the bottom of
page 477: overflow control.
<p>
The following are modifications of Knuth's properties on page 478:
<p>
<ol>
<li> Every InnerNode has at most Order keys, and at most Order+1 sub-trees.
<li> Every LeafNode has at most 2*(Order+1) keys.
<li> An InnerNode with k keys has k+1 sub-trees.
<li> Every InnerNode that is not the root has at least InnerLowWaterMark keys.
<li> Every LeafNode that is not the root has at least LeafLowWaterMark keys.
<li> If the root is a LeafNode, it has at least one key.
<li> If the root is an InnerNode, it has at least one key and two sub-trees.
<li> All LeafNodes are the same distance from the root as all the other
LeafNodes.
<li> For InnerNode n with key n[i].key, then sub-tree n[i-1].tree contains
all keys < n[i].key, and sub-tree n[i].tree contains all keys
>= n[i].key.
<li> Order is at least 3.
</ol>
<p>
The values of InnerLowWaterMark and LeafLowWaterMark may actually be set
by the user when the tree is initialized, but currently they are set
automatically to:
<p><pre>
InnerLowWaterMark = ceiling(Order/2)
LeafLowWaterMark = Order - 1
</pre><p>
If the tree is only filled, then all the nodes will be at least 2/3 full.
They will almost all be exactly 2/3 full if the elements are added to the
tree in order (either increasing or decreasing). [Knuth says McCreight's
experiments showed almost 100% memory utilization. I don't see how that
can be given the algorithms that Knuth gives. McCreight must have used
a different scheme for balancing. [No, he used a different scheme for
splitting: he did a two-way split instead of the three way split as we do
here. Which means that McCreight does better on insertion of ordered data,
but we should do better on insertion of random data.]]
<p>
It must also be noted that B-trees were designed for DISK access algorithms,
not necessarily in-memory sorting, as we intend it to be used here. However,
if the order is kept small (< 6?) any inefficiency is negligible for
in-memory sorting. Knuth points out that balanced trees are actually
preferable for memory sorting. I'm not sure that I believe this, but
it's interesting. Also, deleting elements from balanced binary trees, being
beyond the scope of Knuth's book (p. 465), is beyond my scope. B-trees
are good enough.
<p>
A B-tree is declared to be of a certain ORDER (3 by default). This number
determines the number of keys contained in any interior node of the tree.
Each interior node will contain ORDER keys, and therefore ORDER+1 pointers
to sub-trees. The keys are numbered and indexed 1 to ORDER while the
pointers are numbered and indexed 0 to ORDER. The 0th ptr points to the
sub-tree of all elements that are less than key[1]. Ptr[1] points to the
sub-tree that contains all the elements greater than key[1] and less than
key[2]. etc. The array of pointers and keys is allocated as ORDER+1
pairs of keys and nodes, meaning that one key field (key[0]) is not used
and therefore wasted. Given that the number of interior nodes is
small, that this waste allows fewer cases of special code, and that it
is useful in certain of the methods, it was felt to be a worthwhile waste.
<p>
The size of the exterior nodes (leaf nodes) does not need to be related to
the size of the interior nodes at all. Since leaf nodes contain only
keys, they may be as large or small as we like independent of the size
of the interior nodes. For no particular reason other than it seems like
a good idea, we will allocate 2*(ORDER+1) keys in each leaf node, and they
will be numbered and indexed from 0 to 2*ORDER+1. It does have the advantage
of keeping the size of the leaf and interior arrays the same, so that if we
find allocation and de-allocation of these arrays expensive, we can modify
their allocation to use a garbage ring, or something.
<p>
Both of these numbers will be run-time constants associated with each tree
(each tree at run-time can be of a different order). The variable "order"
is the order of the tree, and the inclusive upper limit on the indices of
the keys in the interior nodes. The variable "order2" is the inclusive
upper limit on the indices of the leaf nodes, and is designed
<p><pre>
(1) to keep the sizes of the two kinds of nodes the same;
(2) to keep the expressions involving the arrays of keys looking
somewhat the same: lower limit upper limit
for inner nodes: 1 order
for leaf nodes: 0 order2
Remember that index 0 of the inner nodes is special.
</pre><p>
Currently, order2 = 2*(order+1).
<p><pre>
Picture: (also see Knuth Vol 3 pg 478)
+--+--+--+--+--+--...
| | | | | |
parent--->| | | |
| | | |
+*-+*-+*-+--+--+--...
| | |
+----+ | +-----+
| +-----+ |
V | V
+----------+ | +----------+
| | | | |
this->| | | | |<--sib
+----------+ | +----------+
V
data
</pre><p>
It is conceptually VERY convenient to think of the data as being the
very first element of the sib node. Any primitive that tells sib to
perform some action on n nodes should include this 'hidden' element.
For InnerNodes, the hidden element has (physical) index 0 in the array,
and in LeafNodes, the hidden element has (virtual) index -1 in the array.
Therefore, there are two 'size' primitives for nodes:
<p><pre>
Psize - the physical size: how many elements are contained in the
array in the node.
Vsize - the 'virtual' size; if the node is pointed to by
element 0 of the parent node, then Vsize == Psize;
otherwise the element in the parent item that points to this
node 'belongs' to this node, and Vsize == Psize+1;
</pre><p>
Parent nodes are always InnerNodes.
<p>
These are the primitive operations on Nodes:
<p><pre>
Append(elt) - adds an element to the end of the array of elements in a
node. It must never be called where appending the element
would fill the node.
Split() - divide a node in two, and create two new nodes.
SplitWith(sib) - create a third node between this node and the sib node,
divvying up the elements of their arrays.
PushLeft(n) - move n elements into the left sibling
PushRight(n) - move n elements into the right sibling
BalanceWithRight() - even up the number of elements in the two nodes.
BalanceWithLeft() - ditto
</pre><p>
To allow this implementation of btrees to also be an implementation of
sorted arrays/lists, the overhead is included to allow O(log n) access
of elements by their rank (`give me the 5th largest element').
Therefore, each Item keeps track of the number of keys in and below it
in the tree (remember, each item's tree is all keys to the RIGHT of the
item's own key).
<p><pre>
[ [ < 0 1 2 3 > 4 < 5 6 7 > 8 < 9 10 11 12 > ] 13 [ < 14 15 16 > 17 < 18 19 20 > ] ]
4 1 1 1 1 4 1 1 1 5 1 1 1 1 7 3 1 1 1 4 1 1 1
</pre><p>
<!--*/
// -->END_HTML
#include <stdlib.h>
#include "TBtree.h"
ClassImp(TBtree)
TBtree::TBtree(int order)
{
Init(order);
}
TBtree::~TBtree()
{
if (fRoot) {
Clear();
SafeDelete(fRoot);
}
}
void TBtree::Add(TObject *obj)
{
if (IsArgNull("Add", obj)) return;
if (!obj->IsSortable()) {
Error("Add", "object must be sortable");
return;
}
if (!fRoot) {
fRoot = new TBtLeafNode(0, obj, this);
R__CHECK(fRoot != 0);
IncrNofKeys();
} else {
TBtNode *loc;
Int_t idx;
if (fRoot->Found(obj, &loc, &idx) != 0) {
}
loc->Add(obj, idx);
}
}
TObject *TBtree::After(const TObject *) const
{
MayNotUse("After");
return 0;
}
TObject *TBtree::Before(const TObject *) const
{
MayNotUse("Before");
return 0;
}
void TBtree::Clear(Option_t *)
{
if (IsOwner())
Delete();
else {
SafeDelete(fRoot);
fSize = 0;
}
}
void TBtree::Delete(Option_t *)
{
for (Int_t i = 0; i < fSize; i++) {
TObject *obj = At(i);
if (obj && obj->IsOnHeap())
TCollection::GarbageCollect(obj);
}
SafeDelete(fRoot);
fSize = 0;
}
TObject *TBtree::FindObject(const char *name) const
{
return TCollection::FindObject(name);
}
TObject *TBtree::FindObject(const TObject *obj) const
{
if (!obj->IsSortable()) {
Error("FindObject", "object must be sortable");
return 0;
}
if (!fRoot)
return 0;
else {
TBtNode *loc;
Int_t idx;
return fRoot->Found(obj, &loc, &idx);
}
}
Int_t TBtree::IdxAdd(const TObject &obj)
{
Int_t r;
if (!obj.IsSortable()) {
Error("IdxAdd", "object must be sortable");
return -1;
}
if (!fRoot) {
fRoot = new TBtLeafNode(0, &obj, this);
R__ASSERT(fRoot != 0);
IncrNofKeys();
r = 0;
} else {
TBtNode *loc;
int idx;
if (fRoot->Found(&obj, &loc, &idx) != 0) {
} else {
R__CHECK(loc->fIsLeaf);
}
if (loc->fIsLeaf) {
if (loc->fParent == 0)
r = idx;
else
r = idx + loc->fParent->FindRankUp(loc);
} else {
TBtInnerNode *iloc = (TBtInnerNode*) loc;
r = iloc->FindRankUp(iloc->GetTree(idx));
}
loc->Add(&obj, idx);
}
R__CHECK(r == Rank(&obj) || &obj == (*this)[r]);
return r;
}
void TBtree::Init(Int_t order)
{
if (order < 3) {
Warning("Init", "order must be at least 3");
order = 3;
}
fRoot = 0;
fOrder = order;
fOrder2 = 2 * (fOrder+1);
fLeafMaxIndex = fOrder2 - 1;
fInnerMaxIndex = fOrder;
fLeafLowWaterMark = ((fLeafMaxIndex+1)-1) / 2 - 1;
fInnerLowWaterMark = (fOrder-1) / 2;
}
TIterator *TBtree::MakeIterator(Bool_t dir) const
{
return new TBtreeIter(this, dir);
}
Int_t TBtree::Rank(const TObject *obj) const
{
if (!obj->IsSortable()) {
Error("Rank", "object must be sortable");
return -1;
}
if (!fRoot)
return -1;
else
return fRoot->FindRank(obj);
}
TObject *TBtree::Remove(TObject *obj)
{
if (!obj->IsSortable()) {
Error("Remove", "object must be sortable");
return 0;
}
if (!fRoot)
return 0;
TBtNode *loc;
Int_t idx;
TObject *ob = fRoot->Found(obj, &loc, &idx);
if (!ob)
return 0;
loc->Remove(idx);
return ob;
}
void TBtree::RootIsFull()
{
TBtNode *oldroot = fRoot;
fRoot = new TBtInnerNode(0, this, oldroot);
R__ASSERT(fRoot != 0);
oldroot->Split();
}
void TBtree::RootIsEmpty()
{
if (fRoot->fIsLeaf) {
TBtLeafNode *lroot = (TBtLeafNode*)fRoot;
R__CHECK(lroot->Psize() == 0);
delete lroot;
fRoot = 0;
} else {
TBtInnerNode *iroot = (TBtInnerNode*)fRoot;
R__CHECK(iroot->Psize() == 0);
fRoot = iroot->GetTree(0);
fRoot->fParent = 0;
delete iroot;
}
}
void TBtree::Streamer(TBuffer &b)
{
UInt_t R__s, R__c;
if (b.IsReading()) {
b.ReadVersion(&R__s, &R__c);
b >> fOrder;
b >> fOrder2;
b >> fInnerLowWaterMark;
b >> fLeafLowWaterMark;
b >> fInnerMaxIndex;
b >> fLeafMaxIndex;
TSeqCollection::Streamer(b);
b.CheckByteCount(R__s, R__c,TBtree::IsA());
} else {
R__c = b.WriteVersion(TBtree::IsA(), kTRUE);
b << fOrder;
b << fOrder2;
b << fInnerLowWaterMark;
b << fLeafLowWaterMark;
b << fInnerMaxIndex;
b << fLeafMaxIndex;
TSeqCollection::Streamer(b);
b.SetByteCount(R__c, kTRUE);
}
}
TBtItem::TBtItem()
{
fNofKeysInTree = 0;
fTree = 0;
fKey = 0;
}
TBtItem::TBtItem(TBtNode *n, TObject *obj)
{
fNofKeysInTree = n->NofKeys()+1;
fTree = n;
fKey = obj;
}
TBtItem::TBtItem(TObject *obj, TBtNode *n)
{
fNofKeysInTree = n->NofKeys()+1;
fTree = n;
fKey = obj;
}
TBtItem::~TBtItem()
{
}
TBtNode::TBtNode(Int_t isleaf, TBtInnerNode *p, TBtree *t)
{
fLast = -1;
fIsLeaf = isleaf;
fParent = p;
if (p == 0) {
R__CHECK(t != 0);
fTree = t;
} else
#ifdef cxxbug
fTree = p->GetParentTree();
#else
fTree = p->fTree;
#endif
}
TBtNode::~TBtNode()
{
}
ClassImp(TBtreeIter)
TBtreeIter::TBtreeIter(const TBtree *t, Bool_t dir)
: fTree(t), fCurCursor(0), fCursor(0), fDirection(dir)
{
Reset();
}
TBtreeIter::TBtreeIter(const TBtreeIter &iter) : TIterator(iter)
{
fTree = iter.fTree;
fCursor = iter.fCursor;
fCurCursor = iter.fCurCursor;
fDirection = iter.fDirection;
}
TIterator &TBtreeIter::operator=(const TIterator &rhs)
{
if (this != &rhs && rhs.IsA() == TBtreeIter::Class()) {
const TBtreeIter &rhs1 = (const TBtreeIter &)rhs;
fTree = rhs1.fTree;
fCursor = rhs1.fCursor;
fCurCursor = rhs1.fCurCursor;
fDirection = rhs1.fDirection;
}
return *this;
}
TBtreeIter &TBtreeIter::operator=(const TBtreeIter &rhs)
{
if (this != &rhs) {
fTree = rhs.fTree;
fCursor = rhs.fCursor;
fCurCursor = rhs.fCurCursor;
fDirection = rhs.fDirection;
}
return *this;
}
void TBtreeIter::Reset()
{
if (fDirection == kIterForward)
fCursor = 0;
else
fCursor = fTree->GetSize() - 1;
fCurCursor = fCursor;
}
TObject *TBtreeIter::Next()
{
fCurCursor = fCursor;
if (fDirection == kIterForward) {
if (fCursor < fTree->GetSize())
return (*fTree)[fCursor++];
} else {
if (fCursor >= 0)
return (*fTree)[fCursor--];
}
return 0;
}
bool TBtreeIter::operator!=(const TIterator &aIter) const
{
if (nullptr == (&aIter))
return (fCurCursor < fTree->GetSize());
if (aIter.IsA() == TBtreeIter::Class()) {
const TBtreeIter &iter(dynamic_cast<const TBtreeIter &>(aIter));
return (fCurCursor != iter.fCurCursor);
}
return false;
}
bool TBtreeIter::operator!=(const TBtreeIter &aIter) const
{
if (nullptr == (&aIter))
return (fCurCursor < fTree->GetSize());
return (fCurCursor != aIter.fCurCursor);
}
TObject* TBtreeIter::operator*() const
{
return (((fCurCursor >= 0) && (fCurCursor < fTree->GetSize())) ?
(*fTree)[fCurCursor] : nullptr);
}
TBtInnerNode::TBtInnerNode(TBtInnerNode *p, TBtree *t) : TBtNode(0,p,t)
{
const Int_t index = MaxIndex() + 1;
fItem = new TBtItem[ index ];
if (fItem == 0)
::Fatal("TBtInnerNode::TBtInnerNode", "no more memory");
}
TBtInnerNode::TBtInnerNode(TBtInnerNode *parent, TBtree *tree, TBtNode *oldroot)
: TBtNode(0, parent, tree)
{
fItem = new TBtItem[MaxIndex()+1];
if (fItem == 0)
::Fatal("TBtInnerNode::TBtInnerNode", "no more memory");
Append(0, oldroot);
}
TBtInnerNode::~TBtInnerNode()
{
if (fLast > 0)
delete fItem[0].fTree;
for (Int_t i = 1; i <= fLast; i++)
delete fItem[i].fTree;
delete [] fItem;
}
void TBtInnerNode::Add(const TObject *obj, Int_t index)
{
R__ASSERT(index >= 1 && obj->IsSortable());
TBtLeafNode *ln = GetTree(index-1)->LastLeafNode();
ln->Add(obj, ln->fLast+1);
}
void TBtInnerNode::AddElt(TBtItem &itm, Int_t at)
{
R__ASSERT(0 <= at && at <= fLast+1);
R__ASSERT(fLast < MaxIndex());
for (Int_t i = fLast+1; i > at ; i--)
GetItem(i) = GetItem(i-1);
SetItem(at, itm);
fLast++;
}
void TBtInnerNode::AddElt(Int_t at, TObject *k, TBtNode *t)
{
TBtItem newitem(k, t);
AddElt(newitem, at);
}
void TBtInnerNode::Add(TBtItem &itm, Int_t at)
{
AddElt(itm, at);
if (IsFull())
InformParent();
}
void TBtInnerNode::Add(Int_t at, TObject *k, TBtNode *t)
{
TBtItem newitem(k, t);
Add(newitem, at);
}
void TBtInnerNode::AppendFrom(TBtInnerNode *src, Int_t start, Int_t stop)
{
if (start > stop)
return;
R__ASSERT(0 <= start && start <= src->fLast);
R__ASSERT(0 <= stop && stop <= src->fLast );
R__ASSERT(fLast + stop - start + 1 < MaxIndex());
for (Int_t i = start; i <= stop; i++)
SetItem(++fLast, src->GetItem(i));
}
void TBtInnerNode::Append(TObject *d, TBtNode *n)
{
R__ASSERT(fLast < MaxIndex());
if (d) R__ASSERT(d->IsSortable());
SetItem(++fLast, d, n);
}
void TBtInnerNode::Append(TBtItem &itm)
{
R__ASSERT(fLast < MaxIndex());
SetItem(++fLast, itm);
}
void TBtInnerNode::BalanceWithLeft(TBtInnerNode *leftsib, Int_t pidx)
{
R__ASSERT(Vsize() >= leftsib->Psize());
R__ASSERT(fParent->GetTree(pidx) == this);
Int_t newThisSize = (Vsize() + leftsib->Psize())/2;
Int_t noFromThis = Psize() - newThisSize;
PushLeft(noFromThis, leftsib, pidx);
}
void TBtInnerNode::BalanceWithRight(TBtInnerNode *rightsib, Int_t pidx)
{
R__ASSERT(Psize() >= rightsib->Vsize());
R__ASSERT(fParent->GetTree(pidx) == rightsib);
Int_t newThisSize = (Psize() + rightsib->Vsize())/2;
Int_t noFromThis = Psize() - newThisSize;
PushRight(noFromThis, rightsib, pidx);
}
void TBtInnerNode::BalanceWith(TBtInnerNode *rightsib, Int_t pindx)
{
if (Psize() < rightsib->Vsize())
rightsib->BalanceWithLeft(this, pindx);
else
BalanceWithRight(rightsib, pindx);
}
void TBtInnerNode::DecrNofKeys(TBtNode *that)
{
Int_t i = IndexOf(that);
fItem[i].fNofKeysInTree--;
if (fParent != 0)
fParent->DecrNofKeys(this);
else
fTree->DecrNofKeys();
}
Int_t TBtInnerNode::FindRank(const TObject *what) const
{
if (((TObject *)what)->Compare(GetKey(1)) < 0)
return GetTree(0)->FindRank(what);
Int_t sum = GetNofKeys(0);
for (Int_t i = 1; i < fLast; i++) {
if (((TObject*)what)->Compare(GetKey(i)) == 0)
return sum;
sum++;
if (((TObject *)what)->Compare(GetKey(i+1)) < 0)
return sum + GetTree(i)->FindRank(what);
sum += GetNofKeys(i);
}
if (((TObject*)what)->Compare(GetKey(fLast)) == 0)
return sum;
sum++;
return sum + GetTree(fLast)->FindRank(what);
}
Int_t TBtInnerNode::FindRankUp(const TBtNode *that) const
{
Int_t l = IndexOf(that);
Int_t sum = 0;
for (Int_t i = 0; i < l; i++)
sum += GetNofKeys(i);
return sum + l + (fParent == 0 ? 0 : fParent->FindRankUp(this));
}
TBtLeafNode *TBtInnerNode::FirstLeafNode()
{
return GetTree(0)->FirstLeafNode();
}
TObject *TBtInnerNode::Found(const TObject *what, TBtNode **which, Int_t *where)
{
R__ASSERT(what->IsSortable());
for (Int_t i = 1 ; i <= fLast; i++) {
if (GetKey(i)->Compare(what) == 0) {
*which = this;
*where = i;
return GetKey(i);
}
if (GetKey(i)->Compare(what) > 0)
return GetTree(i-1)->Found(what, which, where);
}
return GetTree(fLast)->Found(what, which, where);
}
void TBtInnerNode::IncrNofKeys(TBtNode *that)
{
Int_t i = IndexOf(that);
fItem[i].fNofKeysInTree++;
if (fParent != 0)
fParent->IncrNofKeys(this);
else
fTree->IncrNofKeys();
}
Int_t TBtInnerNode::IndexOf(const TBtNode *that) const
{
for (Int_t i = 0; i <= fLast; i++)
if (GetTree(i) == that)
return i;
R__CHECK(0);
return 0;
}
void TBtInnerNode::InformParent()
{
if (fParent == 0) {
R__ASSERT(fTree->fRoot == this);
fTree->RootIsFull();
} else
fParent->IsFull(this);
}
void TBtInnerNode::IsFull(TBtNode *that)
{
if (that->fIsLeaf) {
TBtLeafNode *leaf = (TBtLeafNode *)that;
TBtLeafNode *left = 0;
TBtLeafNode *right= 0;
Int_t leafidx = IndexOf(leaf);
Int_t hasRightSib = (leafidx < fLast) &&
((right = (TBtLeafNode*)GetTree(leafidx+1)) != 0);
Int_t hasLeftSib = (leafidx > 0) &&
((left = (TBtLeafNode*)GetTree(leafidx-1)) != 0);
Int_t rightSibFull = (hasRightSib && right->IsAlmostFull());
Int_t leftSibFull = (hasLeftSib && left->IsAlmostFull());
if (rightSibFull) {
if (leftSibFull) {
left->SplitWith(leaf, leafidx);
} else if (hasLeftSib) {
leaf->BalanceWithLeft(left, leafidx);
} else {
leaf->SplitWith(right, leafidx+1);
}
} else if (hasRightSib) {
leaf->BalanceWithRight(right, leafidx+1);
} else if (leftSibFull) {
left->SplitWith(leaf, leafidx);
} else if (hasLeftSib) {
leaf->BalanceWithLeft(left, leafidx);
} else {
R__CHECK(0);
}
} else {
TBtInnerNode *inner = (TBtInnerNode *)that;
Int_t inneridx = IndexOf(inner);
TBtInnerNode *left = 0;
TBtInnerNode *right= 0;
Int_t hasRightSib = (inneridx < fLast) &&
((right = (TBtInnerNode*)GetTree(inneridx+1)) != 0);
Int_t hasLeftSib = (inneridx > 0) &&
((left=(TBtInnerNode*)GetTree(inneridx-1)) != 0);
Int_t rightSibFull = (hasRightSib && right->IsAlmostFull());
Int_t leftSibFull = (hasLeftSib && left->IsAlmostFull());
if (rightSibFull) {
if (leftSibFull) {
left->SplitWith(inner, inneridx);
} else if (hasLeftSib) {
inner->BalanceWithLeft(left, inneridx);
} else {
inner->SplitWith(right, inneridx+1);
}
} else if (hasRightSib) {
inner->BalanceWithRight(right, inneridx+1);
} else if (leftSibFull) {
left->SplitWith(inner, inneridx);
} else if (hasLeftSib) {
inner->BalanceWithLeft(left, inneridx);
} else {
R__CHECK(0);
}
}
}
void TBtInnerNode::IsLow(TBtNode *that)
{
if (that->fIsLeaf) {
TBtLeafNode *leaf = (TBtLeafNode *)that;
TBtLeafNode *left = 0;
TBtLeafNode *right= 0;
Int_t leafidx = IndexOf(leaf);
Int_t hasRightSib = (leafidx < fLast) &&
((right = (TBtLeafNode*)GetTree(leafidx+1)) != 0);
Int_t hasLeftSib = (leafidx > 0) &&
((left = (TBtLeafNode*)GetTree(leafidx-1)) != 0);
if (hasRightSib && (leaf->Psize() + right->Vsize()) >= leaf->MaxPsize()) {
leaf->BalanceWith(right, leafidx+1);
} else if (hasLeftSib && (leaf->Vsize() + left->Psize()) >= leaf->MaxPsize()) {
left->BalanceWith(leaf, leafidx);
} else if (hasLeftSib) {
left->MergeWithRight(leaf, leafidx);
} else if (hasRightSib) {
leaf->MergeWithRight(right, leafidx+1);
} else {
R__CHECK(0);
}
} else {
TBtInnerNode *inner = (TBtInnerNode *)that;
Int_t inneridx = IndexOf(inner);
TBtInnerNode *left = 0;
TBtInnerNode *right= 0;
Int_t hasRightSib = (inneridx < fLast) &&
((right = (TBtInnerNode*)GetTree(inneridx+1)) != 0);
Int_t hasLeftSib = (inneridx > 0) &&
((left = (TBtInnerNode*)GetTree(inneridx-1)) != 0);
if (hasRightSib && (inner->Psize() + right->Vsize()) >= inner->MaxPsize()) {
inner->BalanceWith(right, inneridx+1);
} else if (hasLeftSib && (inner->Vsize() + left->Psize()) >= inner->MaxPsize()) {
left->BalanceWith(inner, inneridx);
} else if (hasLeftSib) {
left->MergeWithRight(inner, inneridx);
} else if (hasRightSib) {
inner->MergeWithRight(right, inneridx+1);
} else {
R__CHECK(0);
}
}
}
TBtLeafNode *TBtInnerNode::LastLeafNode()
{
return GetTree(fLast)->LastLeafNode();
}
void TBtInnerNode::MergeWithRight(TBtInnerNode *rightsib, Int_t pidx)
{
R__ASSERT(Psize() + rightsib->Vsize() < MaxIndex());
if (rightsib->Psize() > 0)
rightsib->PushLeft(rightsib->Psize(), this, pidx);
rightsib->SetKey(0, fParent->GetKey(pidx));
AppendFrom(rightsib, 0, 0);
fParent->IncNofKeys(pidx-1, rightsib->GetNofKeys(0)+1);
fParent->RemoveItem(pidx);
delete rightsib;
}
Int_t TBtInnerNode::NofKeys() const
{
Int_t sum = 0;
for (Int_t i = 0; i <= fLast; i++)
sum += GetNofKeys(i);
return sum + Psize();
}
TObject *TBtInnerNode::operator[](Int_t idx) const
{
for (Int_t j = 0; j <= fLast; j++) {
Int_t r;
if (idx < (r = GetNofKeys(j)))
return (*GetTree(j))[idx];
if (idx == r) {
if (j == fLast) {
::Error("TBtInnerNode::operator[]", "should not happen, 0 returned");
return 0;
} else
return GetKey(j+1);
}
idx -= r+1;
}
::Error("TBtInnerNode::operator[]", "should not happen, 0 returned");
return 0;
}
void TBtInnerNode::PushLeft(Int_t noFromThis, TBtInnerNode *leftsib, Int_t pidx)
{
R__ASSERT(fParent->GetTree(pidx) == this);
R__ASSERT(noFromThis > 0 && noFromThis <= Psize());
R__ASSERT(noFromThis + leftsib->Psize() < MaxPsize());
SetKey(0, fParent->GetKey(pidx));
leftsib->AppendFrom(this, 0, noFromThis-1);
ShiftLeft(noFromThis);
fParent->SetKey(pidx, GetKey(0));
fParent->SetNofKeys(pidx-1, leftsib->NofKeys());
fParent->SetNofKeys(pidx, NofKeys());
}
void TBtInnerNode::PushRight(Int_t noFromThis, TBtInnerNode *rightsib, Int_t pidx)
{
R__ASSERT(noFromThis > 0 && noFromThis <= Psize());
R__ASSERT(noFromThis + rightsib->Psize() < rightsib->MaxPsize());
R__ASSERT(fParent->GetTree(pidx) == rightsib);
Int_t start = fLast - noFromThis + 1;
Int_t tgt, src;
tgt = rightsib->fLast + noFromThis;
src = rightsib->fLast;
rightsib->fLast = tgt;
rightsib->SetKey(0, fParent->GetKey(pidx));
IncNofKeys(0);
while (src >= 0) {
rightsib->GetItem(tgt--) = rightsib->GetItem(src--);
}
for (Int_t i = fLast; i >= start; i-- ) {
rightsib->SetItem(tgt--, GetItem(i));
}
fParent->SetKey(pidx, rightsib->GetKey(0));
DecNofKeys(0);
R__CHECK(tgt == -1);
fLast -= noFromThis;
fParent->SetNofKeys(pidx-1, NofKeys());
fParent->SetNofKeys(pidx, rightsib->NofKeys());
}
void TBtInnerNode::Remove(Int_t index)
{
R__ASSERT(index >= 1 && index <= fLast);
TBtLeafNode *lf = GetTree(index)->FirstLeafNode();
SetKey(index, lf->fItem[0]);
lf->RemoveItem(0);
}
void TBtInnerNode::RemoveItem(Int_t index)
{
R__ASSERT(index >= 1 && index <= fLast);
for (Int_t to = index; to < fLast; to++)
fItem[to] = fItem[to+1];
fLast--;
if (IsLow()) {
if (fParent == 0) {
if (Psize() == 0)
fTree->RootIsEmpty();
} else
fParent->IsLow(this);
}
}
void TBtInnerNode::ShiftLeft(Int_t cnt)
{
if (cnt <= 0)
return;
for (Int_t i = cnt; i <= fLast; i++)
GetItem(i-cnt) = GetItem(i);
fLast -= cnt;
}
void TBtInnerNode::Split()
{
TBtInnerNode *newnode = new TBtInnerNode(fParent);
R__CHECK(newnode != 0);
fParent->Append(GetKey(fLast), newnode);
newnode->AppendFrom(this, fLast, fLast);
fLast--;
fParent->IncNofKeys(1, newnode->GetNofKeys(0));
fParent->DecNofKeys(0, newnode->GetNofKeys(0));
BalanceWithRight(newnode, 1);
}
void TBtInnerNode::SplitWith(TBtInnerNode *rightsib, Int_t keyidx)
{
R__ASSERT(keyidx > 0 && keyidx <= fParent->fLast);
rightsib->SetKey(0, fParent->GetKey(keyidx));
Int_t nofKeys = Psize() + rightsib->Vsize();
Int_t newSizeThis = nofKeys / 3;
Int_t newSizeNew = (nofKeys - newSizeThis) / 2;
Int_t newSizeSib = (nofKeys - newSizeThis - newSizeNew);
Int_t noFromThis = Psize() - newSizeThis;
Int_t noFromSib = rightsib->Vsize() - newSizeSib;
R__CHECK(noFromThis >= 0);
R__CHECK(noFromSib >= 1);
TBtInnerNode *newNode = new TBtInnerNode(fParent);
R__CHECK(newNode != 0);
if (noFromThis > 0) {
newNode->Append(GetItem(fLast));
fParent->AddElt(keyidx, GetKey(fLast--), newNode);
if (noFromThis > 2)
this->PushRight(noFromThis-1, newNode, keyidx);
rightsib->PushLeft(noFromSib, newNode, keyidx+1);
} else {
newNode->Append(rightsib->GetItem(0));
fParent->AddElt(keyidx+1, rightsib->GetKey(1), rightsib);
rightsib->ShiftLeft(1);
fParent->SetTree(keyidx, newNode);
rightsib->PushLeft(noFromSib-1, newNode, keyidx+1);
}
fParent->SetNofKeys(keyidx-1, this->NofKeys());
fParent->SetNofKeys(keyidx, newNode->NofKeys());
fParent->SetNofKeys(keyidx+1, rightsib->NofKeys());
if (fParent->IsFull())
fParent->InformParent();
}
TBtLeafNode::TBtLeafNode(TBtInnerNode *p, const TObject *obj, TBtree *t): TBtNode(1, p, t)
{
fItem = new TObject *[MaxIndex()+1];
memset(fItem, 0, (MaxIndex()+1)*sizeof(TObject*));
R__ASSERT(fItem != 0);
if (obj != 0)
fItem[++fLast] = (TObject*)obj;
}
TBtLeafNode::~TBtLeafNode()
{
delete [] fItem;
}
void TBtLeafNode::Add(const TObject *obj, Int_t index)
{
R__ASSERT(obj->IsSortable());
R__ASSERT(0 <= index && index <= fLast+1);
R__ASSERT(fLast <= MaxIndex());
for (Int_t i = fLast+1; i > index ; i--)
fItem[i] = fItem[i-1];
fItem[index] = (TObject *)obj;
fLast++;
if (fParent == 0)
fTree->IncrNofKeys();
else
fParent->IncrNofKeys(this);
if (IsFull()) {
if (fParent == 0) {
R__CHECK(fTree->fRoot == this);
fTree->RootIsFull();
} else {
fParent->IsFull(this);
}
}
}
void TBtLeafNode::AppendFrom(TBtLeafNode *src, Int_t start, Int_t stop)
{
if (start > stop)
return;
R__ASSERT(0 <= start && start <= src->fLast);
R__ASSERT(0 <= stop && stop <= src->fLast);
R__ASSERT(fLast + stop - start + 1 < MaxIndex());
for (Int_t i = start; i <= stop; i++)
fItem[++fLast] = src->fItem[i];
R__CHECK(fLast < MaxIndex());
}
void TBtLeafNode::Append(TObject *obj)
{
R__ASSERT(obj->IsSortable());
fItem[++fLast] = obj;
R__CHECK(fLast < MaxIndex());
}
void TBtLeafNode::BalanceWithLeft(TBtLeafNode *leftsib, Int_t pidx)
{
R__ASSERT(Vsize() >= leftsib->Psize());
Int_t newThisSize = (Vsize() + leftsib->Psize())/2;
Int_t noFromThis = Psize() - newThisSize;
PushLeft(noFromThis, leftsib, pidx);
}
void TBtLeafNode::BalanceWithRight(TBtLeafNode *rightsib, Int_t pidx)
{
R__ASSERT(Psize() >= rightsib->Vsize());
Int_t newThisSize = (Psize() + rightsib->Vsize())/2;
Int_t noFromThis = Psize() - newThisSize;
PushRight(noFromThis, rightsib, pidx);
}
void TBtLeafNode::BalanceWith(TBtLeafNode *rightsib, Int_t pidx)
{
if (Psize() < rightsib->Vsize())
rightsib->BalanceWithLeft(this, pidx);
else
BalanceWithRight(rightsib, pidx);
}
Int_t TBtLeafNode::FindRank(const TObject *what) const
{
for (Int_t i = 0; i <= fLast; i++) {
if (fItem[i]->Compare(what) == 0)
return i;
if (fItem[i]->Compare(what) > 0)
return -1;
}
return -1;
}
TBtLeafNode *TBtLeafNode::FirstLeafNode()
{
return this;
}
TObject *TBtLeafNode::Found(const TObject *what, TBtNode **which, Int_t *where)
{
R__ASSERT(what->IsSortable());
for (Int_t i = 0; i <= fLast; i++) {
if (fItem[i]->Compare(what) == 0) {
*which = this;
*where = i;
return fItem[i];
}
if (fItem[i]->Compare(what) > 0) {
*which = this;
*where = i;
return 0;
}
}
*which = this;
*where = fLast+1;
return 0;
}
Int_t TBtLeafNode::IndexOf(const TObject *that) const
{
for (Int_t i = 0; i <= fLast; i++) {
if (fItem[i] == that)
return i;
}
R__CHECK(0);
return -1;
}
TBtLeafNode *TBtLeafNode::LastLeafNode()
{
return this;
}
void TBtLeafNode::MergeWithRight(TBtLeafNode *rightsib, Int_t pidx)
{
R__ASSERT(Psize() + rightsib->Vsize() < MaxPsize());
rightsib->PushLeft(rightsib->Psize(), this, pidx);
Append(fParent->GetKey(pidx));
fParent->SetNofKeys(pidx-1, NofKeys());
fParent->RemoveItem(pidx);
delete rightsib;
}
Int_t TBtLeafNode::NofKeys(Int_t ) const
{
return 1;
}
Int_t TBtLeafNode::NofKeys() const
{
return Psize();
}
void TBtLeafNode::PushLeft(Int_t noFromThis, TBtLeafNode *leftsib, Int_t pidx)
{
R__ASSERT(noFromThis > 0 && noFromThis <= Psize());
R__ASSERT(noFromThis + leftsib->Psize() < MaxPsize());
R__ASSERT(fParent->GetTree(pidx) == this);
leftsib->Append(fParent->GetKey(pidx));
if (noFromThis > 1)
leftsib->AppendFrom(this, 0, noFromThis-2);
fParent->SetKey(pidx, fItem[noFromThis-1]);
ShiftLeft(noFromThis);
fParent->SetNofKeys(pidx-1, leftsib->NofKeys());
fParent->SetNofKeys(pidx, NofKeys());
}
void TBtLeafNode::PushRight(Int_t noFromThis, TBtLeafNode *rightsib, Int_t pidx)
{
R__ASSERT(noFromThis > 0 && noFromThis <= Psize());
R__ASSERT(noFromThis + rightsib->Psize() < MaxPsize());
R__ASSERT(fParent->GetTree(pidx) == rightsib);
Int_t start = fLast - noFromThis + 1;
Int_t tgt, src;
tgt = rightsib->fLast + noFromThis;
src = rightsib->fLast;
rightsib->fLast = tgt;
while (src >= 0)
rightsib->fItem[tgt--] = rightsib->fItem[src--];
rightsib->fItem[tgt--] = fParent->GetKey(pidx);
for (Int_t i = fLast; i > start; i--)
rightsib->fItem[tgt--] = fItem[i];
R__CHECK(tgt == -1);
fParent->SetKey(pidx, fItem[start]);
fLast -= noFromThis;
fParent->SetNofKeys(pidx-1, NofKeys());
fParent->SetNofKeys(pidx, rightsib->NofKeys());
}
void TBtLeafNode::Remove(Int_t index)
{
R__ASSERT(index >= 0 && index <= fLast);
for (Int_t to = index; to < fLast; to++)
fItem[to] = fItem[to+1];
fLast--;
if (fParent == 0)
fTree->DecrNofKeys();
else
fParent->DecrNofKeys(this);
if (IsLow()) {
if (fParent == 0) {
if (Psize() == 0)
fTree->RootIsEmpty();
} else
fParent->IsLow(this);
}
}
void TBtLeafNode::ShiftLeft(Int_t cnt)
{
if (cnt <= 0)
return;
for (Int_t i = cnt; i <= fLast; i++)
fItem[i-cnt] = fItem[i];
fLast -= cnt;
}
void TBtLeafNode::Split()
{
TBtLeafNode *newnode = new TBtLeafNode(fParent);
R__ASSERT(newnode != 0);
fParent->Append(fItem[fLast--], newnode);
fParent->SetNofKeys(0, fParent->GetTree(0)->NofKeys());
fParent->SetNofKeys(1, fParent->GetTree(1)->NofKeys());
BalanceWithRight(newnode, 1);
}
void TBtLeafNode::SplitWith(TBtLeafNode *rightsib, Int_t keyidx)
{
R__ASSERT(fParent == rightsib->fParent);
R__ASSERT(keyidx > 0 && keyidx <= fParent->fLast);
Int_t nofKeys = Psize() + rightsib->Vsize();
Int_t newSizeThis = nofKeys / 3;
Int_t newSizeNew = (nofKeys - newSizeThis) / 2;
Int_t newSizeSib = (nofKeys - newSizeThis - newSizeNew);
Int_t noFromThis = Psize() - newSizeThis;
Int_t noFromSib = rightsib->Vsize() - newSizeSib;
R__CHECK(noFromThis >= 0);
R__CHECK(noFromSib >= 1);
TBtLeafNode *newNode = new TBtLeafNode(fParent);
R__ASSERT(newNode != 0);
fParent->AddElt(keyidx, fItem[fLast--], newNode);
fParent->SetNofKeys(keyidx, 0);
fParent->DecNofKeys(keyidx-1);
this->PushRight(noFromThis-1, newNode, keyidx);
rightsib->PushLeft(noFromSib, newNode, keyidx+1);
if (fParent->IsFull())
fParent->InformParent();
}