Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Multidimensional dynamic arrays

Author
Message
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 23rd Jul 2012 14:58
Hey guys


My problem is that when I have multidimensional arrays, I can't use array insert at bottom to expand it's size when needed. I have to define a "maximum size", but that's exactly what I want to avoid.

How can I fix this?

The documentation says that I can optionally pass an index when using the command, but that doesn't seem to do anything.

array insert at bottom <array> , <index> <-- What is that?

TheComet

Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 23rd Jul 2012 16:32 Edited at: 23rd Jul 2012 17:27
The index parameter will probably insert a new item at that index instead of the end of the array, but I am not sure about this.
If you need (multidimensional) dynamic arrays I can upload my custom array system.

edit
just did a litle test, you can use this to resize the array:


Sergey K
20
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 23rd Jul 2012 18:36
in dbp, multiply arrays been always bugged.
but we learned to go around it using another array.

this way u can do:
dim MyArray(X,Y)
and convert it to:
Dim MyArrayX(X)
Dim MyArrayY(Y)

or in code sample:
type TMainArray
SubArray as Integer
Var as String
endtype
type TSubArray
MainArray as Integer
Var as string
endtype

dim MainArray(3) as TMainArray
dim SubArray(2) as TSubArray

MainArray(0).Var = "Hello"
MainArray(0).SubArray = 0

MainArray(1).Var = "Hello"
MainArray(1).SubArray = 1

MainArray(2).Var = "Hello"
MainArray(2).SubArray = 2

and so on..

Advanced Updater for your games!
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th Jul 2012 01:51
@ Max P

I am very interested in your custom array system. Is it fast? Can it allow UDTs to have arrays? Is it safe?

I tried your resize method and found that most of the data is lost upon resizing it. It would be possible to copy the array into a temporary array, resize, then copy it back into the original. I think that's the method I'll go for if everything else fails.

@ Sergey K

Thanks for the suggestion, but there's a big flaw with it. The total size of a multidimensional array can be calculated by Dim1 * Dim2 * TypeSize:



With your method however it's calculated by (Dim1 + Dim2) * TypeSize because it's essentially just 2 arrays, so you have to add them together.

The point I'm making is that your solution doesn't have a way to get this:

MyArray(4,7)

Your solution only works if both numbers are the same.

TheComet

Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 24th Jul 2012 03:24 Edited at: 24th Jul 2012 03:27
You can just redim the array to change its size. To correctly preserve the contents you need to dim a new array, copy over the elements you want to keep and then get rid of the old one. You can do this very easily with the link/unlink array commands in IanM's plugins.

To improve the speed you can leave some redundency instead of resizing every time. If for example you double the total number of elements in the array each time you run out of space you will get better results.

[b]
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 24th Jul 2012 13:06 Edited at: 24th Jul 2012 13:14
@TheComet
My array system is safe, only removing items isn't added yet.
The system works like this:


I just did a speed test for you with this code:


Default array: 641 ms
Custom array: 929 ms

For custom types you can use this:


edit
as long as you don't delete items/arrays with string in them everything works perfectly. When you delete an item with a string the memory for the string won't be deleted. This can be easily fixed, but it will be a bit slower. Because I don't use strings a lot I didn't fix it yet. If you want to prevent this you can delete the strings yourself before clearing the array using clearArrayItem$(array, index), this will delete the content of the string.

edit 2
I will change a few things and then upload the system

Sergey K
20
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 24th Jul 2012 13:25
@TheComent: wait till i get home, i will tell ya exactly what i meant.

Advanced Updater for your games!
Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 24th Jul 2012 15:04 Edited at: 24th Jul 2012 15:10
Here is the full array system:

I have added a few things:
- strings will now be deleted, no problems there
- you can delete array items
- added safe functions: Safe functions will give an error message when you try to acces an array that doesn't exist, an index that doesn't exist or when you try to get a wrong datatype from the array

Here is a simple speed test:


As you can see my arrays are slower at set/get, but faster at resizing.
Also my arrays use an id, so you can pass the id to a function and edit the array from inside the function, for example to sort the array.
You can also use it to keep track of limbs, childs, etc.


Attachments

Login to view attachments
Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 24th Jul 2012 21:33
Dimming a multidimensional array to resize it does not lose its data.



[img][/img]


WindowsXP SP3,Vista,Windows 7 SP1, DBpro v7.7RC7
Stab In The Dark Editor
The coffee is lovely dark and deep,and I have code to write before I sleep.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 25th Jul 2012 15:55 Edited at: 25th Jul 2012 17:40
Quote: "Dimming a multidimensional array to resize it does not lose its data."


Huh, so it doesn't. In that case I've found an easy solution to my problem, assuming that dimming an already existing array doesn't cause any weird memory leaks.

Thank you all for the help and thanks Max P for that!

TheComet

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 26th Jul 2012 04:50
Dimming an already existing array does delete the data. Here, see for yourself:



TheComet

Max P
14
Years of Service
User Offline
Joined: 23rd Jan 2010
Location:
Posted: 26th Jul 2012 12:37
It works when you only have 2 dimensions:


Apparently not with 3...
I guess you either have to create a static array or use my (slower) array system (4 posts up, in case you missed it)

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 26th Jul 2012 12:49
That's why I'd love to see a confirmation that dimming an existing array is safe.

And yep, I did see your array system, thanks a lot for posting that!

TheComet

Login to post a reply

Server time is: 2024-04-20 11:09:24
Your offset time is: 2024-04-20 11:09:24