diff --git a/xunit.runner.wpf/Extensions.FuncComparer.cs b/xunit.runner.wpf/Extensions.FuncComparer.cs new file mode 100644 index 0000000..5c41ce1 --- /dev/null +++ b/xunit.runner.wpf/Extensions.FuncComparer.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; + +namespace xunit.runner.wpf +{ + public static partial class Extensions + { + private class FuncComparer : IComparer + { + private readonly Func _comparison; + + public FuncComparer(Func comparison) + { + _comparison = comparison; + } + + public int Compare(T x, T y) => _comparison(x, y); + } + } +} diff --git a/xunit.runner.wpf/Extensions.cs b/xunit.runner.wpf/Extensions.cs index 8be16ca..7eb6838 100644 --- a/xunit.runner.wpf/Extensions.cs +++ b/xunit.runner.wpf/Extensions.cs @@ -1,13 +1,10 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace xunit.runner.wpf { - public static class Extensions + public static partial class Extensions { public static void AddRange(this ICollection list, IEnumerable items) where TEnumerable : TList { @@ -24,5 +21,80 @@ namespace xunit.runner.wpf list.Add(i); } } + + public static int BinarySearch(this ObservableCollection collection, int index, int length, TValue value, IComparer comparer, Func selector) + { + comparer = comparer ?? Comparer.Default; + + var low = index; + var high = (index + length) - 1; + + while (low <= high) + { + var mid = low + ((high - low) / 2); + var comp = comparer.Compare(selector(collection[mid]), value); + + if (comp == 0) + { + return mid; + } + + if (comp < 0) + { + low = mid + 1; + } + else + { + high = mid - 1; + } + } + + return ~low; + } + + public static int BinarySearch(this ObservableCollection collection, TValue value, IComparer comparer, Func selector) + { + return collection.BinarySearch(0, collection.Count, value, comparer, selector); + } + + public static int BinarySearch(this ObservableCollection collection, int index, int length, TValue value, Func comparison, Func selector) + { + return collection.BinarySearch(index, length, value, new FuncComparer(comparison), selector); + } + + public static int BinarySearch(this ObservableCollection collection, TValue value, Func comparison, Func selector) + { + return collection.BinarySearch(0, collection.Count, value, new FuncComparer(comparison), selector); + } + + public static int BinarySearch(this ObservableCollection collection, TValue value, Func selector) + { + return collection.BinarySearch(0, collection.Count, value, comparer: null, selector: selector); + } + + public static int BinarySearch(this ObservableCollection collection, int index, int length, T value, IComparer comparer) + { + return collection.BinarySearch(index, length, value, comparer, x => x); + } + + public static int BinarySearch(this ObservableCollection collection, T value, IComparer comparer) + { + return collection.BinarySearch(0, collection.Count, value, comparer, x => x); + } + + public static int BinarySearch(this ObservableCollection collection, T value) + { + return collection.BinarySearch(0, collection.Count, value, Comparer.Default, x => x); + } + + public static int BinarySearch(this ObservableCollection collection, int index, int length, T value, Func comparison) + { + return collection.BinarySearch(index, length, value, new FuncComparer(comparison), x => x); + } + + public static int BinarySearch(this ObservableCollection collection, T value, Func comparison) + { + return collection.BinarySearch(0, collection.Count, value, new FuncComparer(comparison), x => x); + } } } diff --git a/xunit.runner.wpf/FilteredCollectionView.cs b/xunit.runner.wpf/FilteredCollectionView.cs index a2e4762..a37ad1a 100644 --- a/xunit.runner.wpf/FilteredCollectionView.cs +++ b/xunit.runner.wpf/FilteredCollectionView.cs @@ -45,11 +45,7 @@ namespace xunit.runner.wpf protected virtual void OnItemChanged(T sender, PropertyChangedEventArgs args) { - var itemChanged = this.ItemChanged; - if (itemChanged != null) - { - itemChanged(sender, args); - } + this.ItemChanged?.Invoke(sender, args); } private TFilterArg filterArgument; @@ -187,11 +183,7 @@ namespace xunit.runner.wpf protected void OnCollectionChanged(NotifyCollectionChangedEventArgs args) { - var collectionChanged = this.CollectionChanged; - if (collectionChanged != null) - { - collectionChanged(this, args); - } + this.CollectionChanged?.Invoke(this, args); } public void Add(T item) diff --git a/xunit.runner.wpf/MainWindow.xaml b/xunit.runner.wpf/MainWindow.xaml index 6c8055d..67cae1c 100644 --- a/xunit.runner.wpf/MainWindow.xaml +++ b/xunit.runner.wpf/MainWindow.xaml @@ -119,13 +119,13 @@ - + - + @@ -139,29 +139,44 @@