PDA

View Full Version : Dictionary example



Petr Schreiber
20-04-2007, 21:52
Hi Catventure,

use is quite general.
Take this example:



USES "dictionary"

Dim pInventoryDict As Long ' -- Pointer to dictionary

%NUM_Characters = 3
pInventoryDict = Dictionary_Create(%NUM_Characters * 2) ' -- Character name + inventory

Dictionary_Add(pInventoryDict, "Old orc", "Axe, Sword, Deodorant")
Dictionary_Add(pInventoryDict, "Wizard", "Magic wand, Beard dummy")
Dictionary_Add(pInventoryDict, "Woodpecker", "Red hat")

Msgbox 0, "Wizard invenotry is:"+$CRLF+$CRLF+Dictionary_Find(pInventoryDict, "Wizard")

Dictionary_Free(pInventoryDict)


... in this case you can use it like array, but instead of numeric index use directly name of character :)


Bye,
Petr

catventure
21-04-2007, 03:57
Hi Petr,

Sorry still don't get it!

What exactly is dictionary?

Look to be good idea I could use in adventure.

I've been out out tonite and had a few drinks so not thinking properly! :)

catventure

Petr Schreiber
21-04-2007, 09:52
Hi,

this was just one example, maybe not the best.
I will try to take a glass of Jameson and see if it can be understood differentely then ;D

Imagine you need to store some data, which can be hardly indexed using numbers.
You can still do that using arrays + equates, to keep it clear, but this is not as universal.

So you can index items using strings, this is how I understand it :)
Eros released new thinBASIC preview with nice example on dictionaries, worth to have a look !


Bye,
Petr

catventure
21-04-2007, 12:09
Thanks Psch,

I have recovered a bit from last night ;)

I understand your example better now. Very good.
Well this definitely opens up some new possibilities, and I wasn't expecting anything like this to be added to thinBasic, so fantastic job!

catventure

ErosOlmi
21-04-2007, 12:49
he he ;D
I lived in London for one year working for a multinational company.
Because I do not drink any alchool (beer included) I remember my "emargination" during first months. Most couldn't bealive I didn't drink anything other than water :P
Than people started to appreciate my no alchool attitude. I was always the "driver" to get them home. :D

Eros

Petr Schreiber
21-04-2007, 13:22
I am happy you can see it now :)

;D Eros, this is nice story!


Bye,
Petr

kryton9
21-04-2007, 22:52
Petr, I don't understand this code:
%NUM_Characters = 3
pInventoryDict = Dictionary_Create(%NUM_Characters * 2) ' -- Character name + inventory

Why not:
pInventoryDict = Dictionary_Create(3 * 2)
or
pInventoryDict = Dictionary_Create(6)
or
pInventoryDict = Dictionary_Create(3 * sizeof(char)) : I don't understand where the 3 comes from when the words are much longer characters than 3 or 3*2 of 6 characters?

ErosOlmi
22-04-2007, 00:20
Ken,

it is always good programming practice to define constants (rather than writing fixed numbers) even if it will be longer or boring.
This mainly for 2 important factors: readability and maintainability.

Readability:
it give much more sense to read something like %MaxNumberOfEnemies rather than something like 10 or 6 or 100

Maintainability:
if along the code you need to use again that constant it will be much, much more easy to maintain just a constant value rathar then searching all the code for all the places where you use a number and change it.

Also consider len of variable will not make any difference in compiled languages and a minimum difference in thinBasic too because every token is just parsed once.
So, use all the power of the language to always create code to be readable and maintanable.

Ciao
Eros

kryton9
22-04-2007, 04:36
Thanks Eros, can you explain where the 3 and 2 came from to size the dictionary with the create command, how do you determine those values?

Petr Schreiber
22-04-2007, 09:53
Hi kryton,

it is simple, we have 3 characters...
Each of them has assigned some data ( inventory ).

So if we need 3 entries for names to dictionary, we will use numberOfCharacters + numberOfInventories. As number of inventories is the same as numberOfCharacters, I put there * 2. It is always about pair of data. So keys * 2.

This is also explained under thinBasic language / Modules / Dictionary / Dictionary_Create in help file.

You can imagine it as "How many cells I need to stor following?":


"Old orc""Axe, Sword, Deodorant"


"Wizard""Magic wand, Beard dummy"


"Woodpecker""Red hat"



Bye,
Petr

ErosOlmi
22-04-2007, 10:18
Ken, Petr,

it is programmer responsability to know the number of keys he/she needs to store.
In help file I mentioned to create a Dictionary with a number of slots about the double of the keys you need to store.
This not because the key/data is a pair system but to avoid as much as possible repeating HASH values calculation inside Dictionary.

If you read http://en.wikipedia.org/wiki/Hash_table you will see how a hash table is built.
Dictionary_Create(MaxSlots) creates an in memory structure with the indicated free hash slots.
When you add a new key, the Dictionary:

calculates the hash value (a number between 1 and MaxSlots), let say n
see if slot at that position n is free
if slot is free, key/data is stored directly
if slot is not free, a local linked list is created. This linked list will contain all the keys that produced the same hash value

As you can see, the more slots, the less the possibility to create duplicated hash values, the more the speed because to find the key it is needed just one step while finding a key that produced duplicates more steps will be needed.

I know, it is not so easy, but really, try to read http://en.wikipedia.org/wiki/Hash_table It is very interesting.
Let me know if you need other info.
Ciao
Eros

kryton9
22-04-2007, 12:09
Will read thanks for the links Eros.

Petr Schreiber
22-04-2007, 12:59
Eros,

thanks a lot for the info!


Bye,
Petr