Array operations -- not actually so difficult
Don Clugston
dac at nospam.com.au
Fri Dec 15 01:40:16 PST 2006
Array operations are one of the very few 2.0 features that haven't made
it into 1.0. My experiments into expression templates suggest that it
might not be very complicated after all.
Consider something like a dot product,
real a[], b[], c[];
real d = dot(a+b, c);
For performance, it would be better if the compiler actually *didn't*
evaluate a+b. Instead, that should compile to:
real sum = 0;
for (int i=0; i<c.length; ++i) {
sum+=(a[i]+b[i])*c[i];
}
d=sum;
In fact, we could create a fantastic library implementation of array
operations with a tiny bit of syntactic sugar:
instead of generating an error message for array operations, look for an
opXXX function, and treat it the same way the implicit array properties
work.
----------
void opMulAssign(real [] a, real w)
{
for(int i=0; i<a.length; ++i) { a[i]*=w; }
}
void main()
{
real b[] = [1.0L, 2, 3, 4];
b.opMulAssign(3.5); // OK.
b *= 3.5; // Fails with "Error: 'b' is not a scalar, it is a real[]"
}
----------
So the message to Walter is, support for array operations will not
require any change to the back end. They are just a bit of syntactic
sugar at the front end.
More information about the Digitalmars-d
mailing list