[Traverse] Add edc's traverse code (not hooked up)

This commit is contained in:
Matt Nadareski
2017-03-14 20:09:44 -07:00
parent 33f777f8aa
commit 2a70e9c381
2 changed files with 73 additions and 0 deletions

72
SabreTools.Helper/External/Traverse.cs vendored Normal file
View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
#if MONO
using System.IO;
#else
using Alphaleonis.Win32.Filesystem;
using IOException = System.IO.IOException;
using SearchOption = System.IO.SearchOption;
#endif
using SabreTools.Helper.Data;
namespace SabreTools.Helper.External
{
public class Traverse
{
/// Original version: Microsoft (example code), updated by edc
public void TraverseTreeParallelForEach(string root, Action<FileInfo> action)
{
List<string> dirs = new List<string>();
if (!Directory.Exists(root))
{
throw new ArgumentException();
}
dirs.Add(root);
List<string> subdirs = new List<string>();
while (dirs.Count > 0 || subdirs.Count > 0)
{
foreach (string dir in subdirs)
{
dirs.Add(dir);
}
subdirs.Clear();
Parallel.ForEach(dirs,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
currentDir =>
{
string[] subDirs = Directory.GetDirectories(currentDir);
lock (subdirs)
{
foreach (string str in subDirs)
{
subdirs.Add(str);
}
}
var dir = new DirectoryInfo(currentDir);
try
{
FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly);
Parallel.ForEach(files,
new ParallelOptions() { MaxDegreeOfParallelism = Globals.MaxDegreeOfParallelism },
info =>
{
action(info);
});
}
catch { }
});
dirs.Clear();
}
}
}
}

View File

@@ -124,6 +124,7 @@
<Compile Include="External\NaturalSort\NaturalComparer.cs" /> <Compile Include="External\NaturalSort\NaturalComparer.cs" />
<Compile Include="External\NaturalSort\NaturalReversedComparer.cs" /> <Compile Include="External\NaturalSort\NaturalReversedComparer.cs" />
<Compile Include="External\OptimizedCRC.cs" /> <Compile Include="External\OptimizedCRC.cs" />
<Compile Include="External\Traverse.cs" />
<Compile Include="External\xxHash.cs" /> <Compile Include="External\xxHash.cs" />
<Compile Include="External\Zlib\CRC32.cs" /> <Compile Include="External\Zlib\CRC32.cs" />
<Compile Include="External\Zlib\Deflate.cs" /> <Compile Include="External\Zlib\Deflate.cs" />