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

3 Dimensional array using ArrayCreate()?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
AdrenalinesWrath
Junior Member
Join Date: Aug 2017
Old 12-29-2021 , 18:56   3 Dimensional array using ArrayCreate()?
Reply With Quote #1

Greetings,

I have been using the "normal" method of creating arrays for example:
Code:
new some_array[33],
some_other_array[33][128];
And I read here that it's not really good for performance specially when it comes with looping, not sure tho.

lately I started using ArrayCreate, which works great on my end, but I'm lost in ideas using it in 3D arrays, for example: new some_array[32][64][128];

the only idea i think of is creating a normal array then using it inside an "Array:" example:

Code:
new some_array[64][128];
new Array:another_array;

another_array = ArrayCreate(32,1);
ArrayPushArray(another_array,some_array[ID])
untested but, is there another method? or a better one? thanks in advance.
AdrenalinesWrath is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-29-2021 , 20:02   Re: 3 Dimensional array using ArrayCreate()?
Reply With Quote #2

A dynamic array does not support multiple dimensions. It would probably be better to understand why you think you need a 3D array. Many of the types of questions like this end up simply needing to think about the use case differently. So, what type of data are you trying to store?
__________________
fysiks is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 12-30-2021 , 06:54   Re: 3 Dimensional array using ArrayCreate()?
Reply With Quote #3

Quote:
And I read here that it's not really good for performance specially when it comes with looping, not sure tho.
Not sure what you read or where you read it, but this is completely wrong. Looping over a "normal" array or a dynamic array has the same asymptotic complexity of O(array size) because you have to step thru the entire array one cell at a time, there is no way to do it faster. Asymptotic complexity aside, using dynamic arrays is slightly slower because you have to make module calls to ArraySize and ArrayGet*/ArrayPush*, but it doesn't matter.

If you know the maximum number of elements then you use a normal array. If you do not(the number of elements can grow over time with no known upper bound) you use a dynamic array as it can grow in size. This is the only criterion to choose between a normal and a dynamic array. Performance is not a concern.

Dynamic arrays are usually implemented as "normal" arrays, but allocated dynamically on the heap(as opposed to the stack which is used for regular arrays). When the dynamic arrays runs out of space it is simply reallocated with a bigger size.
For example, it is as if you did the following(pseudocode):

PHP Code:
create array of 10 elements

//write 10 elements to array -> array is now full

//try to write another element, the array has no space for it so it grows by doubling its size
resize the array to be able to hold 2 10 20 elements
write the 
new element 
You can achieve multi-dimensional dynamic arrays, but I don't think you really need that. However, here is a brief explanation(forgive my paint skills):



(there is a smarter way to do this, by liniarising all dimensions into a single array and manually doing the math to compute the real index)
__________________

Last edited by HamletEagle; 12-30-2021 at 15:21.
HamletEagle is offline
AdrenalinesWrath
Junior Member
Join Date: Aug 2017
Old 12-30-2021 , 16:52   Re: 3 Dimensional array using ArrayCreate()?
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
...
I see now, thank you! well I had this client side configuration plugin (api) which I wrote long ago, and it had variables such as:
Code:
new SETTING_NAME[MAX_SETTINGS][64],SETTING_PLUGIN[MAX_SETTINGS][64],
SETTING_CODE[MAX_SETTINGS][16],
bool:SETTING_EDITABLE[MAX_SETTINGS],
SETTING_DEFAULT[MAX_SETTINGS][128],
SETTING_TYPE[MAX_SETTINGS][32],
SETTING_VMIN[MAX_SETTINGS],
SETTING_VMAX[MAX_SETTINGS],
SETTING_LANG[MAX_SETTINGS][32],
SETTINGS_COUNT = 0,
SETTING_VALUE_GET[MAX_SETTINGS][MAX_VALUE_NAMES][32],
SETTING_VALUE_NAME[MAX_SETTINGS][MAX_VALUE_NAMES][64],
SETTING_VALUE_LANG[MAX_SETTINGS][MAX_VALUE_NAMES][32],
SETTING_VALUE_COUNT[MAX_SETTINGS];
I'm fine with:
Code:
new SETTING_NAME[MAX_SETTINGS][64],SETTING_PLUGIN[MAX_SETTINGS][64],
SETTING_CODE[MAX_SETTINGS][16],
bool:SETTING_EDITABLE[MAX_SETTINGS],
SETTING_DEFAULT[MAX_SETTINGS][128],
SETTING_TYPE[MAX_SETTINGS][32],
SETTING_VMIN[MAX_SETTINGS],
SETTING_VMAX[MAX_SETTINGS],
SETTING_LANG[MAX_SETTINGS][32]
but I was lost in ideas at:
Code:
SETTING_VALUE_GET[MAX_SETTINGS][MAX_VALUE_NAMES][32],
SETTING_VALUE_NAME[MAX_SETTINGS][MAX_VALUE_NAMES][64],
SETTING_VALUE_LANG[MAX_SETTINGS][MAX_VALUE_NAMES][32]
which is why I needed a 3D Array using ArrayCreate();

Quote:
Originally Posted by HamletEagle View Post
...
to be honest, I don't really remember where I read it exactly. tho here: https://wiki.alliedmods.net/Array_Module_(AMX_Mod_X)
"brings fast, easy, and efficient dynamic storage into PAWN coding"

and I recall reading it somewhere in some post, and for looping I know it will do the same effect.
tho to search for something you have to loop unlike ArrayFind, maybe it's a lot better? or does it have the same effect?

the idea and information you've posted is what I needed, thank you very much!
__________________
"True Strength's keeping everything together, while everyone expects you to fall apart."
AdrenalinesWrath is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 12-31-2021 , 02:38   Re: 3 Dimensional array using ArrayCreate()?
Reply With Quote #5

"Bring fast and efficient DYNAMIC storage". As I said before, dynamic means the array is dynamically allocated on the heap and can be resized if full, to make space for more elements. Normal arrays can not be resized, this is the only difference.
The statement you quoted does not say or imply dynamic arrays are somehow magically faster than normal arrays.

ArrayFind uses a loop to find the element.
__________________

Last edited by HamletEagle; 12-31-2021 at 05:59.
HamletEagle is offline
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 06:44.


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