January 25, 2006
Dave wrote:
> In article <dr70cq$f0j$1@digitaldaemon.com>, Sean Kelly says...
>> Agent Orange wrote:
>>> you cant create a struct template
>> DMD allows it, though the docs say it's illegal.
>>
> 
> Can you link to where you saw that... (After all, there are whole libraries
> built using struct templates and (obviously) supporting code in the reference
> compiler, so I think the docs. are wrong)?
> 
> Just want to point it out to Walter so the docs. can be fixed.

He may have already fixed them.  I can't find the clause I remember seeing.


Sean
January 25, 2006
Kyle Furlong wrote:
> I just started work on Velocity, a game engine. I want the math module to use templated structs for vectors and matrices. Theses structs will overload the different operators (+,*,-)with fast assembly implementations of the operations.
> 
> I'm running into some errors which I cant understand due to my shaky grasp of the language. Attached is what I have so far for the Vector struct; any pointers are appreciated.
> 
> p.s. - Velocity is hosted at dsource. At the moment the license is tbd.
> 
> 
> 
> ------------------------------------------------------------------------
> 
> //|
> //|		___    __    ______           __________
> //|		__ |  / /_______  /______________(_)_  /_____  __
> //|		__ | / /_  _ \_  /_  __ \  ___/_  /_  __/_  / / /
> //|		__ |/ / /  __/  / / /_/ / /__ _  / / /_ _  /_/ /
> //|		_____/  \___//_/  \____/\___/ /_/  \__/ _\__, /
> //|		                                      __/____/
> //|			Copyright (c) 2006 Kyle Furlong
> //|
> 
> module velocity.math.Vector;
> 
> /**************************************************
> 	A fast vector implementation
> 
> 	Examples:
> 	---
> 	alias Vector!(int,2) Point;
> 	Point origin = Point(0,0);
> 	---
> **************************************************/
> struct Vector(T, int Dim = 4)
> {
> 	private T[Dim] storage;
> 
> 	T opIndex(int index)
> 	{
> 		return storage[index];
> 	}
> 
> 	T opIndexAssign(T value, int index)
> 	{
> 		storage[index] = value;
> 		return value;
> 	}
> 
> 	T opSlice()
> 	{
> 		return storage;
> 	}
> 
> 	T opSlice(int i, int j)
> 	{
> 		return storage[i..j];
> 	}
> 
> 	Vector!(T,Dim) opAdd(Vector!(T,Dim) a)
> 	{
> 		Vector!(T,Dim) ret;
> 		for(int i = 0; i < Dim; i++)
> 		{
> 			ret[i] = storage[i] + a[i];
> 		}
> 		return ret;
> 	}
> 
> 	Vector!(T,Dim) opSub(Vector!(T,Dim) a)
> 	{
> 		Vector!(T,Dim,Row) ret;
> 		for(int i = 0; i < Dim; i++)
> 		{
> 			ret[i] = storage[i] - a[i];
> 		}
> 		return ret;
> 	}
> 
> 	Vector!(T,Dim) opAddAssign(Vector!(T,Dim) a)
> 	{
> 		Vector!(T,Dim,Row) ret;
> 		for(int i = 0; i < Dim; i++)
> 		{
> 			ret[i] = storage[i] - a[i];
> 		}
> 		return ret;
> 	}
> 
> 	static Vector!(T,Dim) opCall(...)
> 	{
> 		Vector!(T,Dim) v;
> 
> 		for (int i = 0; i < Dim; i++)
> 	    {
> 			if (_arguments[i] == typeid(T))
> 			{
> 			    v[i] = *cast(T *)_argptr;
> 			    _argptr += T.sizeof;
> 			}
> 			else assert(0);
> 	    }
> 	    return v;
> 	}
> }
> 

Thank you all for your input. There is still some ambiguity though. Attached is my copy that works now, but some of you suggested keeping the methods in this form:

Vector!(T,Dim) opAdd(Vector!(T,Dim) a)
{
	Vector!(T,Dim) ret;
	for(int i = 0; i < Dim; i++)
	{
		ret[i] = storage[i] + a[i];
	}
	return ret;
}

Which didn't compile for me. Are there cases where this syntax will work? Or is it permissible to only use the struct name for returning the struct within the struct itself?




January 25, 2006
Kyle Furlong wrote:
> Kyle Furlong wrote:
>> I just started work on Velocity, a game engine. I want the math module to use templated structs for vectors and matrices. Theses structs will overload the different operators (+,*,-)with fast assembly implementations of the operations.
>>
>> I'm running into some errors which I cant understand due to my shaky grasp of the language. Attached is what I have so far for the Vector struct; any pointers are appreciated.
>>
>> p.s. - Velocity is hosted at dsource. At the moment the license is tbd.
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> //|
>> //|        ___    __    ______           __________         //|
>> __ |  / /_______  /______________(_)_  /_____  __
>> //|        __ | / /_  _ \_  /_  __ \  ___/_  /_  __/_  / / /
>> //|        __ |/ / /  __/  / / /_/ / /__ _  / / /_ _  /_/ / //|
>> _____/  \___//_/  \____/\___/ /_/  \__/ _\__, /
>> //|                                              __/____/
>> //|            Copyright (c) 2006 Kyle Furlong
>> //|
>>
>> module velocity.math.Vector;
>>
>> /**************************************************
>>     A fast vector implementation
>> 
>>     Examples:
>>     ---
>>     alias Vector!(int,2) Point;
>>     Point origin = Point(0,0);
>>     ---
>> **************************************************/
>> struct Vector(T, int Dim = 4)
>> {
>>     private T[Dim] storage;
>> 
>>     T opIndex(int index)
>>     {
>>         return storage[index];
>>     }
>> 
>>     T opIndexAssign(T value, int index)
>>     {
>>         storage[index] = value;
>>         return value;
>>     }
>> 
>>     T opSlice()
>>     {
>>         return storage;
>>     }
>> 
>>     T opSlice(int i, int j)
>>     {
>>         return storage[i..j];
>>     }
>> 
>>     Vector!(T,Dim) opAdd(Vector!(T,Dim) a)
>>     {
>>         Vector!(T,Dim) ret;
>>         for(int i = 0; i < Dim; i++)
>>         {
>>             ret[i] = storage[i] + a[i];
>>         }
>>         return ret;
>>     }
>> 
>>     Vector!(T,Dim) opSub(Vector!(T,Dim) a)
>>     {
>>         Vector!(T,Dim,Row) ret;
>>         for(int i = 0; i < Dim; i++)
>>         {
>>             ret[i] = storage[i] - a[i];
>>         }
>>         return ret;
>>     }
>> 
>>     Vector!(T,Dim) opAddAssign(Vector!(T,Dim) a)
>>     {
>>         Vector!(T,Dim,Row) ret;
>>         for(int i = 0; i < Dim; i++)
>>         {
>>             ret[i] = storage[i] - a[i];
>>         }
>>         return ret;
>>     }
>> 
>>     static Vector!(T,Dim) opCall(...)
>>     {
>>         Vector!(T,Dim) v;
>> 
>>         for (int i = 0; i < Dim; i++)
>>         {
>>             if (_arguments[i] == typeid(T))
>>             {
>>                 v[i] = *cast(T *)_argptr;
>>                 _argptr += T.sizeof;
>>             }
>>             else assert(0);
>>         }
>>         return v;
>>     }
>> }
>>
> 
> Thank you all for your input. There is still some ambiguity though. Attached is my copy that works now, but some of you suggested keeping the methods in this form:
> 
> Vector!(T,Dim) opAdd(Vector!(T,Dim) a)
> {
>     Vector!(T,Dim) ret;
>     for(int i = 0; i < Dim; i++)
>     {
>         ret[i] = storage[i] + a[i];
>     }
>     return ret;
> }
> 
> Which didn't compile for me. Are there cases where this syntax will work? Or is it permissible to only use the struct name for returning the struct within the struct itself?
> 
> 
> 
> ------------------------------------------------------------------------
> 
> //|
> //|		___    __    ______           __________
> //|		__ |  / /_______  /______________(_)_  /_____  __
> //|		__ | / /_  _ \_  /_  __ \  ___/_  /_  __/_  / / /
> //|		__ |/ / /  __/  / / /_/ / /__ _  / / /_ _  /_/ /
> //|		_____/  \___//_/  \____/\___/ /_/  \__/ _\__, /
> //|		                                      __/____/
> //|			Copyright (c) 2006 Kyle Furlong
> //|
> 
> module velocity.math.Vector;
> 
> /**************************************************
> 	A fast vector implementation
> 
> 	Examples:
> 	---
> 	alias Vector!(int,2) Point;
> 	Point origin = Point(0,0);
> 	---
> **************************************************/
> class Vector(T, int Dim = 4)
> {
> 	private T[Dim] storage;
> 
> 	T opIndex(int index)
> 	{
> 		return storage[index];
> 	}
> 
> 	T opIndexAssign(T value, int index)
> 	{
> 		storage[index] = value;
> 		return value;
> 	}
> 
> 	T opSlice()
> 	{
> 		return storage;
> 	}
> 
> 	T opSlice(int i, int j)
> 	{
> 		return storage[i..j];
> 	}
> 
> 	Vector!(T,Dim) opAdd(Vector!(T,Dim) a)
> 	{
> 		Vector!(T,Dim) ret;
> 		for(int i = 0; i < Dim; i++)
> 		{
> 			ret[i] = storage[i] + a[i];
> 		}
> 		return ret;
> 	}
> 
> 	Vector!(T,Dim) opSub(Vector!(T,Dim) a)
> 	{
> 		Vector!(T,Dim,Row) ret;
> 		for(int i = 0; i < Dim; i++)
> 		{
> 			ret[i] = storage[i] - a[i];
> 		}
> 		return ret;
> 	}
> 
> 	Vector!(T,Dim) opAddAssign(Vector!(T,Dim) a)
> 	{
> 		Vector!(T,Dim,Row) ret;
> 		for(int i = 0; i < Dim; i++)
> 		{
> 			ret[i] = storage[i] - a[i];
> 		}
> 		return ret;
> 	}
> 	/*
> 	static Vector!(T,Dim) opCall(...)
> 	{
> 		Vector!(T,Dim) v;
> 
> 		for (int i = 0; i < Dim; i++)
> 	    {
> 			if (_arguments[i] == typeid(T))
> 			{
> 			    v[i] = *cast(T *)_argptr;
> 			    _argptr += T.sizeof;
> 			}
> 			else assert(0);
> 	    }
> 	    return v;
> 	}*/
> }
> 

Sorry attached the old one on accident.


January 25, 2006
Change the second 'V' to 'v' in velocity.math.Vector and copy this code into 'main.d', then compile: dmd main.d vector.d

main.d
------
import std.stdio, velocity.math.vector;

alias Vector!(int,2) Point;
void main()
{
Point origin = Point(10,10);
writefln(origin[0],",",origin[1]);
}
------

>module velocity.math.Vector;
>
>/**************************************************
>	A fast vector implementation
>
>	Examples:
>	---
>	alias Vector!(int,2) Point;
>	Point origin = Point(0,0);
>	---
>**************************************************/
>struct Vector(T, int Dim = 4)
>{
>	private T[Dim] storage;
>
>	T opIndex(int index)
>	{
>		return storage[index];
>	}
>
>	T opIndexAssign(T value, int index)
>	{
>		storage[index] = value;
>		return value;
>	}
>
>	Vector opAdd(Vector a)
>	{
>		Vector ret;
>		for(int i = 0; i < Dim; i++)
>		{
>			ret[i] = storage[i] + a[i];
>		}
>		return ret;
>	}
>
>	Vector opSub(Vector a)
>	{
>		Vector ret;
>		for(int i = 0; i < Dim; i++)
>		{
>			ret[i] = storage[i] - a[i];
>		}
>		return ret;
>	}
>
>	Vector opAddAssign(Vector a)
>	{
>		for(int i = 0; i < Dim; i++)
>		{
>			storage[i] += a[i];
>		}
>		return *this;
>	}
>
>	Vector opSubAssign(Vector a)
>	{
>		for(int i = 0; i < Dim; i++)
>		{
>			storage[i] -= a[i];
>		}
>		return *this;
>	}
>
>	static Vector opCall(...)
>    {
>        Vector v;
> 
>        for (int i = 0; i < Dim; i++)
>        {
>            if (_arguments[i] == typeid(T))
>            {
>                v[i] = *cast(T *)_argptr;
>                _argptr += T.sizeof;
> 
>            }
>           	else assert(0);
>        }
>        return v;
>    }
>}
>
>
>--------------050605010104060809010402--


January 26, 2006
In article <dr8oqj$2nqo$1@digitaldaemon.com>, Kyle Furlong says...

>Vector!(T,Dim) opAdd(Vector!(T,Dim) a)
>{
>	Vector!(T,Dim) ret;
>	for(int i = 0; i < Dim; i++)
>	{
>		ret[i] = storage[i] + a[i];
>	}
>	return ret;
>}
>
>Which didn't compile for me. Are there cases where this syntax will work? Or is it permissible to only use the struct name for returning the struct within the struct itself?

Up until DMD version 0.143 this had to be written as:

#.Vector!(T,Dim) opAdd(.Vector!(T,Dim) a)
#{
#	.Vector!(T,Dim) ret;
#	for(int i = 0; i < Dim; i++)
#	{
#		ret[i] = storage[i] + a[i];
#	}
#	return ret;
#}

But with the latest version (0.144), the dot (.) before the template identifier
is no longer necessary. (Its meaning was to refer to the Vector template in the
outer scope rather than the Vector struct in the inner scope.)

Within this single member template, Vector refers to the same thing as
Vector!(T,Dim).

/Oskar


January 26, 2006
Oskar Linde wrote:
> In article <dr8oqj$2nqo$1@digitaldaemon.com>, Kyle Furlong says...
> 
>> Vector!(T,Dim) opAdd(Vector!(T,Dim) a)
>> {
>> 	Vector!(T,Dim) ret;
>> 	for(int i = 0; i < Dim; i++)
>> 	{
>> 		ret[i] = storage[i] + a[i];
>> 	}
>> 	return ret;
>> }
>>
>> Which didn't compile for me. Are there cases where this syntax will work? Or is it permissible to only use the struct name for returning the struct within the struct itself?
> 
> Up until DMD version 0.143 this had to be written as:
> 
> #.Vector!(T,Dim) opAdd(.Vector!(T,Dim) a)
> #{
> #	.Vector!(T,Dim) ret;
> #	for(int i = 0; i < Dim; i++)
> #	{
> #		ret[i] = storage[i] + a[i];
> #	}
> #	return ret;
> #}
> 
> But with the latest version (0.144), the dot (.) before the template identifier
> is no longer necessary. (Its meaning was to refer to the Vector template in the
> outer scope rather than the Vector struct in the inner scope.)
> 
> Within this single member template, Vector refers to the same thing as
> Vector!(T,Dim).
> 
> /Oskar
> 
> 
Thanks for the explanation.
1 2 3
Next ›   Last »