From 817a6cc180999d77ae6e6d3d1839bcc0a92866db Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 25 Sep 2017 12:56:45 -0700 Subject: [PATCH] [DatFile] Modify "Contains" a little --- RombaSharp/Partials/RombaSharp.Helpers.cs | 2 +- SabreTools.Library/Dats/DatFile.cs | 70 +++++++++++++++++++---- SabreTools.Library/Dats/DatItem.cs | 4 +- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/RombaSharp/Partials/RombaSharp.Helpers.cs b/RombaSharp/Partials/RombaSharp.Helpers.cs index 4713c07a..a3f8b1d1 100644 --- a/RombaSharp/Partials/RombaSharp.Helpers.cs +++ b/RombaSharp/Partials/RombaSharp.Helpers.cs @@ -424,7 +424,7 @@ namespace RombaSharp { sldr.Read(); string hash = sldr.GetString(0); - if (datroot.ContainsKey(hash)) + if (datroot.Contains(hash)) { datroot.Remove(hash); databaseDats.Add(hash); diff --git a/SabreTools.Library/Dats/DatFile.cs b/SabreTools.Library/Dats/DatFile.cs index 92443a25..121acb33 100644 --- a/SabreTools.Library/Dats/DatFile.cs +++ b/SabreTools.Library/Dats/DatFile.cs @@ -402,8 +402,10 @@ namespace SabreTools.Library.Dats /// /// Key in the dictionary to check /// True if the key exists, false otherwise - public bool ContainsKey(string key) + public bool Contains(string key) { + bool contains = false; + // If the dictionary is null, create it if (_items == null) { @@ -413,13 +415,48 @@ namespace SabreTools.Library.Dats // If the key is null, we return false since keys can't be null if (key == null) { - return false; + return contains; } lock (_items) { - return _items.ContainsKey(key); + contains = _items.ContainsKey(key); } + + return contains; + } + + /// + /// Get if the file dictionary contains the key and value + /// + /// Key in the dictionary to check + /// Value in the dictionary to check + /// True if the key exists, false otherwise + public bool Contains(string key, DatItem value) + { + bool contains = false; + + // If the dictionary is null, create it + if (_items == null) + { + _items = new SortedDictionary>(); + } + + // If the key is null, we return false since keys can't be null + if (key == null) + { + return contains; + } + + lock (_items) + { + if (_items.ContainsKey(key)) + { + contains = _items.ContainsKey(key); + } + } + + return contains; } /// @@ -466,19 +503,22 @@ namespace SabreTools.Library.Dats _items = new SortedDictionary>(); } + // If the key doesn't exist, return + if (!Contains(key)) + { + return; + } + lock (_items) { - // If the key is in the dictionary, remove it - if (_items.ContainsKey(key)) + // Remove the statistics first + foreach (DatItem item in _items[key]) { - // Remove the statistics first - foreach (DatItem item in _items[key]) - { - RemoveItemStatistics(item); - } - - _items.Remove(key); + RemoveItemStatistics(item); } + + // Remove the key from the dictionary + _items.Remove(key); } } @@ -495,6 +535,12 @@ namespace SabreTools.Library.Dats _items = new SortedDictionary>(); } + // If the key and value doesn't exist, return + if (!Contains(key, value)) + { + return; + } + lock (_items) { // While the key is in the dictionary and the item is there, remove it diff --git a/SabreTools.Library/Dats/DatItem.cs b/SabreTools.Library/Dats/DatItem.cs index 850ff727..9a2b81dd 100644 --- a/SabreTools.Library/Dats/DatItem.cs +++ b/SabreTools.Library/Dats/DatItem.cs @@ -264,7 +264,7 @@ namespace SabreTools.Library.Dats string key = SortAndGetKey(datdata, sorted); // If the key doesn't exist, return the empty list - if (!datdata.ContainsKey(key)) + if (!datdata.Contains(key)) { return false; } @@ -303,7 +303,7 @@ namespace SabreTools.Library.Dats string key = SortAndGetKey(datdata); // If the key doesn't exist, return the empty list - if (!datdata.ContainsKey(key)) + if (!datdata.Contains(key)) { return output; }