Pages

Monday, December 21, 2009

Lotusscript HashMap

I often had issues when it came to tracking complex relations between strings in lotusscript.

For example, in a situation where, you got to keep adding email ids and names of people to multiple lists and to ensure that no person has multiple email ids recorded, is an cumbersome situation. It is possible, but involves lot of thinking

I felt a feature like HashMap in Java would be handy. Hence I got the Idea of developing the same in lotus script.

So here we go, the following is a lotus script hash map. Dont hate me if u have a better one...:).

Note : This class works only for string inputs...i.e, both the Key and Value must be a String

/*******************************************************/
Public Class HashMap

    'global variables
    Private hashMapList List As String
    Private mapSize As Long
    Private keyArray() As String
    Private valueArray() As String

    'property to add a new key value pair to the hashmap
    Function putValue(key As String,value As String)
        If Not Me.contains(key) Then
            ReDim Preserve keyArray(mapSize) As String
            ReDim Preserve valueArray(mapSize) As String
            keyArray(mapSize)=key
            valueArray(mapSize)=value
            mapSize=mapSize+1
        End If
        valueArray(ArrayGetIndex(keyArray,key))=value
        Me.hashMapList(key)=value
    End Function

    'property to get the value of the key in the hashmap
    Function getValue(key) As String
        On Error GoTo errHandler
        getValue = CStr(hashMapList(key))
        Exit Function
    errHandler:
        getValue= ""
        Exit Function
    End Function

    'property to check if the given key is already present in the hash map
    Function contains(key) As Boolean
        On Error GoTo errHandler
        If hashMapList(key)<>"" Then
            contains=True
        End If
        Exit Function
    errHandler:
        contains=False
        Exit Function
    End Function

    'property to return the size of the hashmap
    Function getSize As Long
        getSize=mapSize
    End Function

    'function to sort the contents of the Hash map in ascending order
    Function sort(ascending As Boolean)
        Dim sortItr As Integer
        Dim tempValue As String
        Dim tempKey As String
        Dim currMapSize As Integer
        Dim swapi As Integer,swapj As Integer
        Dim posItr As Integer
  
        On Error GoTo errHandler
        currMapSize=Me.getSize()-1
        For swapi=0 To currMapSize
            For swapj=swapi+1 To currMapSize
                If ascending Then
                    If CLng(valueArray(swapj))>CLng(valueArray(swapi)) Then
                        tempValue=valueArray(swapi)
                        valueArray(swapi)=valueArray(swapj)
                        valueArray(swapj)=tempValue
                        tempKey=keyArray(swapi)
                        keyArray(swapi)=keyArray(swapj)
                        keyArray(swapj)=tempKey
                    End If
                Else
                    If  CLng(valueArray(swapj)) < CLng(valueArray(swapi)) then
                        tempValue=valueArray(swapi)
                        valueArray(swapi)=valueArray(swapj)
                        valueArray(swapj)=tempValue
                        tempKey=keyArray(swapi)
                        keyArray(swapi)=keyArray(swapj)
                        keyArray(swapj)=tempKey
                    End If
                End If
            Next
        Next

        'clear the contents of the existing list
        Call Me.clear()
      
        'populate the list in the sorted order
        For posItr=0 To currMapSize
            hashMapList(keyArray(posItr))=valueArray(posItr)
        Next

        Exit Function
        'inform the user regarding the error that resulted in abrupt termination
    errHandler:
        MsgBox "An error ***" & Error & "*** occured on line ***" & CStr(Erl) & "*** with error number ***" _
        & CStr(Err) & "*** in function Sort in class HashMap",,"Error occured"
        Print "An error ***" & Error & "*** occured on line ***" & CStr(Erl) & "*** with error number ***" _
        & CStr(Err) & "*** in function Sort in class HashMap",,"Error occured"
        Exit Function
    End Function
  
    'destructor
    Function clear
        Erase hashMapList
    End Function

    Function getKeyArray()
        getKeyArray=keyArray
    End Function

    Function getValueArray()
        getValueArray=valueArray
    End Function
End Class
/********************************************************?

I have tested the above class using a button with the following code, seems to work for me
/***********************/
Dim hashMp As Variant
Set hashMp=New HashMap

Call hashMp.putValue("a","b")
Call hashMp.putValue("b","c")
Call hashMp.putValue("c","d")
Msgbox hashMp.getValue("a")    ' alerts b
Msgbox hashMp.getValue("b")    ' alerts c
Msgbox hashMp.getValue("c")    ' alerts d 
Msgbox hashMp.getSize()        ' alerts 3
Msgbox hashMp.contains("a"),,hashMp.contains("d")
' alerts true,,false  //(msgbox sense hi hi)

/***********************/

Best wishes,
Karthick

5 comments:

  1. Hi Karthik,
    I appreciate your efforts to put the HashMap in LotusScript.

    The code is not compiling for some reason...do you have the sample database available.

    Please send it to jamesgeorgem@gmail.com

    Regards,
    JG

    ReplyDelete
  2. Hi James,

    I have made a few changes to the code here. The problem that I found when attempting to reuse it was, the encoded html problem...

    eg: " gets replaced by " - sort of issue..

    Thanks for mentioning it...

    Click the following link to download sample database.

    HashMap.nsf

    Hope that helps :)

    ReplyDelete
  3. Lotusscript what a poor language!
    Anyway good job.

    ReplyDelete
  4. I believe its very good as a scripting language :)

    ReplyDelete
  5. Ironically, LotusScript runs *on top* of Java (all of Lotus Notes runs on a JVM). If only there was a way for LotusScript to expose the Java interface for method invocation...

    ReplyDelete