From 83958c76a32380f645ee0d36576f2ca33843fb4c Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 13 May 2025 21:38:08 -0400 Subject: [PATCH] Replace some logic with FilterGroup --- SabreTools.Core/Filter/FilterGroup.cs | 44 +++++++++++++++++--------- SabreTools.Core/Filter/FilterRunner.cs | 25 ++++++--------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/SabreTools.Core/Filter/FilterGroup.cs b/SabreTools.Core/Filter/FilterGroup.cs index fa36765d..e5fa844d 100644 --- a/SabreTools.Core/Filter/FilterGroup.cs +++ b/SabreTools.Core/Filter/FilterGroup.cs @@ -8,20 +8,20 @@ namespace SabreTools.Core.Filter /// public class FilterGroup { + /// + /// How to apply the group filters + /// + public readonly GroupType GroupType; + /// /// All standalone filters in the group /// - public readonly List Subfilters = []; + private readonly List _subfilters = []; /// /// All filter groups contained in the group /// - public readonly List Subgroups = []; - - /// - /// How to apply the group filters - /// - public readonly GroupType GroupType; + private readonly List _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 + + /// + /// Add a FilterObject to the set + /// + public void AddFilter(FilterObject filter) => _subfilters.Add(filter); + + /// + /// Add a FilterGroup to the set + /// + public void AddGroup(FilterGroup group) => _subgroups.Add(group); + + #endregion + #region Matching /// @@ -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)) diff --git a/SabreTools.Core/Filter/FilterRunner.cs b/SabreTools.Core/Filter/FilterRunner.cs index 7410877e..512fd4d3 100644 --- a/SabreTools.Core/Filter/FilterRunner.cs +++ b/SabreTools.Core/Filter/FilterRunner.cs @@ -13,7 +13,7 @@ namespace SabreTools.Core.Filter /// /// Set of filters to be run against an object /// - public readonly Dictionary> Filters = []; + public readonly Dictionary Filters = []; /// /// Cached item type names for filter selection @@ -23,7 +23,7 @@ namespace SabreTools.Core.Filter /// /// Set of machine type keys that are logically grouped /// - /// 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; }