This is great Rene!
I am splitting this to separate thread, as it has potential for expansion!
Petr
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...
testing-sample-script
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168' 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647Uses
"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()
Last edited by ReneMiner; 06-05-2015 at 11:35.
I think there are missing some Forum-sections as beta-testing and support
This is great Rene!
I am splitting this to separate thread, as it has potential for expansion!
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
I think there are missing some Forum-sections as beta-testing and support
Bookmarks