PDA

View Full Version : tDictionary - class for easy Dictionary handling



ReneMiner
05-05-2015, 09:26
just in case none will read it i'll post another useless script here.

There's already some cDictionary-class, but it's missing important functions and of no real use this way.
Some replacement/workaround is to create a dictionary-type that wraps the dictionary-functions. Easy to handle, but if speed matters i'm not sure this is the right way...



' Filename: tDictionary.tBasicU

Uses "dictionary"

' ###########################################################################
Type tDictionary
' ###########################################################################

pDict As DWord

Create As Function
Free As Function

MemorySize As Function

DataSet As Function
DataGet As Function
DataPtr As Function
DataSize As Function

KeyCount As Function
KeyList As Function
KeyDelete As Function

End Type

'-----------------------------------------------------------------------------
'resources-functions

'-----------------------------------------------------------------------------
Function tDictionary.Create(ByVal lNumKeys As Long, _
Optional ByVal KeyIsUcase As Boolean _
) As DWord
'-----------------------------------------------------------------------------

If Me.pDict <> 0 Then Return 0 ' must .Free() Me then!
lNumKeys = MinMax(lNumKeys, 1, &H7FFFFFFF )
Me.pDict = Dictionary_Create( lNumKeys, KeyIsUcase )
Function = Me.pDict

End Function
'-----------------------------------------------------------------------------
Function tDictionary.Free() As DWord

'-----------------------------------------------------------------------------

If Me.pDict Then
Dictionary_Free(Me.pDict)
Me.pDict = 0
EndIf
' all .Free()-methods should return a 0

End Function

'-----------------------------------------------------------------------------
Function tDictionary.MemorySize( _
Optional ByVal lInfoType As Long = %DICTIONARY_MEMINFO_TOTAL _
) As Long

'-----------------------------------------------------------------------------
' lInfoTypes:
' %DICTIONARY_MEMINFO_TOTAL (default)
' %DICTIONARY_MEMINFO_KEYS
' %DICTIONARY_MEMINFO_DATA

If Me.pDict Then
Function = Dictionary_MemInfo(Me.pDict, lInfoType)
EndIf

End Function

'-----------------------------------------------------------------------------
' data-functions

'-----------------------------------------------------------------------------
Function tDictionary.DataSet(ByVal sKey As String, _
ByVal sData As String _
) As DWord

'-----------------------------------------------------------------------------

If Me.pDict Then
Function = Dictionary_Add(Me.pDict, sKey, sData)
EndIf

End Function

'-----------------------------------------------------------------------------
Function tDictionary.DataGet(ByVal sKey As String) As String
'-----------------------------------------------------------------------------

If Me.pDict Then
Function = Dictionary_Find(Me.pDict, sKey)
EndIf

End Function


'-----------------------------------------------------------------------------
Function tDictionary.DataPtr(ByVal sKey As String) As DWord
'-----------------------------------------------------------------------------

If Me.pDict Then
Local pData As DWord At Dictionary_Exists(Me.pDict, sKey)
If GetAt(pData) Then
Function = pData
EndIf
EndIf

End Function

'-----------------------------------------------------------------------------
Function tDictionary.DataSize(ByVal sKey As String) As Long
'-----------------------------------------------------------------------------

If Me.pDict Then
Local pData As DWord At Dictionary_Exists(Me.pDict, sKey)
If GetAt( pData ) Then
Function = StrPtrLen(pData)
EndIf
EndIf

End Function

'-----------------------------------------------------------------------------
' key-functions

'-----------------------------------------------------------------------------
Function tDictionary.KeyCount() As Long
'-----------------------------------------------------------------------------

If Me.pDict Then
Function = Dictionary_Count(Me.pDict)
EndIf

End Function

'-----------------------------------------------------------------------------
Function tDictionary.KeyList(Optional ByVal sSeperator As String, _
ByVal lOptions As Long _
) As String
'-----------------------------------------------------------------------------

If Me.pDict Then
If Function_CParams = 2 Then
Function = Dictionary_ListKeys(Me.pDict, sSeperator, lOptions)
Else
Function = Dictionary_ListKeys(Me.pDict, sSeperator)
EndIf
EndIf

End Function

'-----------------------------------------------------------------------------
Function tDictionary.KeyDelete(ByVal sKey As String) As Boolean
'-----------------------------------------------------------------------------

If Me.pDict Then
Function = Dictionary_Delete(Me.pDict, sKey)
EndIf

End Function

'-----------------------------------------------------------------------------
'Function tDictionary.()
'-----------------------------------------------------------------------------

'End Function


testing-sample-script


Uses "Console"
#INCLUDE Once "tDictionary.tBasicU"


Dim myDict1 As tDictionary

myDict1.Create(100)


myDict1.DataSet("KEY001", "this is data")
myDict1.DataSet("KEY002", "this is data too")

PrintL "myDict1, number of keys:" & Str$(myDict1.KeyCount)
PrintL "myDict1, key list:" & $CRLF & mydict1.KeyList($CRLF)

PrintL myDict1.DataGet("KEY001")
PrintL "Len Data 1" & Str$( myDict1.DataSize("KEY001") )
PrintL myDict1.DataGet("KEY002")
PrintL "Len Data 2" & Str$( myDict1.DataSize("KEY002") )
PrintL

PrintL "check via Pointers:"
PrintL Memory_Get(myDict1.DataPtr("KEY001"), Peek(DWord, myDict1.DataPtr("KEY001") - 4 ))
PrintL Memory_Get(myDict1.DataPtr("KEY002"), Peek(DWord, myDict1.DataPtr("KEY002") - 4 ))

PrintL "memory used" & Str$(myDict1.MemorySize)
PrintL

PrintL "delete key 1"
If myDict1.KeyDelete("KEY001") Then
PrintL "DONE."
Else
PrintL "failed !!!"
EndIf

PrintL
PrintL "keys left:" & Str$(myDict1.KeyCount)
PrintL "myDict1, key list:" & $CRLF & mydict1.KeyList($CRLF)

PrintL "memory for keys" & Str$(myDict1.MemorySize(%DICTIONARY_MEMINFO_KEYS))
PrintL "memory for data" & Str$(myDict1.MemorySize(%DICTIONARY_MEMINFO_DATA))
PrintL "memory used now" & Str$(myDict1.MemorySize)

PrintL "---------------------- key to end"

WaitKey
myDict1.Free()

Petr Schreiber
05-05-2015, 21:53
This is great Rene!

I am splitting this to separate thread, as it has potential for expansion!


Petr

ReneMiner
06-05-2015, 11:42
This is great Rene!

I am splitting this to separate thread, as it has potential for expansion!


Petr

Not just EXPANSION but also EXTENSION.

specialize your own dictionaries simply by:


Type tUserDictionary Extends tDictionary
UserFun As Function
Static UserSetting As Long
End Type


I fear this is impossible with module-classes? ...