mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 10:12:54 +08:00
Ensure equality member consistency for SampleInfo
The previous implementation of `SampleInfo`'s equality members was not completely correct in its treatment of the `sampleNames` array. While `Equals()` compared the values of `sampleNames` using `SequenceEqual()`, therefore performing a structural check that inspects the contents of both arrays, `GetHashCode()` used `HashCode.Combine()` directly on the arrays, therefore operating on reference equality. This could cause the pooling mechanism of samples to fail, as pointed out in #11079. To resolve, change the `GetHashCode()` implementation such that it also considers the contents of the array rather than just the reference to the array itself. This is achieved by leveraging `StructuralEqualityComparer`. Additionally, as a bonus, an array sort was added to the constructor of `SampleInfo`. This is intended to be a "canonicalisation" processing step for the array of sample names. Thanks to that sort, two instances of `SampleInfo` that have the same sample names but permutated will also turn out to be equal and have the same hash codes, given the implementation of both equality members. This gives `SampleInfo` set-like semantics.
This commit is contained in:
parent
71fa0da7f4
commit
15d9147edd
@ -2,6 +2,7 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
@ -17,6 +18,7 @@ namespace osu.Game.Audio
|
||||
public SampleInfo(params string[] sampleNames)
|
||||
{
|
||||
this.sampleNames = sampleNames;
|
||||
Array.Sort(sampleNames);
|
||||
}
|
||||
|
||||
public IEnumerable<string> LookupNames => sampleNames;
|
||||
@ -25,7 +27,9 @@ namespace osu.Game.Audio
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(sampleNames, Volume);
|
||||
return HashCode.Combine(
|
||||
StructuralComparisons.StructuralEqualityComparer.GetHashCode(sampleNames),
|
||||
Volume);
|
||||
}
|
||||
|
||||
public bool Equals(SampleInfo other)
|
||||
|
Loading…
Reference in New Issue
Block a user