array operations enhancements

F. Almeida francisco.m.almeida at gmail.com
Thu Aug 19 14:30:45 PDT 2010


== Quote from KennyTM~ (kennytm at gmail.com)'s article
> On Aug 18, 10 14:41, F. Almeida wrote:
> > What if the compiler was able to introduce them in the
assignment
> > loop, provided that the functions pass ref double (in this
case)?
> >
> > double anyfun(ref double x) { ... }
> >
> > c = anyfun(a[]) + 2.0*b[];
> import std.algorithm;
> import std.stdio;
> import std.array;
> auto vectorize(alias f, U)(U a) {
> 	return array(map!f(a));
> }
> double square(double x) {
> 	return x*x;
> }
> void main () {
>    auto a = [1.0, 2.0, 3.0, 4.0];
>    auto b = [5.0, 6.0, 7.0, 8.0];
>    auto c = [0.0, 0.0, 0.0, 0.0];
>    c[] = vectorize!square(a)[] + 2.0 * b[];
>    writeln(">", c);
> }

This doesn't help, as vectorize!() is being called to create an
array before passing it to the array expression (one additional
assignment loop). Additional functions passed through vectorize!()
would lead to N+1 loops per operation, with N the number of
vectorize!() calls, instead of the intended single loop. What is
intended is to pass as many functions as possible, and allow the
assignment to occur within a single loop.

The best alternative so far is to allow the compiler to take
initiative whenever a pure function accepting one argument (value,
not ref) of the same type exists.

pure double myf(double x) { ... }

c[] = myf(a[]) + 2.0*myf(b[]);

Is such an optimization possible?


More information about the Digitalmars-d mailing list