Example #1
0
            public NodeVector(Btree <T> tree, T key, bool leftEdge) : this(tree)
            {
                for (Node node = tree.root;;)
                {
                    int hi = node.KeyCount;
                    if (leftEdge)
                    {
                        for (int lo = 0; lo != hi;)
                        {
                            int mid  = (lo + hi) >> 1;
                            int diff = tree.keyComparer.Compare(key, node.GetKey(mid));
                            if (diff <= 0)
                            {
                                if (diff == 0)
                                {
                                    this.IsFound = true;
                                }
                                hi = mid;
                            }
                            else
                            {
                                lo = mid + 1;
                            }
                        }
                    }
                    else
                    {
                        for (int lo = 0; lo != hi;)
                        {
                            int mid  = (lo + hi) >> 1;
                            int diff = tree.keyComparer.Compare(key, node.GetKey(mid));
                            if (diff < 0)
                            {
                                hi = mid;
                            }
                            else
                            {
                                if (diff == 0)
                                {
                                    this.IsFound = true;
                                }
                                lo = mid + 1;
                            }
                        }
                    }

                    this.indexStack.Add(hi);
                    this.nodeStack.Add(node);
                    if (node is Branch branch)
                    {
                        node = branch.GetChild(hi);
                    }
                    else
                    {
                        return;
                    }
                }
            }
Example #2
0
            /// <summary>Make a path to leftmost branch or leaf at the supplied level.</summary>
            /// <param name="tree">Target of path.</param>
            /// <param name="level">Level of node to seek (root is level 0).</param>
            /// <remarks>Used only for diagnostics.</remarks>
            public NodeVector(Btree <T> tree, int level) : this(tree)
            {
                this.IsFound = false;
                Push(tree.root, 0);

                for (Node node = TopNode; level > 0; --level)
                {
                    node = ((Branch)node).GetChild(0);
                    Push(node, 0);
                }
            }
            public BaseEnumerator(Btree <T> owner, int count)
            {
                this.tree        = owner;
                this.stageFreeze = owner.stage;
                this.state       = -1;

                if (count > 0)
                {
                    this.start = count;
                }

                if (this.start <= owner.leftmostLeaf.KeyCount)
                {
                    this.leaf      = owner.leftmostLeaf;
                    this.leafIndex = this.start;
                }
            }
 public BaseEnumerator(Btree <T> owner, bool isReverse = false)
 {
     this.tree        = owner;
     this.isReverse   = isReverse;
     this.stageFreeze = tree.stage;
     this.state       = -1;
     if (isReverse)
     {
         this.start     = owner.root.Weight - 1;
         this.leaf      = owner.rightmostLeaf;
         this.leafIndex = owner.rightmostLeaf.KeyCount - 1;
     }
     else
     {
         this.leaf = owner.leftmostLeaf;
     }
 }
Example #5
0
            /// <summary>Perform search and store the result.</summary>
            /// <param name="tree">Tree to search.</param>
            /// <param name="key">Value to find.</param>
            public NodeVector(Btree <T> tree, T key) : this(tree)
            {
                for (Node node = tree.root;;)
                {
                    this.nodeStack.Add(node);
                    int ix = tree.keyComparer == Comparer <T> .Default?node.Search(key)
                                 : node.Search(key, tree.keyComparer);

                    if (node is Branch branch)
                    {
                        ix = (ix < 0) ? ~ix : ix + 1;
                        this.indexStack.Add(ix);
                        node = branch.GetChild(ix);
                    }
                    else
                    {
                        this.IsFound = ix >= 0;
                        this.indexStack.Add(this.IsFound ? ix : ~ix);
                        return;
                    }
                }
            }
Example #6
0
 public PairEnumerator(Btree <T> owner, Func <KeyValuePair <T, V>, bool> condition) : this(owner)
Example #7
0
 public PairEnumerator(Btree <T> owner, int count) : base(owner, count)
 {
 }
Example #8
0
 public PairEnumerator(Btree <T> owner, bool isReverse = false, bool nonGeneric = false) : base(owner, isReverse)
     => this.NonGeneric = nonGeneric;
 public ValueEnumerator(Btree <T> owner, Func <V, int, bool> condition) : base(owner)
     => Bypass3(condition, (leaf, ix) => ((PairLeaf <V>)leaf).GetValue(ix));
 public ValueEnumerator(Btree <T> owner, int count) : base(owner, count)
 {
 }
 public ValueEnumerator(Btree <T> owner, bool isReverse = false) : base(owner, isReverse)
 {
 }
Example #12
0
 public KeyEnumerator (Btree<T> owner, Func<T,int,bool> condition) : base (owner)
  => Bypass3 (condition, (leaf,ix) => leaf.GetKey (ix));
Example #13
0
 /// <summary>Make an empty path.</summary>
 /// <param name="tree">Target of path.</param>
 public NodeVector(Btree <T> tree)
 {
     this.tree       = tree;
     this.indexStack = new List <int>();
     this.nodeStack  = new List <Node>();
 }
Example #14
0
            public static NodeVector CreateFromIndex(Btree <T> tree, int index)
            {
                Debug.Assert(index <= tree.root.Weight);

                var path = new NodeVector(tree);

                if (index == 0)
                {
                    for (Node n0 = tree.root;;)
                    {
                        path.indexStack.Add(0);
                        path.nodeStack.Add(n0);

                        if (n0 is Branch bh)
                        {
                            n0 = bh.GetChild(0);
                        }
                        else
                        {
                            return(path);
                        }
                    }
                }
                else if (index >= tree.root.Weight)
                {
                    for (Node n0 = tree.root;;)
                    {
                        path.indexStack.Add(n0.KeyCount);
                        path.nodeStack.Add(n0);

                        if (n0 is Branch bh)
                        {
                            n0 = bh.GetChild(bh.KeyCount);
                        }
                        else
                        {
                            return(path);
                        }
                    }
                }

                Node node = tree.root;

                while (node is Branch branch)
                {
                    for (int ix = 0; ; ++ix)
                    {
                        Debug.Assert(ix <= node.KeyCount);

                        Node child = branch.GetChild(ix);
                        int  cw    = child.Weight;
                        if (cw > index)
                        {
                            path.indexStack.Add(ix);
                            path.nodeStack.Add(node);
                            node = child;
                            break;
                        }
                        index -= cw;
                    }
                }

                path.indexStack.Add(index);
                path.nodeStack.Add(node);
                return(path);
            }