Types - still confused

Blitz3D Forums/Blitz3D Beginners Area/Types - still confused

jfk EO-11110(Posted 2005) [#1]
Well, this is my coming out. I confess, I avoid to use types whenever I can, but lately I was trying to do something with Type Structs that should be accessible with a 3 dimensional Array as Identifiers.

I got an array, like

dim arr(10,10,10)
or maybe
dim arr.mytype(10,10,10) ; (as desribed in the docs)

and I want to use each one of the 11*11*11 indexes as a Type struct instance, so each one can hold a list of values (of individual length).

How would I have to do that? Thanks for your help!


Beaker(Posted 2005) [#2]
Type mytype
	Field a,b,c
End Type

Dim arr.mytype(10,10,10)

For x = 0 To 10
	For y = 0 To 10
		For z = 0 To 10
			arr(x,y,z) = New mytype
			arr(x,y,z)\a = x+(y*11)+(z*101)
		Next
	Next
Next

End



jfk EO-11110(Posted 2005) [#3]
Ok, thanks, but if I add multiple values to a certain index,like:
arr(x,y,z) = New mytype
arr(x,y,z)\a = 1
arr(x,y,z) = New mytype
arr(x,y,z)\a = 2
arr(x,y,z) = New mytype
arr(x,y,z)\a = 3

it will add additional list entries, right? So I have 3 values stored in this instance. But how to read them using a for loop?

when I try
For arr.mytype(0,0,0)= Each mytype
 Print arr(0,0,0)\a
next

..then it prints all values (also those with an index other than 0,0,0), ignoring the index 0,0,0.
What am I doing wrong?


sswift(Posted 2005) [#4]
"it will add additional list entries, right? So I have 3 values stored in this instance. But how to read them using a for loop?"

Imagine each type as one big list.

Your array stores pointers to entries in that list.

If you have a 10x10x10 array, with 1000 total elements, and you use New to create one new type instance for each array index, then you will have 1000 instances of that type in the type list.

If you then create additional instances of the type, say, five of them, with new, and assign the pointers to various indexes in your array, you will have 1005 entries in that type.

But you will still only have 1000 pointers in your array pointing into that list of type instances, and all the new types you create will be stuck at the end of the list.

If you want to have an array where each location can hold a list of types, then you need to do some clever stuff with your types.

There's many ways of doing this. None of them that I can think of are particularly simple to implement, or pretty.

Basically, you need to do one of three things:


Option 1:
For each array index, store the number of types you have created for that location in a seperate array. You will need to update this number each time you add or delete a type instance.

To make this work, you will need to keep your types in the correct order.

Let's say you want to insert a type at arr(1,1,1).

If you already have a type at arr(1,1,1) then instead of going arr(1,1,1) = New mytype you need to go like this:

insert new mytype after arr(1,1,1)

I think that will work anyway. If not, then you need to do this:

temp.mytype = new mytype
insert temp after arr(1,1,1)

Once you have done that you need to increment your counter for that location in the other array.


Option 2:
Have each type have a "lastinlist" field. Then do the above, but instead of incrementing a counter, you need to make sure you set and reset that variable in the type as needed so that the the last item in each sublist attached to the array index has that field set to true, so you know when to stop.

option 3:
Have two arrays. The first array tells you the first type in each sublist. The second array tells you the last type in each sublist. You can then loop from the first item to the last in the sublist only.

To do a loop between two types in a list, you need to use a while loop or something. You can't use a for loop. You use a temporary type variable. Temp.mytype for example. You set that to equal the pointer to the first item in the list you want. Then you use temp = after temp to move to the next item in the list. And finally you use if temp = lastitem.mytype where that is the pointer to the last item in your sublist, to see if you have eached the end of your sublist.


sswift(Posted 2005) [#5]
Ps:

It might be a whole lot easier, but use a little more ram if you just made a four dimensional array of the max size you'd think you'll need and then just stored the types in there. Then you don't even need to keep them ordered. You could just loop through all ten in each index, and ignore those with null pointers after you delete them. (You may have to set them to null mnaually.)

Of course if you have a lot of data keeping them sorted so you can stop as soon as you hit a null would be useful. So when you delete one, you would move the last one in that portion of the array into the hole you have created to fill it.


Beaker(Posted 2005) [#6]
jfk - this sort of code will lead to insanity:
arr(x,y,z) = New mytype
arr(x,y,z)\a = 1
arr(x,y,z) = New mytype
arr(x,y,z)\a = 2
arr(x,y,z) = New mytype
arr(x,y,z)\a = 3


So I have 3 values stored in this instance

No, you still have one, but you also have 2 dangling with no reference that will only show up in a For..Each loop.


jfk EO-11110(Posted 2005) [#7]
Yes, they are still there in memory, so I was hoping it would be possible to check if they where created with a certain array index. Honestly, this is the only usage of types that seems useful to me. (anyway, no discussion pls :))

Right now I think it's gonna be a lot easier to implement this thing with banks, simply say
dim arr(10,10,10)
then create a bank for each index dynamicly. Each bank can have an individual size, and it can even be resized without to lose data.

Thanks nevertheless.


Beaker(Posted 2005) [#8]
You could just store the array index in the type itself. Then you will always know where/what it belongs to:
Type mytype
	Field a,b,c
	Field x,y,z
End Type

Dim arr.mytype(10,10,10)

For x = 0 To 10
	For y = 0 To 10
		For z = 0 To 10
			arr(x,y,z) = New mytype
			arr(x,y,z)\a = x+(y*11)+(z*101)
			arr(x,y,z)\x = x
			arr(x,y,z)\y = y
			arr(x,y,z)\z = z
		Next
	Next
Next

End



jfk EO-11110(Posted 2005) [#9]
I think it would be slow this way, since I couldn't access them directly, but had to check the index each time, also I had to parse the entire type list for a single index "group". As far as I see the bank solution is the best way to solve this thing. for i=0 to ((banksize(bank(x,y,z))-1)*4), real simple. anyway, thanks alot!