Avoid list construction when doing filtering

This commit is contained in:
Dean Herbert 2023-12-05 14:53:47 +09:00
parent 27e778ae09
commit 42010574b5
No known key found for this signature in database
3 changed files with 29 additions and 33 deletions

View File

@ -1,8 +1,8 @@
// 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.
using System.Collections.Generic;
using osu.Framework.Localisation;
using osu.Game.Screens.Select;
namespace osu.Game.Beatmaps
{
@ -29,20 +29,18 @@ namespace osu.Game.Beatmaps
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);
addIfNotNull(beatmapInfo.DifficultyName);
BeatmapMetadataInfoExtensions.CollectSearchableTerms(beatmapInfo.Metadata, termsList);
return termsList;
void addIfNotNull(string? s)
foreach (var filter in filters)
{
if (!string.IsNullOrEmpty(s))
termsList.Add(s);
if (filter.Matches(beatmapInfo.DifficultyName))
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}]";

View File

@ -3,11 +3,14 @@
using System.Collections.Generic;
using osu.Framework.Localisation;
using osu.Game.Screens.Select;
namespace osu.Game.Beatmaps
{
public static class BeatmapMetadataInfoExtensions
{
internal const int MAX_SEARCHABLE_TERM_COUNT = 7;
/// <summary>
/// An array of all searchable terms provided in contained metadata.
/// </summary>
@ -18,7 +21,21 @@ namespace osu.Game.Beatmaps
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)
{

View File

@ -66,26 +66,7 @@ namespace osu.Game.Screens.Select.Carousel
if (criteria.SearchTerms.Length > 0)
{
var searchableTerms = BeatmapInfo.GetSearchableTerms();
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;
}
match = BeatmapInfo.Match(criteria.SearchTerms);
// 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.