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

126
BTree.cs
View File

@ -160,19 +160,19 @@ namespace ln.collections
if (node != null)
return node.Value;
throw new KeyNotFoundException("BTree is empty");
}
public bool TryGetFirstValue(out V value)
{
TreeNode node = First(headNode);
if (node != null)
{
value = node.Value;
return true;
}
value = default(V);
return false;
}
}
public bool TryGetFirstValue(out V value)
{
TreeNode node = First(headNode);
if (node != null)
{
value = node.Value;
return true;
}
value = default(V);
return false;
}
public K Last()
{
TreeNode node = Last(headNode);
@ -187,16 +187,16 @@ namespace ln.collections
return node.Value;
throw new KeyNotFoundException("BTree is empty");
}
public bool TryGetLastValue(out V value)
{
TreeNode node = Last(headNode);
if (node != null)
{
value = node.Value;
return true;
}
value = default(V);
return false;
public bool TryGetLastValue(out V value)
{
TreeNode node = Last(headNode);
if (node != null)
{
value = node.Value;
return true;
}
value = default(V);
return false;
}
public K Previous(K current)
@ -206,24 +206,24 @@ namespace ln.collections
return node.Key;
throw new KeyNotFoundException();
}
public bool TryGetPrevious(K current, out K previous)
{
public bool TryGetPrevious(K current, out K previous)
{
TreeNode node = Previous(Find(current));
if (node != null)
{
if (node != null)
{
previous = node.Key;
return true;
return true;
}
previous = default(K);
return false;
}
public bool TryGetPreviousValue(K current, out V previous)
{
public bool TryGetPreviousValue(K current, out V previous)
{
TreeNode node = Previous(Find(current));
if (node != null)
{
if (node != null)
{
previous = node.Value;
return true;
return true;
}
previous = default(V);
return false;
@ -236,29 +236,67 @@ namespace ln.collections
return node.Key;
throw new KeyNotFoundException();
}
public bool TryGetNext(K current, out K next)
{
public bool TryGetNext(K current, out K next)
{
TreeNode node = Next(Find(current));
if (node != null)
{
if (node != null)
{
next = node.Key;
return true;
return true;
}
next = default(K);
return false;
}
public bool TryGetNextValue(K current, out V next)
{
public bool TryGetNextValue(K current, out V next)
{
TreeNode node = Next(Find(current));
if (node != null)
{
if (node != null)
{
next = node.Value;
return true;
return true;
}
next = default(V);
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;
}
@ -302,7 +340,7 @@ namespace ln.collections
{
TreeNode node = headNode;
TreeNode minNode = null;
while (node != null)
{
if ((Comparison(node.Key, key) >= 0) && ((minNode == null) || (Comparison(node.Key, minNode.Key) < 0)))