module zeroCount;

import std.exception;
import std.file;
import std.functional;
import std.algorithm;
import std.stdio: writefln;
import std.parallelism;
import std.range;

ubyte[1024*1024*10] arr;
void main(string[] args)
{
	enforce(args.length >= 2, "Using: zeroCount <file>...");
	writefln("%12s|%6s|%12s|%12s|%12s", "Zero bytes", "%", "Non-zero", "Total bytes", "File");
	enum fmt = "%12s|%6.2f|%12s|%12s|%12s";
	foreach(file; args[1 .. $])
	{
		auto bytes = cast(const(ubyte)[])read(file);
		scope(exit) delete bytes;

		auto counts = new ulong[taskPool.size + 1];
		foreach(el; parallel(chunks(bytes, bytes.length/counts.length)))
			counts[taskPool.workerIndex] += reduce!"a + !b"(0UL, el);
		
		immutable total = bytes.length, zeros = reduce!"a + b"(counts);
		writefln(fmt, zeros, 100.0L * zeros / total, total - zeros, total, file);
	}
}
