From 57439f969770ffa1018473b7494ef117cc91a38c Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 15 Mar 2024 05:56:26 +1000 Subject: [PATCH] Reduce LINQ calls on `CellList` --- .../CustomControls/InputRoll/Cell.cs | 16 ++++++++++++++++ .../CustomControls/InputRoll/InputRoll.cs | 19 ++++++++++--------- src/BizHawk.Common/CustomCollections.cs | 6 ++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs index 9d11d4fc9c..57fdf31d5f 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs @@ -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 diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs index 2bbcc1456f..eff0cdf069 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs @@ -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 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 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)); diff --git a/src/BizHawk.Common/CustomCollections.cs b/src/BizHawk.Common/CustomCollections.cs index b3bad239c6..87b1e11395 100644 --- a/src/BizHawk.Common/CustomCollections.cs +++ b/src/BizHawk.Common/CustomCollections.cs @@ -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 GetEnumerator() => _list.GetEnumerator(); /// throws if list is empty @@ -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