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 BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -105,6 +106,21 @@ namespace BizHawk.Client.EmuHawk
}
_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

View File

@ -969,7 +969,7 @@ namespace BizHawk.Client.EmuHawk
=> SelectedRowsWithDuplicates.First();
public bool IsRowSelected(int rowIndex)
=> _selectedItems.Any(cell => cell.RowIndex == rowIndex);
=> _selectedItems.IncludesRow(rowIndex);
public IEnumerable<ToolStripItem> GenerateContextMenuItems()
{
@ -1159,12 +1159,12 @@ namespace BizHawk.Client.EmuHawk
}
else if (ModifierKeys == Keys.Shift && CurrentCell.Column.Type == ColumnType.Text)
{
if (_selectedItems.Any())
if (_selectedItems.Count is not 0)
{
if (FullRowSelect)
{
var targetRow = CurrentCell.RowIndex.Value;
if (!_selectedItems.Any(c => c.RowIndex == targetRow))
if (!_selectedItems.IncludesRow(targetRow))
{
int additionStart, additionEndExcl;
SortedList<int> rowIndices = new(SelectedRows);
@ -1465,8 +1465,8 @@ namespace BizHawk.Client.EmuHawk
{
if (MultiSelect && _lastSelectedRow > 0)
{
if (_selectedItems.Any(i => i.RowIndex == _lastSelectedRow.Value)
&& _selectedItems.Any(i => i.RowIndex == _lastSelectedRow - 1)) // Unhighlight if already highlighted
if (_selectedItems.IncludesRow(_lastSelectedRow.Value)
&& _selectedItems.IncludesRow(_lastSelectedRow.Value - 1)) // Unhighlight if already highlighted
{
SelectRow(_lastSelectedRow.Value, false);
}
@ -1482,8 +1482,8 @@ namespace BizHawk.Client.EmuHawk
{
if (MultiSelect && _lastSelectedRow < RowCount - 1)
{
if (_selectedItems.Any(i => i.RowIndex == _lastSelectedRow.Value)
&& _selectedItems.Any(i => i.RowIndex == _lastSelectedRow + 1)) // Unhighlight if already highlighted
if (_selectedItems.IncludesRow(_lastSelectedRow.Value)
&& _selectedItems.IncludesRow(_lastSelectedRow.Value + 1)) // Unhighlight if already highlighted
{
var origIndex = _lastSelectedRow.Value;
SelectRow(origIndex, false);
@ -1787,8 +1787,9 @@ namespace BizHawk.Client.EmuHawk
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;
}
else
@ -1803,7 +1804,7 @@ namespace BizHawk.Client.EmuHawk
else
{
_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
.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 T FirstOrDefault()
=> _list.Count is 0 ? default! : _list[0];
public virtual IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
/// <remarks>throws if list is empty</remarks>
@ -87,6 +90,9 @@ namespace BizHawk.Common
return i < 0 ? -1 : i;
}
public T LastOrDefault()
=> _list.Count is 0 ? default! : _list[_list.Count - 1];
public virtual bool Remove(T item)
{
#if true