From 974bdd180485057bdd830183b39b8924da1bd489 Mon Sep 17 00:00:00 2001 From: MikeFH Date: Sat, 13 Mar 2021 19:43:18 +0100 Subject: [PATCH] Refactor InsertResult We now use BinarySearch in all cases to perform sorted insertion --- ILSpy/ExtensionMethods.cs | 19 +++++++++++++++++++ ILSpy/Search/SearchPane.cs | 30 ++++++------------------------ ILSpy/Search/SearchResult.cs | 14 ++++++++++++-- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/ILSpy/ExtensionMethods.cs b/ILSpy/ExtensionMethods.cs index 5ebe7ecef..23a376c1a 100644 --- a/ILSpy/ExtensionMethods.cs +++ b/ILSpy/ExtensionMethods.cs @@ -93,6 +93,25 @@ namespace ICSharpCode.ILSpy } return ~start; } + + public static void InsertSorted(this IList list, T item, IComparer comparer) + { + if (list == null) + throw new ArgumentNullException(nameof(list)); + if (comparer == null) + throw new ArgumentNullException(nameof(comparer)); + + if (list.Count == 0) + { + list.Add(item); + } + else + { + int index = list.BinarySearch(item, 0, list.Count, comparer); + list.Insert(index < 0 ? ~index : index, item); + } + } + /* public static bool IsCustomAttribute(this TypeDefinition type) { diff --git a/ILSpy/Search/SearchPane.cs b/ILSpy/Search/SearchPane.cs index d877e55eb..204457c08 100644 --- a/ILSpy/Search/SearchPane.cs +++ b/ILSpy/Search/SearchPane.cs @@ -34,6 +34,7 @@ using System.Windows.Threading; using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.ILSpy.Docking; +using ICSharpCode.ILSpy.Options; using ICSharpCode.ILSpy.Search; using ICSharpCode.ILSpy.ViewModels; @@ -253,30 +254,11 @@ namespace ICSharpCode.ILSpy void InsertResult(IList results, SearchResult result) { - if (results.Count == 0) - { - results.Add(result); - } - else if (Options.DisplaySettingsPanel.CurrentDisplaySettings.SortResults) - { - // Keep results collection sorted by "Fitness" by inserting result into correct place - // Inserts in the beginning shifts all elements, but there can be no more than 1000 items. - for (int i = 0; i < results.Count; i++) - { - if (results[i].Fitness < result.Fitness) - { - results.Insert(i, result); - return; - } - } - results.Add(result); - } - else - { - // Original Code - int index = results.BinarySearch(result, 0, results.Count - 1, SearchResult.Comparer); - results.Insert(index < 0 ? ~index : index, result); - } + var comparer = DisplaySettingsPanel.CurrentDisplaySettings.SortResults ? + SearchResult.ComparerByFitness : + SearchResult.ComparerByName; + + results.InsertSorted(result, comparer); } void JumpToSelectedItem() diff --git a/ILSpy/Search/SearchResult.cs b/ILSpy/Search/SearchResult.cs index 5450a0b14..124a2fe7d 100644 --- a/ILSpy/Search/SearchResult.cs +++ b/ILSpy/Search/SearchResult.cs @@ -29,7 +29,8 @@ namespace ICSharpCode.ILSpy { public class SearchResult { - public static readonly IComparer Comparer = new SearchResultComparer(); + public static readonly IComparer ComparerByName = new SearchResultNameComparer(); + public static readonly IComparer ComparerByFitness = new SearchResultFitnessComparer(); public virtual object Reference { get { @@ -57,13 +58,22 @@ namespace ICSharpCode.ILSpy return Name; } - class SearchResultComparer : IComparer + class SearchResultNameComparer : IComparer { public int Compare(SearchResult x, SearchResult y) { return StringComparer.Ordinal.Compare(x?.Name ?? "", y?.Name ?? ""); } } + + class SearchResultFitnessComparer : IComparer + { + public int Compare(SearchResult x, SearchResult y) + { + //elements with higher Fitness come first + return Comparer.Default.Compare(y?.Fitness ?? 0, x?.Fitness ?? 0); + } + } } public class MemberSearchResult : SearchResult