Added BTree.PreviousOrCurrent* method family

master
Harald Wolff-Thobaben 2020-11-19 20:40:25 +01:00
parent d07d0e3dae
commit 21519c831c
1 changed files with 82 additions and 44 deletions

View File

@ -259,6 +259,44 @@ namespace ln.collections
return false;
}
public K PreviousOrCurrent(K current)
{
if (!TryGetPreviousOrCurrent(current, out K previousOrCurrent))
throw new KeyNotFoundException();
return previousOrCurrent;
}
public bool TryGetPreviousOrCurrent(K current, out K previousOrCurrent)
{
if (Empty)
{
previousOrCurrent = default(K);
return false;
}
TreeNode next = FindFirstGE(current);
if (next == null)
previousOrCurrent = Last();
else
previousOrCurrent = Previous(next).Key;
return true;
}
public bool TryGetPreviousOrCurrentValue(K current, out V previousOrCurrentValue)
{
if (Empty)
{
previousOrCurrentValue = default(V);
return false;
}
TreeNode next = FindFirstGE(current);
if (next == null)
previousOrCurrentValue = LastValue();
else
previousOrCurrentValue = Previous(next).Value;
return true;
}