prune with dirEntries

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Nov 30 10:56:09 PST 2012


11/30/2012 11:29 AM, Joshua Niehus пишет:
> 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
>
> oh wait... it probably still looks through all those dir's.
> What about this?
>
> import std.algorithm, std.regex, std.stdio, std.file;
> import std.parallelism;
> DirEntry[] prune(string path, ref DirEntry[] files)
> {
>    auto exclude = regex(r"\.git|\.DS_Store", "g");
>    foreach(_path; taskPool.parallel(dirEntries(path, SpanMode.shallow)
>      .filter!(a => match(a.name, exclude).empty)))
>    {
>      files ~= _path;

I do think that there is a race on 'files' variable. parallel doesn't 
auto-magically lock anything.

>      if (isDir(_path.name)) { prune(_path.name, files); }

An yes, I have a bad feeling that spawning a few threads per directory 
recursively is a bad idea.


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

Otherwise I think there is a better way to filter out directories inside 
because here you a basically doing what dirEntries depth search does 
(but with recursion vs queue).

Maybe file it as an enhancement?


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list