Metaprogramming with D.

TheFlyingFiddle theflyingfiddle at gmail.com
Sat Oct 19 17:18:38 PDT 2013


On Saturday, 19 October 2013 at 15:22:31 UTC, Agustin wrote:
> I'm trying to implement a template Vector and i would like to 
> know if this is possible.
>
> My current vector class is:
>
> public class Vector(T, size_t size) {
>     private T components[size];
> }
>
> And i would like to implement a property as this:
>
> @property
> public size_t length() const {
>     return Trait.ExpandFor(size, components, Function(+, (n, 
> j))); // Kind of messy but you get my point.
> }
>
> instead of doing for(...) over a array i would like to make a 
> trait that produce this code
>
> return (x * x + y * y + z * z + ...)

 From your post i guess that the goal here is to do
loop unrolling.

A simple way to achive this is to force a loop unrool
by iterating over a TypeTuple.

module vector;
import std.typetuple;


class Vector(T, size_t size)	{
     private T[size] components;

     public T length() const  //Guessing this is vector magnitude
     {	
	T res = 0;
	foreach(i; staticIota!(0u, size)) //Unrools the loop at 
compile-time.
	{
   	    res += components[i];
	}

	return cast(T)std.math.sqrt(res);
     }
}

//Creates a range from start inclusive to end exclusive.
template staticIota(size_t start, size_t end)
{
     static if(start == end)
	alias TypeTuple!() staticIota;
     else static if(start < end)
	alias TypeTuple!(start, staticIota!(start + 1, end)) staticIota;
     else
	static assert(0, "start cannot be greater then end!");
}


This works since the value _size_ is avalible at compile-time.


More information about the Digitalmars-d-learn mailing list