Results 1 to 5 of 5

Thread: Using Dictionary to store data structures

  1. #1
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Using Dictionary to store data structures

    Here a not so clever example but can give you an idea of what is also possible to do with Dictionaries.
    You can store and retrieve full data structure. Because data stored into a Dictionary is not limited to strings but the full ASCII set is supported, you can store any kind of data into a dictionary: a string, a structure, a file of any type, numbers in their binary format, ...

    [code=thinbasic] USES "Dictionary"

    '---Define a structure
    type MyType
    Name as asciiz * 32
    NumberLong as long
    NumberExtended as ext
    FixedString as string * 256
    FixedAsciiZ as asciiz * 256
    ALittleArray(10) as long
    ABiggerArray(200) as long
    end type

    msgbox 0, "Size of structure: " & sizeof(MyType)

    '---Define a LONG that will hold a reference to a new dictionary
    DIM MyDict AS LONG

    '---Create a new dictionary with 1000 unique single buckets
    MyDict = Dictionary_Create(1000, %true)

    dim dummyReset as MyType '---Used to reset/clean variable type
    dim dummyVarType as MyType '---Used to store temp data

    '---Fill first structure and add to the dictionary
    dummyVarType.Name = "Robot1"
    dummyVarType.NumberLong = 12345678
    Dictionary_Add(MyDict, dummyVarType.Name , dummyVarType)
    msgbox 0, "Added: '" & dummyVarType.Name & "' to dictionary"

    '---Fill first structure and add to the dictionary
    dummyVarType = dummyReset '---Before reusing dummyVarTpe, better to reset to initial value
    dummyVarType.Name = "Robot2"
    dummyVarType.NumberLong = 67345
    Dictionary_Add(MyDict, dummyVarType.Name , dummyVarType)
    msgbox 0, "Added: '" & dummyVarType.Name & "' to dictionary"

    '---Now search and get back data
    dummyVarType = Dictionary_Find(MyDict, "Robot1")

    msgbox 0, "Current structure name: " & dummyVarType.Name

    '---Remove the whole dictionary and all its keys and data from memory
    Dictionary_Free(MyDict)
    [/code]

    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Using Dictionary to store data structures

    Eros, if you had all the fields of the structure assigned a value, could you just add the whole structure at once?

    Or using your example, you are adding to the dictionary the name, but what about the other fields, would they all needed to be addd one by one or can you add them as just the type and it would place all the fields assigned a value or not into the dictionary?
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Using Dictionary to store data structures

    Sorry, I was lazy to fill all the fields but, yes, the whoole structure is moved into the dictionary.

    A structure is nothing more than a sequence of bytes interpreted by logical fields. So when I add the structure variable to the dictionary I'm adding the whoole structure. If the structure fields are filled, ok, but if not it doesn't matter.

    Of course this example has no meaning. It was just a demo about possibilities.

    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Using Dictionary to store data structures

    Thanks Eros, this is very powerful indeed!!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  5. #5
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,525
    Rep Power
    170
    for the very reason of this thread i would like to call type-functions directly on a dictionary-bucket without the need of some pre-overlay.
    What is dictionary good for if it can store data of types but can not call the types function?


    See here for more detail: http://www.thinbasic.com/community/p...hp?issueid=459
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. Config parameters: where to store them?
    By ErosOlmi in forum thinAir General
    Replies: 5
    Last Post: 31-05-2006, 08:21

Members who have read this thread: 1

Posting Permissions

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