B-tree class.
TBtree inherits from the TSeqCollection ABC.
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.
The following are modifications of Knuth's properties on page 478:
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:
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.]]
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.
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.
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.
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
Currently, order2 = 2*(order+1).
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:
Parent nodes are always InnerNodes.
These are the primitive operations on Nodes:
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).
Public Types | |
typedef TBtreeIter | Iterator_t |
Public Types inherited from TCollection | |
enum | { kInitCapacity = 16 , kInitHashTableCapacity = 17 } |
Public Types inherited from TObject | |
enum | { kIsOnHeap = 0x01000000 , kNotDeleted = 0x02000000 , kZombie = 0x04000000 , kInconsistent = 0x08000000 , kBitMask = 0x00ffffff } |
enum | { kSingleKey = (1ULL << ( 0 )) , kOverwrite = (1ULL << ( 1 )) , kWriteDelete = (1ULL << ( 2 )) } |
enum | EDeprecatedStatusBits { kObjInCanvas = (1ULL << ( 3 )) } |
enum | EStatusBits { kCanDelete = (1ULL << ( 0 )) , kMustCleanup = (1ULL << ( 3 )) , kIsReferenced = (1ULL << ( 4 )) , kHasUUID = (1ULL << ( 5 )) , kCannotPick = (1ULL << ( 6 )) , kNoContextMenu = (1ULL << ( 8 )) , kInvalidObject = (1ULL << ( 13 )) } |
Public Member Functions | |
TBtree (Int_t ordern=3) | |
Create a B-tree of certain order (by default 3). | |
virtual | ~TBtree () |
Delete B-tree. | |
void | Add (TObject *obj) override |
Add object to B-tree. | |
void | AddAfter (const TObject *, TObject *obj) override |
void | AddAt (TObject *obj, Int_t) override |
void | AddBefore (const TObject *, TObject *obj) override |
void | AddFirst (TObject *obj) override |
void | AddLast (TObject *obj) override |
TObject * | After (const TObject *obj) const override |
Cannot use this method since B-tree decides order. | |
TObject * | At (Int_t idx) const override |
TObject * | Before (const TObject *obj) const override |
May not use this method since B-tree decides order. | |
void | Clear (Option_t *option="") override |
Remove all objects from B-tree. | |
void | Delete (Option_t *option="") override |
Remove all objects from B-tree AND delete all heap based objects. | |
TObject * | FindObject (const char *name) const override |
Find object using its name (see object's GetName()). | |
TObject * | FindObject (const TObject *obj) const override |
Find object using the objects Compare() member function. | |
TObject * | First () const override |
TObject ** | GetObjectRef (const TObject *) const override |
TClass * | IsA () const override |
TObject * | Last () const override |
TIterator * | MakeIterator (Bool_t dir=kIterForward) const override |
Returns a B-tree iterator. | |
TObject * | operator[] (Int_t i) const |
Int_t | Order () |
Int_t | Rank (const TObject *obj) const |
Returns the rank of the object in the tree. | |
TObject * | Remove (TObject *obj) override |
Remove an object from the tree. | |
void | Streamer (TBuffer &) override |
Stream all objects in the btree to or from the I/O buffer. | |
void | StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b) |
Public Member Functions inherited from TSeqCollection | |
virtual | ~TSeqCollection () |
void | Add (TObject *obj) override |
virtual Int_t | GetLast () const |
Returns index of last object in collection. | |
virtual Int_t | IndexOf (const TObject *obj) const |
Return index of object in collection. | |
TClass * | IsA () const override |
virtual Bool_t | IsSorted () const |
Int_t | LastIndex () const |
Long64_t | Merge (TCollection *list) |
Merge this collection with all collections coming in the input list. | |
virtual void | RemoveAfter (TObject *after) |
virtual TObject * | RemoveAt (Int_t idx) |
virtual void | RemoveBefore (TObject *before) |
virtual void | RemoveFirst () |
virtual void | RemoveLast () |
void | Streamer (TBuffer &) override |
Stream all objects in the collection to or from the I/O buffer. | |
void | StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b) |
void | UnSort () |
Public Member Functions inherited from TCollection | |
virtual | ~TCollection () |
TNamed destructor. | |
virtual void | AddAll (const TCollection *col) |
Add all objects from collection col to this collection. | |
void | AddVector (TObject *obj1,...) |
Add all arguments to the collection. | |
Bool_t | AssertClass (TClass *cl) const |
Make sure all objects in this collection inherit from class cl. | |
TIter | begin () const |
void | Browse (TBrowser *b) override |
Browse this collection (called by TBrowser). | |
Int_t | Capacity () const |
TObject * | Clone (const char *newname="") const override |
Make a clone of an collection using the Streamer facility. | |
Int_t | Compare (const TObject *obj) const override |
Compare two TCollection objects. | |
Bool_t | Contains (const char *name) const |
Bool_t | Contains (const TObject *obj) const |
void | Draw (Option_t *option="") override |
Draw all objects in this collection. | |
void | Dump () const override |
Dump all objects in this collection. | |
TIter | end () const |
TObject * | FindObject (const char *name) const override |
Find an object in this collection using its name. | |
TObject * | FindObject (const TObject *obj) const override |
Find an object in this collection using the object's IsEqual() member function. | |
virtual Int_t | GetEntries () const |
const char * | GetName () const override |
Return name of this collection. | |
virtual Int_t | GetSize () const |
Return the capacity of the collection, i.e. | |
virtual Int_t | GrowBy (Int_t delta) const |
Increase the collection's capacity by delta slots. | |
ULong_t | Hash () const override |
Return hash value for this object. | |
Bool_t | IsArgNull (const char *where, const TObject *obj) const |
Returns true if object is a null pointer. | |
virtual Bool_t | IsEmpty () const |
Bool_t | IsFolder () const override |
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects). | |
Bool_t | IsOwner () const |
Bool_t | IsSortable () const override |
R__ALWAYS_INLINE Bool_t | IsUsingRWLock () const |
void | ls (Option_t *option="") const override |
List (ls) all objects in this collection. | |
virtual TIterator * | MakeReverseIterator () const |
Bool_t | Notify () override |
'Notify' all objects in this collection. | |
TObject * | operator() (const char *name) const |
Find an object in this collection by name. | |
void | Paint (Option_t *option="") override |
Paint all objects in this collection. | |
virtual void | Print (Option_t *option, const char *wildcard, Int_t recurse=1) const |
Print the collection header and its elements that match the wildcard. | |
virtual void | Print (Option_t *option, Int_t recurse) const |
Print the collection header and its elements. | |
virtual void | Print (Option_t *option, TPRegexp ®exp, Int_t recurse=1) const |
Print the collection header and its elements that match the regexp. | |
void | Print (Option_t *option="") const override |
Default print for collections, calls Print(option, 1). | |
void | RecursiveRemove (TObject *obj) override |
Remove object from this collection and recursively remove the object from all other objects (and collections). | |
void | RemoveAll () |
virtual void | RemoveAll (TCollection *col) |
Remove all objects in collection col from this collection. | |
void | SetCurrentCollection () |
Set this collection to be the globally accessible collection. | |
void | SetName (const char *name) |
virtual void | SetOwner (Bool_t enable=kTRUE) |
Set whether this collection is the owner (enable==true) of its content. | |
void | StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b) |
virtual bool | UseRWLock (Bool_t enable=true) |
Set this collection to use a RW lock upon access, making it thread safe. | |
Int_t | Write (const char *name=nullptr, Int_t option=0, Int_t bufsize=0) const override |
Write all objects in this collection. | |
Int_t | Write (const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override |
Write all objects in this collection. | |
Public Member Functions inherited from TObject | |
TObject () | |
TObject constructor. | |
TObject (const TObject &object) | |
TObject copy ctor. | |
virtual | ~TObject () |
TObject destructor. | |
void | AbstractMethod (const char *method) const |
Use this method to implement an "abstract" method that you don't want to leave purely abstract. | |
virtual void | AppendPad (Option_t *option="") |
Append graphics object to current pad. | |
ULong_t | CheckedHash () |
Check and record whether this class has a consistent Hash/RecursiveRemove setup (*) and then return the regular Hash value for this object. | |
virtual const char * | ClassName () const |
Returns name of class to which the object belongs. | |
virtual void | Copy (TObject &object) const |
Copy this to obj. | |
virtual Int_t | DistancetoPrimitive (Int_t px, Int_t py) |
Computes distance from point (px,py) to the object. | |
virtual void | DrawClass () const |
Draw class inheritance tree of the class to which this object belongs. | |
virtual TObject * | DrawClone (Option_t *option="") const |
Draw a clone of this object in the current selected pad with: gROOT->SetSelectedPad(c1) . | |
virtual void | Error (const char *method, const char *msgfmt,...) const |
Issue error message. | |
virtual void | Execute (const char *method, const char *params, Int_t *error=nullptr) |
Execute method on this object with the given parameter string, e.g. | |
virtual void | Execute (TMethod *method, TObjArray *params, Int_t *error=nullptr) |
Execute method on this object with parameters stored in the TObjArray. | |
virtual void | ExecuteEvent (Int_t event, Int_t px, Int_t py) |
Execute action corresponding to an event at (px,py). | |
virtual void | Fatal (const char *method, const char *msgfmt,...) const |
Issue fatal error message. | |
virtual Option_t * | GetDrawOption () const |
Get option used by the graphics system to draw this object. | |
virtual const char * | GetIconName () const |
Returns mime type name of object. | |
virtual char * | GetObjectInfo (Int_t px, Int_t py) const |
Returns string containing info about the object at position (px,py). | |
virtual Option_t * | GetOption () const |
virtual const char * | GetTitle () const |
Returns title of object. | |
virtual UInt_t | GetUniqueID () const |
Return the unique object id. | |
virtual Bool_t | HandleTimer (TTimer *timer) |
Execute action in response of a timer timing out. | |
Bool_t | HasInconsistentHash () const |
Return true is the type of this object is known to have an inconsistent setup for Hash and RecursiveRemove (i.e. | |
virtual void | Info (const char *method, const char *msgfmt,...) const |
Issue info message. | |
virtual Bool_t | InheritsFrom (const char *classname) const |
Returns kTRUE if object inherits from class "classname". | |
virtual Bool_t | InheritsFrom (const TClass *cl) const |
Returns kTRUE if object inherits from TClass cl. | |
virtual void | Inspect () const |
Dump contents of this object in a graphics canvas. | |
void | InvertBit (UInt_t f) |
Bool_t | IsDestructed () const |
IsDestructed. | |
virtual Bool_t | IsEqual (const TObject *obj) const |
Default equal comparison (objects are equal if they have the same address in memory). | |
R__ALWAYS_INLINE Bool_t | IsOnHeap () const |
R__ALWAYS_INLINE Bool_t | IsZombie () const |
void | MayNotUse (const char *method) const |
Use this method to signal that a method (defined in a base class) may not be called in a derived class (in principle against good design since a child class should not provide less functionality than its parent, however, sometimes it is necessary). | |
void | Obsolete (const char *method, const char *asOfVers, const char *removedFromVers) const |
Use this method to declare a method obsolete. | |
void | operator delete (void *ptr) |
Operator delete. | |
void | operator delete (void *ptr, void *vp) |
Only called by placement new when throwing an exception. | |
void | operator delete[] (void *ptr) |
Operator delete []. | |
void | operator delete[] (void *ptr, void *vp) |
Only called by placement new[] when throwing an exception. | |
void * | operator new (size_t sz) |
void * | operator new (size_t sz, void *vp) |
void * | operator new[] (size_t sz) |
void * | operator new[] (size_t sz, void *vp) |
TObject & | operator= (const TObject &rhs) |
TObject assignment operator. | |
virtual void | Pop () |
Pop on object drawn in a pad to the top of the display list. | |
virtual Int_t | Read (const char *name) |
Read contents of object with specified name from the current directory. | |
void | ResetBit (UInt_t f) |
virtual void | SaveAs (const char *filename="", Option_t *option="") const |
Save this object in the file specified by filename. | |
virtual void | SavePrimitive (std::ostream &out, Option_t *option="") |
Save a primitive as a C++ statement(s) on output stream "out". | |
void | SetBit (UInt_t f) |
void | SetBit (UInt_t f, Bool_t set) |
Set or unset the user status bits as specified in f. | |
virtual void | SetDrawOption (Option_t *option="") |
Set drawing option for object. | |
virtual void | SetUniqueID (UInt_t uid) |
Set the unique object id. | |
void | StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b) |
virtual void | SysError (const char *method, const char *msgfmt,...) const |
Issue system error message. | |
R__ALWAYS_INLINE Bool_t | TestBit (UInt_t f) const |
Int_t | TestBits (UInt_t f) const |
virtual void | UseCurrentStyle () |
Set current style settings in this object This function is called when either TCanvas::UseCurrentStyle or TROOT::ForceStyle have been invoked. | |
virtual void | Warning (const char *method, const char *msgfmt,...) const |
Issue warning message. | |
Static Public Member Functions | |
static TClass * | Class () |
static const char * | Class_Name () |
static constexpr Version_t | Class_Version () |
static const char * | DeclFileName () |
Static Public Member Functions inherited from TSeqCollection | |
static TClass * | Class () |
static const char * | Class_Name () |
static constexpr Version_t | Class_Version () |
static const char * | DeclFileName () |
static Int_t | ObjCompare (TObject *a, TObject *b) |
Compare to objects in the collection. Use member Compare() of object a. | |
static void | QSort (TObject **a, Int_t first, Int_t last) |
Sort array of TObject pointers using a quicksort algorithm. | |
static void | QSort (TObject **a, Int_t nBs, TObject ***b, Int_t first, Int_t last) |
Sort array a of TObject pointers using a quicksort algorithm. | |
static void | QSort (TObject **a, TObject **b, Int_t first, Int_t last) |
Static Public Member Functions inherited from TCollection | |
static TClass * | Class () |
static const char * | Class_Name () |
static constexpr Version_t | Class_Version () |
static const char * | DeclFileName () |
static void | EmptyGarbageCollection () |
Do the garbage collection. | |
static void | GarbageCollect (TObject *obj) |
Add to the list of things to be cleaned up. | |
static TCollection * | GetCurrentCollection () |
Return the globally accessible collection. | |
static void | StartGarbageCollection () |
Set up for garbage collection. | |
Static Public Member Functions inherited from TObject | |
static TClass * | Class () |
static const char * | Class_Name () |
static constexpr Version_t | Class_Version () |
static const char * | DeclFileName () |
static Longptr_t | GetDtorOnly () |
Return destructor only flag. | |
static Bool_t | GetObjectStat () |
Get status of object stat flag. | |
static void | SetDtorOnly (void *obj) |
Set destructor only flag. | |
static void | SetObjectStat (Bool_t stat) |
Turn on/off tracking of objects in the TObjectTable. | |
Protected Member Functions | |
void | DecrNofKeys () |
Int_t | IdxAdd (const TObject &obj) |
Add object and return its index in the tree. | |
void | IncrNofKeys () |
Protected Member Functions inherited from TSeqCollection | |
TSeqCollection () | |
virtual void | Changed () |
Protected Member Functions inherited from TCollection | |
TCollection () | |
virtual const char * | GetCollectionEntryName (TObject *entry) const |
For given collection entry return the string that is used to identify the object and, potentially, perform wildcard/regexp filtering on. | |
virtual void | PrintCollectionEntry (TObject *entry, Option_t *option, Int_t recurse) const |
Print the collection entry. | |
virtual void | PrintCollectionHeader (Option_t *option) const |
Print the collection header. | |
Protected Member Functions inherited from TObject | |
virtual void | DoError (int level, const char *location, const char *fmt, va_list va) const |
Interface to ErrorHandler (protected). | |
void | MakeZombie () |
Private Member Functions | |
void | Init (Int_t i) |
Initialize a B-tree. | |
void | RootIsEmpty () |
If root is empty clean up its space. | |
void | RootIsFull () |
The root of the tree is full. | |
Private Attributes | |
Int_t | fInnerLowWaterMark |
Int_t | fInnerMaxIndex |
Int_t | fLeafLowWaterMark |
Int_t | fLeafMaxIndex |
Int_t | fOrder |
Int_t | fOrder2 |
TBtNode * | fRoot |
Friends | |
class | TBtInnerNode |
class | TBtLeafNode |
class | TBtNode |
Additional Inherited Members | |
Protected Types inherited from TCollection | |
enum | EStatusBits { kIsOwner = (1ULL << ( 14 )) , kUseRWLock = (1ULL << ( 16 )) } |
Protected Types inherited from TObject | |
enum | { kOnlyPrepStep = (1ULL << ( 3 )) } |
Protected Attributes inherited from TSeqCollection | |
Bool_t | fSorted |
Protected Attributes inherited from TCollection | |
TString | fName |
Int_t | fSize |
#include <TBtree.h>
typedef TBtreeIter TBtree::Iterator_t |
TBtree::TBtree | ( | Int_t | ordern = 3 | ) |
Create a B-tree of certain order (by default 3).
Definition at line 180 of file TBtree.cxx.
|
virtual |
Delete B-tree.
Objects are not deleted unless the TBtree is the owner (set via SetOwner()).
Definition at line 189 of file TBtree.cxx.
|
overridevirtual |
Implements TSeqCollection.
Implements TSeqCollection.
Implements TSeqCollection.
|
inlineoverridevirtual |
Implements TSeqCollection.
|
inlineoverridevirtual |
Implements TSeqCollection.
Cannot use this method since B-tree decides order.
Implements TSeqCollection.
Definition at line 228 of file TBtree.cxx.
Implements TSeqCollection.
May not use this method since B-tree decides order.
Implements TSeqCollection.
Definition at line 237 of file TBtree.cxx.
|
static |
|
inlinestaticconstexpr |
|
overridevirtual |
Remove all objects from B-tree.
Does NOT delete objects unless the TBtree is the owner (set via SetOwner()).
Implements TCollection.
Definition at line 247 of file TBtree.cxx.
|
inlinestatic |
|
overridevirtual |
Remove all objects from B-tree AND delete all heap based objects.
Implements TCollection.
Definition at line 260 of file TBtree.cxx.
|
overridevirtual |
Find object using its name (see object's GetName()).
Requires sequential search of complete tree till object is found.
Reimplemented from TObject.
Definition at line 275 of file TBtree.cxx.
Find object using the objects Compare() member function.
Reimplemented from TObject.
Definition at line 283 of file TBtree.cxx.
|
inlineoverridevirtual |
Implements TSeqCollection.
Implements TCollection.
Add object and return its index in the tree.
Definition at line 301 of file TBtree.cxx.
|
private |
Initialize a B-tree.
Definition at line 344 of file TBtree.cxx.
|
inlineoverridevirtual |
|
inlineoverridevirtual |
Implements TSeqCollection.
|
overridevirtual |
Returns the rank of the object in the tree.
Definition at line 393 of file TBtree.cxx.
|
private |
If root is empty clean up its space.
Definition at line 441 of file TBtree.cxx.
|
private |
The root of the tree is full.
Create an InnerNode that points to it, and then inform the InnerNode that it is full.
Definition at line 430 of file TBtree.cxx.
|
overridevirtual |
Stream all objects in the btree to or from the I/O buffer.
Reimplemented from TObject.
Definition at line 460 of file TBtree.cxx.
|
inline |
|
friend |
|
friend |