mirror of
https://github.com/ppy/osu.git
synced 2025-02-21 09:32:55 +08:00
Avoid list construction when doing filtering
This commit is contained in:
parent
27e778ae09
commit
42010574b5
@ -1,8 +1,8 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using osu.Framework.Localisation;
|
using osu.Framework.Localisation;
|
||||||
|
using osu.Game.Screens.Select;
|
||||||
|
|
||||||
namespace osu.Game.Beatmaps
|
namespace osu.Game.Beatmaps
|
||||||
{
|
{
|
||||||
@ -29,20 +29,18 @@ namespace osu.Game.Beatmaps
|
|||||||
return new RomanisableString($"{metadata.GetPreferred(true)}".Trim(), $"{metadata.GetPreferred(false)}".Trim());
|
return new RomanisableString($"{metadata.GetPreferred(true)}".Trim(), $"{metadata.GetPreferred(false)}".Trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<string> GetSearchableTerms(this IBeatmapInfo beatmapInfo)
|
public static bool Match(this IBeatmapInfo beatmapInfo, params FilterCriteria.OptionalTextFilter[] filters)
|
||||||
{
|
{
|
||||||
var termsList = new List<string>(BeatmapMetadataInfoExtensions.MAX_SEARCHABLE_TERM_COUNT + 1);
|
foreach (var filter in filters)
|
||||||
|
|
||||||
addIfNotNull(beatmapInfo.DifficultyName);
|
|
||||||
|
|
||||||
BeatmapMetadataInfoExtensions.CollectSearchableTerms(beatmapInfo.Metadata, termsList);
|
|
||||||
return termsList;
|
|
||||||
|
|
||||||
void addIfNotNull(string? s)
|
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(s))
|
if (filter.Matches(beatmapInfo.DifficultyName))
|
||||||
termsList.Add(s);
|
return true;
|
||||||
|
|
||||||
|
if (BeatmapMetadataInfoExtensions.Match(beatmapInfo.Metadata, filters))
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string getVersionString(IBeatmapInfo beatmapInfo) => string.IsNullOrEmpty(beatmapInfo.DifficultyName) ? string.Empty : $"[{beatmapInfo.DifficultyName}]";
|
private static string getVersionString(IBeatmapInfo beatmapInfo) => string.IsNullOrEmpty(beatmapInfo.DifficultyName) ? string.Empty : $"[{beatmapInfo.DifficultyName}]";
|
||||||
|
@ -3,11 +3,14 @@
|
|||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using osu.Framework.Localisation;
|
using osu.Framework.Localisation;
|
||||||
|
using osu.Game.Screens.Select;
|
||||||
|
|
||||||
namespace osu.Game.Beatmaps
|
namespace osu.Game.Beatmaps
|
||||||
{
|
{
|
||||||
public static class BeatmapMetadataInfoExtensions
|
public static class BeatmapMetadataInfoExtensions
|
||||||
{
|
{
|
||||||
|
internal const int MAX_SEARCHABLE_TERM_COUNT = 7;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An array of all searchable terms provided in contained metadata.
|
/// An array of all searchable terms provided in contained metadata.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -18,7 +21,21 @@ namespace osu.Game.Beatmaps
|
|||||||
return termsList.ToArray();
|
return termsList.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal const int MAX_SEARCHABLE_TERM_COUNT = 7;
|
public static bool Match(IBeatmapMetadataInfo metadataInfo, FilterCriteria.OptionalTextFilter[] filters)
|
||||||
|
{
|
||||||
|
foreach (var filter in filters)
|
||||||
|
{
|
||||||
|
if (filter.Matches(metadataInfo.Author.Username)) return true;
|
||||||
|
if (filter.Matches(metadataInfo.Artist)) return true;
|
||||||
|
if (filter.Matches(metadataInfo.ArtistUnicode)) return true;
|
||||||
|
if (filter.Matches(metadataInfo.Title)) return true;
|
||||||
|
if (filter.Matches(metadataInfo.TitleUnicode)) return true;
|
||||||
|
if (filter.Matches(metadataInfo.Source)) return true;
|
||||||
|
if (filter.Matches(metadataInfo.Tags)) return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
internal static void CollectSearchableTerms(IBeatmapMetadataInfo metadataInfo, IList<string> termsList)
|
internal static void CollectSearchableTerms(IBeatmapMetadataInfo metadataInfo, IList<string> termsList)
|
||||||
{
|
{
|
||||||
|
@ -66,26 +66,7 @@ namespace osu.Game.Screens.Select.Carousel
|
|||||||
|
|
||||||
if (criteria.SearchTerms.Length > 0)
|
if (criteria.SearchTerms.Length > 0)
|
||||||
{
|
{
|
||||||
var searchableTerms = BeatmapInfo.GetSearchableTerms();
|
match = BeatmapInfo.Match(criteria.SearchTerms);
|
||||||
|
|
||||||
foreach (FilterCriteria.OptionalTextFilter criteriaTerm in criteria.SearchTerms)
|
|
||||||
{
|
|
||||||
bool any = false;
|
|
||||||
|
|
||||||
// ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
|
|
||||||
foreach (string searchTerm in searchableTerms)
|
|
||||||
{
|
|
||||||
if (!criteriaTerm.Matches(searchTerm)) continue;
|
|
||||||
|
|
||||||
any = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (any) continue;
|
|
||||||
|
|
||||||
match = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if a match wasn't found via text matching of terms, do a second catch-all check matching against online IDs.
|
// if a match wasn't found via text matching of terms, do a second catch-all check matching against online IDs.
|
||||||
// this should be done after text matching so we can prioritise matching numbers in metadata.
|
// this should be done after text matching so we can prioritise matching numbers in metadata.
|
||||||
|
Loading…
Reference in New Issue
Block a user