Help required on Array appender

Vino.B via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 2 08:47:31 PDT 2017


On Saturday, 2 September 2017 at 12:54:48 UTC, Nicholas Wilson 
wrote:
> On Saturday, 2 September 2017 at 10:15:04 UTC, Vino.B wrote:
>> Hi All,
>>
>>  Can you please guide me how can i use array appender for the 
>> below piece of code
>>
>> string[][] cleanFiles (string FFs, string Step) {
>> auto dFiles = dirEntries(FFs, SpanMode.shallow).filter!(a => 
>> a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;
>> 	foreach (d; dFiles) {
>> 		if (Step == "dryrun")
>> 		{
>>                     Subdata ~=  [d[0], d[1].toSimpleString[0 
>> .. 20]];
>>              }
>> 		else if (Step == "run")
>>              {
>> 			remove(d[0]);
>> 			if (!d[0].exists)
>> 				Subdata ~=  [d[0], d[1].toSimpleString[0 .. 20]];
>> 		}
>> 	}
>> 	return Subdata;
>> }
>>
>> From,
>> Vino.B
>
> If you're wanting to use appender just make an appender and 
> replace the ~= to calls to appender.put(data);
>
> if you're trying to make it faster, consider that Step could be 
> a bool, your return type could be string[2][], the `if 
> (!d[0].exists)` is redundant since `remove` will throw if it 
> fails. That leaves you with
>
> string[2][] cleanFiles(string FFs, bool dryrun)
> {
>     auto dFiles = dirEntries(FFs, SpanMode.shallow)
>                   .filter!(a => a.isFile)
>                   .map!(a =>[a.name , 
> a.timeCreated.toSimpleString[0 .. 20])
>                   .array;
>     if (! dryrun)
>         dFiles.each!(f => f[0].remove);
> }

Hi,

  Thank you very much, and your idea help a lot, and the reason to 
use appender is for both faster and performance as my program 
use's many ~= function and found that using appender is the best 
way when compared with the ~= e.g;(MCresult.get ~= 
MCleanTaskData;)

void mCleanFiles (string[] Dirlist, File logF, File logE, string 
Step) {
  try {
  string[][] MCtext;
  string[][] MCEresult;
  auto MCresult = taskPool.workerLocalStorage(MCtext);
  logF.writeln("Function \t : List of the File's which are not 
placed in correct Location and list those deleted");
  logF.writeln("Dir. Scanned \t :", Dirlist);
  
logF.writeln("************************************************************************************");
  logF.writefln("%-63s %.20s", "File Name", "CreationTime");
  
logF.writeln("************************************************************************************");
  foreach (string Fs; parallel(Dirlist[0 .. $], 1)) {
			auto FFs = Fs.strip;
			auto MCleanTask = task(&cleanFiles, FFs, Step);
			MCleanTask.executeInNewThread();
			auto MCleanTaskData = MCleanTask.workForce;
			MCresult.get ~= MCleanTaskData;
		}
		foreach(i; MCresult.toRange)
			logF.writefln("%(%-(%-63s %)\n%)", i.sort!((a,b) => a[0] < 
b[0]).uniq);
logF.writeln("************************************************************************************");
} catch (Exception e) { logE.writeln(e.msg); }
}

From,
Vino.B


More information about the Digitalmars-d-learn mailing list