1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 21:02:54 +08:00

Expose as ReadOnlyDictionary

This commit is contained in:
Dan Balasescu 2024-01-31 22:52:57 +09:00
parent 6b1de5446a
commit 0642d74014
No known key found for this signature in database
4 changed files with 14 additions and 11 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Graphics;
@ -35,7 +36,7 @@ namespace osu.Game.Rulesets.Objects.Pooling
/// <remarks>
/// The enumeration order is undefined.
/// </remarks>
public IEnumerable<(TEntry Entry, TDrawable Drawable)> AliveEntries => AliveDrawableMap.Select(x => (x.Key, x.Value));
public readonly ReadOnlyDictionary<TEntry, TDrawable> AliveEntries;
/// <summary>
/// Whether to remove an entry when clock goes backward and crossed its <see cref="LifetimeEntry.LifetimeStart"/>.
@ -53,7 +54,7 @@ namespace osu.Game.Rulesets.Objects.Pooling
/// </summary>
internal double FutureLifetimeExtension { get; set; }
public readonly Dictionary<TEntry, TDrawable> AliveDrawableMap = new Dictionary<TEntry, TDrawable>();
private readonly Dictionary<TEntry, TDrawable> aliveDrawableMap = new Dictionary<TEntry, TDrawable>();
private readonly HashSet<TEntry> allEntries = new HashSet<TEntry>();
private readonly LifetimeEntryManager lifetimeManager = new LifetimeEntryManager();
@ -63,6 +64,8 @@ namespace osu.Game.Rulesets.Objects.Pooling
lifetimeManager.EntryBecameAlive += entryBecameAlive;
lifetimeManager.EntryBecameDead += entryBecameDead;
lifetimeManager.EntryCrossedBoundary += entryCrossedBoundary;
AliveEntries = new ReadOnlyDictionary<TEntry, TDrawable>(aliveDrawableMap);
}
/// <summary>
@ -101,10 +104,10 @@ namespace osu.Game.Rulesets.Objects.Pooling
private void entryBecameAlive(LifetimeEntry lifetimeEntry)
{
var entry = (TEntry)lifetimeEntry;
Debug.Assert(!AliveDrawableMap.ContainsKey(entry));
Debug.Assert(!aliveDrawableMap.ContainsKey(entry));
TDrawable drawable = GetDrawable(entry);
AliveDrawableMap[entry] = drawable;
aliveDrawableMap[entry] = drawable;
AddDrawable(entry, drawable);
}
@ -119,10 +122,10 @@ namespace osu.Game.Rulesets.Objects.Pooling
private void entryBecameDead(LifetimeEntry lifetimeEntry)
{
var entry = (TEntry)lifetimeEntry;
Debug.Assert(AliveDrawableMap.ContainsKey(entry));
Debug.Assert(aliveDrawableMap.ContainsKey(entry));
TDrawable drawable = AliveDrawableMap[entry];
AliveDrawableMap.Remove(entry);
TDrawable drawable = aliveDrawableMap[entry];
aliveDrawableMap.Remove(entry);
RemoveDrawable(entry, drawable);
}
@ -148,7 +151,7 @@ namespace osu.Game.Rulesets.Objects.Pooling
foreach (var entry in Entries.ToArray())
Remove(entry);
Debug.Assert(AliveDrawableMap.Count == 0);
Debug.Assert(aliveDrawableMap.Count == 0);
}
protected override bool CheckChildrenLife()

View File

@ -105,7 +105,7 @@ namespace osu.Game.Rulesets.UI
// If required, we can make this lookup more efficient by adding support to get next-future-entry in LifetimeEntryManager.
var candidate =
// Use alive entries first as an optimisation.
hitObjectContainer.AliveEntries.Select(tuple => tuple.Entry).Where(e => !isAlreadyHit(e)).MinBy(e => e.HitObject.StartTime)
hitObjectContainer.AliveEntries.Keys.Where(e => !isAlreadyHit(e)).MinBy(e => e.HitObject.StartTime)
?? hitObjectContainer.Entries.Where(e => !isAlreadyHit(e)).MinBy(e => e.HitObject.StartTime);
// In the case there are no non-judged objects, the last hit object should be used instead.

View File

@ -21,7 +21,7 @@ namespace osu.Game.Rulesets.UI
{
public IEnumerable<DrawableHitObject> Objects => InternalChildren.Cast<DrawableHitObject>().OrderBy(h => h.HitObject.StartTime);
public IEnumerable<DrawableHitObject> AliveObjects => AliveEntries.Select(pair => pair.Drawable).OrderBy(h => h.HitObject.StartTime);
public IEnumerable<DrawableHitObject> AliveObjects => AliveEntries.Values.OrderBy(h => h.HitObject.StartTime);
/// <summary>
/// Invoked when a <see cref="DrawableHitObject"/> is judged.

View File

@ -186,7 +186,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
// to prevent hit objects displayed in a wrong position for one frame.
// Only AliveEntries need to be considered for layout (reduces overhead in the case of scroll speed changes).
// We are not using AliveObjects directly to avoid selection/sorting overhead since we don't care about the order at which positions will be updated.
foreach (var entry in AliveDrawableMap)
foreach (var entry in AliveEntries)
{
var obj = entry.Value;