Beispiel #1
0
 internal int balanceRightBranch(ref TtreePage pgRef)
 {
     if (balance > 0)
     {
         balance = 0;
         return(UNDERFLOW);
     }
     else if (balance == 0)
     {
         balance = -1;
         return(OK);
     }
     else
     {
         TtreePage lp = this.left;
         lp.Load();
         lp.Modify();
         if (lp.balance <= 0)
         { // single LL turn
             this.left = lp.right;
             lp.right  = this;
             if (lp.balance == 0)
             {
                 balance    = -1;
                 lp.balance = 1;
                 pgRef      = lp;
                 return(OK);
             }
             else
             {
                 balance    = 0;
                 lp.balance = 0;
                 pgRef      = lp;
                 return(UNDERFLOW);
             }
         }
         else
         { // double LR turn
             TtreePage rp = lp.right;
             rp.Load();
             rp.Modify();
             lp.right   = rp.left;
             rp.left    = lp;
             this.left  = rp.right;
             rp.right   = this;
             balance    = rp.balance < 0 ? 1 : 0;
             lp.balance = rp.balance > 0 ? -1 : 0;
             rp.balance = 0;
             pgRef      = rp;
             return(UNDERFLOW);
         }
     }
 }
Beispiel #2
0
        internal int remove(PersistentComparator comparator, IPersistent mbr, ref TtreePage pgRef)
        {
            Load();
            TtreePage pg;
            int       n    = nItems;
            int       diff = comparator.CompareMembers(mbr, loadItem(0));

            if (diff <= 0)
            {
                if (left != null)
                {
                    Modify();
                    pg    = pgRef;
                    pgRef = left;
                    int h = left.remove(comparator, mbr, ref pgRef);
                    left  = pgRef;
                    pgRef = pg;
                    if (h == UNDERFLOW)
                    {
                        return(balanceLeftBranch(ref pgRef));
                    }
                    else if (h == OK)
                    {
                        return(OK);
                    }
                }
            }
            diff = comparator.CompareMembers(mbr, loadItem(n - 1));
            if (diff <= 0)
            {
                for (int i = 0; i < n; i++)
                {
                    if (item[i] == mbr)
                    {
                        if (n == 1)
                        {
                            if (right == null)
                            {
                                Deallocate();
                                pgRef = left;
                                return(UNDERFLOW);
                            }
                            else if (left == null)
                            {
                                Deallocate();
                                pgRef = right;
                                return(UNDERFLOW);
                            }
                        }
                        Modify();
                        if (n <= minItems)
                        {
                            if (left != null && balance <= 0)
                            {
                                TtreePage prev = left;
                                prev.Load();
                                while (prev.right != null)
                                {
                                    prev = prev.right;
                                    prev.Load();
                                }
                                Array.Copy(item, 0, item, 1, i);
                                //while (--i >= 0)
                                //{
                                //    item[i+1] = item[i];
                                //}
                                item[0] = prev.item[prev.nItems - 1];
                                pg      = pgRef;
                                pgRef   = left;
                                int h = left.remove(comparator, loadItem(0), ref pgRef);
                                left  = pgRef;
                                pgRef = pg;
                                if (h == UNDERFLOW)
                                {
                                    h = balanceLeftBranch(ref pgRef);
                                }
                                return(h);
                            }
                            else if (right != null)
                            {
                                TtreePage next = right;
                                next.Load();
                                while (next.left != null)
                                {
                                    next = next.left;
                                    next.Load();
                                }
                                Array.Copy(item, i + 1, item, i, n - i - 1);
                                //while (++i < n)
                                //{
                                //    item[i-1] = item[i];
                                //}
                                item[n - 1] = next.item[0];
                                pg          = pgRef;
                                pgRef       = right;
                                int h = right.remove(comparator, loadItem(n - 1), ref pgRef);
                                right = pgRef;
                                pgRef = pg;
                                if (h == UNDERFLOW)
                                {
                                    h = balanceRightBranch(ref pgRef);
                                }
                                return(h);
                            }
                        }
                        Array.Copy(item, i + 1, item, i, n - i - 1);
                        //while (++i < n)
                        //{
                        //    item[i-1] = item[i];
                        //}
                        item[n - 1] = null;
                        nItems     -= 1;
                        return(OK);
                    }
                }
            }
            if (right != null)
            {
                Modify();
                pg    = pgRef;
                pgRef = right;
                int h = right.remove(comparator, mbr, ref pgRef);
                right = pgRef;
                pgRef = pg;
                if (h == UNDERFLOW)
                {
                    return(balanceRightBranch(ref pgRef));
                }
                else
                {
                    return(h);
                }
            }
            return(NOT_FOUND);
        }
Beispiel #3
0
        internal int insert(PersistentComparator comparator, IPersistent mbr, bool unique, ref TtreePage pgRef)
        {
            Load();
            int       n = nItems;
            TtreePage pg;
            int       diff = comparator.CompareMembers(mbr, loadItem(0));

            if (diff <= 0)
            {
                if (unique && diff == 0)
                {
                    return(NOT_UNIQUE);
                }
                if ((left == null || diff == 0) && n != maxItems)
                {
                    Modify();
                    //for (int i = n; i > 0; i--) item[i] = item[i-1];
                    Array.Copy(item, 0, item, 1, n);
                    item[0] = mbr;
                    nItems += 1;
                    return(OK);
                }
                if (left == null)
                {
                    Modify();
                    left = new TtreePage(mbr);
                }
                else
                {
                    pg    = pgRef;
                    pgRef = left;
                    int result = left.insert(comparator, mbr, unique, ref pgRef);
                    if (result == NOT_UNIQUE)
                    {
                        return(NOT_UNIQUE);
                    }
                    Modify();
                    left  = pgRef;
                    pgRef = pg;
                    if (result == OK)
                    {
                        return(OK);
                    }
                }
                if (balance > 0)
                {
                    balance = 0;
                    return(OK);
                }
                else if (balance == 0)
                {
                    balance = -1;
                    return(OVERFLOW);
                }
                else
                {
                    TtreePage lp = this.left;
                    lp.Load();
                    lp.Modify();
                    if (lp.balance < 0)
                    { // single LL turn
                        this.left  = lp.right;
                        lp.right   = this;
                        balance    = 0;
                        lp.balance = 0;
                        pgRef      = lp;
                    }
                    else
                    { // double LR turn
                        TtreePage rp = lp.right;
                        rp.Load();
                        rp.Modify();
                        lp.right   = rp.left;
                        rp.left    = lp;
                        this.left  = rp.right;
                        rp.right   = this;
                        balance    = (rp.balance < 0) ? 1 : 0;
                        lp.balance = (rp.balance > 0) ? -1 : 0;
                        rp.balance = 0;
                        pgRef      = rp;
                    }
                    return(OK);
                }
            }
            diff = comparator.CompareMembers(mbr, loadItem(n - 1));
            if (diff >= 0)
            {
                if (unique && diff == 0)
                {
                    return(NOT_UNIQUE);
                }
                if ((right == null || diff == 0) && n != maxItems)
                {
                    Modify();
                    item[n] = mbr;
                    nItems += 1;
                    return(OK);
                }
                if (right == null)
                {
                    Modify();
                    right = new TtreePage(mbr);
                }
                else
                {
                    pg    = pgRef;
                    pgRef = right;
                    int result = right.insert(comparator, mbr, unique, ref pgRef);
                    if (result == NOT_UNIQUE)
                    {
                        return(NOT_UNIQUE);
                    }
                    Modify();
                    right = pgRef;
                    pgRef = pg;
                    if (result == OK)
                    {
                        return(OK);
                    }
                }
                if (balance < 0)
                {
                    balance = 0;
                    return(OK);
                }
                else if (balance == 0)
                {
                    balance = 1;
                    return(OVERFLOW);
                }
                else
                {
                    TtreePage rp = this.right;
                    rp.Load();
                    rp.Modify();
                    if (rp.balance > 0)
                    { // single RR turn
                        this.right = rp.left;
                        rp.left    = this;
                        balance    = 0;
                        rp.balance = 0;
                        pgRef      = rp;
                    }
                    else
                    { // double RL turn
                        TtreePage lp = rp.left;
                        lp.Load();
                        lp.Modify();
                        rp.left    = lp.right;
                        lp.right   = rp;
                        this.right = lp.left;
                        lp.left    = this;
                        balance    = (lp.balance > 0) ? -1 : 0;
                        rp.balance = (lp.balance < 0) ? 1 : 0;
                        lp.balance = 0;
                        pgRef      = lp;
                    }
                    return(OK);
                }
            }
            int l = 1, r = n - 1;

            while (l < r)
            {
                int i = (l + r) >> 1;
                diff = comparator.CompareMembers(mbr, loadItem(i));
                if (diff > 0)
                {
                    l = i + 1;
                }
                else
                {
                    r = i;
                    if (diff == 0)
                    {
                        if (unique)
                        {
                            return(NOT_UNIQUE);
                        }
                        break;
                    }
                }
            }
            // Insert before item[r]
            Modify();
            if (n != maxItems)
            {
                Array.Copy(item, r, item, r + 1, n - r);
                //for (int i = n; i > r; i--) item[i] = item[i-1];
                item[r] = mbr;
                nItems += 1;
                return(OK);
            }
            else
            {
                IPersistent reinsertItem;
                if (balance >= 0)
                {
                    reinsertItem = loadItem(0);
                    Array.Copy(item, 1, item, 0, r - 1);
                    //for (int i = 1; i < r; i++) item[i-1] = item[i];
                    item[r - 1] = mbr;
                }
                else
                {
                    reinsertItem = loadItem(n - 1);
                    Array.Copy(item, r, item, r + 1, n - r - 1);
                    //for (int i = n-1; i > r; i--) item[i] = item[i-1];
                    item[r] = mbr;
                }
                return(insert(comparator, reinsertItem, unique, ref pgRef));
            }
        }