More edge cases

This commit is contained in:
TheXamlGuy
2024-05-26 14:51:25 +01:00
parent 6a2708c111
commit 2bd7b54fa0
6 changed files with 40 additions and 14 deletions
+22
View File
@@ -44,6 +44,17 @@ public class Cache<TValue>(IComparer<TValue>? comparer = default) :
return false;
}
public bool Contains(TValue key)
{
int index = items.BinarySearch(key, comparer);
if (index >= 0)
{
return true;
}
return false;
}
public bool TryGetValue(TValue key, out TValue? item)
{
int index = items.BinarySearch(key, comparer);
@@ -147,6 +158,17 @@ public class Cache<TKey, TValue>(IComparer<TKey> comparer) :
return false;
}
public bool Contains(TKey key)
{
int index = items.FindIndex(kvp => comparer.Compare(kvp.Key, key) == 0);
if (index >= 0)
{
items.RemoveAt(index);
return true;
}
return false;
}
private class KeyValuePairComparer<TK, TV>(IComparer<TK> comparer) :
IComparer<KeyValuePair<TK, TV>>
{