Results 1 to 3 of 3

Thread: tDictionary - class for easy Dictionary handling

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171

    Lightbulb tDictionary - class for easy Dictionary handling

    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()
    
    Last edited by ReneMiner; 06-05-2015 at 11:35.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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

  3. #3
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    Quote Originally Posted by Petr Schreiber View Post
    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? ...
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. creating my own class
    By ReneMiner in forum thinBasic General
    Replies: 4
    Last Post: 17-11-2012, 20:07
  2. How to run a Dot Net 4.0 class from Powerbasic
    By zak in forum Software discussion
    Replies: 1
    Last Post: 17-12-2011, 11:27
  3. class+object+dialog
    By Lionheart008 in forum UI (User Interface)
    Replies: 3
    Last Post: 17-11-2009, 22:09
  4. Class Libraries using Oxygen
    By Charles Pegge in forum O2h Compiler
    Replies: 3
    Last Post: 10-08-2009, 05:15
  5. TBGL makes OpenGL so easy
    By kryton9 in forum TBGL General
    Replies: 2
    Last Post: 10-02-2007, 19:46

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •