Domino Code Fragment

Code Name*
Queue, stack and enumerator
Date*
10/13/97
Source (or email address if you prefer)*
Paul Everett/IBM
IP address:.3.133.159.224
Description*
The very-most-basic functions of an object queue, stack and enumerator.
Type*
LotusScript
Categories*
List Processing/Sorting, Numeric Processing
Implementation:
None (plug and play)
Required Client:
Server:
Limitations:
Comments:
The object references are stored internally
as "ObjectCollectionNode" objects, which implement a linked list.
Files/Graphics attachments (if applicable): Code:
%REM
Methods:

ObjectCollection = ObjectCollection.Push(object): Place an object at the head of the collection (stack). Returns a reference to the collection, so that
commands can be stacked.

ObjectCollection = ObjectCollection.Queue(object): Place an object at the tail of the collection (queue). Returns a reference to the collection.

Variant = ObjectCollection.Pull(): Removes and returns the object at the head of the collection. If none, returns Nothing.

Variant = ObjectCollection.GetFirstObject(): Returns the object at the head of the stack, without removing it. If the collection is empty, returns
Nothing.

Variant = ObjectCollection.GetNextObject(): Returns the next object after the last one returned by GetFirstObject or GetNextObject. If the there is no
next object, returns Nothing.

long = ObjectCollection.count(): Returns the number of objects in the collection.

%END REM



' -------------------------------------------------------------------------------------------------------------------------------------
' ----------------------------------------------------------------------------------------------------------------------------------------------------
Class ObjectCollectionNode ' Contains a reference to an object, and to it's leader and follower
    Public guyBehind As ObjectCollectionNode ' Next Node in List, or Nothing
    Public contents As Variant ' Reference to the contents of the Link
   
    Sub new (newData As Variant) ' newdata: a reference to an object
         Set contents = newData
         Set guyBehind = Nothing
    End Sub
   
    Function countoff () As Long
         If guyBehind Is Nothing Then countoff = 1 Else countoff = 1+ guyBehind.countoff
    End Function
   
End Class


Class ObjectCollection
    Head As ObjectCollectionNode ' Attach the first link in the chain to this Anchor link
    Tail As ObjectCollectionNode ' the Last Link in the chain (save time on Queue)

     bookmark As Variant ' Placeholder for GetFirst/GetNext methods
   
    Sub new
         Set Head = Nothing
         Set LastNode = Nothing
         Set bookmark = Nothing
    End Sub
   
    Function Push( contents As Variant ) ' Add new object Node at top of Chain
         Dim newGuy As New ObjectCollectionNode ( contents )
         If head Is Nothing Then Set tail = newGuy ' If the list is empty newGuy is tail too.
         Set newGuy.GuyBehind = head ' put the queue (or Nothing) behind the new Guy
         Set head = newGuy ' put the new Guy at the Head position
         Set Push = Me ' return a reference to the collection
    End Function
   
    Function Pop() As Variant ' Return contents of Head Node, or Nothing
         If head Is Nothing Then
              Set pop = Nothing
         Else
              Set pop = head.contents
              Set head = head.GuyBehind
         End If
    End Function
   
    Function Queue( contents As Variant ) ' Add new object Node at tail of Chain

          Dim newGuy As New ObjectCollectionNode ( contents ) ' There's a new guy in the program...
         If head Is Nothing Then ' If queue is empty
              Set head = newGuy ' newguy is head and tail
         Else
              Set tail.GuyBehind = newGuy ' set him behind the guy at the end of the line
         End If
         Set tail = newGuy ' label him as the last guy
         Set Queue = Me 'Return a reference to the collection
    End Function
   
    Function GetFirstObject() As Variant
         If head Is Nothing Then
              Set GetFirstObject = Nothing
         Else
              Set GetFirstObject = head.contents
              Set bookmark = head.guybehind ' if head is nothing, ok
         End If
    End Function
   
    Function GetNextObject() As Variant
         If bookmark Is Nothing Then
              Set GetNextObject = Nothing
         Else
              Set GetNextObject = bookmark.contents
              Set bookmark = bookmark.GuyBehind ' may well be Nothing

          End If
    End Function
   
    Function count() As Integer
         If head Is Nothing Then count = 0 Else count = head.countoff
    End Function
   
End Class


Class thing ' Just something to put in the queue as a testcase
    Public contents As String
   
    Sub new (myThing As String)
         Me.contents = myThing
    End Sub
End Class