public List <Tuple <File, File> > Compare(ExtensionFilesGroup _other, ProgressDelegate _progress) { int maxFilesCount = Math.Max(m_files.Count, _other.m_files.Count); // First, compare file sizes Dictionary <long, Tuple <List <File>, List <File> > > size2Files = new Dictionary <long, Tuple <List <File>, List <File> > >(maxFilesCount); foreach (File file in m_files) { if (!size2Files.ContainsKey(file.m_size)) { size2Files.Add(file.m_size, new Tuple <List <File>, List <File> >(new List <File>(), new List <File>())); } size2Files[file.m_size].Item1.Add(file); } foreach (File file in _other.m_files) { if (!size2Files.ContainsKey(file.m_size)) { size2Files.Add(file.m_size, new Tuple <List <File>, List <File> >(new List <File>(), new List <File>())); } size2Files[file.m_size].Item2.Add(file); } // Compare among files of the same size List <Tuple <File, File> > result = new List <Tuple <File, File> >(maxFilesCount); int sameSizeFilesProgressCount = Math.Max(1, size2Files.Values.Count / 100); int sameSizeFilesIndex = 0; foreach (Tuple <List <File>, List <File> > sameSizeFiles in size2Files.Values) { if ((++sameSizeFilesIndex % sameSizeFilesProgressCount) == 0) { // Notify of progress _progress((float)sameSizeFilesIndex / size2Files.Values.Count, result.Count); } for (int fileIndex0 = 0; fileIndex0 < sameSizeFiles.Item1.Count; fileIndex0++) { File file0 = sameSizeFiles.Item1[fileIndex0]; for (int fileIndex1 = 0; fileIndex1 < sameSizeFiles.Item2.Count; fileIndex1++) { File file1 = sameSizeFiles.Item2[fileIndex1]; if (file0.m_fullName == file1.m_fullName) { continue; // Ignore same files... } if (file0.CompareBinary(file1)) { result.Add(new Tuple <File, File>(file0, file1)); // Same! } } } } return(result); }
public List< Tuple< File, File > > Compare( ExtensionFilesGroup _other, ProgressDelegate _progress ) { int maxFilesCount = Math.Max( m_files.Count, _other.m_files.Count ); // First, compare file sizes Dictionary< long, Tuple< List< File >, List< File > > > size2Files = new Dictionary< long, Tuple< List< File >, List< File > > >( maxFilesCount ); foreach ( File file in m_files ) { if ( !size2Files.ContainsKey( file.m_size ) ) size2Files.Add( file.m_size, new Tuple< List< File >, List< File > >( new List< File >(), new List< File >() ) ); size2Files[file.m_size].Item1.Add( file ); } foreach ( File file in _other.m_files ) { if ( !size2Files.ContainsKey( file.m_size ) ) size2Files.Add( file.m_size, new Tuple< List< File >, List< File > >( new List< File >(), new List< File >() ) ); size2Files[file.m_size].Item2.Add( file ); } // Compare among files of the same size List< Tuple< File, File > > result = new List< Tuple< File, File > >( maxFilesCount ); int sameSizeFilesProgressCount = Math.Max( 1, size2Files.Values.Count / 100 ); int sameSizeFilesIndex = 0; foreach ( Tuple< List< File >, List< File > > sameSizeFiles in size2Files.Values ) { if ( (++sameSizeFilesIndex % sameSizeFilesProgressCount) == 0 ) { // Notify of progress _progress( (float) sameSizeFilesIndex / size2Files.Values.Count, result.Count ); } for ( int fileIndex0=0; fileIndex0 < sameSizeFiles.Item1.Count; fileIndex0++ ) { File file0 = sameSizeFiles.Item1[fileIndex0]; for ( int fileIndex1=0; fileIndex1 < sameSizeFiles.Item2.Count; fileIndex1++ ) { File file1 = sameSizeFiles.Item2[fileIndex1]; if ( file0.m_fullName == file1.m_fullName ) continue; // Ignore same files... if ( file0.CompareBinary( file1 ) ) { result.Add( new Tuple<File,File>( file0, file1 ) ); // Same! } } } } return result; }
void PopulateListBox(List <File> _files, ListBox _listBox, Label _label, Dictionary <string, ExtensionFilesGroup> _extensionGroup) { _listBox.SuspendLayout(); _listBox.Items.Clear(); _listBox.Items.AddRange(_files.ToArray()); _listBox.ResumeLayout(); // Build the extension group _extensionGroup.Clear(); foreach (File F in _files) { if (!_extensionGroup.ContainsKey(F.m_extension)) { _extensionGroup.Add(F.m_extension, new ExtensionFilesGroup(F.m_extension)); } ExtensionFilesGroup group = _extensionGroup[F.m_extension]; group.AddFile(F); } _label.Text = _files.Count + " files. " + _extensionGroup.Keys.Count + " extensions."; }
void Compare() { progressBar.Value = 0; progressBar.Visible = true; m_identicalFiles.Clear(); int extensionIndex = 0; float currentProgress = 0.0f; foreach (string extension in m_extensionGroupLeft.Keys) { if (!m_extensionGroupRight.ContainsKey(extension)) { continue; } ExtensionFilesGroup leftGroup = m_extensionGroupLeft[extension]; ExtensionFilesGroup rightGroup = m_extensionGroupRight[extension]; float extensionProgress = (float)leftGroup.m_files.Count / m_filesLeft.Count; float currentExtensionProgress = extensionIndex++ *extensionProgress; List <Tuple <File, File> > identicalFiles = leftGroup.Compare(rightGroup, (float _progress, int _identicalFilesCount) => { progressBar.Value = (int)(progressBar.Maximum * (currentProgress + _progress * extensionProgress)); labelResults.Text = "Processing " + leftGroup.m_files.Count + " \"*" + extension + "\" files. " + _identicalFilesCount + " identical so far. Total = " + (m_identicalFiles.Count + _identicalFilesCount) + " identical files of all types."; progressBar.Refresh(); labelResults.Refresh(); }); m_identicalFiles.AddRange(identicalFiles); currentProgress += extensionProgress; } labelResults.Text = m_identicalFiles.Count + " identical files that can be optimized."; progressBar.Visible = false; }