public void BTree_IndexOf_Empty() { var btree = new BTreeDictionary <string, int>(); int i = btree.Keys.IndexOf("3"); Assert.AreEqual(-1, i); }
public void BTree_Get() { var btree = new BTreeDictionary <string, int>(); btree.Add("3", 3); Assert.AreEqual(3, btree["3"]); }
public void BTree_SetNonExisting() { var btree = new BTreeDictionary <string, int>(); btree["3"] = 3; Assert.AreEqual(3, btree["3"]); }
/// <summary> /// Splits this node into subnodes by creating a new "right" node /// and adds the (key,value) to the appropriate subnode. /// </summary> private Node SplitAndInsert(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree) { var iSplit = (count + 1) / 2; var right = new LeafNode(tree.LeafNodeChildren); right.count = count - iSplit; this.count = iSplit; right.totalCount = right.count; this.totalCount = this.count; Array.Copy(this.keys, iSplit, right.keys, 0, right.count); Array.Clear(this.keys, iSplit, right.count); Array.Copy(this.values, iSplit, right.values, 0, right.count); Array.Clear(this.values, iSplit, right.count); right.nextLeaf = this.nextLeaf; this.nextLeaf = right; if (tree.cmp.Compare(right.keys[0], key) < 0) { right.Add(key, value, tree); } else { this.Add(key, value, tree); } return(right); }
public void BTree_AddItem() { var btree = new BTreeDictionary <string, int>(); btree.Add("3", 3); Assert.AreEqual(1, btree.Count); }
public void BTree_IndexOf_existing_leaf_item() { var btree = new BTreeDictionary <string, int> { { "3", 3 } }; int i = btree.Keys.IndexOf("3"); Assert.AreEqual(0, i); }
public void BTree_IndexOf_nonexisting_large_leafitem() { var btree = new BTreeDictionary <string, int> { { "4", 4 }, { "2", 2 } }; int i = btree.Keys.IndexOf("5"); Assert.AreEqual(~2, i); }
public void BTree_IndexOf_nonexisting_small_leafitem() { var btree = new BTreeDictionary <string, int> { { "3", 3 }, { "2", 2 } }; int i = btree.Keys.IndexOf("1"); Assert.AreEqual(~0, i); }
public override (TValue, bool) Get(TKey key, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 0, count, key, tree.cmp); if (idx < 0) { return(default(TValue), false); } return(values[idx], true); }
private BTreeDictionary <string, int> Given_Dictionary(IEnumerable <int> items) { var btree = new BTreeDictionary <string, int>(); foreach (var item in items) { btree.Add(item.ToString(), item); } return(btree); }
public void BTree_ForceInternalNode() { var btree = new BTreeDictionary <string, int>(); foreach (var i in Enumerable.Range(0, 256)) { btree.Add(i.ToString(), i); } btree.Add("256", 256); }
public override (Node, Node) Add(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 0, count, key, tree.cmp); if (idx >= 0) { throw new ArgumentException("Duplicate key."); } return(Insert(~idx, key, value, tree)); }
public override (Node, Node) Set(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 0, count, key, tree.cmp); if (idx >= 0) { values[idx] = value; return(this, null); } return(Insert(~idx, key, value, tree)); }
public void BTree_GetFromDeepTree() { var btree = new BTreeDictionary <string, int>(); foreach (var i in Enumerable.Range(0, 1000)) { btree.Add(i.ToString(), i); } btree.Dump(); Assert.AreEqual(0, btree["0"]); Assert.AreEqual(500, btree["500"]); }
public Node Add(TKey key, Node node, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.cmp); if (idx >= 0) { throw new ArgumentException("Duplicate key."); } var subnode = nodes[~idx]; return(Insert(~idx, node.keys[0], node, tree).Item1); }
public override (TValue, bool) Get(TKey key, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.cmp); if (idx >= 0) { return(nodes[idx].Get(key, tree)); } else { var iPos = (~idx) - 1; return(nodes[iPos].Get(key, tree)); } }
public override bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 0, count, key, tree.cmp); if (idx >= 0) { --count; --totalCount; Array.Copy(keys, idx + 1, keys, idx, count - idx); Array.Copy(values, idx + 1, values, idx, count - idx); keys[count] = default(TKey); values[count] = default(TValue); return(true); } return(false); }
public void BTree_Enumerate() { var btree = new BTreeDictionary <string, int>(); btree.Add("3", 3); btree.Add("2", 2); var e = btree.GetEnumerator(); Assert.IsTrue(e.MoveNext()); Assert.AreEqual("2", e.Current.Key); Assert.AreEqual(2, e.Current.Value); Assert.IsTrue(e.MoveNext()); Assert.AreEqual("3", e.Current.Key); Assert.AreEqual(3, e.Current.Value); Assert.IsFalse(e.MoveNext()); }
public override bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.cmp); bool removed; if (idx >= 0) { removed = nodes[idx].Remove(key, tree); } else { var iPos = (~idx) - 1; removed = nodes[iPos].Remove(key, tree); } --this.totalCount; return(removed); }
private (Node, Node) Insert(int idx, TKey key, Node node, BTreeDictionary <TKey, TValue> tree) { if (count == keys.Length) { var newRight = SplitAndInsert(key, node, tree); return(this, newRight); } if (idx < count) { Array.Copy(keys, idx, keys, idx + 1, count - idx); Array.Copy(nodes, idx, nodes, idx + 1, count - idx); } keys[idx] = key; nodes[idx] = node; ++this.count; this.totalCount = SumNodeCounts(this.nodes, this.count); return(this, null); }
public void BTree_EnumeratorThrowIfMutated() { var btree = new BTreeDictionary <string, int>(); btree.Add("3", 3); var e = btree.GetEnumerator(); Assert.True(e.MoveNext()); btree.Add("2", 2); try { e.MoveNext(); Assert.Fail("Should have thrown exception"); } catch (InvalidOperationException) { } }
public void BTree_IndexOf() { var rnd = new Random(42); var btree = new BTreeDictionary <string, int>(); while (btree.Count < 100) { var n = rnd.Next(200); var s = n.ToString(); btree[s] = n; } var items = btree.Keys.ToArray(); for (int i = 0; i < items.Length; ++i) { Assert.AreEqual(i, btree.Keys.IndexOf(items[i])); } }
private (Node, Node) Insert(int idx, TKey key, TValue value, BTreeDictionary <TKey, TValue> tree) { if (count == keys.Length) { var newRight = SplitAndInsert(key, value, tree); return(this, newRight); } else if (idx < count) { // Make a hole Array.Copy(keys, idx, keys, idx + 1, count - idx); Array.Copy(values, idx, values, idx + 1, count - idx); } keys[idx] = key; values[idx] = value; ++this.count; ++this.totalCount; return(this, null); }
public override (Node, Node) Set(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.cmp); int iPos = (idx >= 0) ? idx : (~idx) - 1; var subnode = nodes[iPos]; var(leftNode, rightNode) = subnode.Set(key, value, tree); if (rightNode == null) { this.totalCount = SumNodeCounts(this.nodes, this.count); tree.Validate(this); return(leftNode, null); } else { return(Insert(iPos + 1, rightNode.keys[0], rightNode, tree)); } }
public void BTree_ItemsSorted() { var rnd = new Random(42); var btree = new BTreeDictionary <string, int>(); while (btree.Count < 500) { var n = rnd.Next(3000); var s = n.ToString(); btree[s] = n; } string prev = ""; foreach (var item in btree) { var cur = item.Key; Debug.Print("item.Key: {0}", item.Key); Assert.Less(prev, cur); prev = cur; } }
private Node SplitAndInsert(TKey key, Node node, BTreeDictionary <TKey, TValue> tree) { var iSplit = (count + 1) / 2; var right = new InternalNode(tree.InternalNodeChildren); right.count = count - iSplit; this.count = iSplit; Array.Copy(this.keys, iSplit, right.keys, 0, right.count); Array.Clear(this.keys, iSplit, right.count); Array.Copy(this.nodes, iSplit, right.nodes, 0, right.count); Array.Clear(this.nodes, iSplit, right.count); if (tree.cmp.Compare(right.keys[0], key) < 0) { right.Add(key, node, tree); this.totalCount = SumNodeCounts(this.nodes, this.count); } else { this.Add(key, node, tree); right.totalCount = SumNodeCounts(right.nodes, right.count); } return(right); }
public abstract bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree);
internal ValueCollection(BTreeDictionary <TKey, TValue> btree) : base(btree) { }
public abstract (Node, Node) Set(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree);
public abstract (TValue, bool) Get(TKey key, BTreeDictionary <TKey, TValue> tree);