[DatFile] Modify "Contains" a little

This commit is contained in:
Matt Nadareski
2017-09-25 12:56:45 -07:00
parent 7797b668ee
commit 817a6cc180
3 changed files with 61 additions and 15 deletions

View File

@@ -424,7 +424,7 @@ namespace RombaSharp
{ {
sldr.Read(); sldr.Read();
string hash = sldr.GetString(0); string hash = sldr.GetString(0);
if (datroot.ContainsKey(hash)) if (datroot.Contains(hash))
{ {
datroot.Remove(hash); datroot.Remove(hash);
databaseDats.Add(hash); databaseDats.Add(hash);

View File

@@ -402,8 +402,10 @@ namespace SabreTools.Library.Dats
/// </summary> /// </summary>
/// <param name="key">Key in the dictionary to check</param> /// <param name="key">Key in the dictionary to check</param>
/// <returns>True if the key exists, false otherwise</returns> /// <returns>True if the key exists, false otherwise</returns>
public bool ContainsKey(string key) public bool Contains(string key)
{ {
bool contains = false;
// If the dictionary is null, create it // If the dictionary is null, create it
if (_items == null) 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 the key is null, we return false since keys can't be null
if (key == null) if (key == null)
{ {
return false; return contains;
} }
lock (_items) lock (_items)
{ {
return _items.ContainsKey(key); contains = _items.ContainsKey(key);
} }
return contains;
}
/// <summary>
/// Get if the file dictionary contains the key and value
/// </summary>
/// <param name="key">Key in the dictionary to check</param>
/// <param name="value">Value in the dictionary to check</param>
/// <returns>True if the key exists, false otherwise</returns>
public bool Contains(string key, DatItem value)
{
bool contains = false;
// If the dictionary is null, create it
if (_items == null)
{
_items = new SortedDictionary<string, List<DatItem>>();
}
// 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;
} }
/// <summary> /// <summary>
@@ -466,19 +503,22 @@ namespace SabreTools.Library.Dats
_items = new SortedDictionary<string, List<DatItem>>(); _items = new SortedDictionary<string, List<DatItem>>();
} }
// If the key doesn't exist, return
if (!Contains(key))
{
return;
}
lock (_items) lock (_items)
{ {
// If the key is in the dictionary, remove it // Remove the statistics first
if (_items.ContainsKey(key)) foreach (DatItem item in _items[key])
{ {
// Remove the statistics first RemoveItemStatistics(item);
foreach (DatItem item in _items[key])
{
RemoveItemStatistics(item);
}
_items.Remove(key);
} }
// Remove the key from the dictionary
_items.Remove(key);
} }
} }
@@ -495,6 +535,12 @@ namespace SabreTools.Library.Dats
_items = new SortedDictionary<string, List<DatItem>>(); _items = new SortedDictionary<string, List<DatItem>>();
} }
// If the key and value doesn't exist, return
if (!Contains(key, value))
{
return;
}
lock (_items) lock (_items)
{ {
// While the key is in the dictionary and the item is there, remove it // While the key is in the dictionary and the item is there, remove it

View File

@@ -264,7 +264,7 @@ namespace SabreTools.Library.Dats
string key = SortAndGetKey(datdata, sorted); string key = SortAndGetKey(datdata, sorted);
// If the key doesn't exist, return the empty list // If the key doesn't exist, return the empty list
if (!datdata.ContainsKey(key)) if (!datdata.Contains(key))
{ {
return false; return false;
} }
@@ -303,7 +303,7 @@ namespace SabreTools.Library.Dats
string key = SortAndGetKey(datdata); string key = SortAndGetKey(datdata);
// If the key doesn't exist, return the empty list // If the key doesn't exist, return the empty list
if (!datdata.ContainsKey(key)) if (!datdata.Contains(key))
{ {
return output; return output;
} }