danbaron
17-05-2010, 09:10
[font=courier new]Here is a script that implements a stack of Quads.
:oops:
Dan :x :P
'-----------------------------------------------------------------------------------------------------------------------
'file = qstack.tbasicc
'-----------------------------------------------------------------------------------------------------------------------
Uses "console"
'-----------------------------------------------------------------------------------------------------------------------
'Implementation of a stack, to hold Quads.
'As I bet you know, the name, "stack", comes from the analogy to a stack of plates.
'The first plate, the one that you start a stack with (on the bottom), is the last plate that you remove from it.
'The last plate, that you put on the top, is the first plate that you remove from it.
'So, in computer science, a stack is a LIFO (last in first out) structure.
'"push" means, "put a plate on the stack".
'"pop" means, "remove a plate from the stack".
'Here, "plate", means "Quad".
'I made it to use with something else.
'I did it without much thinking - instead, typing and changing until it seemed to work.
'I know that is the lazy, unreliable way to do it; detail by detail, until,
'it seems to magically do what it is supposed to do. In that case, the plus side is - it works,
'the minus side is - you may not know how or why.
'It could be buggy, but it seems to function OK.
'-----------------------------------------------------------------------------------------------------------------------
%quadsize = SizeOf(Quad)
%nodesize = %quadsize + SizeOf(DWord)
Global stack As DWord = 0
'-----------------------------------------------------------------------------------------------------------------------
Function TBMain()
Local ss As DWord = 4
Local i, j As Integer
Local v As Quad
For i = 1 To 2
For j = -ss To ss
push(j)
Next
For j = -ss To ss + 1
If emptystack() Then
Console_WriteLine("Stack empty.")
Console_WriteLine()
Else
v = pop()
Console_WriteLine(v)
EndIf
Next
Next
Console_WriteLine("Done. Press a key.")
WaitKey
End Function
'-----------------------------------------------------------------------------------------------------------------------
Function push(v As Quad)
Local plate As DWord
plate = HEAP_Alloc(%nodesize)
Poke(Quad, plate, v)
Poke(DWord, plate + %quadsize, stack)
stack = plate
End Function
'-----------------------------------------------------------------------------------------------------------------------
Function pop() As Quad
Local v As Quad
Local crackedplate As DWord
crackedplate = stack
v = Peek(Quad, stack)
stack = Peek(DWord, stack + %quadsize)
HEAP_Free(crackedplate)
Return v
End Function
'-----------------------------------------------------------------------------------------------------------------------
Function emptystack() As Byte
If stack = 0 Then Return TRUE
Return FALSE
End Function
'-----------------------------------------------------------------------------------------------------------------------
:oops:
Dan :x :P
'-----------------------------------------------------------------------------------------------------------------------
'file = qstack.tbasicc
'-----------------------------------------------------------------------------------------------------------------------
Uses "console"
'-----------------------------------------------------------------------------------------------------------------------
'Implementation of a stack, to hold Quads.
'As I bet you know, the name, "stack", comes from the analogy to a stack of plates.
'The first plate, the one that you start a stack with (on the bottom), is the last plate that you remove from it.
'The last plate, that you put on the top, is the first plate that you remove from it.
'So, in computer science, a stack is a LIFO (last in first out) structure.
'"push" means, "put a plate on the stack".
'"pop" means, "remove a plate from the stack".
'Here, "plate", means "Quad".
'I made it to use with something else.
'I did it without much thinking - instead, typing and changing until it seemed to work.
'I know that is the lazy, unreliable way to do it; detail by detail, until,
'it seems to magically do what it is supposed to do. In that case, the plus side is - it works,
'the minus side is - you may not know how or why.
'It could be buggy, but it seems to function OK.
'-----------------------------------------------------------------------------------------------------------------------
%quadsize = SizeOf(Quad)
%nodesize = %quadsize + SizeOf(DWord)
Global stack As DWord = 0
'-----------------------------------------------------------------------------------------------------------------------
Function TBMain()
Local ss As DWord = 4
Local i, j As Integer
Local v As Quad
For i = 1 To 2
For j = -ss To ss
push(j)
Next
For j = -ss To ss + 1
If emptystack() Then
Console_WriteLine("Stack empty.")
Console_WriteLine()
Else
v = pop()
Console_WriteLine(v)
EndIf
Next
Next
Console_WriteLine("Done. Press a key.")
WaitKey
End Function
'-----------------------------------------------------------------------------------------------------------------------
Function push(v As Quad)
Local plate As DWord
plate = HEAP_Alloc(%nodesize)
Poke(Quad, plate, v)
Poke(DWord, plate + %quadsize, stack)
stack = plate
End Function
'-----------------------------------------------------------------------------------------------------------------------
Function pop() As Quad
Local v As Quad
Local crackedplate As DWord
crackedplate = stack
v = Peek(Quad, stack)
stack = Peek(DWord, stack + %quadsize)
HEAP_Free(crackedplate)
Return v
End Function
'-----------------------------------------------------------------------------------------------------------------------
Function emptystack() As Byte
If stack = 0 Then Return TRUE
Return FALSE
End Function
'-----------------------------------------------------------------------------------------------------------------------