From 8c3c6ab3e8f2c0416daf7c5c4fd302e8e8da7700 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 14 Jan 2025 10:38:46 -0500 Subject: [PATCH] ItemDictionary is no longer IDictionary --- .../DatFileTests.FromMetadata.cs | 8 +- SabreTools.DatFiles.Test/DatFileTests.cs | 9 +- SabreTools.DatFiles/DatFile.ToMetadata.cs | 5 +- SabreTools.DatFiles/DatFile.cs | 4 +- SabreTools.DatFiles/DatFileTool.cs | 9 +- SabreTools.DatFiles/ItemDictionary.cs | 84 +------------------ .../DatFiles/ItemDictionaryTests.cs | 41 ++++----- SabreTools/Features/Sort.cs | 2 +- 8 files changed, 36 insertions(+), 126 deletions(-) diff --git a/SabreTools.DatFiles.Test/DatFileTests.FromMetadata.cs b/SabreTools.DatFiles.Test/DatFileTests.FromMetadata.cs index 4cf2465a..64b707b4 100644 --- a/SabreTools.DatFiles.Test/DatFileTests.FromMetadata.cs +++ b/SabreTools.DatFiles.Test/DatFileTests.FromMetadata.cs @@ -18,7 +18,7 @@ namespace SabreTools.DatFiles.Test DatFile datFile = new Formats.Logiqx(null, deprecated: false); datFile.ConvertFromMetadata(item, "filename", indexId: 0, keep: true, statsOnly: false); - Assert.Empty(datFile.Items); + Assert.Equal(0, datFile.DatStatistics.TotalCount); Assert.Empty(datFile.ItemsDB.GetItems()); } @@ -30,7 +30,7 @@ namespace SabreTools.DatFiles.Test DatFile datFile = new Formats.Logiqx(null, deprecated: false); datFile.ConvertFromMetadata(item, "filename", indexId: 0, keep: true, statsOnly: false); - Assert.Empty(datFile.Items); + Assert.Equal(0, datFile.DatStatistics.TotalCount); Assert.Empty(datFile.ItemsDB.GetItems()); } @@ -70,8 +70,8 @@ namespace SabreTools.DatFiles.Test ValidateMachine(actualMachine); // Aggregate for easier validation - DatItems.DatItem[] datItems = datFile.Items - .SelectMany(kvp => kvp.Value ?? []) + DatItems.DatItem[] datItems = datFile.Items.Keys + .SelectMany(key => datFile.GetItemsForBucket(key)) .ToArray(); Adjuster? adjuster = Array.Find(datItems, item => item is Adjuster) as Adjuster; diff --git a/SabreTools.DatFiles.Test/DatFileTests.cs b/SabreTools.DatFiles.Test/DatFileTests.cs index 3818488c..8b16fa71 100644 --- a/SabreTools.DatFiles.Test/DatFileTests.cs +++ b/SabreTools.DatFiles.Test/DatFileTests.cs @@ -19,7 +19,7 @@ namespace SabreTools.DatFiles.Test Assert.NotNull(created.Header); Assert.NotNull(created.Items); - Assert.Empty(created.Items); + Assert.Equal(0, created.DatStatistics.TotalCount); Assert.NotNull(created.ItemsDB); Assert.Empty(created.ItemsDB.GetItems()); } @@ -38,10 +38,7 @@ namespace SabreTools.DatFiles.Test Assert.Equal("name", created.Header.GetStringFieldValue(Models.Metadata.Header.NameKey)); Assert.NotNull(created.Items); - KeyValuePair?> itemsKvp = Assert.Single(created.Items); - Assert.Equal("key", itemsKvp.Key); - Assert.NotNull(itemsKvp.Value); - DatItem datItem = Assert.Single(itemsKvp.Value); + DatItem datItem = Assert.Single(created.GetItemsForBucket("key")); Assert.True(datItem is Rom); Assert.NotNull(created.ItemsDB); @@ -190,7 +187,7 @@ namespace SabreTools.DatFiles.Test Assert.NotNull(datFile.Header); Assert.NotNull(datFile.Items); - Assert.Empty(datFile.Items); + Assert.Equal(0, datFile.DatStatistics.TotalCount); Assert.NotNull(datFile.ItemsDB); Assert.Empty(datFile.ItemsDB.GetItems()); } diff --git a/SabreTools.DatFiles/DatFile.ToMetadata.cs b/SabreTools.DatFiles/DatFile.ToMetadata.cs index b2135818..3b5b003c 100644 --- a/SabreTools.DatFiles/DatFile.ToMetadata.cs +++ b/SabreTools.DatFiles/DatFile.ToMetadata.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Linq; using SabreTools.Core.Tools; @@ -16,7 +15,7 @@ namespace SabreTools.DatFiles internal Models.Metadata.MetadataFile? ConvertToMetadata(bool ignoreblanks = false) { // If we don't have items, we can't do anything - if (Items.Count == 0) + if (DatStatistics.TotalCount == 0) return null; // Create an object to hold the data @@ -41,7 +40,7 @@ namespace SabreTools.DatFiles internal Models.Metadata.MetadataFile? ConvertToMetadataDB(bool ignoreblanks = false) { // If we don't have items, we can't do anything - if (ItemsDB.GetItems().Count == 0) + if (ItemsDB.DatStatistics.TotalCount == 0) return null; // Create an object to hold the data diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index 4d446b4a..f91a30ba 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -29,7 +29,7 @@ namespace SabreTools.DatFiles /// DatItems and related statistics /// [JsonProperty("items"), XmlElement("items")] - public ItemDictionary Items { get; private set; } = []; + public ItemDictionary Items { get; private set; } = new ItemDictionary(); /// /// DatItems and related statistics @@ -252,7 +252,7 @@ namespace SabreTools.DatFiles /// public void ResetDictionary() { - Items.Clear(); + Items = new ItemDictionary(); ItemsDB = new ItemDictionaryDB(); } diff --git a/SabreTools.DatFiles/DatFileTool.cs b/SabreTools.DatFiles/DatFileTool.cs index 20b4dbda..b62304e7 100644 --- a/SabreTools.DatFiles/DatFileTool.cs +++ b/SabreTools.DatFiles/DatFileTool.cs @@ -536,7 +536,8 @@ namespace SabreTools.DatFiles if (datItem.Clone() is not DatItem newDatItem) continue; - if (!datFile.Items.TryGetValue(key, out var list) || list == null) + var list = datFile.GetItemsForBucket(key); + if (list.Count == 0) continue; if (datFile.Items.ContainsKey(key) && list.Count > 0) @@ -697,7 +698,8 @@ namespace SabreTools.DatFiles if (useGames) { // If the key is null, keep it - if (!intDat.Items.TryGetValue(key, out var intList) || intList == null) + var intList = intDat.GetItemsForBucket(key); + if (intList.Count == 0) #if NET40_OR_GREATER || NETCOREAPP return; #else @@ -705,7 +707,8 @@ namespace SabreTools.DatFiles #endif // If the base DAT doesn't contain the key, keep it - if (!datFile.Items.TryGetValue(key, out var list) || list == null) + var list = datFile.GetItemsForBucket(key); + if (list.Count == 0) #if NET40_OR_GREATER || NETCOREAPP return; #else diff --git a/SabreTools.DatFiles/ItemDictionary.cs b/SabreTools.DatFiles/ItemDictionary.cs index 732bcdee..4b5a86e7 100644 --- a/SabreTools.DatFiles/ItemDictionary.cs +++ b/SabreTools.DatFiles/ItemDictionary.cs @@ -1,5 +1,4 @@ -using System.Collections; -#if NET40_OR_GREATER || NETCOREAPP +#if NET40_OR_GREATER || NETCOREAPP using System.Collections.Concurrent; #endif using System.Collections.Generic; @@ -15,14 +14,13 @@ using SabreTools.Hashing; using SabreTools.IO.Logging; using SabreTools.Matching.Compare; -// TODO: Remove IDictionary implementation namespace SabreTools.DatFiles { /// /// Item dictionary with statistics, bucketing, and sorting /// [JsonObject("items"), XmlRoot("items")] - public class ItemDictionary : IDictionary?> + public class ItemDictionary { #region Private instance variables @@ -111,34 +109,6 @@ namespace SabreTools.DatFiles #region Accessors - /// - /// Passthrough to access the file dictionary - /// - /// Key in the dictionary to reference - public List? this[string key] - { - get - { - // Explicit lock for some weird corner cases - lock (key) - { - // Ensure the key exists - EnsureKey(key); - - // Now return the value - return _items[key]; - } - } - set - { - Remove(key); - if (value == null) - _items[key] = null; - else - Add(key, value); - } - } - /// /// Add a value to the file dictionary /// @@ -842,55 +812,5 @@ namespace SabreTools.DatFiles } #endregion - - #region IDictionary Implementations - - public ICollection?> Values => ((IDictionary?>)_items).Values; - - public int Count => ((ICollection?>>)_items).Count; - - public bool IsReadOnly => ((ICollection?>>)_items).IsReadOnly; - - public bool TryGetValue(string key, out List? value) - { - return ((IDictionary?>)_items).TryGetValue(key, out value); - } - - public void Add(KeyValuePair?> item) - { - ((ICollection?>>)_items).Add(item); - } - - public void Clear() - { - ((ICollection?>>)_items).Clear(); - } - - public bool Contains(KeyValuePair?> item) - { - return ((ICollection?>>)_items).Contains(item); - } - - public void CopyTo(KeyValuePair?>[] array, int arrayIndex) - { - ((ICollection?>>)_items).CopyTo(array, arrayIndex); - } - - public bool Remove(KeyValuePair?> item) - { - return ((ICollection?>>)_items).Remove(item); - } - - public IEnumerator?>> GetEnumerator() - { - return ((IEnumerable?>>)_items).GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return ((IEnumerable)_items).GetEnumerator(); - } - - #endregion } } diff --git a/SabreTools.Test/DatFiles/ItemDictionaryTests.cs b/SabreTools.Test/DatFiles/ItemDictionaryTests.cs index a61f4788..c54026b4 100644 --- a/SabreTools.Test/DatFiles/ItemDictionaryTests.cs +++ b/SabreTools.Test/DatFiles/ItemDictionaryTests.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using SabreTools.DatFiles; using SabreTools.DatItems; using SabreTools.DatItems.Formats; @@ -50,11 +51,9 @@ namespace SabreTools.Test.DatFiles rom1.CopyMachineInformation(machine2); // Setup the dictionary - var dict = new ItemDictionary - { - ["game-1"] = [rom1, rom2], - ["game-2"] = [rom3, rom4], - }; + var dict = new ItemDictionary(); + dict.Add("game-1", [rom1, rom2]); + dict.Add("game-2", [rom3, rom4]); dict.BucketBy(itemKey, DedupeType.None); Assert.Equal(expected, dict.Keys.Count); @@ -64,12 +63,10 @@ namespace SabreTools.Test.DatFiles public void ClearEmptyTest() { // Setup the dictionary - var dict = new ItemDictionary - { - ["game-1"] = [new Rom(),], - ["game-2"] = [], - ["game-3"] = null, - }; + var dict = new ItemDictionary(); + dict.Add("game-1", [new Rom()]); + dict.Add("game-2", []); + dict.Add("game-3", (List?)null); dict.ClearEmpty(); Assert.Single(dict.Keys); @@ -98,16 +95,14 @@ namespace SabreTools.Test.DatFiles rom1.CopyMachineInformation(machine1); // Setup the dictionary - var dict = new ItemDictionary - { - ["game-1"] = [rom1, rom2], - }; + var dict = new ItemDictionary(); + dict.Add("game-1", [rom1, rom2]); dict.ClearMarked(); string key = Assert.Single(dict.Keys); Assert.Equal("game-1", key); - Assert.NotNull(dict[key]); - Assert.Single(dict[key]!); + List items = dict.GetItemsForBucket(key); + Assert.Single(items); } [Theory] @@ -132,10 +127,8 @@ namespace SabreTools.Test.DatFiles rom1.CopyMachineInformation(machine1); // Setup the dictionary - var dict = new ItemDictionary - { - ["game-1"] = [rom1, rom2], - }; + var dict = new ItemDictionary(); + dict.Add("game-1", [rom1, rom2]); // Setup the test item var rom = new Rom(); @@ -170,10 +163,8 @@ namespace SabreTools.Test.DatFiles rom1.CopyMachineInformation(machine1); // Setup the dictionary - var dict = new ItemDictionary - { - ["game-1"] = [rom1, rom2], - }; + var dict = new ItemDictionary(); + dict.Add("game-1", [rom1, rom2]); // Setup the test item var rom = new Rom(); diff --git a/SabreTools/Features/Sort.cs b/SabreTools/Features/Sort.cs index 1a87fca5..4e3be78e 100644 --- a/SabreTools/Features/Sort.cs +++ b/SabreTools/Features/Sort.cs @@ -97,7 +97,7 @@ namespace SabreTools.Features Parser.ParseInto(datdata, datfile, int.MaxValue, keep: true); // Skip if nothing was parsed - if (datdata.Items.Count == 0) // datdata.ItemsDB.SortedKeys.Length == 0 + if (datdata.DatStatistics.TotalCount == 0) // datdata.ItemsDB.SortedKeys.Length == 0 continue; // Set depot information