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

[ TUT ]Dynamic Array


Post New Thread Reply   
 
Thread Tools Display Modes
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 02-17-2017 , 08:49   Re: [ TUT ]Dynamic Array
Reply With Quote #21

Quote:
Originally Posted by HamletEagle View Post
Do you even read the answers?

Addons said:


Klippy said:


I said:


Let's try one more time:
PHP Code:
new Array:Test ArrayCreate(1
The 1 tells that you want to save single-cell data(basically numbers).

PHP Code:
new Array:Test ArrayCrete(some_other_number_here), where some_other_number_here 
This means that you will store multicell data(basically arrays). They can be strings or array of numbers.

PHP Code:
new Array:Test ArrayCreate(32
In "Test" you can save as many strings as you want, but one string can't be bigger than 32.

View the array like a list:
PHP Code:
new Array:Test ArrayCreate(1)

Translates to:
/*
var0
var1
var2
var3
.
.
.
varn
*/

new Array:Test ArrayCreate(32)
/*
var0[32]
var1[32]
var2[32]
.
.
.
.
varn[32]
*/ 

If you create an array with cellsize = 1(not of size 1), then you are creating an array of numbers. You can store as much data as you want, but data must be a number, not an array.
There is no cap.

PHP Code:
new Array:MyArray ArrayCreate(1)
for(new 
i9999i++)
{
     
ArrayPushCell(MyArrayi)

We just saved 9999 values.
Oohh, I was just confused because in Zombie Plague MerCyLeZZ used 32/64 for models, sounds etc...

Sorry I understand things a little slowly. So if I want to use integers, I can make the cellsize 1, but what if I want to use huge values like 1000000, do I have to use cellsize 7?
__________________

Last edited by edon1337; 02-17-2017 at 08:58.
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 02-17-2017 , 10:11   Re: [ TUT ]Dynamic Array
Reply With Quote #22

Quote:
Originally Posted by edon1337 View Post
Btw could you make a tutorial about Tries ? I know it exists but it's old and outdated.
It can't be outdated since the Trie API remained the same since then. It's just that you are not understanding it.

Last edited by klippy; 02-17-2017 at 10:11.
klippy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 02-17-2017 , 10:15   Re: [ TUT ]Dynamic Array
Reply With Quote #23

Hamlet's explanations and examples are more understandable :/
__________________
edon1337 is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 02-17-2017 , 12:33   Re: [ TUT ]Dynamic Array
Reply With Quote #24

Quote:
Originally Posted by edon1337 View Post
Sorry I understand things a little slowly. So if I want to use integers, I can make the cellsize 1, but what if I want to use huge values like 1000000, do I have to use cellsize 7?
No. If you set the size to 7, you can store arrays within 7 cell entries.
Code:
// Max cell entries = 7 new Array:array = ArrayCreate( 7 ) new max_array[] = { 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647 } ArrayPushArray( array, max_array )
The max value you can story in a array, or pawn variable (also known as Pawn cell) is the value of the max signed integer of 4 bytes (cellsize of 4 bytes), which is here 2.147.483.647
Code:
// Max cell entries = 1 new Array:array = ArrayCreate( 1 ) new max_interger = 2147483647 ArrayPushCell( array, max_interger )

The word cellsize is misleading, the cellsize should means how many bytes a single Pawn cell (an ordinary variable) can hold on. Here each Pawn cell has the `cellsize` of 4 bytes, which means it can hold signed integers until `2.147.483.647`.
If you still do not understand read:
  1. https://en.wikipedia.org/wiki/Integer_(computer_science)
  2. https://github.com/compuphase/pawn/raw/master/doc/Pawn_Language_Guide.pdf
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 02-17-2017 at 16:03.
addons_zz is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-17-2017 , 15:10   Re: [ TUT ]Dynamic Array
Reply With Quote #25

Note: you have built-in constants for that in the compiler: 'cellmin' and 'cellmax'.
__________________
Arkshine is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 02-17-2017 , 15:22   Re: [ TUT ]Dynamic Array
Reply With Quote #26

Quote:
Originally Posted by addons_zz View Post
No. If you set the size to 7, you can store arrays within 7 cell entries.
Code:
// Max cell entries = 7 new Array:array = ArrayCreate( 7 ) new max_array[] = { 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647 } ArrayPushArray( array, max_array )
The max value you can story in a array, or pawn variable (also known as Pawn cell) is the value of the max signed integer of 4 bytes (cellsize of 4 bytes), which is here 2.147.483.647
Code:
// Max cell entries = 1 new Array:array = ArrayCreate( 1 ) new max_interger = 2147483647 ArrayPushCell( array, max_interger )

The word cellsize is misleading, the cellsize should means how many bytes a single Pawn cell (an ordinary variable) can hold on. Here each Pawn cell has the `cellsize` of 4 bytes, which means it can hold signed integers until `2.147.483.647`.
If you still do not understand read:
  1. https://en.wikipedia.org/wiki/Integer_(computer_science)
  2. https://github.com/compuphase/pawn/raw/master/doc/Pawn_Language_Guide.pdf
These examples confused me even more.. Why did you use ArrayPushArray while Hamlet used ArrayPushCell? And why is the size 1 in Hamlet's example and not 10
PHP Code:
#include < amxmodx >

//Create a new variable, it's tagged as Array, so it's for a dyn array
new Array: g_aArray

public plugin_init( )
{
    
register_plugin"CellArray Example""0.1""HamletEagle" )
    
        
//Create our array
    
g_aArray ArrayCreate)
    
    
register_srvcmd"cellarray_writetest""ServerCommand_WriteToArray" )
    
register_srvcmd"cellarray_readtest""ServerCommand_ReadFromArray" )
}

public 
plugin_end( )
{
        
//Destroy our array
    
ArrayDestroyg_aArray )
}

public 
ServerCommand_WriteToArray( )
{
    
//A constant to store some numbers
    
new szConst[ ] = { 012345678}
    
    
//Create a for loop
    
for( new isizeof szConsti++ )
    {
        
//Add everything to the array
        
ArrayPushCellg_aArrayszConst] )
    }
}

public 
ServerCommand_ReadFromArray( )
{
    
//A for loop from 0 to arraysize
    
for( new iArraySizeg_aArray ); i++ )
    {
        
//Print what's stored into dyn array
        
server_print"Number: %i"ArrayGetCellg_aArray) )
    }

__________________
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 02-17-2017 , 15:32   Re: [ TUT ]Dynamic Array
Reply With Quote #27

I told you before, stop seeing things as a whole and analyze them piece by piece.
I'm pushing the numbers one by one, so I'm saving single-cell data. Here you have a dynamic array of numbers.
addons is saving the entire array once, he has multi-cell data. Here you have a dynamic array of arrays.

My snippet translates to:
Code:
0
1
2
3
4
5
6
7
8
9

//the dynamic array has 10 elements
"cellsize" param is 1 because, again, we are saving numbers.

addon's snippet translates to:
Code:
{2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647}

//the dynamic array has 1 element(only one array)
"cellsize" param is 7 because that's the size of the array that you will save.

Another example:
PHP Code:
new Array1[] = {01,3}
new 
Array2[] = {4567}
new 
Array3[] = {891011}

new Array:
Test ArrayCreate(4//4 because that's the array size.
ArrayPushArray(TestArray1)
ArrayPushArray(TestArray2)
ArrayPushArray(TestArray3
It "looks" like:
Code:
{0, 1, 2, 3}
{4, 5, 6, 7}
{8, 9, 10, 11}

//array has 3 elements.
We have 12 numbers, but they are organized in arrays, and each array is pushed into the dynamic array.
__________________

Last edited by HamletEagle; 02-17-2017 at 15:34.
HamletEagle is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-17-2017 , 23:27   Re: [ TUT ]Dynamic Array
Reply With Quote #28

tl;dr

Another way to think of it is that "dynamic arrays" are only dynamic in the first dimension. The second dimension must be defined statically.
__________________

Last edited by fysiks; 02-17-2017 at 23:27.
fysiks is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 02-18-2017 , 05:10   Re: [ TUT ]Dynamic Array
Reply With Quote #29

Quote:
Originally Posted by HamletEagle View Post
I told you before, stop seeing things as a whole and analyze them piece by piece.
I'm pushing the numbers one by one, so I'm saving single-cell data. Here you have a dynamic array of numbers.
addons is saving the entire array once, he has multi-cell data. Here you have a dynamic array of arrays.

My snippet translates to:
Code:
0
1
2
3
4
5
6
7
8
9

//the dynamic array has 10 elements
"cellsize" param is 1 because, again, we are saving numbers.

addon's snippet translates to:
Code:
{2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647}

//the dynamic array has 1 element(only one array)
"cellsize" param is 7 because that's the size of the array that you will save.

Another example:
PHP Code:
new Array1[] = {01,3}
new 
Array2[] = {4567}
new 
Array3[] = {891011}

new Array:
Test ArrayCreate(4//4 because that's the array size.
ArrayPushArray(TestArray1)
ArrayPushArray(TestArray2)
ArrayPushArray(TestArray3
It "looks" like:
Code:
{0, 1, 2, 3}
{4, 5, 6, 7}
{8, 9, 10, 11}

//array has 3 elements.
We have 12 numbers, but they are organized in arrays, and each array is pushed into the dynamic array.
Thank you! I just didn't notice the loop you made. Now I understand it. Thanks again.
__________________
edon1337 is offline
Kotoamatsukami
Member
Join Date: Jan 2017
Location: Malaysia
Old 02-18-2017 , 10:23   Re: [ TUT ]Dynamic Array
Reply With Quote #30

Hello, I have a question.
It is possible to insert a data using normal array then use ArrayPushArray to push the normal array into a dynamic array as the storage? As dynamic array is slower than normal array when inserting data.
As far as I understand from this topic, normal arrays are faster at inserting while dynamic arrays are faster at retrieving. So, is it possible to use normal array for data insertion?
Kotoamatsukami is offline
Reply


Thread Tools
Display Modes

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 05:00.


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