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