示例#1
0
        private Node Search(object[] d)
        {
            Node x = _root;

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

                int c = CompareRow(d, x.GetData());

                if (c == 0)
                {
                    return(x);
                }
                else if (c < 0)
                {
                    x = x.GetLeft();
                }
                else
                {
                    x = x.GetRight();
                }
            }

            return(null);
        }
示例#2
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);
        }
示例#3
0
        public Node Find(object[] data)
        {
            Node x = _root, n;

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

                int i = CompareRowNonUnique(data, x.GetData());

                if (i == 0)
                {
                    return(x);
                }
                else if (i > 0)
                {
                    n = x.GetRight();
                }
                else
                {
                    n = x.GetLeft();
                }

                if (n == null)
                {
                    return(null);
                }

                x = n;
            }

            return(null);
        }
示例#4
0
 private Node Child(Node x, bool w)
 {
     return(w ? x.GetLeft() : x.GetRight());
 }
示例#5
0
        public Node FindFirst(object value, ExpressionType compare)
        {
            Trace.Assert(compare == ExpressionType.Bigger ||
                         compare == ExpressionType.Equal ||
                         compare == ExpressionType.BiggerEqual,
                         "Index.findFirst");

            Node x     = _root;
            int  iTest = 1;

            if (compare == ExpressionType.Bigger)
            {
                iTest = 0;
            }

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

                bool t = CompareValue(value, x.GetData()[_column_0]) >= iTest;

                if (t)
                {
                    Node r = x.GetRight();

                    if (r == null)
                    {
                        break;
                    }

                    x = r;
                }
                else
                {
                    Node l = x.GetLeft();

                    if (l == null)
                    {
                        break;
                    }

                    x = l;
                }
            }

            while (x != null &&
                   CompareValue(value, x.GetData()[_column_0]) >= iTest)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                x = Next(x);
            }

            return(x);
        }
示例#6
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();
            }
        }
示例#7
0
 private Node Child(Node x, bool w)
 {
     return w ? x.GetLeft() : x.GetRight();
 }
示例#8
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;
        }