On Friday, 30 November 2012 at 07:29:59 UTC, Joshua Niehus wrote:
On Friday, 30 November 2012 at 06:29:01 UTC, Joshua Niehus wrote:
I think if you go breadth first, you can filter out the unwanted directories before it delves into them

Good idea, thanks. I could not get original to compile as is - but the concept is just what was needed. I got an error on line 8: Error: not a property dirEntries(path, cast(SpanMode)0, true).filter!(__lambda2)
I'm using a quite recent version of dmd and phobos.

But, I pulled the lamda out into a function and it works great. I assume the parallel is for performance, and it actually runs significantly slower than without on my test case - but no work is being done other than build the list of files, so that is probably normal. For my case the breakdown is:

No Pruning: 11 sec
Pruning Parallel: 4.78 sec
Pruning Serial: 0.377 sec

Thanks
Dan

---------------------
import std.algorithm, std.regex, std.stdio, std.file;
import std.parallelism;

bool interested(DirEntry path) {
  static auto exclude = regex(r"\.git|\.DS_Store", "g");
  return match(path.name, exclude).empty;
}

DirEntry[] prune(string path, ref DirEntry[] files)
{
  static if(0) {
foreach(_path; taskPool.parallel(filter!interested(dirEntries(path, SpanMode.shallow)))) {
      files ~= _path;
      if (isDir(_path.name)) { prune(_path.name, files); }
    }
  } else {
foreach(_path; filter!(interested)(dirEntries(path, SpanMode.shallow))) {
      files ~= _path;
      if (isDir(_path.name)) { prune(_path.name, files); }
    }
  }
  return files;
}

void main()
{
  DirEntry[] files;
  prune("/path", files);
  //  foreach(file;files) { writeln(file.name); }
}

Reply via email to