Esempio n. 1
0
 public void Clear()
 {
     if (root != null)
     {
         root.prune();
         Modify();
         root     = null;
         nMembers = 0;
     }
 }
Esempio n. 2
0
        internal int balanceRightBranch(ref TtreePage pgRef)
        {
            TtreePage lp, rp;
#endif
            if (balance > 0)
            {
                balance = 0;
                return(UNDERFLOW);
            }
            else if (balance == 0)
            {
                balance = -1;
                return(OK);
            }
            else
            {
                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
                    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);
                }
            }
        }
Esempio n. 3
0
 internal int balanceLeftBranch(ref TtreePage pgRef)
 {
     if (balance < 0)
     {
         balance = 0;
         return(UNDERFLOW);
     }
     else if (balance == 0)
     {
         balance = 1;
         return(OK);
     }
     else
     {
         TtreePage rp = this.right;
         rp.Load();
         rp.Modify();
         if (rp.balance >= 0)
         { // single RR turn
             this.right = rp.left;
             rp.left    = this;
             if (rp.balance == 0)
             {
                 this.balance = 1;
                 rp.balance   = -1;
                 pgRef        = rp;
                 return(OK);
             }
             else
             {
                 balance    = 0;
                 rp.balance = 0;
                 pgRef      = rp;
                 return(UNDERFLOW);
             }
         }
         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(UNDERFLOW);
         }
     }
 }
Esempio n. 4
0
        public void Remove(IPersistent obj)
        {
            if (root == null)
            {
                throw new StorageError(StorageError.ErrorCode.KEY_NOT_FOUND);
            }
            TtreePage newRoot = root;

            if (root.remove(comparator, obj, ref newRoot) == TtreePage.NOT_FOUND)
            {
                throw new StorageError(StorageError.ErrorCode.KEY_NOT_FOUND);
            }
            Modify();
            root      = newRoot;
            nMembers -= 1;
        }
Esempio n. 5
0
        public void Add(object obj)
        {
            TtreePage newRoot = root;

            if (root == null)
            {
                newRoot = new TtreePage(Storage, obj);
            }
            else
            {
                if (root.insert(comparator, obj, unique, ref newRoot) == TtreePage.NOT_UNIQUE)
                {
                    return;
                }
            }
            Modify();
            root      = newRoot;
            nMembers += 1;
        }
Esempio n. 6
0
        public override void Add(V obj)
        {
            TtreePage <K, V> newRoot = root;

            if (root == null)
            {
                newRoot = new TtreePage <K, V>(Storage, obj);
            }
            else
            {
                if (root.insert(comparator, obj, unique, ref newRoot) == TtreePage <K, V> .NOT_UNIQUE)
                {
                    return;
                }
            }
            Modify();
            root      = newRoot;
            nMembers += 1;
        }
Esempio n. 7
0
        public bool Add(IPersistent obj)
        {
            TtreePage newRoot = root;

            if (root == null)
            {
                newRoot = new TtreePage(obj);
            }
            else
            {
                if (root.insert(comparator, obj, unique, ref newRoot) == TtreePage.NOT_UNIQUE)
                {
                    return(false);
                }
            }
            Modify();
            root      = newRoot;
            nMembers += 1;
            return(true);
        }
Esempio n. 8
0
        public bool Remove(object obj)
#endif
        {
            if (root == null)
            {
                return(false);
            }
#if USE_GENERICS
            TtreePage <K, V> newRoot = root;
            if (root.remove(comparator, obj, ref newRoot) == TtreePage <K, V> .NOT_FOUND)
#else
            TtreePage newRoot = root;
            if (root.remove(comparator, obj, ref newRoot) == TtreePage.NOT_FOUND)
#endif
            {
                throw new StorageError(StorageError.ErrorCode.KEY_NOT_FOUND);
            }
            Modify();
            root      = newRoot;
            nMembers -= 1;
            return(true);
        }
Esempio n. 9
0
        internal int remove(PersistentComparator comparator, object mbr, ref TtreePage pgRef)
        {
            TtreePage pg, next, prev;
#endif
            Load();
            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)
                            {
                                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)
                            {
                                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);
        }
Esempio n. 10
0
 internal int remove(PersistentComparator <K, V> comparator, V mbr, ref TtreePage <K, V> pgRef)
 {
     TtreePage <K, V> pg, next, prev;
Esempio n. 11
0
 internal int balanceRightBranch(ref TtreePage <K, V> pgRef)
 {
     TtreePage <K, V> lp, rp;
Esempio n. 12
0
        internal int insert(PersistentComparator comparator, object mbr, bool unique, ref TtreePage pgRef)
        {
            TtreePage pg, lp, rp;
            object    reinsertItem;
#endif
            Load();
            int n    = nItems;
            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();
#if USE_GENERICS
                    left = new TtreePage <K, V>(Storage, mbr);
#else
                    left = new TtreePage(Storage, mbr);
#endif
                }
                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
                {
                    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
                        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();
#if USE_GENERICS
                    right = new TtreePage <K, V>(Storage, mbr);
#else
                    right = new TtreePage(Storage, mbr);
#endif
                }
                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
                {
                    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
                        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
            {
                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));
            }
        }
Esempio n. 13
0
 internal int insert(PersistentComparator <K, V> comparator, V mbr, bool unique, ref TtreePage <K, V> pgRef)
 {
     TtreePage <K, V> pg, lp, rp;
     V reinsertItem;