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

Array initialization formatting

 
Post new topic   Reply to topic     Forum Index -> Descent
View previous topic :: View next topic  
Author Message
kihjin



Joined: 28 Dec 2006
Posts: 18

PostPosted: Sat Oct 06, 2007 2:22 pm    Post subject: Array initialization formatting Reply with quote

Hi,

I've got several "large" multidimensional array initializations and I haven't been able to find a line wrapping setting which would produce something decent (please forgive the pun).

Do not wrap:
Code:
   static uint
               index[20][3] = [[0, 4, 1], [0, 9, 4], [9, 5, 4], [4, 5, 8], [4, 8, 1], [8, 10, 1], [8, 3, 10], [5, 3, 8], [5, 2, 3], [2, 7, 3], [7, 10, 3], [7, 6, 10], [7, 11, 6], [11, 0, 6], [0, 1, 6], [6, 1, 10], [9, 0, 11], [9, 11, 2], [9, 2, 5], [7, 2, 11]];


Wrap and indent (best, but there are issues):
Code:
   static float data[12][3] = [[-X, 0.0, Z], [X, 0.0, Z], [-X, 0.0, -Z], [
         X, 0.0, -Z], [0.0, Z, X], [0.0, Z, -X], [0.0, -Z, X],
         [0.0, -Z, -X], [Z, X, 0.0], [-Z, X, 0.0], [Z, -X, 0.0], [-Z, -X,
               0.0]];

   static uint index[20][3] = [[0, 4, 1], [0, 9, 4], [9, 5, 4],
         [4, 5, 8], [4, 8, 1], [8, 10, 1], [8, 3, 10], [5, 3, 8], [5, 2, 3],
         [2, 7, 3], [7, 10, 3], [7, 6, 10], [7, 11, 6], [11, 0, 6],
         [0, 1, 6], [6, 1, 10], [9, 0, 11], [9, 11, 2], [9, 2, 5],
         [7, 2, 11]];


Here the inner array atoms are often split, which is not readable.

Wrap to column:
Code:
   static uint
               index[20][3] = [[0, 4, 1], [0, 9, 4], [9, 5, 4], [4, 5,
                                                         8],
                              [4, 8, 1], [8, 10, 1], [8, 3, 10], [5,
                                                         3,
                                                         8],
                              [5, 2, 3], [2, 7, 3], [7, 10, 3],
                              [7, 6, 10], [7, 11, 6], [11, 0, 6],
                              [0, 1, 6], [6, 1, 10], [9, 0, 11], [9,
                                                         11,
                                                         2],
                              [9, 2, 5], [7, 2, 11]];



Finally, one fragment per line...

Code:
   static float data[12][3] = [   [   -X,
                              0.0,
                              Z],
                           [   X,
                              0.0,
                              Z],
                           [   -X,
                              0.0,
                              -Z],
                           [   X,
                              0.0,
                              -Z],
                           [   0.0,
                              Z,
                              X],
                           [   0.0,
                              Z,
                              -X],
                           [   0.0,
                              -Z,
                              X],
                           [   0.0,
                              -Z,
                              -X],
                           [   Z,
                              X,
                              0.0],
                           [   -Z,
                              X,
                              0.0],
                           [   Z,
                              -X,
                              0.0],
                           [   -Z,
                              -X,
                              0.0]];


This one does have the added benefit of multiplying a project's line
count by a factor of 100, but is otherwise insufficient.

Ideally, I would like to format my arrays as such:

Code:
   static uint array = [
         [1,2,3],
         [4,5,6],
         [7,8,9],
         [10,11,12]
   ];


Alternatively, "Wrap and indent" would be good if it didn't have the
habit of splitting inner array elements.

For now, I've found that I can hack my arrays into a somewhat pleasing format using comments:

Code:
   static float data[12][3] = [
         //----------------------------------------
         [-X, 0.0, Z], [X, 0.0, Z], [-X, 0.0, -Z], [X, 0.0, -Z],
         [0.0, Z, X], [0.0, Z, -X], [0.0, -Z, X], [0.0, -Z, -X],
         [Z, X, 0.0], [-Z, X, 0.0], [Z, -X, 0.0], [-Z, -X, 0.0]];
   static uint index[20][3] = [
         //----------------------------------------
         [0, 4, 1], [0, 9, 4], [9, 5, 4], [4, 5, 8], [4, 8, 1], [8, 10, 1],
         [8, 3, 10], [5, 3, 8], [5, 2, 3], [2, 7, 3], [7, 10, 3],
         [7, 6, 10], [7, 11, 6], [11, 0, 6], [0, 1, 6], [6, 1, 10], //
         [9, 0, 11], [9, 11, 2], [9, 2, 5], [7, 2, 11]];


This seems to work OK when using the "Wrap and indent" option.
Back to top
View user's profile Send private message
fraserofthenight



Joined: 08 Apr 2007
Posts: 33
Location: Seattle, WA

PostPosted: Sun Oct 07, 2007 5:08 pm    Post subject: Reply with quote

The formatter isn't perfect, and there's really no easy way to tell it to wrap some array elements but not others. It would be possible to say "in a multidimensional array, only wrap outer elements," but this would be an arbitrary rule that doesn't work right for all arrays.

Can you think of a good way to do this in a general case?
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
kihjin



Joined: 28 Dec 2006
Posts: 18

PostPosted: Sun Oct 07, 2007 5:23 pm    Post subject: Reply with quote

The formatter can't be perfect, but I figured if I didn't say anything then perhaps the issue might not be considered at all, whether a good solution actually exists or not.

The array initialization would need to search for contained array initializations (or analyze the declared type and "know" that it is multidimensional). In D's case, array initializations would begin with [, so the array initialization formatter could be a recursive operation which locates a [, and formats it much in the same way it would format a single array initialization would be.

However I've done no work with Descent's code, thus I don't know how nontrivial or inefficient such a method would be.
Back to top
View user's profile Send private message
asterite



Joined: 01 Jun 2006
Posts: 235
Location: Buenos Aires, Argentina

PostPosted: Mon Oct 08, 2007 5:14 am    Post subject: Reply with quote

The formatter is pretty powerful, and it works on the AST, not on the source code. In that way it's very easy to see if an array initialization is made of other array intiailizations (just check the type of the children).

I'll try to see if I can get nice results with two dimensional arrays.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
asterite



Joined: 01 Jun 2006
Posts: 235
Location: Buenos Aires, Argentina

PostPosted: Tue Oct 09, 2007 5:26 am    Post subject: Reply with quote

It's done. If you'd like to test it, grab the latest SVN copy. Try a minimal example, since you may get errors because the semantic analysis is not finished.

See if it's ok like it is now, or what should be changed.

Regards,
Ary
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Descent 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