Resolve a TODO in `InputRoll.RowCount`

This commit is contained in:
YoshiRulz 2025-02-15 05:54:49 +10:00
parent 5a236fdbee
commit d7b0af9f4e
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 25 additions and 5 deletions

View File

@ -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<Cell>
{
public CellList() {}
public CellList(IEnumerable<Cell> collection)
: base(collection) {}
/// <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)
{
@ -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

View File

@ -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();

View File

@ -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<T>();
protected SortedList(List<T> wrapped)
=> _list = wrapped;
public SortedList()
: this(new()) {}
public SortedList(IEnumerable<T> collection)
: this(new(collection))
{
_list = new List<T>(collection);
_list.Sort();
}
@ -131,6 +136,12 @@ namespace BizHawk.Common
}
}
public SortedList<T> Slice(int start, int length)
=> new(SliceImpl(start: start, length: length));
protected List<T> SliceImpl(int start, int length)
=> _list.Skip(start).Take(length).ToList();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}