PDA

View Full Version : More array dimensions - Pawn


SnoW
01-22-2010, 11:55
Emp made a solution to get more array dimensions with Arrays with N Dimensions (http://forums.alliedmods.net/showthread.php?t=116183). 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:
FirstArray[ 1 ][ 2 ][ 3 ][ 4 ] = SecondArray[ 1 ][ 2 ][ 3 ][ 4 ][ 5 ];

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.

/*
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_plugin( PLUGIN, VERSION, AUTHOR );
test;
}

test( )
{

//array4
new dArray4[ FIRST_DIMENSION ][ SECOND_DIMENSION ][ DIMENSIONS4 ] =
{
{
{ 0, 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10, 11 },
{ 12, 13, 14, 15, 16, 17 }
},
{
{ 18, 19, 20, 21, 22, 23 },
{ 24, 25, 26, 27, 28, 29 },
{ 30, 31, 32, 33, 34, 35 }
}
};

//array5
new dArray5[ FIRST_DIMENSION ][ SECOND_DIMENSION ][ DIMENSIONS5 ] =
{
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },
{ 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 },
{ 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 }
},
{
{ 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47 },
{ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 },
{ 60, 61, 62, 63, 63, 65, 66, 67, 68, 69, 70, 71 }
}
};

for( new d1; d1 < FIRST_DIMENSION; d1++ )
for( new d2; d2 < SECOND_DIMENSION; d2++ )
for( new d3; d3 < THIRD_DIMENSION; d3++ )
for( new d4; d4 < FOURTH_DIMENSION; d4++ )
{
server_print( "%i", array4[ d1 ][ d2 ][ d3 ][ d4 ] );

for( new d5; d5 < FIFTH_DIMENSION; d5++ )
{
server_print( "%i", array5[ d1 ][ d2 ][ d3 ][ d4 ][ d5 ] );
}
}

}

Exolent[jNr]
02-02-2010, 20:09
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?

SnoW
02-03-2010, 04:24
;1076568']1. What you are doing is the same thing as Emp` except your's isn't easily compatable with different dimension counts.
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.

;1076568']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.

;1076568']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.

;1076568']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.