Add foundation

This commit is contained in:
TheXamlGuy
2024-04-13 11:29:32 +01:00
parent 6f31aa8513
commit 053d8a851e
264 changed files with 3428 additions and 4683 deletions
+68
View File
@@ -0,0 +1,68 @@
using HyperX;
using System.Collections;
using System.Collections.Concurrent;
using System.Reactive.Disposables;
namespace Toolkit.Foundation;
public class Cache<TValue>(IDisposer disposer) :
ICache<TValue>
{
private readonly List<TValue> cache = [];
public void Add(TValue value)
{
if (value is null)
{
return;
}
disposer.Add(value, Disposable.Create(() => Remove(value)));
cache.Add(value);
}
public void Clear() => cache.Clear();
public IEnumerator<TValue> GetEnumerator() => cache.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public bool Remove(TValue value) => cache.Remove(value);
}
public class Cache<TKey, TValue> :
ICache<TKey, TValue>
where TKey :
notnull
where TValue :
notnull
{
private readonly ConcurrentDictionary<TKey, TValue> cache = new();
public void Add(TKey key,
TValue value)
{
cache.TryAdd(key, value);
}
public void Clear() => cache.Clear();
public bool ContainsKey(TKey key) => cache.ContainsKey(key);
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => cache.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public bool Remove(TKey key) => cache.Remove(key, out _);
public bool TryGetValue(TKey key, out TValue? value)
{
if (cache.TryGetValue(key, out value))
{
return true;
}
value = default;
return false;
}
}