-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathStack.vb
More file actions
89 lines (80 loc) · 3.51 KB
/
Stack.vb
File metadata and controls
89 lines (80 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Namespace Library
''' <summary>
''' This object provides a way of storing values just like stacking up a plate. You can push a value to the top of the stack and pop it off. You can only pop the values one by one off the stack and the last pushed value will be the first one to pop out.
''' </summary>
<SmallVisualBasicType>
Public NotInheritable Class Stack
Friend Shared _stackMap As New Dictionary(Of Primitive, Stack(Of Primitive))
''' <summary>
''' Pushes a value to the specified stack.
''' </summary>
''' <param name="stackName">The name of the stack.</param>
''' <param name="value">The value to push.</param>
Public Shared Sub PushValue(stackName As Primitive, value As Primitive)
Dim stack As Stack(Of Primitive) = Nothing
stackName = Text.ToLower(stackName)
If Not _stackMap.TryGetValue(stackName, stack) Then
stack = New Stack(Of Primitive)
_stackMap(stackName) = stack
End If
stack.Push(value)
End Sub
''' <summary>
''' Gets the count of items in the specified stack.
''' </summary>
''' <param name="stackName">The name of the stack.</param>
''' <returns>
''' The number of items in the specified stack.
''' </returns>
<WinForms.ReturnValueType(VariableType.Double)>
Public Shared Function GetCount(stackName As Primitive) As Primitive
Dim stack As Stack(Of Primitive) = Nothing
stackName = Text.ToLower(stackName)
If Not _stackMap.TryGetValue(stackName, stack) Then
stack = New Stack(Of Primitive)
_stackMap(stackName) = stack
End If
Return stack.Count
End Function
''' <summary>
''' Removees all items from the specified stack.
''' </summary>
''' <param name="stackName">The name of the stack.</param>
<WinForms.ReturnValueType(VariableType.Double)>
Public Shared Sub Clear(stackName As Primitive)
Dim stack As Stack(Of Primitive) = Nothing
stackName = Text.ToLower(stackName)
If _stackMap.TryGetValue(stackName, stack) Then
stack.Clear()
End If
End Sub
''' <summary>
''' Pops the value from the specified stack.
''' </summary>
''' <param name="stackName">The name of the stack.</param>
''' <returns>
''' The value from the stack.
''' </returns>
Public Shared Function PopValue(stackName As Primitive) As Primitive
Dim stack As Stack(Of Primitive) = Nothing
If _stackMap.TryGetValue(Text.ToLower(stackName), stack) Then
If stack.Count > 0 Then Return stack.Pop()
End If
Return New Primitive("")
End Function
''' <summary>
''' reads the last value pushed into the specified stack, whithout poping it out.
''' </summary>
''' <param name="stackName">The name of the stack.</param>
''' <returns>
''' The top value of the stack.
''' </returns>
Public Shared Function PeekValue(stackName As Primitive) As Primitive
Dim stack As Stack(Of Primitive) = Nothing
If _stackMap.TryGetValue(Text.ToLower(stackName), stack) Then
If stack.Count > 0 Then Return stack.Peek()
End If
Return New Primitive("")
End Function
End Class
End Namespace