View previous topic :: View next topic |
Author |
Message |
Eelco
Joined: 28 Jun 2004 Posts: 10 Location: Netherlands
|
Posted: Tue Aug 03, 2004 8:59 am Post subject: 2d arrays |
|
|
is there an elegant way in D to declare a non-static multidimensional array, and instantiate it later?
if so i havnt seen it.
Code: |
Quad[][] Map;
void CreateMap(ubyte w, ubyte h){
ushort off;
byte i, j;
Map.length = w;
for (i = 0; i<w; i++) Map[i].length = h;
|
this is what im doing now, and i think its completely unelegant. |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Tue Aug 03, 2004 8:20 pm Post subject: Re: 2d arrays |
|
|
I don't know if this is quite what you're after, but you might try something like:
Code: |
Quad[][] Map;
void CreateMap(ubyte w, ubyte h) {
ushort off;
byte i, j;
Map = new Quad[][w];
foreach (inout Quad[] elem; Map)
elem = new Quad[h];
// ...
}
|
It would be nice if D supported something like 'Map = new Quad[h][w]' but at the moment it does not. If you try it, you'll get an error about being unable to cast a Quad[h][] to a Quad[][]... Maybe this should get put on the wish list. _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
Eelco
Joined: 28 Jun 2004 Posts: 10 Location: Netherlands
|
Posted: Wed Aug 04, 2004 7:01 am Post subject: |
|
|
thanks, but thats hardly more elegant, just written another way.
but this is really bad... there are a lot of things i really like about D, but when it missed such a trivial function like this i find it hard to take it seriously.
arrays of pointers to arrays have their uses, but i dont want them in this instance, certainly not when they force me to produce ugly code like this. ill just do it c-style then.. |
|
Back to top |
|
|
Joey
Joined: 10 Sep 2004 Posts: 5 Location: Europe, Holland
|
Posted: Fri Sep 10, 2004 8:53 am Post subject: |
|
|
Seriously, you shouldn't make global arrays just like that.
Code: |
class Map(T) {
public:
T data[][];
thig() {}
this(int w = 50, int h = 50) {
data = new T[][w];
foreach(inout T[] elem; data)
elem = new T[h];
}
// overload some operators here
}
public alias Map!(Quad) QuadMap;
QuadMap myMap = new QuadMap(w, h)...;
|
Or whatever. Power to the oo. [/code] |
|
Back to top |
|
|
|