Example #1
0
        public Node Next(Node x)
        {
            if ((++_needCleanUp & 127) == 0)
            {
                x.rData.CleanUpCache();
            }

            Node r = x.GetRight();

            if (r != null)
            {
                x = r;

                Node l = x.GetLeft();

                while (l != null)
                {
                    if (Trace.StopEnabled)
                    {
                        Trace.Stop();
                    }

                    x = l;
                    l = x.GetLeft();
                }

                return(x);
            }

            Node ch = x;

            x = x.GetParent();

            while (x != null && ch.Equals(x.GetRight()))
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                ch = x;
                x  = x.GetParent();
            }

            return(x);
        }
Example #2
0
        private void Replace(Node x, Node n)
        {
            if (x.Equals(_root))
            {
                _root = n;

                if (n != null)
                {
                    n.SetParent(null);
                }
            }
            else
            {
                Set(x.GetParent(), From(x), n);
            }
        }
Example #3
0
        public void Delete(object[] row, bool datatoo)
        {
            Node x = Search(row);

            if (x == null)
            {
                return;
            }

            Node n;

            if (x.GetLeft() == null)
            {
                n = x.GetRight();
            }
            else if (x.GetRight() == null)
            {
                n = x.GetLeft();
            }
            else
            {
                Node d = x;

                x = x.GetLeft();

                // todo: this can be improved
                while (x.GetRight() != null)
                {
                    if (Trace.StopEnabled)
                    {
                        Trace.Stop();
                    }

                    x = x.GetRight();
                }

                // x will be replaced with n later
                n = x.GetLeft();

                // swap d and x
                int b = x.GetBalance();

                x.SetBalance(d.GetBalance());
                d.SetBalance(b);

                // set x.parent
                Node xp = x.GetParent();
                Node dp = d.GetParent();

                if (d == _root)
                {
                    _root = x;
                }

                x.SetParent(dp);

                if (dp != null)
                {
                    if (dp.GetRight().Equals(d))
                    {
                        dp.SetRight(x);
                    }
                    else
                    {
                        dp.SetLeft(x);
                    }
                }

                // for in-memory tables we could use: d.rData=x.rData;
                // but not for cached tables
                // relink d.parent, x.left, x.right
                if (xp == d)
                {
                    d.SetParent(x);

                    if (d.GetLeft().Equals(x))
                    {
                        x.SetLeft(d);
                        x.SetRight(d.GetRight());
                    }
                    else
                    {
                        x.SetRight(d);
                        x.SetLeft(d.GetLeft());
                    }
                }
                else
                {
                    d.SetParent(xp);
                    xp.SetRight(d);
                    x.SetRight(d.GetRight());
                    x.SetLeft(d.GetLeft());
                }

                x.GetRight().SetParent(x);
                x.GetLeft().SetParent(x);

                // set d.left, d.right
                d.SetLeft(n);

                if (n != null)
                {
                    n.SetParent(d);
                }

                d.SetRight(null);

                x = d;
            }

            bool way = From(x);

            Replace(x, n);

            n = x.GetParent();

            x.Delete();

            if (datatoo)
            {
                x.rData.Delete();
            }

            while (n != null)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                x = n;

                int sign = way ? 1 : -1;

                switch (x.GetBalance() * sign)
                {
                case -1:
                    x.SetBalance(0);

                    break;

                case 0:
                    x.SetBalance(sign);

                    return;

                case 1:
                    Node r = Child(x, !way);
                    int  b = r.GetBalance();

                    if (b * sign >= 0)
                    {
                        Replace(x, r);
                        Set(x, !way, Child(r, way));
                        Set(r, way, x);

                        if (b == 0)
                        {
                            x.SetBalance(sign);
                            r.SetBalance(-sign);

                            return;
                        }

                        x.SetBalance(0);
                        r.SetBalance(0);

                        x = r;
                    }
                    else
                    {
                        Node l = Child(r, way);

                        Replace(x, l);

                        b = l.GetBalance();

                        Set(r, way, Child(l, !way));
                        Set(l, !way, r);
                        Set(x, !way, Child(l, way));
                        Set(l, way, x);
                        x.SetBalance((b == sign) ? -sign : 0);
                        r.SetBalance((b == -sign) ? sign : 0);
                        l.SetBalance(0);

                        x = l;
                    }
                    break;
                }

                way = From(x);
                n   = x.GetParent();
            }
        }
Example #4
0
        public void Insert(Node i)
        {
            object[] data = i.GetData();
            Node     n = _root, x = n;
            bool     way     = true;
            int      compare = -1;

            while (true)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                if (n == null)
                {
                    if (x == null)
                    {
                        _root = i;

                        return;
                    }

                    Set(x, way, i);

                    break;
                }

                x       = n;
                compare = CompareRow(data, x.GetData());

                Trace.Check(compare != 0, Trace.VIOLATION_OF_UNIQUE_INDEX);

                way = compare < 0;
                n   = Child(x, way);
            }

            while (true)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                int sign = way ? 1 : -1;

                switch (x.GetBalance() * sign)
                {
                case 1:
                    x.SetBalance(0);

                    return;

                case 0:
                    x.SetBalance(-sign);

                    break;

                case -1:
                    Node l = Child(x, way);

                    if (l.GetBalance() == -sign)
                    {
                        Replace(x, l);
                        Set(x, way, Child(l, !way));
                        Set(l, !way, x);
                        x.SetBalance(0);
                        l.SetBalance(0);
                    }
                    else
                    {
                        Node r = Child(l, !way);

                        Replace(x, r);
                        Set(l, !way, Child(r, way));
                        Set(r, way, l);
                        Set(x, way, Child(r, !way));
                        Set(r, !way, x);

                        int rb = r.GetBalance();

                        x.SetBalance((rb == -sign) ? sign : 0);
                        l.SetBalance((rb == sign) ? -sign : 0);
                        r.SetBalance(0);
                    }

                    return;
                }

                if (x.Equals(_root))
                {
                    return;
                }

                way = From(x);
                x   = x.GetParent();
            }
        }
Example #5
0
        private void Replace(Node x, Node n)
        {
            if (x.Equals(_root))
            {
                _root = n;

                if (n != null)
                {
                    n.SetParent(null);
                }
            }
            else
            {
                Set(x.GetParent(), From(x), n);
            }
        }
Example #6
0
        private bool From(Node x)
        {
            if (x.Equals(_root))
            {
                return true;
            }

            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(x.GetParent() != null);
            }

            return x.Equals(x.GetParent().GetLeft());
        }
Example #7
0
        public Node Next(Node x)
        {
            if ((++_needCleanUp & 127) == 0)
            {
                x.rData.CleanUpCache();
            }

            Node r = x.GetRight();

            if (r != null)
            {
                x = r;

                Node l = x.GetLeft();

                while (l != null)
                {
                    if (TracingHelper.StopEnabled)
                    {
                        TracingHelper.Stop();
                    }

                    x = l;
                    l = x.GetLeft();
                }

                return x;
            }

            Node ch = x;

            x = x.GetParent();

            while (x != null && ch.Equals(x.GetRight()))
            {
                if (TracingHelper.StopEnabled)
                {
                    TracingHelper.Stop();
                }

                ch = x;
                x = x.GetParent();
            }

            return x;
        }