Raised This Month: $32 Target: $400
 8% 

Arrays with N Dimensions


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 01-20-2010 , 16:31   Arrays with N Dimensions
Reply With Quote #1

Introduction
Creation
Deletion
Getting
Setting
Filling
Invalid NDim Arrays
Example
Sponsors


Introduction [top]

While coding in Pawn we like to use arrays.
Code:
new First[10];
new Second[20][10];
new Third[30][20][10];
This works out just fine and dandy until we get past the third dimension.

Pawn limits scripters to only three dimensions and makes it hard to accomplish using more. The method to be discussed makes it easier on scripters.

N Dimensional (NDim for short) Arrays use two cell arrays to accomplish an infinite amount of dimensions.

NDim arrays are used much like cell arrays. (Most of the documentation is the same)
Code:
/**
 * These arrays are intended to be used for a form of global storage without 
 * limiting how many dimensions a person may need.
 * These are not designed to be used as a replacement for normal arrays, as
 * normal arrays are faster and should be used whenever possible.
 */

Creation [top]
Code:
/**
 * Creates an N Dimensional Array
 *
 * @param cellsize		The cellsize for the last dimension.
 * @param ...			Each param is the size for a dimension.
 *
 * @note				The cellsize should only be changed if working with strings or arrays.
 * @note2				NDim Arrays start zeroed.
 *
 * @return				The NDim handle.
 */
NDim:NDimCreate( const cellsize=1, ... )

Deletion [top]
Code:
/**
 * Destroys the NDim Array, and resets the handle to 0 to prevent accidental usage after it is destroyed.
 *
 * @param tracker		The array to destroy.
 */
NDimDestroy( &NDim:tracker )

Getting [top]
Code:
/**
 * Returns a single cell of data from an NDim Array.
 *
 * @param tracker		The array to retrieve the item from.
 * @param ...			Each param is the element for the corresponding dimension.
 *
 * @return				The value of the cell.
 */
any:NDimGetCell( const NDim:tracker, ... )

/**
 * Returns data within an NDim Array.  
 * Make sure the output buffer matches the size the array was created with!
 *
 * @param tracker		The array to retrieve the item from.
 * @param data			The output buffer to write.
 * @param ...			Each param is the element for the corresponding dimension.
 */
NDimGetArray( const NDim:tracker, data[], ... )

/**
 * Returns a string value from an NDim Array.
 *
 * @param tracker		The array to retrieve the item from.
 * @param output		The variable to store the value in.
 * @param size			Character size of the output buffer.
 * @param ...			Each param is the element for the corresponding dimension.
 */
NDimGetString( const NDim:tracker, output[], const size, ... )

Setting [top]
Code:
/**
 * Sets an NDim Array's single cell value.  Use this only on array that were created with a cellsize of 1!
 *
 * @param tracker		The array to set the item from within.
 * @param input			The value to set.
 * @param ...			Each param is the element for the corresponding dimension.
 */
NDimSetCell( const NDim:tracker, const any:input, ... )

/**
 * Sets an item's data with that of a local buffer.  
 * The buffer size must match what the cellsize that the array was created with!
 *
 * @param tracker		The array to set the item from within.
 * @param input			The input buffer to store.
 * @param ...			Each param is the element for the corresponding dimension.
 */
NDimSetArray( const NDim:tracker, const any:input[], ... )

/**
 * Sets a string value for an NDim Array.  
 * The stored string will be truncated if it is longer than the cellsize the array was created with!
 *
 * @param tracker		The array to set the item from within.
 * @param input			The string to set the item as.
 * @param ...			Each param is the element for the corresponding dimension.
 */
stock NDimSetString( const NDim:tracker, const input[], ... )

Filling [top]
(Remember that NDim Arrays start zeroed)
Code:
/**
 * Sets all of an NDim Array's single cell values.  Use this only on array that were created with a cellsize of 1!
 *
 * @param tracker		The array to set the item from within.
 * @param input			The value to set.
 */
NDimFillCell( const NDim:tracker, const any:input )

/**
 * Sets all items' data with that of a local buffer.  
 * The buffer size must match what the cellsize that the array was created with!
 *
 * @param tracker		The array to set the item from within.
 * @param input			The input buffer to store.
 */
NDimFillArray( const NDim:tracker, const any:input[] )

/**
 * Sets all string values for an NDim Array.  
 * The stored string will be truncated if it is longer than the cellsize the array was created with!
 *
 * @param tracker		The array to set the item from within.
 * @param input			The string to set the item as.
 */
NDimFillString( const NDim:tracker, const input[] )

Invalid NDim Array [top]
Obviously you cannot create an NDim Array with negative sizes. If you do for some reason you will be returned:
Code:
Invalid_NDim

Example [top]
Code:
//1st Dimension ranges from 0-32
//2nd Dimension ranges from 0-5
//3rd Dimension ranges from 0-32
//4th Dimension ranges from 0-19
new NDim:my_ndim_array = NDimCreate(_, 33, 6, 33, 20);

//1st Dimension: 0 <= 23 <= 32
//2nd Dimension: 0 <= 3 <= 5
//3rd Dimension: 0 <= 22 <= 32
//4th Dimension: 0 <= 10 <= 19
NDimSetCell( my_ndim_array, 50.0, 23, 3, 22, 10);

//1st Dimension: 0 <= 23 <= 32
//2nd Dimension: 0 <= 3 <= 5
//3rd Dimension: 0 <= 22 <= 32
//4th Dimension: 0 <= 10 <= 19
new Float:my_saved_value = NDimGetCell( my_ndim_array, 23, 3, 22, 10);

Sponsors [top]
This Code Snippet brought to you by the letter:
E
Attached Files
File Type: inc ndimarray.inc (7.8 KB, 431 views)

Last edited by Emp`; 01-22-2010 at 14:32. Reason: Added Example
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-20-2010 , 16:39   Re: Arrays with N Dimensions
Reply With Quote #2

Good work. You should provide an example.
__________________
Arkshine is offline
Zombiezzz
Veteran Member
Join Date: Nov 2009
Location: Nov 2009
Old 01-20-2010 , 19:50   Re: Arrays with N Dimensions
Reply With Quote #3

nice!
__________________
Zombiezzz is offline
fezh
Veteran Member
Join Date: Dec 2008
Location: BANNED
Old 01-20-2010 , 22:14   Re: Arrays with N Dimensions
Reply With Quote #4

Quote:
Originally Posted by Arkshine View Post
Good work. You should provide an example.
I don't think it's hard to understand :\
Anyway good job Emp`
__________________
"There is no knowledge, that is not power"
fezh is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 01-20-2010 , 22:19   Re: Arrays with N Dimensions
Reply With Quote #5

Nice work
__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 01-20-2010 , 23:43   Re: Arrays with N Dimensions
Reply With Quote #6

Here is a simple example:
PHP Code:
enum _:SomeData {
    
SD1,
    
SD2,
    
SD3,
};

#define MAX_OTHER_DATA 3

new g_iSomeData33 ];
new 
g_iOtherData33 ];
new 
g_iAnotherData33 ]; 
PHP Code:
new g_iArray33 ][ SomeData ][ MAX_OTHER_DATA ][ 12 ];

// ---

new iValue g_iArrayiPlayer ][ g_iSomeDataiPlayer ] ][ g_iOtherDataiPlayer ] ][ g_iAnotherDataiPlayer ] ];

// ---

g_iArrayiPlayer ][ g_iSomeDataiPlayer ] ][ g_iOtherDataiPlayer ] ][ g_iAnotherDataiPlayer ] ] = 12;

// ---

g_iArrayiPlayer ][ g_iSomeDataiPlayer ] ][ g_iOtherDataiPlayer ] ][ g_iAnotherDataiPlayer ] ]++;

// ---

ResetAllDataiPlayer ) {
     for( new 
0jkSomeDatai++ ) {
         for( 
0MAX_OTHER_DATAj++ ) {
             for( 
012k++ ) {
                 
g_iArrayiPlayer ][ ][ ][ ] = 0;
             }
         }
     }

PHP Code:
new NDim:g_iArray;

public 
plugin_init( ) {
    
g_iArray NDimCreate_33SomeDataMAX_OTHER_DATA12 );
    
NDimFillCellg_iArray);
}

public 
plugin_end( ) {
    
NDimDestroyg_iArray );
}

// ---

new iValue NDimGetCellg_iArrayiPlayerg_iSomeDataiPlayer ], g_iOtherDataiPlayer ], g_iAnotherDataiPlayer ] );

// ---

NDimSetCellg_iArray12iPlayerg_iSomeDataiPlayer ], g_iOtherDataiPlayer ], g_iAnotherDataiPlayer ] );

// ---

new iValue NDimGetCellg_iArrayiPlayerg_iSomeDataiPlayer ], g_iOtherDataiPlayer ], g_iAnotherDataiPlayer ] );
NDimSetCellg_iArray, ++iValueiPlayerg_iSomeDataiPlayer ], g_iOtherDataiPlayer ], g_iAnotherDataiPlayer ] );

// ---

ResetAllDataiPlayer ) {
     for( new 
0jkSomeDatai++ ) {
         for( 
0MAX_OTHER_DATAj++ ) {
             for( 
012k++ ) {
                 
NDimSetCellg_iArray0iPlayerij);
             }
         }
     }

Good job on this Emp`
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
xbatista
Veteran Member
Join Date: Mar 2008
Location: Lithuania
Old 01-21-2010 , 07:46   Re: Arrays with N Dimensions
Reply With Quote #7

Thanks for this!
__________________
xbatista is offline
Send a message via Skype™ to xbatista
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 01-22-2010 , 10:29   Re: Arrays with N Dimensions
Reply With Quote #8

You should include the natives you use.
SnoW is offline
Send a message via MSN to SnoW
fezh
Veteran Member
Join Date: Dec 2008
Location: BANNED
Old 01-22-2010 , 11:45   Re: Arrays with N Dimensions
Reply With Quote #9

Quote:
Originally Posted by SnoW View Post
You should include the natives you use.
Why? Any plugin won't compile if there is no #include <amxmodx>.. I mean, inside that file were included all dynamic arrays & cell related features:
Code:
#include <cellarray>
#include <celltrie>
__________________
"There is no knowledge, that is not power"
fezh is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 01-22-2010 , 11:50   Re: Arrays with N Dimensions
Reply With Quote #10

Quote:
Originally Posted by fezh View Post
Why? Any plugin won't compile if there is no #include <amxmodx>.. I mean, inside that file were included all dynamic arrays & cell related features:
Code:
#include <cellarray>
#include <celltrie>
I don't see any of those included in the Emps inc. Second that, a plugin will compile easily without amxmodx included.

Last edited by SnoW; 01-22-2010 at 12:00.
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 02:34.


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