public static bool TryGetLowerBoundKey <K, V>(this BTreeDictionary <K, V> list, K key, out K closestKey) { var cmp = list.Comparer; int lo = 0; int hi = list.Count - 1; closestKey = default(K); bool set = false; while (lo <= hi) { int mid = (hi - lo) / 2 + lo; K k = list.Keys[mid]; int c = cmp.Compare(k, key); if (c == 0) { closestKey = k; return(true); } if (c < 0) { closestKey = k; set = true; lo = mid + 1; } else { hi = mid - 1; } } return(set); }
//$TODO: .NET 5 [NotNullWhen(returnValue: true)] public static bool TryGetLowerBound <K, V>(this BTreeDictionary <K, V> list, K key, out V value) { var cmp = list.Comparer; int lo = 0; int hi = list.Count - 1; value = default !;
public static bool TryGetUpperBoundIndex <K, V>(this BTreeDictionary <K, V> list, K key, out int closestIndex) { var cmp = list.Comparer; int lo = 0; int hi = list.Count - 1; closestIndex = -1; bool set = false; while (lo <= hi) { int mid = (hi - lo) / 2 + lo; K k = list.Keys[mid]; int c = cmp.Compare(k, key); if (c == 0) { closestIndex = mid; return(true); } if (c < 0) { lo = mid + 1; } else { closestIndex = mid; set = true; hi = mid - 1; } } return(set); }
public static bool TryGetUpperBound <K, V>(this BTreeDictionary <K, V> list, K key, out V value) { var cmp = list.Comparer; int lo = 0; int hi = list.Count - 1; value = default(V); bool set = false; while (lo <= hi) { int mid = (hi - lo) / 2 + lo; K k = list.Keys[mid]; int c = cmp.Compare(k, key); if (c == 0) { value = list.Values[mid]; return(true); } if (c > 0) { value = list.Values[mid]; set = true; hi = mid - 1; } else { lo = mid + 1; } } return(set); }
/// <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.Comparer.Compare(right.keys[0], key) < 0) { right.Put(key, value, false, tree); } else { this.Put(key, value, false, tree); } return(right); }
public Node AddNode(TKey key, Node node, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.Comparer); if (idx >= 0) { throw new ArgumentException("Duplicate key."); } 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, 0, count, key, tree.Comparer); if (idx < 0) { return(default(TValue), false); } return(values[idx], true); }
public override bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 0, count, key, tree.Comparer); 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 !;
public override (TValue, bool) Get(TKey key, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.Comparer); if (idx >= 0) { return(nodes[idx].Get(key, tree)); } else { var iPos = (~idx) - 1; return(nodes[iPos].Get(key, tree)); } }
public override (Node, Node?) Put(TKey key, TValue value, bool setting, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 0, count, key, tree.Comparer); if (idx >= 0) { if (!setting) { throw new ArgumentException("Duplicate key."); } values[idx] = value; return(this, null); } else { return(Insert(~idx, key, value, tree)); } }
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 override (Node, Node) Put(TKey key, TValue value, bool setting, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.Comparer); int iPos = (idx >= 0) ? idx : (~idx) - 1; var subnode = nodes[iPos]; var(leftNode, rightNode) = subnode.Put(key, value, setting, tree); if (rightNode == null) { this.totalCount = SumNodeCounts(this.nodes, this.count); return(leftNode, null); } else { return(Insert(iPos + 1, rightNode.keys[0], rightNode, tree)); } }
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 bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree) { int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.Comparer); bool removed; if (idx >= 0) { removed = nodes[idx].Remove(key, tree); } else { var iPos = (~idx) - 1; removed = nodes[iPos].Remove(key, tree); } if (removed) { --this.totalCount; } return(removed); }
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.Comparer.Compare(right.keys[0], key) < 0) { right.AddNode(key, node, tree); this.totalCount = SumNodeCounts(this.nodes, this.count); } else { this.AddNode(key, node, tree); right.totalCount = SumNodeCounts(right.nodes, right.count); } return(right); }
protected Collection(BTreeDictionary <TKey, TValue> btree) { this.btree = btree; }
internal ValueCollection(BTreeDictionary <TKey, TValue> btree) : base(btree) { }
public abstract (TValue, bool) Get(TKey key, BTreeDictionary <TKey, TValue> tree);
public abstract (Node, Node?) Put(TKey key, TValue value, bool setting, BTreeDictionary <TKey, TValue> tree);
public abstract bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree);