home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / classlib / desaware / dwstack.cls < prev    next >
Encoding:
Text File  |  1996-02-17  |  1.8 KB  |  78 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "dwStack"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. ' Class dwStack
  11. ' General purpose stack object
  12. ' Copyright (c) 1996 by Desaware Inc.
  13. ' Part of the Desaware API Classes Library
  14.  
  15.  
  16. Private contents() As Variant
  17. Private tos&    ' Top of stack
  18. ' Number of entries to allocate each time
  19. Private iAllocationBlockSize%
  20.  
  21. Public Property Get AllocationBlockSize()
  22.     AllocationBlockSize = iAllocationBlockSize
  23. End Property
  24.  
  25. Public Property Let AllocationBlockSize(vNewValue)
  26.     If vNewValue > 4 Then iAllocationBlockSize = vNewValue
  27. End Property
  28.  
  29. ' Push value onto the stack
  30. Public Sub Push(v As Variant)
  31.     Dim ub&
  32.     
  33.     contents(tos) = v
  34.     ' Must we grow the stack?
  35.     tos = tos + 1
  36.     ub = UBound(contents)
  37.     If tos < ub Then
  38.         ReDim Preserve contents(tos + iAllocationBlockSize)
  39.     End If
  40. End Sub
  41.  
  42.  
  43. Public Function Pop() As Variant
  44.     Dim ub&
  45.     
  46.     If tos = 0 Then
  47.         Pop = Null
  48.         Exit Function
  49.     End If
  50.     tos = tos - 1
  51.     Pop = contents(tos)
  52.     ' Do we shrink the stack?
  53.     ub = UBound(contents)
  54.     If tos + iAllocationBlockSize + 5 < ub Then
  55.         ReDim Preserve contents(tos + iAllocationBlockSize)
  56.     End If
  57. End Function
  58.  
  59. ' Determine the current value of the top of stack
  60. ' Aways the value of the next location for data.
  61. Public Property Get TopOfStack()
  62.     TopOfStack = tos
  63. End Property
  64.  
  65. ' Allows you to peek at a value on the stack
  66. ' starting from position zero to tos-1
  67. Public Function Peek(index%) As Variant
  68.     If index < 0 Or index >= tos Then
  69.         Peek = Null
  70.         Exit Function
  71.     End If
  72.     Peek = contents(index)
  73. End Function
  74.  
  75. Private Sub Class_Initialize()
  76.     iAllocationBlockSize = 4
  77. End Sub
  78.