From b3875e21aee018e1f95454d23e837be4f601b38c Mon Sep 17 00:00:00 2001 From: Zinfidel Date: Wed, 27 Nov 2019 20:52:08 -0800 Subject: [PATCH 1/3] Watch sorting - new algorithm that handles separators. Also moved comparers from fields into a dictionary to make selection more concise. --- .../tools/Watch/WatchList/WatchList.cs | 154 +++++------------- 1 file changed, 39 insertions(+), 115 deletions(-) diff --git a/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs b/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs index 6bd5fa2df0..c032833f63 100644 --- a/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs +++ b/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs @@ -30,14 +30,7 @@ namespace BizHawk.Client.Common public const string DOMAIN = "DomainColumn"; public const string NOTES = "NotesColumn"; - private static readonly WatchDomainComparer DomainComparer = new WatchDomainComparer(); - private static readonly WatchAddressComparer AddressComparer = new WatchAddressComparer(); - private static readonly WatchFullDisplayTypeComparer DisplayTypeComparer = new WatchFullDisplayTypeComparer(); - private static readonly WatchValueComparer ValueComparer = new WatchValueComparer(); - private static readonly WatchPreviousValueComparer PreviousValueComparer = new WatchPreviousValueComparer(); - private static readonly WatchValueDifferenceComparer ValueDifferenceComparer = new WatchValueDifferenceComparer(); - private static readonly WatchChangeCountComparer ChangeCountComparer = new WatchChangeCountComparer(); - private static readonly WatchNoteComparer NoteComparer = new WatchNoteComparer(); + private static readonly Dictionary> WatchComparers; private readonly List _watchList = new List(0); private readonly string _systemId; @@ -47,6 +40,25 @@ namespace BizHawk.Client.Common #region cTor(s) + /// + /// Static constructor for the class. + /// + static WatchList() + { + // Initialize mapping of columns to comparers for sorting. + WatchComparers = new Dictionary> + { + { ADDRESS, new WatchAddressComparer() }, + { VALUE, new WatchValueComparer() }, + { PREV, new WatchPreviousValueComparer() }, + { CHANGES, new WatchChangeCountComparer() }, + { DIFF, new WatchValueDifferenceComparer() }, + { TYPE, new WatchFullDisplayTypeComparer() }, + { DOMAIN, new WatchDomainComparer() }, + { NOTES, new WatchNoteComparer() } + }; + } + /// /// Initializes a new instance of the class /// that will contains a set of @@ -213,117 +225,29 @@ namespace BizHawk.Client.Common } /// - /// Sort the current list based on one of the constant + /// Sort the current list based on one of the column constants. /// - /// Value that specify sorting base - /// Value that define the ordering. Ascending (true) or descending (false) + /// The column to sort by. + /// Defines the order of the sort. Ascending (true) or descending (false) public void OrderWatches(string column, bool reverse) { - switch (column) + var separatorIndices = _watchList.Select((w, i) => new { watch = w, index = i }) + .Where(w => w.watch.IsSeparator) + .Select(w => w.index) + .ToList(); + + separatorIndices.Add(_watchList.Count); + + // Sort "blocks" of addresses between separators. + int startIndex = 0; + foreach (int index in separatorIndices) { - case ADDRESS: - if (reverse) - { - _watchList.Sort(AddressComparer); - _watchList.Reverse(); - } - else - { - _watchList.Sort(); - } - - break; - - case VALUE: - if (reverse) - { - _watchList.Sort(ValueComparer); - _watchList.Reverse(); - } - else - { - _watchList.Sort(ValueComparer); - } - - break; - - case PREV: - if (reverse) - { - _watchList.Sort(PreviousValueComparer); - _watchList.Reverse(); - } - else - { - _watchList.Sort(PreviousValueComparer); - } - - break; - - case DIFF: - if (reverse) - { - _watchList.Sort(ValueDifferenceComparer); - _watchList.Reverse(); - } - else - { - _watchList.Sort(ValueDifferenceComparer); - } - - break; - - case CHANGES: - if (reverse) - { - _watchList.Sort(ChangeCountComparer); - _watchList.Reverse(); - } - else - { - _watchList.Sort(ChangeCountComparer); - } - - break; - - case DOMAIN: - if (reverse) - { - _watchList.Sort(DomainComparer); - _watchList.Reverse(); - } - else - { - _watchList.Sort(DomainComparer); - } - - break; - - case TYPE: - if (reverse) - { - _watchList.Sort(DisplayTypeComparer); - _watchList.Reverse(); - } - else - { - _watchList.Sort(DisplayTypeComparer); - } - - break; - - case NOTES: - if (reverse) - { - _watchList.Sort(NoteComparer); - _watchList.Reverse(); - } - else - { - _watchList.Sort(NoteComparer); - } - - break; + _watchList.Sort(startIndex, index - startIndex, WatchComparers[column]); + if (reverse) + { + _watchList.Reverse(startIndex, index - startIndex); + } + startIndex = index + 1; } } From ac2c42ef74fd7dd94010cc5a4ab0e777e515a9c8 Mon Sep 17 00:00:00 2001 From: Zinfidel Date: Wed, 27 Nov 2019 21:46:49 -0800 Subject: [PATCH 2/3] Correct tab size inconsistency --- BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs b/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs index c032833f63..f259f95784 100644 --- a/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs +++ b/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs @@ -232,9 +232,9 @@ namespace BizHawk.Client.Common public void OrderWatches(string column, bool reverse) { var separatorIndices = _watchList.Select((w, i) => new { watch = w, index = i }) - .Where(w => w.watch.IsSeparator) - .Select(w => w.index) - .ToList(); + .Where(w => w.watch.IsSeparator) + .Select(w => w.index) + .ToList(); separatorIndices.Add(_watchList.Count); From e6218764534c18cf041b54055ddc539d1ef6c799 Mon Sep 17 00:00:00 2001 From: Zach Date: Mon, 2 Dec 2019 16:37:55 -0800 Subject: [PATCH 3/3] Replaced LINQ query with for loop. Upon reflection, the query looked nice, but was inferior to a plain for loop because it would have to create n anonymous objects to process the list, even with LINQ's filtering optimization, due to select preceding where. --- .../tools/Watch/WatchList/WatchList.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs b/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs index f259f95784..0c8b93793c 100644 --- a/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs +++ b/BizHawk.Client.Common/tools/Watch/WatchList/WatchList.cs @@ -231,11 +231,14 @@ namespace BizHawk.Client.Common /// Defines the order of the sort. Ascending (true) or descending (false) public void OrderWatches(string column, bool reverse) { - var separatorIndices = _watchList.Select((w, i) => new { watch = w, index = i }) - .Where(w => w.watch.IsSeparator) - .Select(w => w.index) - .ToList(); - + var separatorIndices = new List(); + for (var i = 0; i < _watchList.Count; i++) + { + if (_watchList[i].IsSeparator) + { + separatorIndices.Add(i); + } + } separatorIndices.Add(_watchList.Count); // Sort "blocks" of addresses between separators.