Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An hash array dictonary step by step
#1
Step1  Presentation of project


Just to keep in theme of the dictionary structure data made by array in QB64, here we attempt to build a dictionary with hash  index.

We start from here
Code: (Select All)
'Assosziatives Array: https://jeff.win/qbhash/
'28. Feb. 2023

Type element
    tag As String * 10
    value As String * 10
End Type
Dim Shared aa(10) As element
Dim Shared aalast ' Last occupied AA() element


setvalue "foo", "bar"
setvalue "foo", "coffee"

Print getvalue$("foo") ' prints bar also after adding coffee
End

Function getvalue$ (tag As String)
    tag = LTrim$(RTrim$(tag))
    tag = tag + String$(10 - Len(tag), " ")
    For i = 0 To aalast
        If (tag = aa(i).tag) Then
            getvalue$ = aa(i).value
            Exit Function
        End If
    Next
End Function

Sub setvalue (tag As String, value As String)
    aa(aalast).tag = tag
    aa(aalast).value = value
    aalast = aalast + 1
End Sub

this code has been taken from this article on this webpage QBHash

this demo is very simple and with many limitations that cannot let us think about it like a real dictonary data structure.
What is the data structure coded has these features:  you can store more than one value linked to the tag value; moreover these collisions (new values linked to the tag) are stored into different cells of the array.  The author to get this result used an external index/counter  (AALAST). In the while the GetValue SUB is broken because it returns only the first value linked to the string index.

However here more information.

Issues:
1 the value stored can fit only 10 characters (ASCII values)
Code: (Select All)
Type element
    tag As String * 10  ' <----- hash value stored as a string of 10 characters that is searched sequentially
    value As String * 10  '<----- max 10 character for value
End Type

2 the hash index is not direct but searched rowly from the start to the end of arrayList

Code: (Select All)
Function getvalue$ (tag As String)
    tag = LTrim$(RTrim$(tag))
    tag = tag + String$(10 - Len(tag), " ")
    For i = 0 To aalast
        If (tag = aa(i).tag) Then
            getvalue$ = aa(i).value
            Exit Function
        End If
    Next
End Function

3 the store value routine does not avoid that the hashindex value has no duplicates.
Code: (Select All)
Sub setvalue (tag As String, value As String)
    aa(aalast).tag = tag
    aa(aalast).value = value
    aalast = aalast + 1
End Sub

4 the search value routine get the first cell of the array that has the hashvalue searched
Code: (Select All)
  If (tag = aa(i).tag) Then
            getvalue$ = aa(i).value
            Exit Function
        End If
Now we try to work to solve these issues.
Reply


Messages In This Thread
An hash array dictonary step by step - by TempodiBasic - 03-14-2023, 12:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple text pixel data to array demo Unseen Machine 5 653 09-12-2025, 05:30 PM
Last Post: bplus
  Unique Random Array Program eoredson 5 838 07-10-2025, 10:29 AM
Last Post: DANILIN
  QB64_GJ_LIB ARRay Library grymmjack 3 983 08-21-2023, 12:50 PM
Last Post: mnrvovrfc
  Get Next Filename into a string (or array) GTC 2 822 02-26-2023, 12:51 PM
Last Post: GTC
  Three-dimensional array Kernelpanic 20 4,214 01-08-2023, 05:39 PM
Last Post: TempodiBasic

Forum Jump:


Users browsing this thread: