public virtual TValue GetNearest <T>(T key) { IRedBlackLeaf <TKey, TValue> leaf = Root; IRedBlackLeaf <TKey, TValue> last = null; var compareKey = (TKey)Convert.ChangeType(key, typeof(TKey)); var compare = CreateLeaf(compareKey, default(TValue)); while (leaf != null) { if (leaf.GreaterThan(compare)) { leaf = leaf.Left; } else if (leaf.LessThan(compare)) { last = leaf; leaf = leaf.Right; } else { return(leaf.Value); } } if (last.IsEmpty()) { return(GetMaxLeaf().Value); } else { return(last.Value); } }
public virtual IRedBlackLeaf <TKey, TValue> Insert(IRedBlackLeaf <TKey, TValue> root, IRedBlackLeaf <TKey, TValue> leaf) { if (root.IsEmpty()) { root = leaf; } else { leaf.Parent = root; var direction = leaf.GreaterThan(root); root[direction] = Insert(root[direction], leaf); root = BalancePostInsert(root, direction); } return(root); }
public virtual TValue Get(TKey key) { Lock.EnterReadLock(); IRedBlackLeaf <TKey, TValue> leaf = Root; var compare = CreateLeaf(key, default(TValue)); while (leaf != null) { if (leaf.GreaterThan(compare)) { leaf = leaf.Left; } else if (leaf.LessThan(compare)) { leaf = leaf.Right; } else { break; } } Lock.ExitReadLock(); return(leaf.IsEmpty() ? default(TValue) : leaf.Value); }