From 2a67cf93ecd89aaa7706d9843945c200002cd90c Mon Sep 17 00:00:00 2001 From: James Groom Date: Wed, 13 Mar 2024 18:50:56 +0000 Subject: [PATCH] Replace `SortedSet` with `SortedList` in `InputRoll` see 6d40c08c3 --- .../CustomControls/InputRoll/Cell.cs | 29 ++++++++++++++++--- .../CustomControls/InputRoll/InputRoll.cs | 15 +++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs index 684f04854f..9d11d4fc9c 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs @@ -1,11 +1,14 @@ -using System.Collections.Generic; +using System; +using System.Diagnostics; + +using BizHawk.Common; namespace BizHawk.Client.EmuHawk { /// /// Represents a single cell of the /// - public class Cell + public sealed class Cell : IComparable { public RollColumn Column { get; internal set; } public int? RowIndex { get; internal set; } @@ -19,6 +22,9 @@ namespace BizHawk.Client.EmuHawk RowIndex = cell.RowIndex; } + public int CompareTo(Cell other) + => SortCell.Compare(this, other); + public override bool Equals(object obj) { if (obj is Cell cell) @@ -45,9 +51,9 @@ namespace BizHawk.Client.EmuHawk } } - internal class SortCell : IComparer + internal static class SortCell { - int IComparer.Compare(Cell c1, Cell c2) + public static int Compare(Cell c1, Cell c2) { if (c1 is null && c2 is null) { @@ -86,6 +92,21 @@ namespace BizHawk.Client.EmuHawk } } + public sealed class CellList : SortedList + { + /// restore the distinctness invariant from ; though I don't think we actually rely on it anywhere --yoshi + public override void Add(Cell item) + { + var i = _list.BinarySearch(item); + if (i >= 0) + { + Debug.Assert(false, $"{nameof(CellList)}'s distinctness invariant was almost broken! CellList.Add({(item is null ? "null" : $"Cell(r: {item.RowIndex}, c: \"{item.Column?.Name ?? "(unnamed)"}\")")})"); + return; + } + _list.Insert(~i, item); + } + } + public static class CellExtensions { public static bool IsDataCell(this Cell cell) diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs index f9ac41b797..2ef5e99ae9 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs @@ -22,7 +22,8 @@ namespace BizHawk.Client.EmuHawk public partial class InputRoll : Control { private readonly IControlRenderer _renderer; - private readonly SortedSet _selectedItems = new SortedSet(new SortCell()); + + private readonly CellList _selectedItems = new(); // scrollbar location(s) are calculated later (e.g. on resize) private readonly VScrollBar _vBar = new VScrollBar { Visible = false }; @@ -265,13 +266,10 @@ namespace BizHawk.Client.EmuHawk _rowCount = value; + //TODO replace this with a binary search + truncate if (_selectedItems.Max(s => s.RowIndex) >= _rowCount) { -#if NETFRAMEWORK _selectedItems.RemoveAll(i => i.RowIndex >= _rowCount); -#else - _selectedItems.RemoveWhere(i => i.RowIndex >= _rowCount); -#endif } RecalculateScrollBars(); @@ -576,7 +574,8 @@ namespace BizHawk.Client.EmuHawk } else { - _selectedItems.RemoveWhere(cell => cell.RowIndex == index); + IEnumerable items = _selectedItems.Where(cell => cell.RowIndex == index); + _selectedItems.RemoveAll(items.Contains); _lastSelectedRow = _selectedItems.LastOrDefault()?.RowIndex; } } @@ -612,7 +611,7 @@ namespace BizHawk.Client.EmuHawk public void TruncateSelection(int index) { - _selectedItems.RemoveWhere(cell => cell.RowIndex > index); + _selectedItems.RemoveAll(cell => cell.RowIndex > index); _lastSelectedRow = _selectedItems.LastOrDefault()?.RowIndex; } @@ -1788,7 +1787,7 @@ namespace BizHawk.Client.EmuHawk if (FullRowSelect) { - if (toggle && (_selectedItems.RemoveWhere(x => x.RowIndex == row) is not 0)) + if (toggle && (_selectedItems.RemoveAll(x => x.RowIndex == row) is not 0)) { _lastSelectedRow = _selectedItems.LastOrDefault()?.RowIndex; }