Reorganize methods in item dictionary

This commit is contained in:
Matt Nadareski
2024-03-13 11:07:53 -04:00
parent 96f84e9c90
commit 70214bfbdf

View File

@@ -368,7 +368,7 @@ namespace SabreTools.DatFiles
#endregion #endregion
#region Custom Functionality #region Bucketing
/// <summary> /// <summary>
/// Take the arbitrarily bucketed Files Dictionary and convert to one bucketed by a user-defined method /// Take the arbitrarily bucketed Files Dictionary and convert to one bucketed by a user-defined method
@@ -408,52 +408,6 @@ namespace SabreTools.DatFiles
} }
} }
/// <summary>
/// Remove any keys that have null or empty values
/// </summary>
public void ClearEmpty()
{
var keys = items.Keys.Where(k => k != null).ToList();
foreach (string key in keys)
{
// If the key doesn't exist, skip
if (!items.ContainsKey(key))
continue;
// If the value is null, remove
else if (items[key] == null)
#if NET40_OR_GREATER || NETCOREAPP
items.TryRemove(key, out _);
#else
items.Remove(key);
#endif
// If there are no non-blank items, remove
else if (!items[key]!.Any(i => i != null && i is not Blank))
#if NET40_OR_GREATER || NETCOREAPP
items.TryRemove(key, out _);
#else
items.Remove(key);
#endif
}
}
/// <summary>
/// Remove all items marked for removal
/// </summary>
public void ClearMarked()
{
var keys = items.Keys.ToList();
foreach (string key in keys)
{
ConcurrentList<DatItem>? oldItemList = items[key];
ConcurrentList<DatItem>? newItemList = oldItemList?.Where(i => i.GetBoolFieldValue(DatItem.RemoveKey) != true)?.ToConcurrentList();
Remove(key);
AddRange(key, newItemList);
}
}
/// <summary> /// <summary>
/// List all duplicates found in a DAT based on a DatItem /// List all duplicates found in a DAT based on a DatItem
/// </summary> /// </summary>
@@ -530,32 +484,6 @@ namespace SabreTools.DatFiles
return roms?.Any(r => datItem.Equals(r)) == true; return roms?.Any(r => datItem.Equals(r)) == true;
} }
/// <summary>
/// Recalculate the statistics for the Dat
/// </summary>
public void RecalculateStats()
{
// Wipe out any stats already there
DatStatistics.ResetStatistics();
// If we have a blank Dat in any way, return
if (items == null)
return;
// Loop through and add
foreach (string key in items.Keys)
{
ConcurrentList<DatItem>? datItems = items[key];
if (datItems == null)
continue;
foreach (DatItem item in datItems)
{
DatStatistics.AddItemStatistics(item);
}
}
}
/// <summary> /// <summary>
/// Get the highest-order Field value that represents the statistics /// Get the highest-order Field value that represents the statistics
/// </summary> /// </summary>
@@ -740,6 +668,86 @@ namespace SabreTools.DatFiles
#endregion #endregion
#region Custom Functionality
/// <summary>
/// Remove any keys that have null or empty values
/// </summary>
public void ClearEmpty()
{
var keys = items.Keys.Where(k => k != null).ToList();
foreach (string key in keys)
{
// If the key doesn't exist, skip
if (!items.ContainsKey(key))
continue;
// If the value is null, remove
else if (items[key] == null)
#if NET40_OR_GREATER || NETCOREAPP
items.TryRemove(key, out _);
#else
items.Remove(key);
#endif
// If there are no non-blank items, remove
else if (!items[key]!.Any(i => i != null && i is not Blank))
#if NET40_OR_GREATER || NETCOREAPP
items.TryRemove(key, out _);
#else
items.Remove(key);
#endif
}
}
/// <summary>
/// Remove all items marked for removal
/// </summary>
public void ClearMarked()
{
var keys = items.Keys.ToList();
foreach (string key in keys)
{
ConcurrentList<DatItem>? oldItemList = items[key];
ConcurrentList<DatItem>? newItemList = oldItemList?.Where(i => i.GetBoolFieldValue(DatItem.RemoveKey) != true)?.ToConcurrentList();
Remove(key);
AddRange(key, newItemList);
}
}
#endregion
#region Statistics
/// <summary>
/// Recalculate the statistics for the Dat
/// </summary>
public void RecalculateStats()
{
// Wipe out any stats already there
DatStatistics.ResetStatistics();
// If we have a blank Dat in any way, return
if (items == null)
return;
// Loop through and add
foreach (string key in items.Keys)
{
ConcurrentList<DatItem>? datItems = items[key];
if (datItems == null)
continue;
foreach (DatItem item in datItems)
{
DatStatistics.AddItemStatistics(item);
}
}
}
#endregion
#region IDictionary Implementations #region IDictionary Implementations
public ICollection<ConcurrentList<DatItem>?> Values => ((IDictionary<string, ConcurrentList<DatItem>?>)items).Values; public ICollection<ConcurrentList<DatItem>?> Values => ((IDictionary<string, ConcurrentList<DatItem>?>)items).Values;