diff --git a/SabreTools.DatFiles/ItemDictionary.cs b/SabreTools.DatFiles/ItemDictionary.cs index 18ac9859..11a0acfb 100644 --- a/SabreTools.DatFiles/ItemDictionary.cs +++ b/SabreTools.DatFiles/ItemDictionary.cs @@ -99,6 +99,25 @@ namespace SabreTools.DatFiles #endregion + #region Constructors + + /// + /// Generic constructor + /// + public ItemDictionary() + { + bucketedBy = ItemKey.NULL; + mergedBy = DedupeType.None; +#if NET40_OR_GREATER || NETCOREAPP + items = new ConcurrentDictionary?>(); +#else + items = new Dictionary?>(); +#endif + logger = new Logger(this); + } + + #endregion + #region Accessors /// @@ -192,6 +211,52 @@ namespace SabreTools.DatFiles } } + /// + /// Remove any keys that have null or empty values + /// + 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 + } + } + + /// + /// Remove all items marked for removal + /// + public void ClearMarked() + { + var keys = items.Keys.ToList(); + foreach (string key in keys) + { + ConcurrentList? oldItemList = items[key]; + ConcurrentList? newItemList = oldItemList?.Where(i => i.GetBoolFieldValue(DatItem.RemoveKey) != true)?.ToConcurrentList(); + + Remove(key); + AddRange(key, newItemList); + } + } + /// /// Get if the file dictionary contains the key /// @@ -349,25 +414,6 @@ namespace SabreTools.DatFiles #endregion - #region Constructors - - /// - /// Generic constructor - /// - public ItemDictionary() - { - bucketedBy = ItemKey.NULL; - mergedBy = DedupeType.None; -#if NET40_OR_GREATER || NETCOREAPP - items = new ConcurrentDictionary?>(); -#else - items = new Dictionary?>(); -#endif - logger = new Logger(this); - } - - #endregion - #region Bucketing /// @@ -668,56 +714,6 @@ namespace SabreTools.DatFiles #endregion - #region Custom Functionality - - /// - /// Remove any keys that have null or empty values - /// - 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 - } - } - - /// - /// Remove all items marked for removal - /// - public void ClearMarked() - { - var keys = items.Keys.ToList(); - foreach (string key in keys) - { - ConcurrentList? oldItemList = items[key]; - ConcurrentList? newItemList = oldItemList?.Where(i => i.GetBoolFieldValue(DatItem.RemoveKey) != true)?.ToConcurrentList(); - - Remove(key); - AddRange(key, newItemList); - } - } - - #endregion - #region Statistics ///