Beispiel #1
0
        public virtual NodeAVL Last(IPersistentStore store, NodeAVL x)
        {
            if (x == null)
            {
                return(null);
            }
            NodeAVL left = x.GetLeft(store);

            if (left != null)
            {
                x = left;
                for (NodeAVL eavl3 = x.GetRight(store); eavl3 != null; eavl3 = x.GetRight(store))
                {
                    x = eavl3;
                }
                return(x);
            }
            NodeAVL eavl4 = x;

            x = x.GetParent(store);
            while ((x != null) && eavl4.Equals(x.GetLeft(store)))
            {
                eavl4 = x;
                x     = x.GetParent(store);
            }
            return(x);
        }
Beispiel #2
0
        public virtual NodeAVL Next(IPersistentStore store, NodeAVL x)
        {
            NodeAVL right = x.GetRight(store);

            if (right != null)
            {
                x = right;
                for (NodeAVL eavl3 = x.GetLeft(store); eavl3 != null; eavl3 = x.GetLeft(store))
                {
                    x = eavl3;
                }
                return(x);
            }
            NodeAVL eavl4 = x;

            x = x.GetParent(store);
            while ((x != null) && (eavl4 == x.GetRight(store)))
            {
                eavl4 = x;
                x     = x.GetParent(store);
            }
            return(x);
        }
Beispiel #3
0
        private void Balance(IPersistentStore store, NodeAVL x, bool isleft)
        {
            while (true)
            {
                int num = isleft ? 1 : -1;
                switch ((x.GetBalance(store) * num))
                {
                case -1:
                {
                    NodeAVL n = x.Child(store, isleft);
                    if (n.GetBalance(store) != -num)
                    {
                        NodeAVL eavl2 = n.Child(store, !isleft);
                        x.Replace(store, this, eavl2);
                        n     = n.Set(store, !isleft, eavl2.Child(store, isleft));
                        eavl2 = eavl2.Set(store, isleft, n);
                        x     = x.Set(store, isleft, eavl2.Child(store, !isleft));
                        eavl2 = eavl2.Set(store, !isleft, x);
                        int balance = eavl2.GetBalance(store);
                        x     = x.SetBalance(store, (balance == -num) ? num : 0);
                        n     = n.SetBalance(store, (balance == num) ? -num : 0);
                        eavl2 = eavl2.SetBalance(store, 0);
                        return;
                    }
                    x.Replace(store, this, n);
                    x = x.Set(store, isleft, n.Child(store, !isleft));
                    n = n.Set(store, !isleft, x);
                    x = x.SetBalance(store, 0);
                    n = n.SetBalance(store, 0);
                    return;
                }

                case 0:
                    x = x.SetBalance(store, -num);
                    break;

                case 1:
                    x = x.SetBalance(store, 0);
                    return;
                }
                if (x.IsRoot(store))
                {
                    return;
                }
                isleft = x.IsFromLeft(store);
                x      = x.GetParent(store);
            }
        }
Beispiel #4
0
        public virtual void Delete(IPersistentStore store, NodeAVL x)
        {
            if (x != null)
            {
                Monitor.Enter(this._lock);
                store.LockStore();
                try
                {
                    NodeAVL parent;
                    if (x.GetLeft(store) == null)
                    {
                        parent = x.GetRight(store);
                    }
                    else if (x.GetRight(store) == null)
                    {
                        parent = x.GetLeft(store);
                    }
                    else
                    {
                        NodeAVL node = x;
                        x = x.GetLeft(store);
                        while (true)
                        {
                            NodeAVL right = x.GetRight(store);
                            if (right == null)
                            {
                                break;
                            }
                            x = right;
                        }
                        parent = x.GetLeft(store);
                        int balance = x.GetBalance(store);
                        x    = x.SetBalance(store, node.GetBalance(store));
                        node = node.SetBalance(store, balance);
                        NodeAVL eavl3 = x.GetParent(store);
                        NodeAVL n     = node.GetParent(store);
                        if (node.IsRoot(store))
                        {
                            store.SetAccessor(this, x);
                        }
                        x = x.SetParent(store, n);
                        if (n != null)
                        {
                            if (!n.IsRight(node))
                            {
                                n.SetLeft(store, x);
                            }
                            else
                            {
                                n.SetRight(store, x);
                            }
                        }
                        if (node.Equals(eavl3))
                        {
                            node = node.SetParent(store, x);
                            if (node.IsLeft(x))
                            {
                                x = x.SetLeft(store, node);
                                NodeAVL right = node.GetRight(store);
                                x = x.SetRight(store, right);
                            }
                            else
                            {
                                x = x.SetRight(store, node);
                                NodeAVL left = node.GetLeft(store);
                                x = x.SetLeft(store, left);
                            }
                        }
                        else
                        {
                            node  = node.SetParent(store, eavl3);
                            eavl3 = eavl3.SetRight(store, node);
                            NodeAVL left  = node.GetLeft(store);
                            NodeAVL right = node.GetRight(store);
                            x = x.SetLeft(store, left);
                            x = x.SetRight(store, right);
                        }
                        x.GetRight(store).SetParent(store, x);
                        x.GetLeft(store).SetParent(store, x);
                        node = node.SetLeft(store, parent);
                        if (parent != null)
                        {
                            parent = parent.SetParent(store, node);
                        }
                        x = node.SetRight(store, null);
                    }
                    bool isleft = x.IsFromLeft(store);
                    x.Replace(store, this, parent);
                    parent = x.GetParent(store);
                    x.Delete();
                    while (parent != null)
                    {
                        NodeAVL eavl10;
                        int     balance;
                        NodeAVL eavl12;
                        x = parent;
                        int b = isleft ? 1 : -1;
                        switch ((x.GetBalance(store) * b))
                        {
                        case -1:
                            x = x.SetBalance(store, 0);
                            goto Label_0359;

                        case 0:
                            x = x.SetBalance(store, b);
                            return;

                        case 1:
                        {
                            eavl10  = x.Child(store, !isleft);
                            balance = eavl10.GetBalance(store);
                            if ((balance * b) < 0)
                            {
                                goto Label_02B7;
                            }
                            x.Replace(store, this, eavl10);
                            NodeAVL n = eavl10.Child(store, isleft);
                            x      = x.Set(store, !isleft, n);
                            eavl10 = eavl10.Set(store, isleft, x);
                            if (balance != 0)
                            {
                                break;
                            }
                            x      = x.SetBalance(store, b);
                            eavl10 = eavl10.SetBalance(store, -b);
                            return;
                        }

                        default:
                            goto Label_0359;
                        }
                        x = x.SetBalance(store, 0);
                        x = eavl10.SetBalance(store, 0);
                        goto Label_0359;
Label_02B7:
                        eavl12 = eavl10.Child(store, isleft);
                        x.Replace(store, this, eavl12);
                        balance = eavl12.GetBalance(store);
                        eavl10  = eavl10.Set(store, isleft, eavl12.Child(store, !isleft));
                        eavl12  = eavl12.Set(store, !isleft, eavl10);
                        x       = x.Set(store, !isleft, eavl12.Child(store, isleft));
                        eavl12  = eavl12.Set(store, isleft, x);
                        x       = x.SetBalance(store, (balance == b) ? -b : 0);
                        eavl10  = eavl10.SetBalance(store, (balance == -b) ? b : 0);
                        x       = eavl12.SetBalance(store, 0);
Label_0359:
                        isleft = x.IsFromLeft(store);
                        parent = x.GetParent(store);
                    }
                }
                finally
                {
                    store.UnlockStore();
                    Monitor.Exit(this._lock);
                }
            }
        }