ItemDictionary is no longer IDictionary

This commit is contained in:
Matt Nadareski
2025-01-14 10:38:46 -05:00
parent b29f7c65a4
commit 8c3c6ab3e8
8 changed files with 36 additions and 126 deletions

View File

@@ -18,7 +18,7 @@ namespace SabreTools.DatFiles.Test
DatFile datFile = new Formats.Logiqx(null, deprecated: false); DatFile datFile = new Formats.Logiqx(null, deprecated: false);
datFile.ConvertFromMetadata(item, "filename", indexId: 0, keep: true, statsOnly: 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()); Assert.Empty(datFile.ItemsDB.GetItems());
} }
@@ -30,7 +30,7 @@ namespace SabreTools.DatFiles.Test
DatFile datFile = new Formats.Logiqx(null, deprecated: false); DatFile datFile = new Formats.Logiqx(null, deprecated: false);
datFile.ConvertFromMetadata(item, "filename", indexId: 0, keep: true, statsOnly: 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()); Assert.Empty(datFile.ItemsDB.GetItems());
} }
@@ -70,8 +70,8 @@ namespace SabreTools.DatFiles.Test
ValidateMachine(actualMachine); ValidateMachine(actualMachine);
// Aggregate for easier validation // Aggregate for easier validation
DatItems.DatItem[] datItems = datFile.Items DatItems.DatItem[] datItems = datFile.Items.Keys
.SelectMany(kvp => kvp.Value ?? []) .SelectMany(key => datFile.GetItemsForBucket(key))
.ToArray(); .ToArray();
Adjuster? adjuster = Array.Find(datItems, item => item is Adjuster) as Adjuster; Adjuster? adjuster = Array.Find(datItems, item => item is Adjuster) as Adjuster;

View File

@@ -19,7 +19,7 @@ namespace SabreTools.DatFiles.Test
Assert.NotNull(created.Header); Assert.NotNull(created.Header);
Assert.NotNull(created.Items); Assert.NotNull(created.Items);
Assert.Empty(created.Items); Assert.Equal(0, created.DatStatistics.TotalCount);
Assert.NotNull(created.ItemsDB); Assert.NotNull(created.ItemsDB);
Assert.Empty(created.ItemsDB.GetItems()); Assert.Empty(created.ItemsDB.GetItems());
} }
@@ -38,10 +38,7 @@ namespace SabreTools.DatFiles.Test
Assert.Equal("name", created.Header.GetStringFieldValue(Models.Metadata.Header.NameKey)); Assert.Equal("name", created.Header.GetStringFieldValue(Models.Metadata.Header.NameKey));
Assert.NotNull(created.Items); Assert.NotNull(created.Items);
KeyValuePair<string, List<DatItem>?> itemsKvp = Assert.Single(created.Items); DatItem datItem = Assert.Single(created.GetItemsForBucket("key"));
Assert.Equal("key", itemsKvp.Key);
Assert.NotNull(itemsKvp.Value);
DatItem datItem = Assert.Single(itemsKvp.Value);
Assert.True(datItem is Rom); Assert.True(datItem is Rom);
Assert.NotNull(created.ItemsDB); Assert.NotNull(created.ItemsDB);
@@ -190,7 +187,7 @@ namespace SabreTools.DatFiles.Test
Assert.NotNull(datFile.Header); Assert.NotNull(datFile.Header);
Assert.NotNull(datFile.Items); Assert.NotNull(datFile.Items);
Assert.Empty(datFile.Items); Assert.Equal(0, datFile.DatStatistics.TotalCount);
Assert.NotNull(datFile.ItemsDB); Assert.NotNull(datFile.ItemsDB);
Assert.Empty(datFile.ItemsDB.GetItems()); Assert.Empty(datFile.ItemsDB.GetItems());
} }

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using SabreTools.Core.Tools; using SabreTools.Core.Tools;
@@ -16,7 +15,7 @@ namespace SabreTools.DatFiles
internal Models.Metadata.MetadataFile? ConvertToMetadata(bool ignoreblanks = false) internal Models.Metadata.MetadataFile? ConvertToMetadata(bool ignoreblanks = false)
{ {
// If we don't have items, we can't do anything // If we don't have items, we can't do anything
if (Items.Count == 0) if (DatStatistics.TotalCount == 0)
return null; return null;
// Create an object to hold the data // Create an object to hold the data
@@ -41,7 +40,7 @@ namespace SabreTools.DatFiles
internal Models.Metadata.MetadataFile? ConvertToMetadataDB(bool ignoreblanks = false) internal Models.Metadata.MetadataFile? ConvertToMetadataDB(bool ignoreblanks = false)
{ {
// If we don't have items, we can't do anything // If we don't have items, we can't do anything
if (ItemsDB.GetItems().Count == 0) if (ItemsDB.DatStatistics.TotalCount == 0)
return null; return null;
// Create an object to hold the data // Create an object to hold the data

View File

@@ -29,7 +29,7 @@ namespace SabreTools.DatFiles
/// DatItems and related statistics /// DatItems and related statistics
/// </summary> /// </summary>
[JsonProperty("items"), XmlElement("items")] [JsonProperty("items"), XmlElement("items")]
public ItemDictionary Items { get; private set; } = []; public ItemDictionary Items { get; private set; } = new ItemDictionary();
/// <summary> /// <summary>
/// DatItems and related statistics /// DatItems and related statistics
@@ -252,7 +252,7 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
public void ResetDictionary() public void ResetDictionary()
{ {
Items.Clear(); Items = new ItemDictionary();
ItemsDB = new ItemDictionaryDB(); ItemsDB = new ItemDictionaryDB();
} }

View File

@@ -536,7 +536,8 @@ namespace SabreTools.DatFiles
if (datItem.Clone() is not DatItem newDatItem) if (datItem.Clone() is not DatItem newDatItem)
continue; continue;
if (!datFile.Items.TryGetValue(key, out var list) || list == null) var list = datFile.GetItemsForBucket(key);
if (list.Count == 0)
continue; continue;
if (datFile.Items.ContainsKey(key) && list.Count > 0) if (datFile.Items.ContainsKey(key) && list.Count > 0)
@@ -697,7 +698,8 @@ namespace SabreTools.DatFiles
if (useGames) if (useGames)
{ {
// If the key is null, keep it // 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 #if NET40_OR_GREATER || NETCOREAPP
return; return;
#else #else
@@ -705,7 +707,8 @@ namespace SabreTools.DatFiles
#endif #endif
// If the base DAT doesn't contain the key, keep it // 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 #if NET40_OR_GREATER || NETCOREAPP
return; return;
#else #else

View File

@@ -1,5 +1,4 @@
using System.Collections; #if NET40_OR_GREATER || NETCOREAPP
#if NET40_OR_GREATER || NETCOREAPP
using System.Collections.Concurrent; using System.Collections.Concurrent;
#endif #endif
using System.Collections.Generic; using System.Collections.Generic;
@@ -15,14 +14,13 @@ using SabreTools.Hashing;
using SabreTools.IO.Logging; using SabreTools.IO.Logging;
using SabreTools.Matching.Compare; using SabreTools.Matching.Compare;
// TODO: Remove IDictionary implementation
namespace SabreTools.DatFiles namespace SabreTools.DatFiles
{ {
/// <summary> /// <summary>
/// Item dictionary with statistics, bucketing, and sorting /// Item dictionary with statistics, bucketing, and sorting
/// </summary> /// </summary>
[JsonObject("items"), XmlRoot("items")] [JsonObject("items"), XmlRoot("items")]
public class ItemDictionary : IDictionary<string, List<DatItem>?> public class ItemDictionary
{ {
#region Private instance variables #region Private instance variables
@@ -111,34 +109,6 @@ namespace SabreTools.DatFiles
#region Accessors #region Accessors
/// <summary>
/// Passthrough to access the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to reference</param>
public List<DatItem>? 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);
}
}
/// <summary> /// <summary>
/// Add a value to the file dictionary /// Add a value to the file dictionary
/// </summary> /// </summary>
@@ -842,55 +812,5 @@ namespace SabreTools.DatFiles
} }
#endregion #endregion
#region IDictionary Implementations
public ICollection<List<DatItem>?> Values => ((IDictionary<string, List<DatItem>?>)_items).Values;
public int Count => ((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Count;
public bool IsReadOnly => ((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).IsReadOnly;
public bool TryGetValue(string key, out List<DatItem>? value)
{
return ((IDictionary<string, List<DatItem>?>)_items).TryGetValue(key, out value);
}
public void Add(KeyValuePair<string, List<DatItem>?> item)
{
((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Add(item);
}
public void Clear()
{
((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Clear();
}
public bool Contains(KeyValuePair<string, List<DatItem>?> item)
{
return ((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Contains(item);
}
public void CopyTo(KeyValuePair<string, List<DatItem>?>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<string, List<DatItem>?> item)
{
return ((ICollection<KeyValuePair<string, List<DatItem>?>>)_items).Remove(item);
}
public IEnumerator<KeyValuePair<string, List<DatItem>?>> GetEnumerator()
{
return ((IEnumerable<KeyValuePair<string, List<DatItem>?>>)_items).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_items).GetEnumerator();
}
#endregion
} }
} }

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using SabreTools.DatFiles; using SabreTools.DatFiles;
using SabreTools.DatItems; using SabreTools.DatItems;
using SabreTools.DatItems.Formats; using SabreTools.DatItems.Formats;
@@ -50,11 +51,9 @@ namespace SabreTools.Test.DatFiles
rom1.CopyMachineInformation(machine2); rom1.CopyMachineInformation(machine2);
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary var dict = new ItemDictionary();
{ dict.Add("game-1", [rom1, rom2]);
["game-1"] = [rom1, rom2], dict.Add("game-2", [rom3, rom4]);
["game-2"] = [rom3, rom4],
};
dict.BucketBy(itemKey, DedupeType.None); dict.BucketBy(itemKey, DedupeType.None);
Assert.Equal(expected, dict.Keys.Count); Assert.Equal(expected, dict.Keys.Count);
@@ -64,12 +63,10 @@ namespace SabreTools.Test.DatFiles
public void ClearEmptyTest() public void ClearEmptyTest()
{ {
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary var dict = new ItemDictionary();
{ dict.Add("game-1", [new Rom()]);
["game-1"] = [new Rom(),], dict.Add("game-2", []);
["game-2"] = [], dict.Add("game-3", (List<DatItem>?)null);
["game-3"] = null,
};
dict.ClearEmpty(); dict.ClearEmpty();
Assert.Single(dict.Keys); Assert.Single(dict.Keys);
@@ -98,16 +95,14 @@ namespace SabreTools.Test.DatFiles
rom1.CopyMachineInformation(machine1); rom1.CopyMachineInformation(machine1);
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary var dict = new ItemDictionary();
{ dict.Add("game-1", [rom1, rom2]);
["game-1"] = [rom1, rom2],
};
dict.ClearMarked(); dict.ClearMarked();
string key = Assert.Single(dict.Keys); string key = Assert.Single(dict.Keys);
Assert.Equal("game-1", key); Assert.Equal("game-1", key);
Assert.NotNull(dict[key]); List<DatItem> items = dict.GetItemsForBucket(key);
Assert.Single(dict[key]!); Assert.Single(items);
} }
[Theory] [Theory]
@@ -132,10 +127,8 @@ namespace SabreTools.Test.DatFiles
rom1.CopyMachineInformation(machine1); rom1.CopyMachineInformation(machine1);
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary var dict = new ItemDictionary();
{ dict.Add("game-1", [rom1, rom2]);
["game-1"] = [rom1, rom2],
};
// Setup the test item // Setup the test item
var rom = new Rom(); var rom = new Rom();
@@ -170,10 +163,8 @@ namespace SabreTools.Test.DatFiles
rom1.CopyMachineInformation(machine1); rom1.CopyMachineInformation(machine1);
// Setup the dictionary // Setup the dictionary
var dict = new ItemDictionary var dict = new ItemDictionary();
{ dict.Add("game-1", [rom1, rom2]);
["game-1"] = [rom1, rom2],
};
// Setup the test item // Setup the test item
var rom = new Rom(); var rom = new Rom();

View File

@@ -97,7 +97,7 @@ namespace SabreTools.Features
Parser.ParseInto(datdata, datfile, int.MaxValue, keep: true); Parser.ParseInto(datdata, datfile, int.MaxValue, keep: true);
// Skip if nothing was parsed // 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; continue;
// Set depot information // Set depot information