Replace some logic with FilterGroup

This commit is contained in:
Matt Nadareski
2025-05-13 21:38:08 -04:00
parent d4b947a2ce
commit 83958c76a3
2 changed files with 38 additions and 31 deletions

View File

@@ -8,20 +8,20 @@ namespace SabreTools.Core.Filter
/// </summary>
public class FilterGroup
{
/// <summary>
/// How to apply the group filters
/// </summary>
public readonly GroupType GroupType;
/// <summary>
/// All standalone filters in the group
/// </summary>
public readonly List<FilterObject> Subfilters = [];
private readonly List<FilterObject> _subfilters = [];
/// <summary>
/// All filter groups contained in the group
/// </summary>
public readonly List<FilterGroup> Subgroups = [];
/// <summary>
/// How to apply the group filters
/// </summary>
public readonly GroupType GroupType;
private readonly List<FilterGroup> _subgroups = [];
public FilterGroup(GroupType groupType)
{
@@ -30,23 +30,37 @@ namespace SabreTools.Core.Filter
public FilterGroup(FilterObject[] filters, GroupType groupType)
{
Subfilters.AddRange(filters);
_subfilters.AddRange(filters);
GroupType = groupType;
}
public FilterGroup(FilterGroup[] groups, GroupType groupType)
{
Subgroups.AddRange(groups);
_subgroups.AddRange(groups);
GroupType = groupType;
}
public FilterGroup(FilterObject[] filters, FilterGroup[] groups, GroupType groupType)
{
Subfilters.AddRange(filters);
Subgroups.AddRange(groups);
_subfilters.AddRange(filters);
_subgroups.AddRange(groups);
GroupType = groupType;
}
#region Accessors
/// <summary>
/// Add a FilterObject to the set
/// </summary>
public void AddFilter(FilterObject filter) => _subfilters.Add(filter);
/// <summary>
/// Add a FilterGroup to the set
/// </summary>
public void AddGroup(FilterGroup group) => _subgroups.Add(group);
#endregion
#region Matching
/// <summary>
@@ -68,7 +82,7 @@ namespace SabreTools.Core.Filter
private bool MatchesAnd(DictionaryBase dictionaryBase)
{
// Run standalone filters
foreach (var filter in Subfilters)
foreach (var filter in _subfilters)
{
// One failed match fails the group
if (!filter.Matches(dictionaryBase))
@@ -76,7 +90,7 @@ namespace SabreTools.Core.Filter
}
// Run filter subgroups
foreach (var group in Subgroups)
foreach (var group in _subgroups)
{
// One failed match fails the group
if (!group.Matches(dictionaryBase))
@@ -92,7 +106,7 @@ namespace SabreTools.Core.Filter
private bool MatchesOr(DictionaryBase dictionaryBase)
{
// Run standalone filters
foreach (var filter in Subfilters)
foreach (var filter in _subfilters)
{
// One successful match passes the group
if (filter.Matches(dictionaryBase))
@@ -100,7 +114,7 @@ namespace SabreTools.Core.Filter
}
// Run filter subgroups
foreach (var group in Subgroups)
foreach (var group in _subgroups)
{
// One successful match passes the group
if (group.Matches(dictionaryBase))

View File

@@ -13,7 +13,7 @@ namespace SabreTools.Core.Filter
/// <summary>
/// Set of filters to be run against an object
/// </summary>
public readonly Dictionary<string, List<FilterObject>> Filters = [];
public readonly Dictionary<string, FilterGroup> Filters = [];
/// <summary>
/// Cached item type names for filter selection
@@ -23,7 +23,7 @@ namespace SabreTools.Core.Filter
/// <summary>
/// Set of machine type keys that are logically grouped
/// </summary>
/// TODO: REMOVE THISWHEN A PROPER IMPLEMENTATION IS FOUND
/// TODO: REMOVE THIS WHEN A PROPER IMPLEMENTATION IS FOUND
private readonly string[] _machineTypeKeys =
[
$"{MetadataFile.MachineKey}.{Machine.IsBiosKey}",
@@ -38,10 +38,10 @@ namespace SabreTools.Core.Filter
// Ensure the key exists
string key = filter.Key.ToString();
if (!Filters.ContainsKey(filter.Key.ToString()))
Filters[key] = [];
Filters[key] = new FilterGroup(GroupType.OR);
// Add the filter to the set
Filters[key].Add(filter);
Filters[key].AddFilter(filter);
}
}
@@ -56,10 +56,10 @@ namespace SabreTools.Core.Filter
// Ensure the key exists
string key = filter.Key.ToString();
if (!Filters.ContainsKey(filter.Key.ToString()))
Filters[key] = [];
Filters[key] = new FilterGroup(GroupType.OR);
// Add the filter to the set
Filters[key].Add(filter);
Filters[key].AddFilter(filter);
}
catch { }
}
@@ -104,10 +104,8 @@ namespace SabreTools.Core.Filter
if (!Filters.ContainsKey(filterKey))
continue;
foreach (var filter in Filters[filterKey])
{
matchAny |= filter.Matches(dictionaryBase);
}
// Check for a match like normal
matchAny |= Filters[filterKey].Matches(dictionaryBase);
}
// If we don't get a match, it's a failure
@@ -129,13 +127,8 @@ namespace SabreTools.Core.Filter
if (Array.Exists(_machineTypeKeys, key => key == filterKey))
continue;
bool matchOne = false;
foreach (var filter in Filters[filterKey])
{
matchOne |= filter.Matches(dictionaryBase);
}
// If we don't get a match, it's a failure
bool matchOne = Filters[filterKey].Matches(dictionaryBase);
if (!matchOne)
return false;
}