Make ItemDictionary responsible for running filters on itself

This commit is contained in:
Matt Nadareski
2024-03-19 14:25:54 -04:00
parent eec50e3d48
commit 5cda1c8b8f
2 changed files with 47 additions and 35 deletions

View File

@@ -158,41 +158,7 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
/// <param name="filterRunner">Preconfigured filter runner to use</param> /// <param name="filterRunner">Preconfigured filter runner to use</param>
public void ExecuteFilters(FilterRunner filterRunner) public void ExecuteFilters(FilterRunner filterRunner)
{ => Items.ExecuteFilters(filterRunner);
List<string> keys = [.. Items.Keys];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(keys, key =>
#else
foreach (var key in keys)
#endif
{
ConcurrentList<DatItem>? items = Items[key];
if (items == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
// Filter all items in the current key
var newItems = new ConcurrentList<DatItem>();
foreach (var item in items)
{
if (item.PassesFilter(filterRunner))
newItems.Add(item);
}
// Set the value in the key to the new set
Items[key] = newItems;
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
#endregion #endregion

View File

@@ -10,6 +10,7 @@ using System.Threading.Tasks;
using System.Xml.Serialization; using System.Xml.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;
using SabreTools.Core; using SabreTools.Core;
using SabreTools.Core.Filter;
using SabreTools.DatItems; using SabreTools.DatItems;
using SabreTools.DatItems.Formats; using SabreTools.DatItems.Formats;
using SabreTools.Hashing; using SabreTools.Hashing;
@@ -710,6 +711,51 @@ namespace SabreTools.DatFiles
#endregion #endregion
#region Filtering
/// <summary>
/// Execute all filters in a filter runner on the items in the dictionary
/// </summary>
/// <param name="filterRunner">Preconfigured filter runner to use</param>
public void ExecuteFilters(FilterRunner filterRunner)
{
List<string> keys = [.. Keys];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(keys, key =>
#else
foreach (var key in keys)
#endif
{
ConcurrentList<DatItem>? items = this[key];
if (items == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
// Filter all items in the current key
var newItems = new ConcurrentList<DatItem>();
foreach (var item in items)
{
if (item.PassesFilter(filterRunner))
newItems.Add(item);
}
// Set the value in the key to the new set
this[key] = newItems;
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
#endregion
#region Statistics #region Statistics
/// <summary> /// <summary>