Skip to content

Instantly share code, notes, and snippets.

Created April 15, 2013 16:27
Show Gist options
  • Save anonymous/5389348 to your computer and use it in GitHub Desktop.
Save anonymous/5389348 to your computer and use it in GitHub Desktop.
Class List
Private m_Items()
Private Sub Class_Initialize()
Redim m_Items(0)
End Sub
Public Property Get Count
Count = UBound(m_Items) - LBound(m_Items)
End Property
Public Property Get Item(ByVal Index)
If Index < 0 Or Index >= Count Then Exit Property
Index = Index + 1
If IsObject(m_Items(Index)) Then Set Item = m_Items(Index): Else Item = m_Items(Index)
End Property
Public Property Get IsEmpty: IsEmpty = Count = 0: End Property
Public Property Get LastIndex: LastIndex = Count - 1: End Property
Public Property Get Head
If IsEmpty Then Exit Property
If IsObject(Item(0)) Then Set Head = Item(0): Else Head = Item(0)
End Property
Public Property Get Tail
If IsEmpty Then Exit Property
If IsObject(Item(LastIndex)) Then Set Tail = Item(LastIndex): Else Tail = Item(LastIndex)
End Property
Public Property Get Top
If IsObject(Tail) Then Set Top = Tail: Else Top = Tail
End Property
Public Property Get Bottom
If IsObject(Head) Then Set Bottom = Head: Else Bottom = Head
End Property
Public Sub Add(ByVal Item)
Redim Preserve m_Items(Count + 1)
If IsObject(Item) Then Set m_Items(Count) = Item: Else m_Items(Count) = Item
End Sub
Public Sub Append(ByVal Item): Add Item: End Sub
Public Function Enqueue(ByVal Item): Add Item: End Function
Public Function Dequeue()
If IsEmpty Then Exit Function
If IsObject(Head) Then Set Dequeue = Head: Else Dequeue = Head
Dim Index
For Index = 1 To Count - 1
Dim OldIndex: OldIndex = Index + 1
If IsObject(m_Items(OldIndex)) Then Set m_Items(Index) = m_Items(OldIndex): Else m_Items(Index) = m_Items(OldIndex)
Next
Redim Preserve m_Items(Count - 1)
End Function
Public Sub Push(ByVal Item): Add Item: End Sub
Public Function Pop()
If IsEmpty Then Exit Function
If IsObject(Top) Then Set Pop = Top: Else Pop = Top
Redim Preserve m_Items(LastIndex)
End Function
Public Sub Clear(): Redim m_Items(0): End Sub
Public Sub Echo(ByVal Output)
Dim Index: For Index = 0 To LastIndex: Output.Echo Item(Index): Next
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment