Resizing two dimensional arrays?

BlitzMax Forums/BlitzMax Programming/Resizing two dimensional arrays?

*(Posted 2012) [#1]
Is it possible to resize two dimensional arrays, im using an array for a map editor but just was wondering if its possible?


Mahan(Posted 2012) [#2]
Do you mean [size_a, size_b] or do you mean jagged [size_a][size_b] style 2d-arrays?

Regardless it should be quite easy/trivial to write an own function to resize by allocating a new array and then copy data to it from the old in the pattern you like.


*(Posted 2012) [#3]
I mean [ a, b ]

the reason I ask is quite simply I wrote a function to do it and the function is fine BUT the array doesnt get resized I get 'array out of bounds error' going off the side of the original array size.


Mahan(Posted 2012) [#4]
It was long since I was playing with BMX so bear with my code, but this still looks like it's working as expected:




*(Posted 2012) [#5]
Ah thanks Mahan didnt know you could return arrays like that :) Works a treat :D


Mahan(Posted 2012) [#6]
Me neither, but if you can imagine it -> try it! :)

BlitzMax is amazing tbh.


col(Posted 2012) [#7]
Just for your notes...

passing in an array in this way passes in the entire array structure, which may cause a slight slowdown if you're passing a large array frequently to a function ( not in your example ) due to moving and allocating memory/data etc. You can pass an array by using 'arrayname:Int[,] Var', for example, to just pass an Int pointer instead.

Have fun.


Mahan(Posted 2012) [#8]
EDIT: Did some testing:



You can comment the line 'arrPasser(arr)' or the line 'lolPasser(lol)' interchangeably to get a estimated count of the number of calls needed to stack overflow.

So on second thought it seems you might be wrong, col. It's simply not possible that I'm passing a ~4Mb array on the stack almost 300k times on this netbook :)

Last edited 2012


*(Posted 2012) [#9]
Type testType
	Field AVar:Int = 0
End Type

Global arr:testType[10, 10]

'Fill array with some junk
For Local i:Int = 0 Until arr.dimensions()[0]
	For Local j:Int = 0 Until arr.dimensions()[1]
		arr[ i, j ] = New testType
		arr[i, j].AVar = j
	Next
Next

Print "size X is now: " + arr.dimensions()[0]
Print "size Y is now: " + arr.dimensions()[1]
Print "Sum of contents is: " + sum2DIntArray(arr)

Print "resizing now ..."
arr = resize2DIntArray(arr, 20, 20)

Print "size X is now: " + arr.dimensions()[0]
Print "size Y is now: " + arr.dimensions()[1]
Print "Sum of contents is: " + sum2DIntArray(arr)
Print "done."
End

Function resize2DIntArray:testType[,] (arr:testType[,], newSizeX:Int, newSizeY:Int)
	Local sx:Int = Min(newSizeX, arr.dimensions()[0])
	Local sy:Int = Min(newSizeY, arr.dimensions()[1])
	Local newArr:testType[,] = New testType[newSizeX, newSizeY]
	For Local x:Int = 0 Until newArr.dimensions()[0]
		For Local y:Int = 0 Until newArr.dimensions()[1]
			'new type MUST be initialised to stop 'null' variable errors.
			newArr[ x, y ] = New testType
			
			'and it has to have a value set
			If x<arr.dimensions()[0] And y<arr.dimensions()[1]
				newArr[x, y] = arr[x, y]
			Else
				newArr[ x, y ].AVar = 0
			EndIf
		Next
	Next
	Return newArr
End Function


Function sum2DIntArray:Int(arr:testType[,])
	Local r:Int
	For Local x:Int = 0 Until arr.dimensions()[0]
		For Local y:Int = 0 Until arr.dimensions()[1]
			If arr[ x, y ]<>Null
				r = r + arr[x, y].AVar
			EndIf
		Next
	Next
	Return r
End Function


Heres the code I came up with for resizing type arrays, thanks to Mahan for the help I think I understand now :D

Last edited 2012


Mahan(Posted 2012) [#10]
It would seem so.

I found this statement in the "BASIC Compatibility"-page in BMX help:

"Instead, arrays in BlitzMax are real types - they can be passed to functions, returned from functions and placed in variables."

So it would seem like arrays are objects in BMX but with some special quirks like returning the size of the contained elements when you do a SizeOf(arr_ref) instead of the size of the ref itself, for instance.


*(Posted 2012) [#11]
Well I have posted some code in my last post that clears it up, thanks Mahan for ya help :)


Mahan(Posted 2012) [#12]
I don't think you need the testType at all, EdzUp[GD], but rather stick with my first example. BMX handles the array passing to functions like a ref anyways as far as I can tell.


col(Posted 2012) [#13]
Well I never :-)

Crashes in my machine at around 150000, but I get your point, it just too fast to be not be passed in by ref especially when upped to 100000,100000 :D

Thanks for the tests. Learn something new every day eh.
I was told that was the case ( what I quoted ), and I took it as true. Maybe it was at the time and its been changed, anyway, its good to know, and sorry for the 'not so bad' bad advice.

I'll now go and crawl into my little corner and carry on... :D

Last edited 2012