Web Design, Programming, Tutorials
Vector class for Lotus Script
The following is a script library written in Lotus Script to allow programmers to use a vector in their Lotus Script code.
Option Declare Option Base 0 Public Class Vector 'Private data members Private vArray As Variant Private vCapacity As Integer Private vSize As Integer Public Sub New() 'Initialize variables Call SetCapacity(0) Call SetSize(0) Call Reserve(10) End Sub Public Sub Delete() Call EraseAll() 'Clear the array Set vArray = Nothing End Sub Private Sub SetCapacity(newCapacity As Integer) vCapacity = newCapacity End Sub Public Function Capacity() As Integer Capacity = vCapacity End Function Private Sub SetSize(newSize As Integer) vSize = newSize End Sub Public Function Size() As Integer Size = vSize End Function Public Function Empty() As Boolean 'Declare variables Dim valid As Boolean 'Get the size If (Size() = 0) Then valid = True Else valid = False End If 'Return if the vector is empty Empty = valid End Function Public Sub Reserve(minCapacity As Integer) 'Declare variables Dim openElements As Integer Dim newCapacity As Integer 'Initialize variables openElements = Capacity() - Size() newCapacity = Capacity() 'Check if there are any open slots remaining If (openElements = 0) Then newCapacity = (Capacity() * 1.5) End If 'Make sure that the minimum capacity is stored If (newCapacity < minCapacity) Then newCapacity = minCapacity End If 'Check if a change to the capacity is being made If (Not(Capacity() = newCapacity)) Then 'Check the size to determine if the array's contents need to be 'saved while redimensioning the array If (Size() = 0) Then 'Nothing stored yet, perform redim Redim vArray(newCapacity) Else 'Keep the array's contents while extending its boundaries Redim Preserve array(newCapacity) End If 'Update the capacity Call SetCapacity(newCapacity) End If End Sub Public Sub PushBack(newItem As Variant) 'Check if the passed item/items is actually an array or a list If ((Not(Isarray(newItem))) And (Not(Islist(newItem)))) Then 'Add the single item Call AddItem(newItem) Else 'Add multiple items Forall item In newItem Call AddItem(item) End Forall End If End Sub Private Sub AddItem(newItem As Variant) 'Declare variables Dim newSize As Integer 'Initialize variables newSize = Size() + 1 'Make sure there is enough capacity in the vector Call Reserve(newSize) 'Insert the item into the vector If (Isobject(newItem)) Then Set vArray(Size()) = newItem Else vArray(Size()) = newItem End If 'Increment the vector size Call SetSize(newSize) End Sub Public Function At(index As Integer) As Variant 'Check the vector's index boundaries If ((index < 0) Or (index => Size())) Then Error 2000, _ "[Vector Class]:(Function: At): Index out of bounds." 'Check if the item is an object If (Isobject(vArray(index))) Then Set At = vArray(index) Else At = vArray(index) End If End Function Public Sub Erase(index As Integer) 'Check if the index is out of bounds If ((index < 0) Or (index => Size())) Then Error 2000, _ "[Vector Class]:(Function: Erase): Index out of bounds." 'Declare variables Dim newArray As Variant Dim x As Integer Dim y As Integer 'Initialize variables y = 0 'Make a new array with the same capacity Redim newArray(Capacity()) 'Loop through each element of the vector For x = 0 To Size() - 1 'Check if the current index is the one to remove If ((x <> index) And (Not(y > Size()))) Then 'Check if the item is an object If (Isobject(vArray(x))) Then Set newArray(y) = vArray(x) Else newArray(y) = vArray(x) End If 'Increment the index counter y = y + 1 End If Next x 'Subtract one from the vector size Call SetSize(Size() - 1) 'Set the new array as the vector vArray = newArray End Sub Public Function BeginIndex() As Integer BeginIndex = 0 End Function Public Function EndIndex() As Integer EndIndex = Size() - 1 End Function Public Sub EraseAll() 'Declare variables Dim x As Integer 'Loop through each element of the array For x = BeginIndex() To EndIndex() 'Delete each element of the arra 'Check if the item is an object If (Isobject(vArray(x))) Then Set vArray(x) = Nothing End If Next 'Reset the size. Call SetSize(0) End Sub End Class
Copy the above code and paste into a script library for the database where you would like to use a vector. Then, you can use the example below to use the vector class.
Use "libVector" Dim vector As New Vector() dim obj As String obj = "hello" 'Load any object into the vector. Call vector.PushBack(obj) Call vector.PushBack(obj) 'Loop through the vector to retrieve objects. For i = vector.BeginIndex() To vector.EndIndex() Print "Item: " & i & " " & vector.At(i) Next