Пример #1
0
        /**
         * Method declaration
         *
         *
         * @param d
         *
         * @return
         *
         * @throws Exception
         */
        private Node search(object[] d)
        {
            Node x = root;

            while (x != null)
            {
                if (Trace.STOP)
                {
                    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
        /**
         * Method declaration
         *
         *
         * @param x
         *
         * @return
         *
         * @throws Exception
         */
        public Node next(Node x)
        {
            if ((++iNeedCleanUp & 127) == 0)
            {
                x.rData.cleanUpCache();
            }

            Node r = x.getRight();

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

                Node l = x.getLeft();

                while (l != null)
                {
                    if (Trace.STOP)
                    {
                        Trace.stop();
                    }

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

                return(x);
            }

            Node ch = x;

            x = x.getParent();

            while (x != null && ch.Equals(x.getRight()))
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

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

            return(x);
        }
Пример #3
0
        /**
         * Method declaration
         *
         *
         * @return
         *
         * @throws Exception
         */
        public Node first()
        {
            Node x = root, l = x;

            while (l != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

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

            return(x);
        }
Пример #4
0
        /**
         * Method declaration
         *
         *
         * @param data
         *
         * @return
         *
         * @throws Exception
         */
        public Node find(object[] data)
        {
            Node x = root, n;

            while (x != null)
            {
                if (Trace.STOP)
                {
                    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);
        }
Пример #5
0
 /**
  * Method declaration
  *
  *
  * @param x
  * @param w
  *
  * @return
  *
  * @throws Exception
  */
 private Node child(Node x, bool w)
 {
     return(w ? x.getLeft() : x.getRight());
 }
Пример #6
0
        /**
         * Method declaration
         *
         *
         * @param value
         * @param compare
         *
         * @return
         *
         * @throws Exception
         */
        public Node findFirst(object value, int compare)
        {
            Trace.assert(compare == Expression.BIGGER ||
                         compare == Expression.EQUAL ||
                         compare == Expression.BIGGER_EQUAL,
                         "Index.findFirst");

            Node x     = root;
            int  iTest = 1;

            if (compare == Expression.BIGGER)
            {
                iTest = 0;
            }

            while (x != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                bool t = compareValue(value, x.getData()[iColumn_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()[iColumn_0]) >= iTest)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                x = next(x);
            }

            return(x);
        }
Пример #7
0
        /**
         * Method declaration
         *
         *
         * @param row
         * @param datatoo
         *
         * @throws Exception
         */
        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.STOP)
                    {
                        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.STOP)
                {
                    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;
                    }
                }

                way = from(x);
                n   = x.getParent();
            }
        }
Пример #8
0
 /**
  * Method declaration
  *
  *
  * @param x
  * @param w
  *
  * @return
  *
  * @throws Exception
  */
 private Node child(Node x, bool w)
 {
     return w ? x.getLeft() : x.getRight();
 }
Пример #9
0
        /**
         * Method declaration
         *
         *
         * @param x
         *
         * @return
         *
         * @throws Exception
         */
        public Node next(Node x)
        {
            if ((++iNeedCleanUp & 127) == 0)
            {
                x.rData.cleanUpCache();
            }

            Node r = x.getRight();

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

                Node l = x.getLeft();

                while (l != null)
                {
                    if (Trace.STOP)
                    {
                        Trace.stop();
                    }

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

                return x;
            }

            Node ch = x;

            x = x.getParent();

            while (x != null && ch.Equals(x.getRight()))
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

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

            return x;
        }