From 35c4d2eb4d37b977540e666b9259c6007a6113b9 Mon Sep 17 00:00:00 2001 From: neuecc Date: Fri, 10 Sep 2021 14:43:42 +0900 Subject: [PATCH] where TKey : notnull, IComparable --- .../Runtime/IObservableCollection.cs | 6 +++--- .../Runtime/Internal/SortedView.cs | 2 +- .../Runtime/Internal/SortedViewViewComparer.cs | 2 +- .../Runtime/ObservableDictionary.Views.cs | 13 +------------ src/ObservableCollections/IObservableCollection.cs | 6 +++--- src/ObservableCollections/Internal/SortedView.cs | 10 ++++++++-- .../Internal/SortedViewViewComparer.cs | 10 ++++++++-- .../ObservableDictionary.Views.cs | 13 +------------ .../ObservableDictionaryTest.cs | 2 +- tools/PostBuildUtility/Program.cs | 1 + 10 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/IObservableCollection.cs b/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/IObservableCollection.cs index 8389311..572fcdb 100644 --- a/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/IObservableCollection.cs +++ b/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/IObservableCollection.cs @@ -51,19 +51,19 @@ namespace ObservableCollections public static class ObservableCollectionsExtensions { public static ISynchronizedView CreateSortedView(this IObservableCollection source, Func identitySelector, Func transform, IComparer comparer) - + where TKey : IComparable { return new SortedView(source, identitySelector, transform, comparer); } public static ISynchronizedView CreateSortedView(this IObservableCollection source, Func identitySelector, Func transform, IComparer viewComparer) - + where TKey : IComparable { return new SortedViewViewComparer(source, identitySelector, transform, viewComparer); } public static ISynchronizedView CreateSortedView(this IObservableCollection source, Func identitySelector, Func transform, Func compareSelector, bool ascending = true) - + where TKey : IComparable { return source.CreateSortedView(identitySelector, transform, new AnonymousComparer(compareSelector, ascending)); } diff --git a/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/Internal/SortedView.cs b/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/Internal/SortedView.cs index d0ca75c..13e6266 100644 --- a/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/Internal/SortedView.cs +++ b/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/Internal/SortedView.cs @@ -7,7 +7,7 @@ using System.Linq; namespace ObservableCollections.Internal { internal class SortedView : ISynchronizedView - + where TKey : IComparable { readonly IObservableCollection source; readonly Func transform; diff --git a/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/Internal/SortedViewViewComparer.cs b/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/Internal/SortedViewViewComparer.cs index c543490..70ec618 100644 --- a/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/Internal/SortedViewViewComparer.cs +++ b/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/Internal/SortedViewViewComparer.cs @@ -7,7 +7,7 @@ using System.Linq; namespace ObservableCollections.Internal { internal class SortedViewViewComparer : ISynchronizedView - + where TKey : IComparable { readonly IObservableCollection source; readonly Func transform; diff --git a/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/ObservableDictionary.Views.cs b/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/ObservableDictionary.Views.cs index 37b9fa2..96e6b8e 100644 --- a/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/ObservableDictionary.Views.cs +++ b/src/ObservableCollections.Unity/Assets/Plugins/ObservableCollections/Runtime/ObservableDictionary.Views.cs @@ -14,18 +14,7 @@ namespace ObservableCollections // reverse is no used. return new View(this, transform); } - - // using key implicitly - public ISynchronizedView, TView> CreateSortedView(Func, TView> transform, IComparer> comparer) - { - return this.CreateSortedView(x => x.Key, transform, comparer); - } - - public ISynchronizedView, TView> CreateSortedView(Func, TView> transform, IComparer viewComparer) - { - return this.CreateSortedView(x => x.Key, transform, viewComparer); - } - + class View : ISynchronizedView, TView> { readonly ObservableDictionary source; diff --git a/src/ObservableCollections/IObservableCollection.cs b/src/ObservableCollections/IObservableCollection.cs index ef21cac..47845ba 100644 --- a/src/ObservableCollections/IObservableCollection.cs +++ b/src/ObservableCollections/IObservableCollection.cs @@ -51,19 +51,19 @@ namespace ObservableCollections public static class ObservableCollectionsExtensions { public static ISynchronizedView CreateSortedView(this IObservableCollection source, Func identitySelector, Func transform, IComparer comparer) - where TKey : notnull + where TKey : notnull, IComparable { return new SortedView(source, identitySelector, transform, comparer); } public static ISynchronizedView CreateSortedView(this IObservableCollection source, Func identitySelector, Func transform, IComparer viewComparer) - where TKey : notnull + where TKey : notnull, IComparable { return new SortedViewViewComparer(source, identitySelector, transform, viewComparer); } public static ISynchronizedView CreateSortedView(this IObservableCollection source, Func identitySelector, Func transform, Func compareSelector, bool ascending = true) - where TKey : notnull + where TKey : notnull, IComparable { return source.CreateSortedView(identitySelector, transform, new AnonymousComparer(compareSelector, ascending)); } diff --git a/src/ObservableCollections/Internal/SortedView.cs b/src/ObservableCollections/Internal/SortedView.cs index 281446b..c3fb30e 100644 --- a/src/ObservableCollections/Internal/SortedView.cs +++ b/src/ObservableCollections/Internal/SortedView.cs @@ -7,7 +7,7 @@ using System.Linq; namespace ObservableCollections.Internal { internal class SortedView : ISynchronizedView - where TKey : notnull + where TKey : notnull, IComparable { readonly IObservableCollection source; readonly Func transform; @@ -212,7 +212,13 @@ namespace ObservableCollections.Internal public int Compare((T value, TKey id) x, (T value, TKey id) y) { - return comparer.Compare(x.value, y.value); + var compare = comparer.Compare(x.value, y.value); + if (compare == 0) + { + compare = Comparer.Default.Compare(x.id, y.id); + } + + return compare; } } } diff --git a/src/ObservableCollections/Internal/SortedViewViewComparer.cs b/src/ObservableCollections/Internal/SortedViewViewComparer.cs index d5633e0..81644d8 100644 --- a/src/ObservableCollections/Internal/SortedViewViewComparer.cs +++ b/src/ObservableCollections/Internal/SortedViewViewComparer.cs @@ -7,7 +7,7 @@ using System.Linq; namespace ObservableCollections.Internal { internal class SortedViewViewComparer : ISynchronizedView - where TKey : notnull + where TKey : notnull, IComparable { readonly IObservableCollection source; readonly Func transform; @@ -231,7 +231,13 @@ namespace ObservableCollections.Internal public int Compare((TView view, TKey id) x, (TView view, TKey id) y) { - return comparer.Compare(x.view, y.view); + var compare = comparer.Compare(x.view, y.view); + if (compare == 0) + { + compare = Comparer.Default.Compare(x.id, y.id); + } + + return compare; } } } diff --git a/src/ObservableCollections/ObservableDictionary.Views.cs b/src/ObservableCollections/ObservableDictionary.Views.cs index c8c00ea..fbeaed0 100644 --- a/src/ObservableCollections/ObservableDictionary.Views.cs +++ b/src/ObservableCollections/ObservableDictionary.Views.cs @@ -14,18 +14,7 @@ namespace ObservableCollections // reverse is no used. return new View(this, transform); } - - // using key implicitly - public ISynchronizedView, TView> CreateSortedView(Func, TView> transform, IComparer> comparer) - { - return this.CreateSortedView(x => x.Key, transform, comparer); - } - - public ISynchronizedView, TView> CreateSortedView(Func, TView> transform, IComparer viewComparer) - { - return this.CreateSortedView(x => x.Key, transform, viewComparer); - } - + class View : ISynchronizedView, TView> { readonly ObservableDictionary source; diff --git a/tests/ObservableCollections.Tests/ObservableDictionaryTest.cs b/tests/ObservableCollections.Tests/ObservableDictionaryTest.cs index 15c58bb..69329c5 100644 --- a/tests/ObservableCollections.Tests/ObservableDictionaryTest.cs +++ b/tests/ObservableCollections.Tests/ObservableDictionaryTest.cs @@ -106,7 +106,7 @@ namespace ObservableCollections.Tests var dict = new ObservableDictionary(); var view1 = dict.CreateView(x => new ViewContainer(x.Value)); var view2 = dict.CreateSortedView(x => x.Key, x => new ViewContainer(x.Value), x => x.Value, true); - var view3 = dict.CreateSortedView(x => new ViewContainer(x.Value), viewComparer: Comparer>.Default); + var view3 = dict.CreateSortedView(x => new ViewContainer(x.Value), x => x.Value, viewComparer: Comparer>.Default); var filter1 = new TestFilter2((x, v) => x.Value % 2 == 0); var filter2 = new TestFilter2((x, v) => x.Value % 2 == 0); var filter3 = new TestFilter2((x, v) => x.Value % 2 == 0); diff --git a/tools/PostBuildUtility/Program.cs b/tools/PostBuildUtility/Program.cs index 0363fda..7a35395 100644 --- a/tools/PostBuildUtility/Program.cs +++ b/tools/PostBuildUtility/Program.cs @@ -24,6 +24,7 @@ namespace PostBuildUtility // Remove nullable {"#nullable disable", "" }, {"where T : notnull", "" }, + {"where TKey : notnull, IComparable", "where TKey : IComparable" }, // override project specified {"where TKey : notnull", "" }, {">?", ">" }, // generics ? {"T?", "T" },