Upgrade BTree<>
parent
b643916f00
commit
a008409bce
|
@ -1,10 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using ln.types.collections;
|
||||||
|
|
||||||
namespace ln.types.btree
|
namespace ln.types.btree
|
||||||
{
|
{
|
||||||
public class BTree<K,V>
|
public class BTree<K,V> : IDict<K,V>
|
||||||
{
|
{
|
||||||
public Comparison<K> Comparison { get; }
|
public Comparison<K> Comparison { get; }
|
||||||
public int Count => count;
|
public int Count => count;
|
||||||
|
@ -229,6 +231,34 @@ namespace ln.types.btree
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
private TreeNode FindFirstGE(K key)
|
||||||
|
{
|
||||||
|
TreeNode node = headNode;
|
||||||
|
TreeNode minNode = null;
|
||||||
|
|
||||||
|
while (node != null)
|
||||||
|
{
|
||||||
|
if ((Comparison(node.Key, key) >= 0) && ((minNode == null) || (Comparison(node.Key, minNode.Key) < 0)))
|
||||||
|
minNode = node;
|
||||||
|
|
||||||
|
int comp = Comparison(key, node.Key);
|
||||||
|
if (comp == 0)
|
||||||
|
return node;
|
||||||
|
else if (comp < 0)
|
||||||
|
{
|
||||||
|
if (node.Left == null)
|
||||||
|
return minNode;
|
||||||
|
node = node.Left;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (node.Right == null)
|
||||||
|
return minNode;
|
||||||
|
node = node.Right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private TreeNode Previous(TreeNode node)
|
private TreeNode Previous(TreeNode node)
|
||||||
{
|
{
|
||||||
|
@ -409,6 +439,21 @@ namespace ln.types.btree
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<K> KeysGreaterEqual(K first)
|
||||||
|
{
|
||||||
|
TreeNode node = FindFirstGE(first);
|
||||||
|
while (node != null)
|
||||||
|
{
|
||||||
|
yield return node.Key;
|
||||||
|
node = Next(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(object key, object value) => Add((K)key, (V)value);
|
||||||
|
public void Remove(object key) => Remove((K)key);
|
||||||
|
public bool ContainsKey(object key) => ContainsKey((K)key);
|
||||||
|
public object this[object key] { get => this[(K)key]; set => this[(K)key] = (V)value; }
|
||||||
|
|
||||||
public IEnumerable<K> Keys
|
public IEnumerable<K> Keys
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -434,6 +479,9 @@ namespace ln.types.btree
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IEnumerable IDict.Keys => Keys;
|
||||||
|
IEnumerable IDict.Values => Values;
|
||||||
|
|
||||||
class TreeNode
|
class TreeNode
|
||||||
{
|
{
|
||||||
public BTree<K, V> Tree { get; }
|
public BTree<K, V> Tree { get; }
|
||||||
|
|
Loading…
Reference in New Issue