PDA

View Full Version : Using Dictionary to store data structures



ErosOlmi
22-04-2007, 10:49
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, ...


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)

kryton9
22-04-2007, 12:05
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?

ErosOlmi
22-04-2007, 12:30
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

kryton9
22-04-2007, 22:50
Thanks Eros, this is very powerful indeed!!

ReneMiner
13-07-2014, 08:46
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/project.php?issueid=459