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); }
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)); } }