using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace mdfinder { public class DuplicateFileGroup : PropertyChangedAlerter { #region Properties /// Gets the hash. /// The hash. public string Hash { get; } /// Gets the number of. /// The count. public int Count { get; } /// Gets the total size of the file group. /// The total number of size. public long TotalSize { get; } /// Gets the potential size saving. /// The potential size saving. public long PotentialSizeSaving { get; } /// Gets or sets the file records. /// The file records. public List FileRecords { get; set; } #endregion /// Constructor. /// The file records. /// (Optional) Type of the size savings to calculate. public DuplicateFileGroup(IEnumerable fileRecords, SavingsType savingsType = SavingsType.SaveBiggest) { this.FileRecords = new List(fileRecords); //Precalculate stats. this.Hash = this.FileRecords.Select(fr => fr.Hash).FirstOrDefault(); this.Count = this.FileRecords.Count(); this.TotalSize = this.FileRecords.Sum(fr => fr.Size); switch (savingsType) { case SavingsType.SaveBiggest: this.PotentialSizeSaving = this.FileRecords.OrderByDescending(fr => fr.Size).Skip(1).Sum(fr => fr.Size); break; case SavingsType.SaveSmallest: this.PotentialSizeSaving = this.FileRecords.OrderBy(fr => fr.Size).Skip(1).Sum(fr => fr.Size); break; case SavingsType.SaveMedian: //This is kind of hacky, but good enough for our purposes here. CLOSE ENOUGH var medianFileRecord = this.FileRecords.OrderBy(fr => fr.Size).ElementAt(this.Count / 2); this.PotentialSizeSaving = this.FileRecords.Except(new[] { medianFileRecord }).Sum(fr => fr.Size); break; default: break; } } /// Values that represent the ways of saving space. public enum SavingsType { /// Saves the biggest, and presumably highest quality, file. SaveBiggest, /// Saves the smallest file. SaveSmallest, /// . SaveMedian } } }