The review of std.hash package
Johannes Pfau
nospam at example.com
Thu Aug 9 02:34:13 PDT 2012
Am Wed, 08 Aug 2012 12:27:39 -0700
schrieb Walter Bright <newshound2 at digitalmars.com>:
> On 8/8/2012 12:08 PM, Johannes Pfau wrote:
> > No where's the difference, except that for hashes the context
> > ('hash') has to be setup and finished manually?
>
>
> The idea is to have hash act like a component - not with special
> added code the user has to write.
Please explain that. Nobody's going to simply replace a call to reduce
with a call to a fictional 'hashReduce'. Why is it so important that
reduce + hash API's match 100% even if the API doesn't fit hashes?
>
> In this case, it needs to work like a reduce algorithm, because it is
> a reduce algorithm. Need to find a way to make this work.
>
We could do some ugly, performance killing hacks to make it possible,
but I just don't see why this is necessary.
----
struct InterHash
{
MD5 ctx;
ubyte[16] finished;
alias finished this;
}
InterHash hashReduce(Range)(Range data)
{
InterHash hash;
hash.ctx.start();
return hashReduce(hash, data);
}
InterHash hashReduce(Range)(InterHash hash, Range data)
{
copy(data, hash);
auto ctxCopy = hash.ctx;
hash.finished = ctxCopy.finish();
return hash;
}
auto a = hashReduce([1,2,3]);
auto b = hashReduce(a, [3,4]);
----
However, a and b are still not really valid hash values. I just don't
see why we should force an interface onto hashes which just doesn't fit.
More information about the Digitalmars-d
mailing list