Raised This Month: $51 Target: $400
 12% 

More array dimensions - Pawn


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 01-22-2010 , 11:55   More array dimensions - Pawn
Reply With Quote #1

Emp made a solution to get more array dimensions with Arrays with N Dimensions. He used module memory which first is slower and second makes a lot of limits, so I wrote up you an example how to do this in only pawn code.

I also kept the same structure which normal arrays have, so you can use any similiar ways to call the variables:
PHP Code:
FirstArray][ ][ ][ ] = SecondArray][ ][ ][ ][ ]; 
In the example you only see fourth and fifth dimensions but you can use this up to 10 dimensions with the same formula.

Thing more to note is that you can came up with a bug which appears with increment and decrement operators. It comes when you use these operators as an expression with a cell of an array which is passed by defining with both pre and post modes. What the bug does is that the expression returns a wrong value and the variable isn't increased or decremented. You may somehow use the increment and decrement operators as statement without worries.

PHP Code:
/*
     Plugin to test fourth and fifth array dimensions.
*/
#include <amxmodx>
 
#define PLUGIN "More array dimensions - Pawn"
#define VERSION "1.0"
#define AUTHOR "SnoW"
 
#define FIRST_DIMENSION 2
#define SECOND_DIMENSION 3
#define THIRD_DIMENSION 2
#define FOURTH_DIMENSION 3
#define FIFTH_DIMENSION 2
 
#define DIMENSIONS4 THIRD_DIMENSION * FOURTH_DIMENSION
#define DIMENSIONS5 DIMENSIONS4 * FIFTH_DIMENSION
#define array4[%1][%2][%3][%4] dArray4[ %1 ][ %2 ][ FOURTH_DIMENSION * %3 + %4 ]
#define array5[%1][%2][%3][%4][%5] dArray5[ %1 ][ %2 ][ ( FOURTH_DIMENSION * %3 + %4 ) * FIFTH_DIMENSION + %5 ]
 
public plugin_init( )
{
     
register_pluginPLUGINVERSIONAUTHOR );
     
test;
}
 
test( )
{
 
     
//array4
     
new dArray4FIRST_DIMENSION ][ SECOND_DIMENSION ][ DIMENSIONS4 ] =
     {
          {
               { 
01234},
               { 
67891011 },
               { 
121314151617 }
          },
          {
               { 
181920212223 },
               { 
242526272829 },
               { 
303132333435 }
          }
     };
 
     
//array5
     
new dArray5FIRST_DIMENSION ][ SECOND_DIMENSION ][ DIMENSIONS5 ] =
     {
          {
               { 
01234567891011 },
               { 
121314151617181920212223 },
               { 
242526272829303132333435 }
          },
          {
               { 
363738394041424344454647 },
               { 
484950515253545556575859 },
               { 
606162636365666768697071 }
          }
     };     
 
     for( new 
d1d1 FIRST_DIMENSIONd1++ )
     for( new 
d2d2 SECOND_DIMENSIONd2++ )
     for( new 
d3d3 THIRD_DIMENSIONd3++ )
     for( new 
d4d4 FOURTH_DIMENSIONd4++ )
     {
          
server_print"%i"array4d1 ][ d2 ][ d3 ][ d4 ] );
 
          for( new 
d5d5 FIFTH_DIMENSIONd5++ )
          {
               
server_print"%i"array5d1 ][ d2 ][ d3 ][ d4 ][ d5 ] );
          }
     }
 

SnoW is offline
Send a message via MSN to SnoW
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-02-2010 , 20:09   Re: More array dimensions - Pawn
Reply With Quote #2

1. What you are doing is the same thing as Emp` except your's isn't easily compatable with different dimension counts.
You barely explain how to use this or what to do if you want more than 5.
You just post a code snippet and say good luck.

2. Why do you create 3 dimensions instead of only 1?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-03-2010 , 04:24   Re: More array dimensions - Pawn
Reply With Quote #3

Quote:
Originally Posted by Exolent[jNr] View Post
1. What you are doing is the same thing as Emp` except your's isn't easily compatable with different dimension counts.
Quote:
Originally Posted by SnoW View Post
He used module memory which first is slower and second makes a lot of limits, so I wrote up you an example how to do this in only pawn code.

I also kept the same structure which normal arrays have...
As other way said, the same thing expect much more faster and easier to use? Definitely.

Obviously declaring the variable is little harder and takes more code, but when you don't need to learn a bunch of new function names, even without counting the module calls, this is far more better for my opinion.

Quote:
Originally Posted by Exolent[jNr] View Post
You barely explain how to use this or what to do if you want more than 5.
I can't imagine a situation to need more than 5 dimensions. The basic formula of arrays is very simple if you've any knowledge in maths. I guess I could write it down to the first post though.

Quote:
Originally Posted by Exolent[jNr] View Post
You just post a code snippet and say good luck.
If you actually read the snippet, you understand that it defines only a new form which works as a pointer to the real cell in the array. Like that there's no usage to explain when I don't want to create a new tutorial for normal pawn arrays.

The source code speaks for itself if the reader is any more than an average coder since in the end this is a simple thing.

I'm able to write a better description and maybe an other example, but I counted that if someone is interested and don't know how to do this, they will reply here. Somehow the only post is yours and I believe you understand the code, so if no one is interested I don't do work for nothing.

Quote:
Originally Posted by Exolent[jNr] View Post
2. Why do you create 3 dimensions instead of only 1?
You are referring to create all the fake dimensions to one real dimension. I don't see any reason to do that especially when it's much slower in any situation you are working with the array.

Last edited by SnoW; 02-03-2010 at 04:32.
SnoW is offline
Send a message via MSN to SnoW
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode