Reduce LINQ calls on `CellList`

This commit is contained in:
YoshiRulz 2024-03-15 05:56:26 +10:00 committed by James Groom
parent 0f9f6f0e53
commit 57439f9697
3 changed files with 32 additions and 9 deletions

View File

@ -2,6 +2,7 @@
using System.Diagnostics; using System.Diagnostics;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
@ -105,6 +106,21 @@ namespace BizHawk.Client.EmuHawk
} }
_list.Insert(~i, item); _list.Insert(~i, item);
} }
public bool IncludesRow(int rowIndex)
#if false
=> _list.Exists(cell => cell.RowIndex == rowIndex);
#elif false
{
var i = _list.BinarySearch(new() { RowIndex = rowIndex, Column = null });
return i >= 0 || (~i < _list.Count && _list[~i].RowIndex == rowIndex);
}
#else
{
var i = _list.LowerBoundBinarySearch(static c => c.RowIndex ?? -1, rowIndex);
return i >= 0 && _list[i].RowIndex == rowIndex;
}
#endif
} }
public static class CellExtensions public static class CellExtensions

View File

@ -969,7 +969,7 @@ namespace BizHawk.Client.EmuHawk
=> SelectedRowsWithDuplicates.First(); => SelectedRowsWithDuplicates.First();
public bool IsRowSelected(int rowIndex) public bool IsRowSelected(int rowIndex)
=> _selectedItems.Any(cell => cell.RowIndex == rowIndex); => _selectedItems.IncludesRow(rowIndex);
public IEnumerable<ToolStripItem> GenerateContextMenuItems() public IEnumerable<ToolStripItem> GenerateContextMenuItems()
{ {
@ -1159,12 +1159,12 @@ namespace BizHawk.Client.EmuHawk
} }
else if (ModifierKeys == Keys.Shift && CurrentCell.Column.Type == ColumnType.Text) else if (ModifierKeys == Keys.Shift && CurrentCell.Column.Type == ColumnType.Text)
{ {
if (_selectedItems.Any()) if (_selectedItems.Count is not 0)
{ {
if (FullRowSelect) if (FullRowSelect)
{ {
var targetRow = CurrentCell.RowIndex.Value; var targetRow = CurrentCell.RowIndex.Value;
if (!_selectedItems.Any(c => c.RowIndex == targetRow)) if (!_selectedItems.IncludesRow(targetRow))
{ {
int additionStart, additionEndExcl; int additionStart, additionEndExcl;
SortedList<int> rowIndices = new(SelectedRows); SortedList<int> rowIndices = new(SelectedRows);
@ -1465,8 +1465,8 @@ namespace BizHawk.Client.EmuHawk
{ {
if (MultiSelect && _lastSelectedRow > 0) if (MultiSelect && _lastSelectedRow > 0)
{ {
if (_selectedItems.Any(i => i.RowIndex == _lastSelectedRow.Value) if (_selectedItems.IncludesRow(_lastSelectedRow.Value)
&& _selectedItems.Any(i => i.RowIndex == _lastSelectedRow - 1)) // Unhighlight if already highlighted && _selectedItems.IncludesRow(_lastSelectedRow.Value - 1)) // Unhighlight if already highlighted
{ {
SelectRow(_lastSelectedRow.Value, false); SelectRow(_lastSelectedRow.Value, false);
} }
@ -1482,8 +1482,8 @@ namespace BizHawk.Client.EmuHawk
{ {
if (MultiSelect && _lastSelectedRow < RowCount - 1) if (MultiSelect && _lastSelectedRow < RowCount - 1)
{ {
if (_selectedItems.Any(i => i.RowIndex == _lastSelectedRow.Value) if (_selectedItems.IncludesRow(_lastSelectedRow.Value)
&& _selectedItems.Any(i => i.RowIndex == _lastSelectedRow + 1)) // Unhighlight if already highlighted && _selectedItems.IncludesRow(_lastSelectedRow.Value + 1)) // Unhighlight if already highlighted
{ {
var origIndex = _lastSelectedRow.Value; var origIndex = _lastSelectedRow.Value;
SelectRow(origIndex, false); SelectRow(origIndex, false);
@ -1787,8 +1787,9 @@ namespace BizHawk.Client.EmuHawk
if (FullRowSelect) if (FullRowSelect)
{ {
if (toggle && (_selectedItems.RemoveAll(x => x.RowIndex == row) is not 0)) if (toggle && _selectedItems.IncludesRow(row))
{ {
_selectedItems.RemoveAll(x => x.RowIndex == row);
_lastSelectedRow = _selectedItems.LastOrDefault()?.RowIndex; _lastSelectedRow = _selectedItems.LastOrDefault()?.RowIndex;
} }
else else
@ -1803,7 +1804,7 @@ namespace BizHawk.Client.EmuHawk
else else
{ {
_lastSelectedRow = null; // TODO: tracking this by cell is a lot more work _lastSelectedRow = null; // TODO: tracking this by cell is a lot more work
if (toggle && _selectedItems.Any(x => x.RowIndex == row)) if (toggle && _selectedItems.IncludesRow(row))
{ {
var item = _selectedItems var item = _selectedItems
.FirstOrDefault(x => x.Equals(cell)); .FirstOrDefault(x => x.Equals(cell));

View File

@ -71,6 +71,9 @@ namespace BizHawk.Common
public virtual void CopyTo(T[] array, int arrayIndex) => _list.CopyTo(array, arrayIndex); public virtual void CopyTo(T[] array, int arrayIndex) => _list.CopyTo(array, arrayIndex);
public T FirstOrDefault()
=> _list.Count is 0 ? default! : _list[0];
public virtual IEnumerator<T> GetEnumerator() => _list.GetEnumerator(); public virtual IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
/// <remarks>throws if list is empty</remarks> /// <remarks>throws if list is empty</remarks>
@ -87,6 +90,9 @@ namespace BizHawk.Common
return i < 0 ? -1 : i; return i < 0 ? -1 : i;
} }
public T LastOrDefault()
=> _list.Count is 0 ? default! : _list[_list.Count - 1];
public virtual bool Remove(T item) public virtual bool Remove(T item)
{ {
#if true #if true