help with lots of code

BlitzMax Forums/BlitzMax Programming/help with lots of code

rdodson41(Posted 2005) [#1]
I'm trying to make a stack type data structure where one stack can hold values of multiple types. I have this code, but what i want only works with a push and pop function for EVERY type. Can someone help me either find a better way or to optomize this code?
s:stack=New stack

s.pushbyte(100)
s.pushshort(20000)
s.pushint(300000)
s.pushlong(4000000)
s.pushfloat(5.005)
s.pushdouble(6.00006)
s.pushstring("YEEHAW!!!")
s.pushobject(s)

Print String(s.popobject()=s)
Print String(s.popstring())
Print String(s.popdouble())
Print String(s.popfloat())
Print String(s.poplong())
Print String(s.popint())
Print String(s.popshort())
Print String(s.popbyte())

End





Type stack
	Field _head:block
	
	'*****PUSH*****'
	Method pushbyte:block(value:Byte)
		b:block=New block
		b._vbyte=value
		b._fore=_head
		_head=b
		Return b
	EndMethod
	
	Method pushshort:block(value:Short)
		b:block=New block
		b._vshort=value
		b._fore=_head
		_head=b
		Return b
	EndMethod
	
	Method pushint:block(value:Int)
		b:block=New block
		b._vint=value
		b._fore=_head
		_head=b
		Return b
	EndMethod
	
	Method pushlong:block(value:Long)
		b:block=New block
		b._vlong=value
		b._fore=_head
		_head=b
		Return b
	EndMethod
	
	Method pushfloat:block(value:Float)
		b:block=New block
		b._vfloat=value
		b._fore=_head
		_head=b
		Return b
	EndMethod
	
	Method pushdouble:block(value:Double)
		b:block=New block
		b._vdouble=value
		b._fore=_head
		_head=b
		Return b
	EndMethod
	
	Method pushstring:block(value:String)
		b:block=New block
		b._vstring=value
		b._fore=_head
		_head=b
		Return b
	EndMethod
	
	Method pushobject:block(value:Object)
		b:block=New block
		b._vobject=value
		b._fore=_head
		_head=b
		Return b
	EndMethod
	
	'*****POP*****'
	Method popbyte:Byte()
		If _head=Null Then Return Null
		b:block=_head
		_head=_head._fore
		vbyte:Byte=b._vbyte
		Release b
		Return vbyte
	EndMethod
	
	Method popshort:Short()
		If _head=Null Then Return Null
		b:block=_head
		_head=_head._fore
		vshort:Short=b._vshort
		Release b
		Return vshort
	EndMethod
	
	Method popint:Int()
		If _head=Null Then Return Null
		b:block=_head
		_head=_head._fore
		vint:Int=b._vint
		Release b
		Return vint
	EndMethod
	
	Method poplong:Long()
		If _head=Null Then Return Null
		b:block=_head
		_head=_head._fore
		vlong:Long=b._vlong
		Release b
		Return vlong
	EndMethod
	
	Method popfloat:Float()
		If _head=Null Then Return Null
		b:block=_head
		_head=_head._fore
		vfloat:Byte=b._vfloat
		Release b
		Return vfloat
	EndMethod
	
	Method popdouble:Double()
		If _head=Null Then Return Null
		b:block=_head
		_head=_head._fore
		vdouble:Double=b._vdouble
		Release b
		Return vdouble
	EndMethod
	
	Method popstring:String()
		If _head=Null Then Return Null
		b:block=_head
		_head=_head._fore
		vstring:String=b._vstring
		Release b
		Return vstring
	EndMethod
	
	Method popobject:Object()
		If _head=Null Then Return Null
		b:block=_head
		_head=_head._fore
		vobject:Object=b._vobject
		Release b
		Return vobject
	EndMethod	
EndType

Type block
	Field _vbyte:Byte
	Field _vshort:Short
	Field _vint:Int
	Field _vlong:Long
	Field _vfloat:Float
	Field _vdouble:Double
	Field _vstring:String
	Field _vobject:Object	
	Field _fore:block
EndType

Yes, it is an eyesore, but I can't figure any way around it.
Please any help is greatly appreciated!


rdodson41(Posted 2005) [#2]
If no one is going to answer because its a lot to read, all it is is a push function and a pop function for every type, basically pushbyte() popbyte(), pushshort() popshort(), etc. I know there has to be some better way to do this, but I just can't figure it out.


ImaginaryHuman(Posted 2005) [#3]
Um. You could simplify by storing the VarPtr to each thing you want to store, since all pointers will be the same size 4 bytes. You could then have separate storage arrays or something like that to act as individual stacks for each type? Having said that you may then need to also store a `type` identification number along each varptr so you know what kind of thing it points to. Several arrays instead of 1, but it's an alternative.


FlameDuck(Posted 2005) [#4]
I know there has to be some better way to do this, but I just can't figure it out.
Encapsulate them in objects? That way you'd be able to push/pull objects from the stack too...


skn3(Posted 2005) [#5]
If bmax could autocast from int/byte/etc to int ptr/byte ptr/etc it would be great.

Something like..
function GetPtr:byte ptr(converted:byte ptr)
	return converted
end function

myint:int = 123456789
mybyte:byte = 123
mystring:string = "helloworld"

notify "myint: "+int(GetPtr(myint))
notify "mybyte: "+int(GetPtr(mybyte))
notify "mystring: "+int(GetPtr(mystring))


... I imagine would come in handy for your problem.


rdodson41(Posted 2005) [#6]
Yeah, I've been trying to work this with pointers, but there is a different pointer type for every variable type.