TYPE within TYPE

BlitzPlus Forums/BlitzPlus Programming/TYPE within TYPE

Lazze(Posted 2004) [#1]
Is it possible to use a TYPE within a TYPE?

I have tried doing something like this

Type A
Field something
End Type

Type B
Field something
Field c.A
End Type

If I then add a number of B's and add a number of c.A's in different B's I end up getting all of the c.A's in every B - if you get my point. Unfortunately I can't show the actual code since I rewrote it to use to seperate types instead.

Lazze


soja(Posted 2004) [#2]
Whenever you create a new instance of a custom type, that type instance is tacked onto the global linked list for that type. In other words, everytime you use the New <whatever> command, your <whatever> list will become one item longer.

What you're thinking intuitively isn't done automatically. In each instance of type A, you should store the type pointers to type B that make sense. Don't iterate through them (For..Each), because, like you said, you'll just iterate through all of them.

It just requires a differnet way of understanding how custom types work. Am I helping at all? =)


Lazze(Posted 2004) [#3]
I think so, but how do I point from Type A to Type B?
As I mentioned earlier i rewrote it all, and instead of using Type in Type I use a indexnumber to connect to Type's. Not as neat as my original idea, but it's working :o).
It's not very efficient though, 'cause my app involves a lot of inserting and deleting in these two Type-sets, and to keep track of the unused indexes I have to run through the instances again and again. That's what I wanted to avoid.

Lazze


soja(Posted 2004) [#4]
Well, I'm not sure what your algorithm is doing, so I can't give any specific advice, but perhaps these general items might help:

First, a (Custom) Type Declaration, as you know, is the "Type...Field...End Type" construct. Now, to clarify the difference between Type pointers and Type instances. A type instance is what's created here, AFTER the = sign, and a type pointer is what's created BEFORE the = sign.

Var.A = New A

Whenever "New <type>" is used, Blitz allocates a chunk of memory for a new instance of all the fields of that type. It then inserts it into the global type list for that particular type declaration. (i.e. every kind has its own list.) These can be referenced by using the commands First, Last, Each, Before, After, etc.

A type pointer is essentially just a value pointing to a single type instance. Once declared, it can point to any instance or none, and it can be local or global, like here:

Global var.A

In this case, var = Null (not pointing to any instance). You could later do any of these:

var = First A
var = Last A
var = After First A
var = var2 (assuming var2.A was previously declared)
For var = Each A (in which case var will be iterated through the entire list from First to Last)

Type pointers can be part of other constructs, including other type declarations, like you did above. They can also be part of arrays.

Dim vars.A(20)
Local vars.A[20]

...
So, having said all that, perhaps it gives you a better idea of how custom types are managed in Blitz, and consequently give you a better idea of what constructs and data types/structures you want to use.


R0B0T0(Posted 2004) [#5]
If you want each type instance to contain its own personal list of sub-types (so in your example each B having its own list of A types), you need to use either linked lists or a type array. A type array would look like the following:

Type B
Field something
Field c.A[20]
End Type

the downside is you have to specify how many types are contained in the array ahead of time, and you cannot redimension the type array.

A linked list would look like the following:

Type A
Field something
Field parent.a
Field child.a
End Type

Type B
Field something
Field first.A
End Type

The first.A type would contain a pointer to the first A type in the list, and then each successive A type would point to the next type in the list and the previous type in the list.

The benefit of this method is that you can add and subtract types dynamically, the downside is that its more complicated to keep track of.

Either method requires custom functions to add/remove/iterate through the list of A types, you won't be able to use Blitz's first, last, next, each etc.


_Skully(Posted 2004) [#6]
Which in fact isnt that hard at all really

temp.a=b\first

' iterate through linked list
while temp<>null
temp=temp\child
wend

and ya you would need functions to delete, add, and insert types from/into the list

I use this system all the time.

Skully