Skip to content

Instantly share code, notes, and snippets.

@seanbamforth
Created February 2, 2025 14:21
Show Gist options
  • Save seanbamforth/4edbc81ebfbd336769dd77de1eeee02c to your computer and use it in GitHub Desktop.
Save seanbamforth/4edbc81ebfbd336769dd77de1eeee02c to your computer and use it in GitHub Desktop.
Use Array to create basic key/value Dictionary class
//Use this in 18.2 or below. This class is 5 times slower than the alternatives.
Object oDictArray is a cObject
Property tDictionaryKey[] paKeyList
Property Boolean pisDirty
Function SearchDictKey tDictionaryKey val1 tDictionaryKey val2 Returns Integer
If (val1.sKey > val2.sKey) Function_Return (GT)
If (val1.sKey < val2.sKey) Function_Return (LT)
Function_Return (EQ)
End_Function
Procedure AddKey String sKey String sValue
Integer hObj
tDictionaryKey[] aKeyList
Integer iPos
tDictionaryKey kvSearch
Get paKeyList to aKeyList
Move sKey to kvSearch.sKey
If (not(pisDirty(Self))) Begin
Move (BinarySearchArray(kvSearch,aKeyList,Self,RefFunc(SearchDictKey))) to iPos
End
Else Begin
Move (SearchArray(kvSearch,aKeyList,Self,RefFunc(SearchDictKey))) to iPos
End
If (iPos=-1) Move (SizeOfArray(aKeyList)) to iPos
Move sValue to aKeyList[iPos].sValue
Move sKey to aKeyList[iPos].sKey
Set paKeyList to aKeyList
Set pisDirty to True
End_Procedure
Procedure RemoveAll
tDictionaryKey[] aKeyList
Set paKeyList to aKeyList
Set pisDirty to False
End_Procedure
Procedure Delete_data
Send RemoveAll
End_Procedure
Procedure EnsureSorted
tDictionaryKey[] aKeyList
If (not(pisDirty(Self))) Procedure_Return
Get paKeyList to aKeyList
Set paKeyList to (SortArray(aKeyList,Self,RefFunc(SearchDictKey)))
Set pisDirty to False
End_Procedure
Function KeyValueExists String sKey Returns Boolean
tDictionaryKey[] aKeyList
Integer iPos
tDictionaryKey kvSearch
Move sKey to kvSearch.sKey
Send EnsureSorted
Get paKeyList to aKeyList
Move (BinarySearchArray(kvSearch,aKeyList,Self,RefFunc(SearchDictKey))) to iPos
Function_Return (iPos<>-1)
End_Function
Function KeyValue String sKey Returns String
tDictionaryKey[] aKeyList
Integer iPos
tDictionaryKey kvSearch
String sValue
Move sKey to kvSearch.sKey
Send EnsureSorted
Get paKeyList to aKeyList
Move (BinarySearchArray(kvSearch,aKeyList,Self,RefFunc(SearchDictKey))) to iPos
If (iPos<>-1) Move aKeyList[iPos].sValue to sValue
Function_Return sValue
End_Function
Function DefaultedKeyValue String sKey String sDefault Returns String
tDictionaryKey[] aKeyList
Integer iPos
tDictionaryKey kvSearch
String sValue
Move sKey to kvSearch.sKey
Send EnsureSorted
Get paKeyList to aKeyList
Move (BinarySearchArray(kvSearch,aKeyList,Self,RefFunc(SearchDictKey))) to iPos
If (iPos<>-1) Function_Return aKeyList[iPos].sValue
Else Function_Return sDefault
End_Function
Function SizeOfDictionary Returns Integer
tDictionaryKey[] aKeyList
Get paKeyList to aKeyList
Function_Return (SizeOfArray(aKeyList))
End_Function
Function ListOfKeys Returns String[]
Function_Return (paKeyList(Self))
End_Function
End_Object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment