using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
using NaturalSort;
namespace SabreTools.Library.DatFiles
{
///
/// Item dictionary with statistics, bucketing, and sorting
///
public class ItemDictionary : IDictionary>
{
#region Private instance variables
///
/// Determine the bucketing key for all items
///
private BucketedBy bucketedBy;
///
/// Determine merging type for all items
///
private DedupeType mergedBy;
///
/// Internal dictionary for the class
///
private Dictionary> items;
#endregion
#region Publically available fields
///
/// Get the keys from the file dictionary
///
/// List of the keys
public ICollection Keys
{
get { return items.Keys; }
}
///
/// Get the keys in sorted order from the file dictionary
///
/// List of the keys in sorted order
public List SortedKeys
{
get
{
var keys = items.Keys.ToList();
keys.Sort(new NaturalComparer());
return keys;
}
}
///
/// DatStats object for reporting
///
public DatStats Statistics { get; private set; }
#endregion
#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
{
AddRange(key, value);
}
}
///
/// Add a value to the file dictionary
///
/// Key in the dictionary to add to
/// Value to add to the dictionary
public void Add(string key, DatItem value)
{
// Explicit lock for some weird corner cases
lock (key)
{
// Ensure the key exists
EnsureKey(key);
// If item is null, don't add it
if (value == null)
return;
// Now add the value
items[key].Add(value);
// Now update the statistics
Statistics.AddItem(value);
}
}
///
/// Add a range of values to the file dictionary
///
/// Key in the dictionary to add to
/// Value to add to the dictionary
public void Add(string key, List value)
{
AddRange(key, value);
}
///
/// Add a range of values to the file dictionary
///
/// Key in the dictionary to add to
/// Value to add to the dictionary
public void AddRange(string key, List value)
{
// Explicit lock for some weird corner cases
lock (key)
{
// Ensure the key exists
EnsureKey(key);
// Now add the value
items[key].AddRange(value);
// Now update the statistics
foreach (DatItem item in value)
{
Statistics.AddItem(item);
}
}
}
///
/// Get if the file dictionary contains the key
///
/// Key in the dictionary to check
/// True if the key exists, false otherwise
public bool ContainsKey(string key)
{
// If the key is null, we return false since keys can't be null
if (key == null)
return false;
// Explicit lock for some weird corner cases
lock (key)
{
return items.ContainsKey(key);
}
}
///
/// 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)
{
// If the key is null, we return false since keys can't be null
if (key == null)
return false;
// Explicit lock for some weird corner cases
lock (key)
{
if (items.ContainsKey(key))
return items[key].Contains(value);
}
return false;
}
///
/// Remove a key from the file dictionary if it exists
///
/// Key in the dictionary to remove
public bool Remove(string key)
{
// If the key doesn't exist, return
if (!ContainsKey(key))
return false;
// Remove the statistics first
foreach (DatItem item in items[key])
{
Statistics.RemoveItem(item);
}
// Remove the key from the dictionary
return items.Remove(key);
}
///
/// Remove the first instance of a value from the file dictionary if it exists
///
/// Key in the dictionary to remove from
/// Value to remove from the dictionary
public bool Remove(string key, DatItem value)
{
// If the key and value doesn't exist, return
if (!Contains(key, value))
return false;
// Remove the statistics first
Statistics.RemoveItem(value);
return items[key].Remove(value);
}
///
/// Override the internal BucketedBy value
///
///
public void SetBucketedBy(BucketedBy newBucket)
{
bucketedBy = newBucket;
}
///
/// Ensure the key exists in the items dictionary
///
/// Key to ensure
private void EnsureKey(string key)
{
// If the key is missing from the dictionary, add it
if (!items.ContainsKey(key))
items.Add(key, new List());
}
#endregion
#region Constructors
///
/// Generic constructor
///
public ItemDictionary()
{
bucketedBy = BucketedBy.Default;
mergedBy = DedupeType.None;
items = new Dictionary>();
Statistics = new DatStats();
}
#endregion
#region Custom Functionality
///
/// Take the arbitrarily bucketed Files Dictionary and convert to one bucketed by a user-defined method
///
/// BucketedBy enum representing how to bucket the individual items
/// Dedupe type that should be used
/// True if the key should be lowercased (default), false otherwise
/// True if games should only be compared on game and file name, false if system and source are counted
public void BucketBy(BucketedBy bucketBy, DedupeType dedupeType, bool lower = true, bool norename = true)
{
// If we have a situation where there's no dictionary or no keys at all, we skip
if (items == null || items.Count == 0)
return;
// If the sorted type isn't the same, we want to sort the dictionary accordingly
if (this.bucketedBy != bucketBy)
{
Globals.Logger.User($"Organizing roms by {bucketBy}");
// Set the sorted type
this.bucketedBy = bucketBy;
// Reset the merged type since this might change the merge
this.mergedBy = DedupeType.None;
// First do the initial sort of all of the roms inplace
List oldkeys = Keys.ToList();
for (int k = 0; k < oldkeys.Count; k++)
{
string key = oldkeys[k];
// Get the unsorted current list
List items = this[key];
// Now add each of the roms to their respective keys
for (int i = 0; i < items.Count; i++)
{
DatItem item = items[i];
if (item == null)
continue;
// We want to get the key most appropriate for the given sorting type
string newkey = item.GetKey(bucketBy, lower, norename);
// If the key is different, move the item to the new key
if (newkey != key)
{
Add(newkey, item);
Remove(key, item);
i--; // This make sure that the pointer stays on the correct since one was removed
}
}
// If the key is now empty, remove it
if (this[key].Count == 0)
Remove(key);
}
}
// If the merge type isn't the same, we want to merge the dictionary accordingly
if (this.mergedBy != dedupeType)
{
Globals.Logger.User($"Deduping roms by {dedupeType}");
// Set the sorted type
this.mergedBy = dedupeType;
Parallel.ForEach(Keys, Globals.ParallelOptions, key =>
{
// Get the possibly unsorted list
List sortedlist = this[key];
// Sort the list of items to be consistent
DatItem.Sort(ref sortedlist, false);
// If we're merging the roms, do so
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == BucketedBy.Game))
sortedlist = DatItem.Merge(sortedlist);
// Add the list back to the dictionary
Remove(key);
AddRange(key, sortedlist);
});
}
// If the merge type is the same, we want to sort the dictionary to be consistent
else
{
Parallel.ForEach(Keys, Globals.ParallelOptions, key =>
{
// Get the possibly unsorted list
List sortedlist = this[key];
// Sort the list of items to be consistent
DatItem.Sort(ref sortedlist, false);
});
}
// Now clean up all empty keys
ClearEmpty();
}
///
/// Remove all items marked for removal
///
public void ClearMarked()
{
foreach (string key in items.Keys)
{
List oldItemList = items[key];
List newItemList = oldItemList.Where(i => !i.Remove).ToList();
Remove(key);
AddRange(key, newItemList);
}
}
///
/// List all duplicates found in a DAT based on a DatItem
///
/// Item to try to match
/// True to mark matched roms for removal from the input, false otherwise (default)
/// True if the DAT is already sorted accordingly, false otherwise (default)
/// List of matched DatItem objects
public List GetDuplicates(DatItem datItem, bool remove = false, bool sorted = false)
{
List output = new List();
// Check for an empty rom list first
if (Statistics.Count == 0)
return output;
// We want to get the proper key for the DatItem
string key = SortAndGetKey(datItem, sorted);
// If the key doesn't exist, return the empty list
if (!ContainsKey(key))
return output;
// Try to find duplicates
List roms = this[key];
List left = new List();
for (int i = 0; i < roms.Count; i++)
{
DatItem other = roms[i];
if (datItem.Equals(other))
{
other.Remove = true;
output.Add(other);
}
else
{
left.Add(other);
}
}
// If we're in removal mode, add back all roms with the proper flags
if (remove)
{
Remove(key);
AddRange(key, output);
AddRange(key, left);
}
return output;
}
///
/// Check if a DAT contains the given DatItem
///
/// Item to try to match
/// True if the DAT is already sorted accordingly, false otherwise (default)
/// True if it contains the rom, false otherwise
public bool HasDuplicates(DatItem datItem, bool sorted = false)
{
// Check for an empty rom list first
if (Statistics.Count == 0)
return false;
// We want to get the proper key for the DatItem
string key = SortAndGetKey(datItem, sorted);
// If the key doesn't exist, return the empty list
if (!ContainsKey(key))
return false;
// Try to find duplicates
List roms = this[key];
return roms.Any(r => datItem.Equals(r));
}
///
/// Recalculate the statistics for the Dat
///
public void RecalculateStats()
{
// Wipe out any stats already there
Statistics.Reset();
// If we have a blank Dat in any way, return
if (this == null || Statistics.Count == 0)
return;
// Loop through and add
Parallel.ForEach(items.Keys, Globals.ParallelOptions, key =>
{
List datItems = items[key];
foreach (DatItem item in datItems)
{
Statistics.AddItem(item);
}
});
}
///
/// Remove any keys that have null or empty values
///
private void ClearEmpty()
{
foreach (string key in items.Keys)
{
if (items[key] == null || items[key].Count == 0)
items.Remove(key);
}
}
///
/// Sort the input DAT and get the key to be used by the item
///
/// Item to try to match
/// True if the DAT is already sorted accordingly, false otherwise (default)
/// Key to try to use
private string SortAndGetKey(DatItem datItem, bool sorted = false)
{
// If we're not already sorted, take care of it
if (!sorted)
BucketBy(Statistics.GetBestAvailable(), DedupeType.None);
// Now that we have the sorted type, we get the proper key
return datItem.GetKey(bucketedBy);
}
#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
}
}