AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Arrays with N Dimensions (https://forums.alliedmods.net/showthread.php?t=116183)

Emp` 01-20-2010 16:31

Arrays with N Dimensions
 
2 Attachment(s)
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

Arkshine 01-20-2010 16:39

Re: Arrays with N Dimensions
 
Good work. You should provide an example.

Zombiezzz 01-20-2010 19:50

Re: Arrays with N Dimensions
 
nice!

fezh 01-20-2010 22:14

Re: Arrays with N Dimensions
 
Quote:

Originally Posted by Arkshine (Post 1061619)
Good work. You should provide an example.

I don't think it's hard to understand :\
Anyway good job Emp`:up:

AntiBots 01-20-2010 22:19

Re: Arrays with N Dimensions
 
Nice work :D

Exolent[jNr] 01-20-2010 23:43

Re: Arrays with N Dimensions
 
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` :)

xbatista 01-21-2010 07:46

Re: Arrays with N Dimensions
 
Thanks for this!

SnoW 01-22-2010 10:29

Re: Arrays with N Dimensions
 
You should include the natives you use.

fezh 01-22-2010 11:45

Re: Arrays with N Dimensions
 
Quote:

Originally Posted by SnoW (Post 1063357)
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>

:mrgreen:

SnoW 01-22-2010 11:50

Re: Arrays with N Dimensions
 
Quote:

Originally Posted by fezh (Post 1063424)
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>

:mrgreen:

I don't see any of those included in the Emps inc. Second that, a plugin will compile easily without amxmodx included.


All times are GMT -4. The time now is 11:44.

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