Exemple #1
0
        bool IDictionary.Contains(object jKey)
        {
            var path = new TreePath <TKey, TValue>(this, (TKey)jKey);

            return(path.IsFound);
        }
Exemple #2
0
        // Leaf has been split so insert the new anchor into a branch.
        private void Promote(TreePath <TKey, TValue> path, TKey key, Node <TKey> newNode)
        {
            for (;;)
            {
                if (path.Height == 1)
                {
                    Debug.Assert(root == path.TopNode);

                    // Graft new root.
                    root = new Branch <TKey>(path.TopNode, Order);
                    root.Add(key, newNode);
                    ++height;
                    break;
                }

                path.Pop();
                Branch <TKey> branch      = (Branch <TKey>)path.TopNode;
                int           branchIndex = path.TopNodeIndex;

                if (branch.NotFull)
                {
                    // Typical case where branch has room.
                    branch.InsertKey(branchIndex, key);
                    branch.Insert(branchIndex + 1, newNode);
                    break;
                }

                // Right split an overflowing branch.
                Branch <TKey> newBranch = new Branch <TKey>(branch);
                int           halfway   = (branch.KeyCount + 1) / 2;

                if (branchIndex < halfway)
                {
                    // Split with left-side insert.
                    for (int i = halfway;; ++i)
                    {
                        if (i >= branch.KeyCount)
                        {
                            newBranch.Add(branch.GetChild(i));
                            break;
                        }
                        newBranch.Add(branch.GetKey(i), branch.GetChild(i));
                    }

                    TKey newPromotion = branch.GetKey(halfway - 1);
                    branch.Truncate(halfway - 1);
                    branch.InsertKey(branchIndex, key);
                    branch.Insert(branchIndex + 1, newNode);
                    key = newPromotion;
                }
                else
                {
                    // Split branch with right-side insert (or cascade promote).
                    int moveIndex = halfway;

                    if (branchIndex > halfway)
                    {
                        for (;;)
                        {
                            ++moveIndex;
                            newBranch.Add(branch.GetChild(moveIndex));
                            if (moveIndex >= branchIndex)
                            {
                                break;
                            }
                            newBranch.AddKey(branch.GetKey(moveIndex));
                        }
                        newBranch.AddKey(key);
                        key = branch.GetKey(halfway);
                    }

                    newBranch.Add(newNode);

                    while (moveIndex < branch.KeyCount)
                    {
                        newBranch.AddKey(branch.GetKey(moveIndex));
                        ++moveIndex;
                        newBranch.Add(branch.GetChild(moveIndex));
                    }

                    branch.Truncate(halfway);
                }

                newNode = newBranch;
            }
        }