May 14, 2024, 06:28:54 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Hashing variables

Started by aurelCB, September 04, 2011, 12:45:04 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

Hi...
Last days i play with hash tables(dictionaries).
As some of you know im trying to replace Linked List stored variables  in ABasic
with variables stored in hash tables.
i know that not everyone interested for this thematic but using hashed arrays have advantages over
linked list,...in first place there is speed of acces to variable.
Because you can avoid iterating.
As you know hash pair are -> key string ,and value string

But first you must create dictionary with statment:
POINTER = DICTCREATE(OPT hashtablesize=17 as INT,OPT blocksize=10 as INT)
In my testing i ignore blocksize but i think that is important to.
Tablesize for best performance should be a prime number.(but is not nececery...)

So i use next code( and create two hash tables, one for numbers and one for variable type):
'HASH array for numbers
numHash = DictCreate(99991)
'HASH array for variable type
typHash = DictCreate(99991)


Next step is to add key and value to dictionary with:
INT = DictAdd(dict as POINTER,key as STRING,value as STRING)
I use this:

DictAdd numHash,varName,varValue
DictAdd typHash,varName,"1"


Next step is search for variable when you want get value of variable.
For this step you need command :
STRING = DictLookup(dict as POINTER,key as STRING)

I use this:
vTyp$=GetVarType(newVarName)
IF vTyp$="1"
  DictAdd(numHash,newVarName,str$(newVarValue))
ELSE
  error=1
ENDIF


This simple subrutine return variable type:
SUB GetVarType(name:STRING),STRING
STRING tmp
error=0
tmp=DictLookup(typHash,name)
IF tmp="" THEN error=1
RETURN tmp
ENDSUB


And next subroutine return variable value:
SUB GetNumVar(name:STRING),STRING
STRING tmp
error=0
tmp=DictLookup(numHash,name)
IF tmp="" THEN error=1
RETURN tmp
ENDSUB


As you see scope is very simple on this way.
Of course you can use something else to scope your data from hash table.
And you must remeber that hash hold strings.
On this way i can say that acces to variable work very well.
Currently i test only numeric variable becouse im not sure how to store strings.
Why?
It looks that value of hash cannot be empty string "".
So my question will be what to use to store empty string?

Any suggestion are welcome.... :)

Aurel