FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

2d arrays

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
Eelco



Joined: 28 Jun 2004
Posts: 10
Location: Netherlands

PostPosted: Tue Aug 03, 2004 8:59 am    Post subject: 2d arrays Reply with quote

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
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Tue Aug 03, 2004 8:20 pm    Post subject: Re: 2d arrays Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger
Eelco



Joined: 28 Jun 2004
Posts: 10
Location: Netherlands

PostPosted: Wed Aug 04, 2004 7:01 am    Post subject: Reply with quote

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
View user's profile Send private message
Joey



Joined: 10 Sep 2004
Posts: 5
Location: Europe, Holland

PostPosted: Fri Sep 10, 2004 8:53 am    Post subject: Reply with quote

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. Wink[/code]
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group