;#Region DESCRIPTION
;; Stack sub-system used for organization of objects
;#End Region
;#Region CLASSES
Type Stack
Field F.StackObject
Field L.StackObject
Field Objects%
End Type
Type StackObject
Field Content$
Field Class$
Field N.StackObject
Field P.StackObject
Field Parent.Stack
Field z
End Type
Global STACK_Class$
Global STACK_Content$
;#End Region
;#Region PROCEDURES
Function CreateStack( )
Local s.Stack = New Stack
Return Handle( s )
End Function
Function PushObject( Stack , Content$ , Class$="", ToFront = 0 )
Local s.Stack = Object.Stack (Stack )
If s = Null Then Return 0
Local Index = sObjects
Local i.StackObject = New StackObject
iContent = Content
iClass = Class
iParent = s
If ToFront <= 0 Then
Local l.StackObject = sL
If l <> Null Then
lN = i
iP = l
EndIf
sL = i
If sF = Null Then sF = i
Else
Local f.StackObject = sF
If f <> Null Then
fP = i
iN = f
EndIf
sF = i
If sL = Null Then sL = i
EndIf
i = sObjects
sObjects = sObjects + 1
If DEVELOP And LOG_STACK Then DebugLog "Push "+Stack +" "+ Index +" "+ Content
Return Index
End Function
Function PopObject$( Stack , FromFront = 0 )
Local s.Stack = Object.Stack( Stack )
Local Content$ = "",Class$ = ""
If s = Null Then Return Content
If FromFront <= 0 Then
If sL = Null Then Return Content
Local l.StackObject = sL
If lP <> Null Then
lPN = Null
sL = lP
EndIf
If sL = Null Then sL = sF
Content = lContent
Class = lClass
Delete l
Else
If sF = Null Then Return Content
Local f.StackObject = sF
If fP <> Null Then
fNP = Null
sF = fN
EndIf
If sF = Null Then sF = sL
Content = fContent
Class = fClass
Delete f
EndIf
If DEVELOP And LOG_STACK Then DebugLog "Pop "+Stack+" "+Content
STACK_Content = Content
STACK_Class = Class
Return Content
End Function
Function GetObject$( Stack, Index, RemoveData=0 )
Local Content$ = "",Class$ = ""
Local s.Stack = Object.Stack( Stack )
If s = Null Then Return Content
Local i
Local f.StackObject = sF
If f = Null Then Return Contents
For i = 0 To Index-1
If fN = Null Then Exit
f = fN
Next
Content = fContent
Class = fClass
If RemoveData > 0 Then
If fN <> Null Then fNP = fP
If fP <> Null Then fPN = fN
If sL = f Then sL = fP
If sF = f Then sF = fN
sObjects = sObjects - 1
Delete f
EndIf
If DEVELOP And LOG_STACK Then DebugLog "Get "+Stack+" "+Index+" "+Content+" "+RemoveData
STACK_Class = Class
STACK_Content = Content
Return Content
End Function
Function GetObjectF#( Stack, Index, RemoveData=0 )
Return Float(GetObject(Stack,Index,RemoveData))
End Function
Function GetObjectI%( Stack, Index, RemoveData=0 )
Return Int(GetObject(Stack,Index,RemoveData))
End Function
Function InsertObject( Stack, At, Content$, Class$="" )
Local s.Stack = Object.Stack( Stack )
If s = Null Then Return -1
Local i
Local f.StackObject = sF
If f = Null Then Return PushObject( Stack, Content)
For i = 0 To At-1
If fN = Null Then Exit
f = fN
Next
Local n.StackObject = New StackObject
nContent = Content
nClass = Class
nParent = s
If f <> Null Then
nN = fN
nP = f
fN = n
EndIf
sObjects = sObjects + 1
If DEVELOP And LOG_STACK Then DebugLog "Insert "+Stack+" "+At+" "+Content
Return i
End Function
Function MoveObject( Stack, TakeFrom, MoveTo )
Local cont$ = GetObject(Stack,TakeFrom,1)
Return InsertObject(Stack, MoveTo-1,cont$,STACK_Class)
End Function
Function MoveObjectToFront( Stack, Index )
Local cont$ = GetObject(Stack,Index,1)
Return PushObject(Stack,cont$,1)
End Function
Function MoveObjectToBack( Stack, Index )
Local cont$ = GetObject(Stack,Index,1)
Return PushObject(Stack,cont$,0)
End Function
Function Objects( Stack )
Local s.Stack = Object.Stack( Stack )
If s = Null Then Return -1
Return sObjects
End Function
Function FreeStack( Stack )
Local s.Stack = Object.Stack( Stack )
If s = Null Then Return 0
Delete s
Local i.StackObject = Null
For i = Each StackObject
If iParent = Null Then
Delete i
EndIf
Next
If DEVELOP And LOG_STACK Then DebugLog "Free "+Stack
Return 1
End Function
Function Debug_StackContents(stack)
For i = 0 To Objects(stack)-1
s$ = s$+" | "+GetObject(stack,i)
Next
; If Right(s$,2) = "| " Then s$ = Left(s$,Len(s)-3)
DebugLog s$
End Function
;#End Region