diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs index d401076bea..4ea717f8d0 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/Cell.cs @@ -1,5 +1,6 @@ #nullable enable +using System.Collections.Generic; using System.Diagnostics; using BizHawk.Common; @@ -91,6 +92,11 @@ namespace BizHawk.Client.EmuHawk public sealed class CellList : SortedList { + public CellList() {} + + public CellList(IEnumerable collection) + : base(collection) {} + /// restore the distinctness invariant from ; though I don't think we actually rely on it anywhere --yoshi public override void Add(Cell item) { @@ -117,6 +123,9 @@ namespace BizHawk.Client.EmuHawk return i >= 0 && _list[i].RowIndex == rowIndex; } #endif + + public new CellList Slice(int start, int length) + => new(SliceImpl(start: start, length: length)); } 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 f7a044686a..dcc2607ca2 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs @@ -22,7 +22,7 @@ namespace BizHawk.Client.EmuHawk { private readonly IControlRenderer _renderer; - private readonly CellList _selectedItems = new(); + private CellList _selectedItems = new(); // scrollbar location(s) are calculated later (e.g. on resize) private readonly VScrollBar _vBar = new VScrollBar { Visible = false }; @@ -269,10 +269,10 @@ namespace BizHawk.Client.EmuHawk _rowCount = value; - //TODO replace this with a binary search + truncate if (_selectedItems.LastOrDefault()?.RowIndex >= _rowCount) { - _selectedItems.RemoveAll(i => i.RowIndex >= _rowCount); + var iLastToKeep = _selectedItems.LowerBoundBinarySearch(static c => c.RowIndex ?? -1, _rowCount); + _selectedItems = _selectedItems.Slice(start: 0, length: iLastToKeep + 1); } RecalculateScrollBars(); diff --git a/src/BizHawk.Common/CustomCollections.cs b/src/BizHawk.Common/CustomCollections.cs index 8e596b7fc1..8f0eec5c63 100644 --- a/src/BizHawk.Common/CustomCollections.cs +++ b/src/BizHawk.Common/CustomCollections.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Linq; namespace BizHawk.Common { @@ -14,11 +15,15 @@ namespace BizHawk.Common public virtual bool IsReadOnly { get; } = false; - public SortedList() => _list = new List(); + protected SortedList(List wrapped) + => _list = wrapped; + + public SortedList() + : this(new()) {} public SortedList(IEnumerable collection) + : this(new(collection)) { - _list = new List(collection); _list.Sort(); } @@ -131,6 +136,12 @@ namespace BizHawk.Common } } + public SortedList Slice(int start, int length) + => new(SliceImpl(start: start, length: length)); + + protected List SliceImpl(int start, int length) + => _list.Skip(start).Take(length).ToList(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } }