Types within Types

Blitz3D Forums/Blitz3D Programming/Types within Types

Could you let me know if the way I'm doing "types within types" is the best way to manage my data.

Basically I want to have a parent type, this one can have a whole bunch of child types inside. It seems that I must give each child type an id that will link it to the parent type in order to keep the child types mixing with other parents. Is this the best method?

t2 value is what I want, however the "id" thing seems to be absurd, that's like adding extra stuff that shouldn't be needed.


Graphics3D 320,240, 0, 2

Global Camera=CreateCamera()

Type Parent
	Field id
	Field c.Child
End Type

Type Child
	Field id
End Type

For i = 1 To 10
	p.parent = New Parent
	p\id = i
	For j = 1 To 10
		p\c.Child = New Child
		p\c\id = i
	Next
Next

For p.parent = Each Parent
	For p\c.Child = Each Child
		t = t + 1
	Next
	Exit
Next

For p.parent = Each Parent
	For p\c.Child = Each Child
		If p\c\id = p\id Then
			t2=t2+1
		EndIf
	Next
	Exit
Next

Color 255, 255, 255
While Not KeyHit(1)
	
	
	RenderWorld
	Text 0, 0, "count: " + t
	Text 0, 20, "tcount: " + t2
	Flip
	
Wend
End



What you're doing is basically right. However, I would add a 'parent' field to the children, instead of a 'child' field to the parents.
In this part:
For p.parent = Each Parent
	For p\c.Child = Each Child
		t = t + 1
	Next
	Exit
Next

Using "p\c" is redundant, as well as using "Exit".
By reversing the dependancy (not sure if that is the correct term), the structure becomes more logical:
Graphics3D 640, 480, 0, 2

Global Camera=CreateCamera()

Type Parent
	Field id
End Type

Type Child
	Field p.Parent
End Type

For i = 1 To 4
	p.parent = New Parent
	p\id = i
	For j = 1 To 10
		c.Child = New Child
		c\p = p
	Next
Next

For p.parent = Each Parent
	Print "List " + p\id
	t = 0
	For c.Child = Each Child
		If c\p = p Then t = t + 1
	Next
	Print "->has " + t + " children"
Next

Print

t = 0
For c.Child = Each Child
	t = t + 1
Next

t2 = 0
For p.Parent = Each Parent
	t2 = t2 + 1
Next

Print "total amount of children: " + t
Print "total amount of parents: " + t2

Flip
WaitKey()

End


Another way of doing lists is storing the First and Last element of each list. Not sure if that is more handy, but that way you don't need to iterate through all the children if you only need to acces a specific list:
	;setup graphics
	Graphics 800, 600, 0, 2
	SetBuffer BackBuffer()

	;setup parent type	
	Type Parent
		Field c_First.Child
		Field c_Last.Child
		Field index ;this field is not needed
	End Type
	
	Type Child
		Field datafield$ ;child can store any data
	End Type
	
	;create 2 lists
	For i = 1 To 2
		
		;create new list
		p.Parent = New Parent
		p\index = i
		
		;create 10 children
		For j = 1 To 10
			
			c.Child = CreateChild(p)
			c\datafield$ = RandomWord$()
			
		Next
		
	Next
	
	;print out all lists
	For p.Parent = Each Parent
	
		;print list index
		Print "List " + p\index
		Print "-------"
	
		;if list has children
		If p\c_First <> Null Then
		
			;get first child
			c.Child = p\c_First
			
			;loop through all children
			Repeat
				
				;print child data
				Print c\datafield$
				;if this is the last child of the list, exit
				If c = p\c_Last Then Exit
				;get next child
				c = After c
				
			Forever
			
		End If
		
		;end of list
		Print 
		
	Next
	
	;wait for key and end
	Flip
	Wait
	Key()
	End

;-----------------------------------------------------------------------------------------------------
;												CreateChild()
;-----------------------------------------------------------------------------------------------------
;creates a child and adds it to a list
Function CreateChild.Child(p.Parent)

	c.Child = New Child
	If p\c_Last <> Null Then Insert c After p\c_Last
	p\c_Last = c
	
	If p\c_First = Null Then p\c_First = c
	
	Return c
	
End Function

;-----------------------------------------------------------------------------------------------------
;												RandomWord()
;-----------------------------------------------------------------------------------------------------
;generates random 'word'
Function RandomWord$()

	s$ = ""
	For i = 1 To Rand(10)		
		s$ = s$ + Chr$(Rand(65, 90))
	Next
	
	Return s$
	
End Function


Assuming you want to include other variables within both the child and parent type .. this is what I would do ...

Graphics3D 320,240, 0, 2

Global Camera=CreateCamera()

Type Parent
	Field Stuff ;???
End Type

Type Child
	Field Otherstuff
	Field Parent.Parent
End Type

For i = 1 To 10
	p.parent = New Parent
	;p\Stuff = ???
	CHILDadd( p , Rand( 5, 10 ) )
Next

Color 255, 255, 255

While Not KeyHit(1)
	
	RenderWorld()
	
	Parents = 0
	TotalChildren = 0
	 
	For p.parent = Each Parent
		Parents = Parents + 1
		Children = 0
		For c.Child = Each Child
			If c\Parent = p 
				Children = Children + 1
			EndIf
		Next
		TotalChildren = TotalChildren + Children
		Text 0, Parents*10 , "Parent "+Parents+" has "+Children+" children"	
	Next
	
	Text 0, 200, "Parents " + Parents
	Text 0, 210, "Children " + TotalChildren

	Flip
	
Wend
End


Function CHILDadd( p.parent, NumberToAdd )

	For N = 1 To NumberToAdd
		c.child = New child
		c\Parent = p
	Next
	
End Function