Type of a type

Blitz3D Forums/Blitz3D Programming/Type of a type

iamdaman13(Posted 2006) [#1]
When I try to create a type with a type as a field, how do I make the inner type initialized from within the type declaration? For instance, in the following code segment, let's say I have something like "a.bst = New bst". For some reason I still have to say "a\root.node = New node" because the bst type isn't creating an instance of the node type like I want it to. How would I do this? I'm trying to make a binary search tree.

Type node
Field value = 0
Field leftNode.node = New node
Field rightNode.node = New node
End Type


Type bst
Field root.node = New node
End Type


Stevie G(Posted 2006) [#2]
You can't initialise field in blitz. You need to create the node's elsewhere in your code ..

The 'Value' field will initialise to 0 by default as soon as a new type is created though.

Not sure what your doing but an example ..

Type Node
 field value
 field leftnode.node
 field rightnode.node
end type

global ROOT.node = NODEcreate()
ROOT\LeftNode = NODEcreate( 5 )
ROOT\RightNode = NODEcreate( 7 )

Function NODEcreate.node( Value=0 )
  this.node = new node
  this\value = Value
  return this
end function



iamdaman13(Posted 2006) [#3]
Thanks for the response. I wish they could implement initialization inside of type definitions in blitz. Are there any object variable types which are generic and can be used to store ints, float, strings, ect., similar to the Object type in Java?
Thanks,
Joe


Dreamora(Posted 2006) [#4]
You mean %, # and $ which you can use as type specifier on fields?


iamdaman13(Posted 2006) [#5]
Those limit the variable to that type. In Java there's a type called Object. You can assign it an integer, a float, a character, a string, whatever you want to assign it. Java handles it either by deducting what the item most likely is or by typecasting. This gives the freedom to make functions that will work for any type without having to make different functions for each type. In my Java project for a school assignment, I made a class that creates BSTs for the Object type. So I could use that class to make a BST with any type for its elements. So far, I haven't seen many other languages do that because the Object type in Java is costly memory-wise. I hope I'm explaining this well. Here's the Java API for object so you know what I'm referring to:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html


b32(Posted 2006) [#6]
You could also initialize the leftnode/rightnode field in the createnode function:
root.Node = CreateNode()

Function CreateNode.Node()
 n.node = new node
 n\leftnode = new node
 n\rightnode = new node
End Function

or
Function CreateNode.Node()
 n.node = new node
 if n <> first node then n\leftnode = before node
 if n <> last node then n\rightnode = after node
End Function

If you want to dereference a custom type, try using
Handle(root) (object->integer) and Object.Node(x) (integer->object)
As for a generic type, you could convert everything into a string, like this:
x$ = 3 ;integer
x$ = 3.0 ;float
x$ = Handle(object) ;object
x$ = "hello" ;string



octothorpe(Posted 2006) [#7]
If the code in the original post worked, it would create a chain reaction that would eat all available RAM, then crash. By creating a new node, two new nodes would be created; those two nodes would cause four nodes to be created; ad nauseum.

I wish they could implement initialization inside of type definitions in blitz.


Just write your own "constructor" function (as the other posters here have done) and never call New directly.

Are there any object variable types which are generic and can be used to store ints, float, strings, ect., similar to the Object type in Java?


Using Object() and Handle(), you can use ints to store "pointers" to objects of any Type, but not to primitives such as ints, floats, or strings. One solution would be to create small wrapper Types for each primitive.

If you're trying to make a generic BST container, check out the other containers I've already designed in my sig. I don't have a BST, but if I did, its interface would be very similar to my others.


iamdaman13(Posted 2006) [#8]
Thanks for the advice. I didn't know I could make constuctor functions for user types. That helps a lot! This whole time I've been passing the nodes as parameters, but constructor functions are much cleaner.
Thanks,
Joe


iamdaman13(Posted 2006) [#9]
I'm having a little trouble with the constuctor functions you mentioned. I'm not sure how to correctly implement it, but here is the original code without using constuctor functions:
Function insertInt(this.bst, item)
p.node = New node
prev.node = New node
p = this\root
While Not p = Null
prev = p
If p\theInt < item
p = p\rightNode
Else
p = p\leftNode
EndIf
Wend
If this\root = Null
this\root = New node
this\root\theInt = item
ElseIf prev\theInt < item
prev\rightNode = New node
prev\rightNode\theInt = item
Else
prev\leftNode = New node
prev\leftNode\theInt = item
EndIf
End Function

I want to make a function that can be called something like this:
myBst\insertInt(item)
Is there a possible way to do something like this?


Stevie G(Posted 2006) [#10]
That looks like some sort of insertion sort to me. What is it you are trying to do exactly? I'm sure there will be a simpler way of doing this.

Cheers
Stevie


iamdaman13(Posted 2006) [#11]
It inserts an element into the binary tree in such an order that an inorder traversal of the tree results in values in numeric order. So far, I haven't been able to find a simpler way to do it with Blitz's limited command set. At least the function works, though. My main question is about functions in general. Can you make a constructor method for a type like in Java where you say something like myBST.insert(value);


Stevie G(Posted 2006) [#12]
Sorry never used Java but something like this?

SeedRnd ( MilliSecs() )

Type node 
	Field Value
End Type


For l = 1 To 20
	NODEinsert( Rand( 1, 100 ) )
Next

For n.node = Each node
	Print n\value
Next

MouseWait
End



Function NODEinsert( Value )


	;create new node
	This.node = New node
	This\Value = value


	;find it's position in node list	
	For Crt.node = Each node

		If Crt <> This

			If This\Value < Crt\Value
				Insert This Before Crt
				Exit
			EndIf
			
			Nxt.node = After Crt
			If Nxt <> Null
				If This\Value > Crt\Value And This\Value < Nxt\Value
					Insert This After Crt
					Exit
				EndIf
			EndIf
			
		EndIf
		
	Next


End Function



octothorpe(Posted 2006) [#13]
iamdaman13, your code leaks like a sieve!

p.node = New node
prev.node = New node


New creates objects! Read up on it.

Also, to make the function more readable, I'd create the new_node to start with, then go through the process of determining where to place it, instead of creating it in one of three places like you've done above.

For example,



This has nothing to do with "constructor" functions or automatic initialization of type fields.


octothorpe(Posted 2006) [#14]

I want to make a function that can be called something like this:
myBst\insertInt(item)
Is there a possible way to do something like this?



This isn't possible in Blitz3d. It's just syntactic sugar; it's the same thing as BST_insertInt(myBst, item).