Skip to content

Instantly share code, notes, and snippets.

@Lokutus
Created June 29, 2012 16:20
Show Gist options
  • Save Lokutus/3018923 to your computer and use it in GitHub Desktop.
Save Lokutus/3018923 to your computer and use it in GitHub Desktop.
Stack
%REM
Class Stack
Represents a simple last-in-first-out (LIFO) non-generic collection of objects.
Use Push/Pop for inserting and taking items
Use Top for viewing top item without removing it
@author Jiri Krakora
@date 15.6.2012
@uses
@revision
EXAMPLE
----------------------------------------------------------------------------
Dim stackObject As New Stack(10)
Dim stackPrimitive As New Stack(10)
Dim dt As NotesDateTime
Dim i As Integer
For i = 1 To 10
Set dt = New NotesDateTime(Now)
Call stackObject.Push(dt)
Call stackPrimitive.Push(i)
Next
' test object stack
MessageBox stackObject.Count
While Not stackObject.IsEmpty
Set dt = stackObject.Pop()
MessageBox dt.LsLocalTime
Wend
MessageBox stackObject.Count
' test primitive stack
While Not stackPrimitive.IsEmpty
MessageBox stackPrimitive.Pop()
Wend
%END REM
Public Class Stack
Private oStack List As Variant
Private oCount As Long
Private oBoundary As Long
%REM
Class constructor
@param top boundary of the stack, set to 0 if you want to use unlimited stack
%END REM
Public Sub New(boundary As Long)
If boundary > 0 Then
Let Me.oBoundary = boundary
End If
End Sub
Public Property Get IsEmpty As Boolean
If Me.oCount <= 0 Then
Let ~IsEmpty = True
End If
End Property
Public Property Get IsFull As Boolean
If Me.oCount >= Me.oBoundary And Me.oBoundary > 0 Then
Let IsFull = True
End If
End Property
Public Property Get Count As Long
Let Count = Me.oCount
End Property
Public Property Get Boundary As Long
Let Boundary = Me.oBoundary
End Property
Public Property Get Top As Variant
Dim d As Integer
If Me.oCount > 0 Then
Let d = DataType(Me.oStack(Me.oCount))
If d = 9 Or d = 34 Or d = 35 Then
Set Top = Me.oStack(Me.oCount)
Else
Let Top = Me.oStack(Me.oCount)
End If
Else
Set Top = Nothing
End If
End Property
%REM
Insert one item onto the top of the stack
@param new stack item
@return true/false
%END REM
Public Function Push(item As Variant) As Boolean
Dim d As Integer
If Not Me.IsFull Then
Let Me.oCount = Me.oCount + 1
Let d = DataType(item)
If d = 9 Or d = 34 Or d = 35 Then
Set Me.oStack(Me.oCount) = item
Else
Let Me.oStack(Me.oCount) = item
End If
End If
End Function
%REM
Retrieve top item from stack and remove it from it
@return top item in the stack
%END REM
Public Function Pop As Variant
Dim d As Integer
If Me.oCount > 0 Then
Let d = DataType(Me.oStack(Me.oCount))
If d = 9 Or d = 34 Or d = 35 Then
Set Pop = Me.oStack(Me.oCount)
Else
Let Pop = Me.oStack(Me.oCount)
End If
Erase Me.oStack(Me.oCount)
Let Me.oCount = Me.oCount - 1
Else
Set Pop = Nothing
End If
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment