Replace `SortedSet` with `SortedList<T>` in `InputRoll`

see 6d40c08c3
This commit is contained in:
James Groom 2024-03-13 18:50:56 +00:00
parent 328d36d0ca
commit 2a67cf93ec
2 changed files with 32 additions and 12 deletions

View File

@ -1,11 +1,14 @@
using System.Collections.Generic;
using System;
using System.Diagnostics;
using BizHawk.Common;
namespace BizHawk.Client.EmuHawk
{
/// <summary>
/// Represents a single cell of the <see cref="InputRoll"/>
/// </summary>
public class Cell
public sealed class Cell : IComparable<Cell>
{
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<Cell>
internal static class SortCell
{
int IComparer<Cell>.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<Cell>
{
/// <remarks>restore the distinctness invariant from <see cref="System.Collections.Generic.SortedSet{T}"/>; though I don't think we actually rely on it anywhere --yoshi</remarks>
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)

View File

@ -22,7 +22,8 @@ namespace BizHawk.Client.EmuHawk
public partial class InputRoll : Control
{
private readonly IControlRenderer _renderer;
private readonly SortedSet<Cell> _selectedItems = new SortedSet<Cell>(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<Cell> 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;
}