ArchiveComparer2.Library.ArchiveDuplicateDetector.Compare C# (CSharp) Method

Compare() private method

Check if file is duplicated
private Compare ( DuplicateArchiveInfo &Origin, DuplicateArchiveInfo &Duplicate, DuplicateSearchOption option ) : bool
Origin DuplicateArchiveInfo
Duplicate DuplicateArchiveInfo
option DuplicateSearchOption
return bool
        private bool Compare(ref DuplicateArchiveInfo Origin, ref DuplicateArchiveInfo Duplicate, DuplicateSearchOption option)
        {
            NotifyCaller("Comparing: " + Origin.Filename + " to " + Duplicate.Filename, OperationStatus.COMPARING);

            // if item count is equal, try to check from crc strings.
            Origin.MatchType = MatchType.ORIGINAL;
            Origin.Percentage = 0.0;
            if (Origin.NoMatches != null) Origin.NoMatches.Clear();

            if (Origin.Items.Count == Duplicate.Items.Count)
            {
                Duplicate.MatchType = MatchType.EQUALCOUNT;

                if (Origin.ToCRCString() == Duplicate.ToCRCString())
                {
                    NotifyCaller("CRC Strings are equal.", OperationStatus.COMPARING);
                    Duplicate.Percentage = 100.0;
                    return true;
                }
                else if (option.OnlyPerfectMatch)
                {
                    return false;
                }
            }

            Duplicate.MatchType = MatchType.SUBSET;

            // Check each files in duplicate
            int limitCount;

            // if only have 'IgnoreLimit' files, then all must match
            if (option.IgnoreLimit > Duplicate.Items.Count) limitCount = 0;
            else limitCount = Duplicate.Items.Count - (Duplicate.Items.Count * option.Limit / 100);

            int skippedCount = 0;
            int i = 0;
            int j = 0;
            while (i < Origin.Items.Count && j < Duplicate.Items.Count && skippedCount <= limitCount)
            {
                // compare the from the biggest crc.
                int result = string.Compare(Origin.Items[i].Crc, Duplicate.Items[j].Crc, true, System.Globalization.CultureInfo.InvariantCulture);
                if (result == 0)
                {
                    ++i; ++j;
                }
                else if (result > 0)
                {
                    // Origin file skipped
                    ++i;
                }
                else
                {
                    // Duplicate file skipped, no match in Origin
                    ++skippedCount;
                    if (Duplicate.NoMatches == null) Duplicate.NoMatches = new List<ArchiveFileInfoSmall>();
                    Duplicate.NoMatches.Add(Duplicate.Items[j]);
                    ++j;
                }
            }

            if (j < Duplicate.Items.Count)
            {
                if (Duplicate.NoMatches == null) Duplicate.NoMatches = new List<ArchiveFileInfoSmall>();
                Duplicate.NoMatches.AddRange(Duplicate.Items.GetRange(j, Duplicate.Items.Count - j));
                skippedCount = Duplicate.NoMatches.Count;
            }

            double percent = (double)(Duplicate.Items.Count - skippedCount) / Duplicate.Items.Count * 100;
            if (percent >= option.Limit && skippedCount < limitCount)
            {
                NotifyCaller("Match: " + percent + "%", OperationStatus.COMPARING);
                Duplicate.Percentage = percent;
                return true;
            }

            NotifyCaller("Not Match", OperationStatus.COMPARING);
            if (Duplicate.NoMatches != null) Duplicate.NoMatches.Clear();
            return false;
        }