www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Arrays

reply Patrick <dude dude.com> writes:
I have a list of vectors and I want to be able to add a vector through a
function like this:
 
float[3] giveVector();
-----
float[][3] vectorList;
vectorList ~= giveVector();
-----

Any ideas on how to implement this? It doesnt really feel right to wrap the
vector up in a class or struct...
Mar 16 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Patrick wrote:
 I have a list of vectors and I want to be able to add a vector through a
function like this:
  
 float[3] giveVector();
 -----
 float[][3] vectorList;
 vectorList ~= giveVector();
 -----
 
 Any ideas on how to implement this? It doesnt really feel right to wrap the
vector up in a class or struct...

Why would wrapping it in a simple struct not "feel right"? That's how vectors are typically implemented in D, judging by posts in these newsgroups. (Hint: you may want to search these groups to see how others have implemented vectors & matrices to avoid reinventing the wheel for the Nth time)
Mar 16 2007
parent reply Patrick <spam spam.at> writes:
A struct which only contains one array of floats:
struct Vector {
   float[3] containingElements; 
}
I may be too finicky but it really seems like codebloat to me. But if there
arent any other solutions...

Frits van Bommel Wrote:

 Patrick wrote:
 I have a list of vectors and I want to be able to add a vector through a
function like this:
  
 float[3] giveVector();
 -----
 float[][3] vectorList;
 vectorList ~= giveVector();
 -----
 
 Any ideas on how to implement this? It doesnt really feel right to wrap the
vector up in a class or struct...

Why would wrapping it in a simple struct not "feel right"? That's how vectors are typically implemented in D, judging by posts in these newsgroups. (Hint: you may want to search these groups to see how others have implemented vectors & matrices to avoid reinventing the wheel for the Nth time)

Mar 16 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Patrick" <spam spam.at> wrote in message 
news:etevk0$1cu6$1 digitalmars.com...
A struct which only contains one array of floats:
 struct Vector {
   float[3] containingElements;
 }
 I may be too finicky but it really seems like codebloat to me. But if 
 there arent any other solutions...

If it makes you feel any better, that won't change the size or behavior of the vector at all. It'll still be 12 bytes and passed by value. But the nice thing is that you can then add methods to the vector struct. :\
Mar 16 2007
parent Patrick <spam spam.at> writes:
Thanks! I was just now thinking about that.

Jarrett Billingsley Wrote:

 "Patrick" <spam spam.at> wrote in message 
 news:etevk0$1cu6$1 digitalmars.com...
A struct which only contains one array of floats:
 struct Vector {
   float[3] containingElements;
 }
 I may be too finicky but it really seems like codebloat to me. But if 
 there arent any other solutions...

If it makes you feel any better, that won't change the size or behavior of the vector at all. It'll still be 12 bytes and passed by value. But the nice thing is that you can then add methods to the vector struct. :\

Mar 17 2007