Fixed more edge cases

This commit is contained in:
TheXamlGuy
2024-05-21 20:32:42 +01:00
parent 1190303044
commit 83fef5e399
18 changed files with 153 additions and 63 deletions
+23 -3
View File
@@ -1,5 +1,5 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Reactive.Disposables;
namespace Toolkit.Foundation;
@@ -10,6 +10,12 @@ public class Cache<TValue>(IDisposer disposer,
{
private readonly SortedSet<TValue> cache = new(comparer);
public int IndexOf(TValue value)
{
ImmutableSortedSet<TValue> hashSet = cache.ToImmutableSortedSet(comparer);
return hashSet.IndexOf(value);
}
public void Add(TValue value)
{
if (value is null)
@@ -21,6 +27,17 @@ public class Cache<TValue>(IDisposer disposer,
cache.Add(value);
}
public bool TryGetValue(TValue key, out TValue? value)
{
if (cache.TryGetValue(key, out value))
{
return true;
}
value = default;
return false;
}
public void Clear() => cache.Clear();
public IEnumerator<TValue> GetEnumerator() => cache.GetEnumerator();
@@ -30,14 +47,15 @@ public class Cache<TValue>(IDisposer disposer,
public bool Remove(TValue value) => cache.Remove(value);
}
public class Cache<TKey, TValue> :
public class Cache<TKey, TValue>(IDisposer disposer,
IComparer<TKey> comparer) :
ICache<TKey, TValue>
where TKey :
notnull
where TValue :
notnull
{
private readonly ConcurrentDictionary<TKey, TValue> cache = new();
private readonly SortedList<TKey, TValue> cache = new(comparer);
public void Add(TKey key,
TValue value)
@@ -49,6 +67,8 @@ public class Cache<TKey, TValue> :
public bool ContainsKey(TKey key) => cache.ContainsKey(key);
public int IndexOf(TKey key) => cache.IndexOfKey(key);
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => cache.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();