public bool TryFindExact(K key, out V value) { value = default(V); Leaf <K, V> leaf = GetLeafThatMayContainKey(key, Root); int index = SearchHelpers.LowerBound(leaf.Keys, leaf.KeyIndex + 1, key); if (index == -1 || leaf.Keys[index].CompareTo(key) != 0) { return(false); } value = leaf.Values[index]; return(true); }
public static Node <K, V> ChooseSubtree(K key, Node <K, V> node) { if (node is Leaf <K, V> ) { return(node); } else { var n = node as InternalNode <K, V>; int index = SearchHelpers.UpperBound(n.Keys, n.KeyIndex + 1, key); if (index == -1) { index = n.KeyIndex + 1; } node = n.Children[index]; return(node); } }
internal void AddKeyChild(K key, Node <K, V> node) { Debug.Assert(KeyIndex < Keys.Length); if (KeyIndex == -1) { KeyIndex++; Keys[KeyIndex] = key; if (Children[KeyIndex] != default(Node <K, V>)) { Children[KeyIndex + 1] = node; } else { Children[KeyIndex] = node; } } else { // Find the index where to do insertion int insertIndex = SearchHelpers.UpperBound(Keys, KeyIndex + 1, key); if (insertIndex == -1) { insertIndex = KeyIndex + 1; } // Do we need to move keys and children? if (insertIndex <= KeyIndex) { // Move to the right all keys starting with insertIndex for (int i = KeyIndex + 1; i > insertIndex; i--) { Keys[i] = Keys[i - 1]; } KeyIndex++; } else { KeyIndex = insertIndex; } Keys[insertIndex] = key; AddChild(node); } }
internal void AddKeyValue(K key, V value) { Debug.Assert(KeyIndex < Keys.Length - 1); if (KeyIndex == -1) { KeyIndex++; Keys[KeyIndex] = key; Values[KeyIndex] = value; } else { int insertIndex = SearchHelpers.UpperBound(Keys, KeyIndex + 1, key); if (insertIndex == -1) { insertIndex = KeyIndex + 1; } if (insertIndex <= KeyIndex) { for (int i = KeyIndex + 1; i > insertIndex; i--) { Keys[i] = Keys[i - 1]; Values[i] = Values[i - 1]; } } Keys[insertIndex] = key; Values[insertIndex] = value; if (insertIndex <= KeyIndex) { KeyIndex++; } else { KeyIndex = insertIndex; } } }
public List <V> FindRange(K begin, K end) { var result = new List <V>(); Leaf <K, V> leaf = GetLeafThatMayContainKey(begin, Root); int index = SearchHelpers.LowerBound(leaf.Keys, leaf.KeyIndex + 1, begin); if (index == -1) { index = leaf.KeyIndex; } bool shouldStop = false; while (leaf != null) { for (; index <= leaf.KeyIndex; index++) { var key = leaf.Keys[index]; if (key.CompareTo(begin) < 0) { continue; } if (end.CompareTo(key) < 0) { shouldStop = true; break; } result.Add(leaf.Values[index]); } if (shouldStop) { break; } leaf = leaf.Next; index = 0; } return(result); }