An hash array dictonary step by step
#2
Step 2 transformation of the Hash Table

in the first post we have a Hash Table that uses an UDT to store key and value, the access to the value is performed with a sequential search until we find the wanted key and so we can get the value related. This hash table managed the collision shifting the next value associated for the same key to the next available cell empty . When we search for a key we get the first value that has been stored in the table at the lower/higher position depending from the order of reading of the table ascending/descending.

In this second post we transform the original Hash Table making variable the lenght of the index and the lenght of value stored. Moreover after populating the hash table with a FOR loop, the Hash table has a direct access to the value using the index without any FOR/DO/WHILE sequential search . For now the collision activates an overwriting of the previous value. But this is one of the 3 available solutions (:overwrite, shifting and listing) for this kind of datatype

IMHO code has enough comments to be almost clear:
Code: (Select All)
'----------model of hash table with string index------------
Type HashTable
    index As String '<---variable lenght of string index
    value As String '<--- variable lenght of value stored
End Type
'-----------------------------------------------------------

Dim HashString(0 To 10) As HashTable '<--- instance of hashtable

' ------------ INITIALIZATION made using a single character ------
For c = 64 To 74 Step 1
    Call StoreValue(Chr$(c), String$(Int(c / 10), (c + 1)), HashString())
Next
'-----------------------------------------------------------------

'----------------SHOWING hashtable filled---------------
For c = 0 To 10
    Print c, HashString(c).index, HashString(c).value
Next c
'-------------------------------------------------------

'extracting some value directly from table without FOR loop and comparison
Print GetValue$("A", HashString())
Print GetValue$("E", HashString())
Print GetValue$("H", HashString())
End

'-------------- subroutines/ Functions'  area --------------------

' this function returns the value linked in the table H
' to the index IND
Function GetValue$ (ind As String, H() As HashTable)
    GetValue$ = H(GetIndex(ind)).value
End Function

'this subroutine stores a value VALU linked to the index IND
' in the hashtable HASH
' In this implementation the SUB overwrites thre previouse value
' stored into the hashtable if it happens a collision
' but this behaviour can be easily changed for storing the new value
' or in OPEN (searching the next cell with no value into it)
' or in list adding the value to the string already staying in hashtable
Sub StoreValue (ind As String, valu As String, Hash() As HashTable)
    Hash(GetIndex(ind)).index = ind
    Hash(GetIndex(ind)).value = valu
End Sub

' this function calculates the Hash value for storing in the hashtable
' the linked value
Function GetIndex% (i As String)
    Dim a As Integer, k As _Integer64, hash As _Integer64
    k = 1
    For a = 1 To Len(i)
        hash = hash + ((Asc(i, a)) * k)
        k = k * 10
    Next a
    GetIndex% = hash Mod 10
End Function


Welcome ideas, constructive criticisms, other code demonstrations and all you, kind people of QB64, want to share about these arguments.
Reply


Messages In This Thread
RE: An hash array dictonary step by step - by TempodiBasic - 03-14-2023, 03:04 PM



Users browsing this thread: 13 Guest(s)