diff --git a/Toolkit.Avalonia/Toolkit.Avalonia.csproj b/Toolkit.Avalonia/Toolkit.Avalonia.csproj
index 795e2a3..a972a43 100644
--- a/Toolkit.Avalonia/Toolkit.Avalonia.csproj
+++ b/Toolkit.Avalonia/Toolkit.Avalonia.csproj
@@ -5,7 +5,7 @@
enable
-
+
diff --git a/Toolkit.Foundation/Cache.cs b/Toolkit.Foundation/Cache.cs
index d067ed4..d649dbf 100644
--- a/Toolkit.Foundation/Cache.cs
+++ b/Toolkit.Foundation/Cache.cs
@@ -2,7 +2,7 @@
namespace Toolkit.Foundation;
-public class Cache(IComparer? comparer = default) :
+public class Cache(IComparer comparer) :
ICache
{
private readonly List items = [];
@@ -15,15 +15,23 @@ public class Cache(IComparer? comparer = default) :
public void Add(TValue item)
{
- int index = items.BinarySearch(item, comparer);
- if (index < 0)
- {
- index = ~index;
- }
+ int index = FindInsertIndex(item);
items.Insert(index, item);
}
- public void Clear() => items.Clear();
+ public void Clear() =>
+ items.Clear();
+
+ public bool Contains(TValue item)
+ {
+ int index = items.IndexOf(item);
+ if (index >= 0)
+ {
+ return true;
+ }
+
+ return false;
+ }
public IEnumerator GetEnumerator() =>
items.GetEnumerator();
@@ -31,33 +39,23 @@ public class Cache(IComparer? comparer = default) :
IEnumerator IEnumerable.GetEnumerator() => items.GetEnumerator();
public int IndexOf(TValue item) =>
- items.BinarySearch(item, comparer);
+ items.IndexOf(item);
public bool Remove(TValue item)
{
- int index = items.BinarySearch(item, comparer);
+ int index = items.IndexOf(item);
if (index >= 0)
{
items.RemoveAt(index);
return true;
}
- 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);
+ int index = items.IndexOf(key);
if (index >= 0)
{
item = items[index];
@@ -67,6 +65,33 @@ public class Cache(IComparer? comparer = default) :
item = default;
return false;
}
+
+ private int FindInsertIndex(TValue item)
+ {
+ int low = 0, high = items.Count - 1;
+
+ while (low <= high)
+ {
+ int mid = (low + high) / 2;
+ int cmp = comparer.Compare(items[mid], item);
+
+ if (cmp < 0)
+ {
+ low = mid + 1;
+ }
+ else
+ {
+ high = mid - 1;
+ }
+ }
+
+ while (low < items.Count && comparer.Compare(items[low], item) == 0)
+ {
+ low++;
+ }
+
+ return low;
+ }
}
public class Cache(IComparer comparer) :
@@ -123,8 +148,19 @@ public class Cache(IComparer comparer) :
public void Clear() => items.Clear();
+ 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;
+ }
+
public IEnumerator> GetEnumerator() =>
- items.GetEnumerator();
+ items.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
@@ -157,18 +193,6 @@ public class Cache(IComparer comparer) :
value = default;
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(IComparer comparer) :
IComparer>
{
diff --git a/Toolkit.Foundation/ObservableCollection.cs b/Toolkit.Foundation/ObservableCollection.cs
index fad0037..e3daf3f 100644
--- a/Toolkit.Foundation/ObservableCollection.cs
+++ b/Toolkit.Foundation/ObservableCollection.cs
@@ -1,5 +1,4 @@
using CommunityToolkit.Mvvm.ComponentModel;
-using Microsoft.Extensions.DependencyInjection;
using System.Collections;
using System.Collections.Specialized;
using System.Reactive.Disposables;
@@ -36,17 +35,18 @@ public partial class ObservableCollection :
{
private readonly System.Collections.ObjectModel.ObservableCollection collection = [];
- private bool clearing;
+ private readonly Queue