internal void Add(Leaf <TKey, TValue> source, int sourceStart, int sourceStop) { for (int i = sourceStart; i < sourceStop; ++i) { Add(source.GetKey(i), source.GetValue(i)); } }
/// <summary> /// This iterator provides range query support. /// </summary> /// <param name="startKey">Minimum inclusive key value of range.</param> /// <param name="endKey">Maximum inclusive key value of range.</param> /// <returns>An enumerator for all key/value pairs between startKey and endKey.</returns> /// <remarks> /// Neither <em>startKey</em> or <em>endKey</em> need to be present in the collection. /// </remarks> /// <example> /// <code source="BtreeExamples\BtreeExample03\BtreeExample03.cs" lang="cs" /> /// </example> public IEnumerable <KeyValuePair <TKey, TValue> > BetweenKeys(TKey startKey, TKey endKey) { int index; Leaf <TKey, TValue> leaf = Find(startKey, out index); // When the supplied start key is not be found, start with the next highest key. if (index < 0) { index = ~index; } for (;;) { if (index < leaf.KeyCount) { if (leaf.GetKey(index).CompareTo(endKey) > 0) { yield break; } yield return(leaf.GetPair(index)); ++index; continue; } leaf = leaf.RightLeaf; if (leaf == null) { yield break; } index = 0; } }
// Insert element at preseeked path. private void Insert(TreePath <TKey, TValue> path, TKey key, TValue value) { Leaf <TKey, TValue> leaf = (Leaf <TKey, TValue>)path.TopNode; int leafIndex = path.TopNodeIndex; if (leaf.NotFull) { leaf.Insert(leafIndex, key, value); ++Count; return; } // Leaf overflow. Right split a new leaf. Leaf <TKey, TValue> newLeaf = new Leaf <TKey, TValue>(leaf); if (newLeaf.RightLeaf == null && leafIndex == leaf.KeyCount) { // Densify sequential loads. newLeaf.Add(key, value); } else { int halfway = leaf.KeyCount / 2 + 1; if (leafIndex < halfway) { // Left-side insert: Copy right side to the split leaf. newLeaf.Add(leaf, halfway - 1, leaf.KeyCount); leaf.Truncate(halfway - 1); leaf.Insert(leafIndex, key, value); } else { // Right-side insert: Copy split leaf parts and new key. newLeaf.Add(leaf, halfway, leafIndex); newLeaf.Add(key, value); newLeaf.Add(leaf, leafIndex, leaf.KeyCount); leaf.Truncate(halfway); } } // Promote anchor of split leaf. ++Count; Promote(path, newLeaf.GetKey(0), (Node <TKey>)newLeaf); }