Example #1
0
        private void EnsureFullEnough(FileStream File, int i)
        {
            BNode <T, TKey> Children = DiskRead(File, Degree, ChildrenPointers[0], ContentFactory, KeyFactory);

            if (Children.N < (Degree / 2 - 1))
            {
                BNode <T, TKey> LeftSibling  = DiskRead(File, Degree, ChildrenPointers[i - 1], ContentFactory, KeyFactory);
                int             LeftSiblingN = LeftSibling.N;

                if (i > 0)
                {
                    LeftSibling = DiskRead(File, Degree, ChildrenPointers[i - 1], ContentFactory, KeyFactory);
                }
                else
                {
                    LeftSibling  = null;
                    LeftSiblingN = 0;
                }
                if (N >= (Degree / 2 - 1))
                {
                    for (int j = Children.N - 1; j >= 0; j--)
                    {
                        Children.Keys[j + 1] = Children.Keys[j];
                    }
                    if (!Children.Leaf)
                    {
                        for (int j = Children.N; j >= 0; j--)
                        {
                            Children.ChildrenPointers[j + 1] = Children.ChildrenPointers[j];
                        }
                    }
                    Children.Keys[0] = Keys[i - 1];
                    Keys[i - 1]      = LeftSibling.Keys[LeftSiblingN - 1];
                    LeftSibling.Keys[LeftSiblingN - 1] = KeyFactory.CreateNull();

                    if (!Children.Leaf)
                    {
                        Children.ChildrenPointers[0] = LeftSibling.ChildrenPointers[LeftSiblingN];
                        LeftSibling.ChildrenPointers[LeftSiblingN] = SizesNSpecialCharacters.NullPointer;
                    }
                    DiskWrite(File, 65);
                    Children.DiskWrite(File, 65);
                    LeftSibling.DiskWrite(File, 65);
                }
                else
                {
                    BNode <T, TKey> RightSibling;
                    int             RightSiblingN;

                    if (i < N)
                    {
                        RightSibling  = DiskRead(File, Degree, ChildrenPointers[i + 1], ContentFactory, KeyFactory);
                        RightSiblingN = RightSibling.N;
                    }
                    else
                    {
                        RightSibling  = null;
                        RightSiblingN = 0;
                    }

                    if (RightSiblingN >= (Degree / 2 - 1))
                    {
                        Children.Keys[Children.N] = Keys[i];
                        Keys[i] = RightSibling.Keys[0];

                        if (!Children.Leaf)
                        {
                            Children.ChildrenPointers[Children.N] = RightSibling.ChildrenPointers[0];
                        }

                        for (int j = 1; j < RightSiblingN; j++)
                        {
                            RightSibling.Keys[j - 1] = RightSibling.Keys[j];
                        }
                        RightSibling.Keys[RightSiblingN - 1] = KeyFactory.CreateNull();
                        if (!RightSibling.Leaf)
                        {
                            for (int j = 1; j <= RightSiblingN; j++)
                            {
                                RightSibling.ChildrenPointers[j - 1] = RightSibling.ChildrenPointers[j];
                            }
                            RightSibling.ChildrenPointers[RightSiblingN] = SizesNSpecialCharacters.NullPointer;
                        }
                        DiskWrite(File, 65);
                        Children.DiskWrite(File, 65);
                        RightSibling.DiskWrite(File, 65);
                    }
                    else
                    {
                        if (LeftSiblingN > 0)
                        {
                            for (int j = Children.N - 1; j >= 0; j--)
                            {
                                Children.Keys[j + (Degree / 2 - 1)] = Children.Keys[j];
                            }
                            if (!Children.Leaf)
                            {
                                for (int j = Children.N; j >= 0; j--)
                                {
                                    Children.ChildrenPointers[j + (Degree / 2 - 1)] = Children.ChildrenPointers[j];
                                }
                            }
                            for (int j = 0; j < LeftSiblingN; j++)
                            {
                                Children.Keys[j]    = LeftSibling.Keys[j];
                                LeftSibling.Keys[j] = KeyFactory.CreateNull();
                            }
                            if (!Children.Leaf)
                            {
                                for (int j = 0; j < LeftSiblingN; j++)
                                {
                                    Children.ChildrenPointers[j]    = LeftSibling.ChildrenPointers[j];
                                    LeftSibling.ChildrenPointers[j] = SizesNSpecialCharacters.NullPointer;
                                }
                            }

                            Children.Keys[(Degree / 2 - 1)] = Keys[i - 1];

                            for (int j = i; j < N; j++)
                            {
                                Keys[j - 1]             = Keys[j];
                                ChildrenPointers[j - 1] = ChildrenPointers[j];
                            }
                            ChildrenPointers[N - 1] = ChildrenPointers[N];
                            Keys[N - 1]             = KeyFactory.CreateNull();
                            ChildrenPointers[N]     = SizesNSpecialCharacters.NullPointer;

                            DiskWrite(File, 65);
                            Children.DiskWrite(File, 65);
                        }
                        else
                        {
                            for (int j = 0; j < RightSiblingN; j++)
                            {
                                Children.Keys[j + Children.N + 1] = RightSibling.Keys[j];
                                RightSibling.Keys[j] = KeyFactory.CreateNull();
                            }
                            if (!Children.Leaf)
                            {
                                for (int j = 0; j <= RightSiblingN; j++)
                                {
                                    Children.ChildrenPointers[j + Children.N + 1] = RightSibling.ChildrenPointers[j];
                                    RightSibling.ChildrenPointers[j] = SizesNSpecialCharacters.NullPointer;
                                }
                            }

                            Children.Keys[(Degree / 2 - 1) - 1] = Keys[i];

                            for (int j = i + 1; j < N; j++)
                            {
                                Keys[j - 1]         = Keys[j];
                                ChildrenPointers[j] = ChildrenPointers[j + 1];
                            }
                            Keys[N - 1]         = KeyFactory.CreateNull();
                            ChildrenPointers[N] = SizesNSpecialCharacters.NullPointer;

                            RightSibling.DiskWrite(File, 65);
                            DiskWrite(File, 65);
                            Children.DiskWrite(File, 65);
                        }
                    }
                }
            }
        }