Thread overview
Arrays
Mar 16, 2007
Patrick
Mar 16, 2007
Frits van Bommel
Mar 16, 2007
Patrick
Mar 17, 2007
Patrick
March 16, 2007
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...
March 16, 2007
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)
March 16, 2007
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)

March 16, 2007
"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.  :\


March 17, 2007
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.  :\
> 
>