Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Forked from anonymous/List.vbs
Last active December 16, 2015 06:09
Show Gist options
  • Save atifaziz/5389361 to your computer and use it in GitHub Desktop.
Save atifaziz/5389361 to your computer and use it in GitHub Desktop.
Class List
Private m_Items()
Private Sub Class_Initialize()
Redim m_Items(0)
End Sub
Property Get Count
Count = UBound(m_Items) - LBound(m_Items)
End Property
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
Property Get IsEmpty: IsEmpty = Count = 0: End Property
Property Get LastIndex: LastIndex = Count - 1: End Property
Property Get Head
If IsEmpty Then Exit Property
If IsObject(Item(0)) Then Set Head = Item(0): Else Head = Item(0)
End Property
Property Get Tail
If IsEmpty Then Exit Property
If IsObject(Item(LastIndex)) Then Set Tail = Item(LastIndex): Else Tail = Item(LastIndex)
End Property
Property Get Top
If IsObject(Tail) Then Set Top = Tail: Else Top = Tail
End Property
Property Get Bottom
If IsObject(Head) Then Set Bottom = Head: Else Bottom = Head
End Property
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
Sub Append(ByVal Item): Add Item: End Sub
Function Enqueue(ByVal Item): Add Item: End Function
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
Sub Push(ByVal Item): Add Item: End Sub
Function Pop()
If IsEmpty Then Exit Function
If IsObject(Top) Then Set Pop = Top: Else Pop = Top
Redim Preserve m_Items(LastIndex)
End Function
Sub Clear(): Redim m_Items(0): End Sub
Sub ForEach(ByVal Output)
Dim Index: For Index = 0 To LastIndex: Call Output(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