VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "dwStack"
Attribute VB_Creatable = True
Attribute VB_Exposed = False
Option Explicit

' Class dwStack
' General purpose stack object
' Copyright (c) 1996 by Desaware Inc.
' Part of the Desaware API Classes Library


Private contents() As Variant
Private tos&    ' Top of stack
' Number of entries to allocate each time
Private iAllocationBlockSize%

Public Property Get AllocationBlockSize()
    AllocationBlockSize = iAllocationBlockSize
End Property

Public Property Let AllocationBlockSize(vNewValue)
    If vNewValue > 4 Then iAllocationBlockSize = vNewValue
End Property

' Push value onto the stack
Public Sub Push(v As Variant)
    Dim ub&
    
    contents(tos) = v
    ' Must we grow the stack?
    tos = tos + 1
    ub = UBound(contents)
    If tos < ub Then
        ReDim Preserve contents(tos + iAllocationBlockSize)
    End If
End Sub


Public Function Pop() As Variant
    Dim ub&
    
    If tos = 0 Then
        Pop = Null
        Exit Function
    End If
    tos = tos - 1
    Pop = contents(tos)
    ' Do we shrink the stack?
    ub = UBound(contents)
    If tos + iAllocationBlockSize + 5 < ub Then
        ReDim Preserve contents(tos + iAllocationBlockSize)
    End If
End Function

' Determine the current value of the top of stack
' Aways the value of the next location for data.
Public Property Get TopOfStack()
    TopOfStack = tos
End Property

' Allows you to peek at a value on the stack
' starting from position zero to tos-1
Public Function Peek(index%) As Variant
    If index < 0 Or index >= tos Then
        Peek = Null
        Exit Function
    End If
    Peek = contents(index)
End Function

Private Sub Class_Initialize()
    iAllocationBlockSize = 4
End Sub
