Author Topic: A demo of Dictionary or hash table made with a array bidimensional(Rosetta Code)  (Read 3088 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: A demo of Dictionary or hash table made with a array bidimensional
« Reply #15 on: April 08, 2021, 09:48:19 am »
@bplus
Cool! Now you have done a complete basic dictionary data  (associated arrays or arrays of couple of value (a key of order not numeric + a value or sequence values)

and with the AddAppendDictionary you have covered the Achille's ankle of AddModDictionary!.

Good gimme just some time and I'll complete the Dictionary engine using bidimensional array and that other with string .
For now You can publish on RosettaCode website.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: A demo of Dictionary or hash table made with a array bidimensional
« Reply #16 on: April 08, 2021, 12:24:52 pm »
I have come to realize that Dictionary allowing multiple values (or no values) for a Key's Value is a way to get an array of arrays functionality, as AStrings (Array-Strings) of course.

So next comes Saving and Loading Dictionary's to/from file and converting them for use in a Type String.

Quote
For now You can publish on RosettaCode website.
Perhaps after more critical review from experts. I was excited about posting something for Knapsack problem in JB then learned I missed an important aspect of problem even if I got the sample task correct. And after getting Word Search posted Richard Frost showed way to a stronger code method. The more brains involved the better.
« Last Edit: April 08, 2021, 12:33:18 pm by bplus »

Offline Aurel

  • Forum Regular
  • Posts: 167
Re: A demo of Dictionary or hash table made with a array bidimensional
« Reply #17 on: April 09, 2021, 05:23:24 pm »
well
first of all hash table or dictionary or map is not exactly same as associative array
of course hash table can be used as token list with token values ...ordinary as interger types like
key (print) , value(10)
hash or hashed means using prime number as hashing method but not nesesery.
lookup() function can be used here of course...
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: A demo of Dictionary or hash table made with a array bidimensional
« Reply #18 on: April 10, 2021, 04:58:50 pm »
Hi
I have followed the definition on the Rosetta Code website but the definition on wikipedia seems very close
https://en.wikipedia.org/wiki/Associative_array#:~:text=In%20computer%20science%2C%20an%20associative,most%20once%20in%20the%20collection.

Quote
In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.

After a complete dictionary engine in QB64 it will be fine to have some demonstrations in different fields of application of associative arrays.

what do you think about?
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: A demo of Dictionary or hash table made with a array bidimensional
« Reply #19 on: April 10, 2021, 05:18:28 pm »
I'm thinking of doing oh with dictionaries to track variables in actual Subs and / or Functions along with Main module.

Offline Aurel

  • Forum Regular
  • Posts: 167
Re: A demo of Dictionary or hash table made with a array bidimensional
« Reply #20 on: April 11, 2021, 06:32:52 am »
That is good thinking Mark..
i tried to make interpreter using hash table in past ...well  but i don't get any real boost
with that ..only more options

ps ..and please don't use too much ReDim -s that eat execution time .
(ps... ps ...but maybe i am in wrong )
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
@bplus
do you think that your engine for dictionary data type is completed and your code is ready to be published on Rosetta Code? Or do you need more time to  think about this task?
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
@bplus
do you think that your engine for dictionary data type is completed and your code is ready to be published on Rosetta Code? Or do you need more time to  think about this task?

I think I did all I would want from a Dictionary Library. Dang haven't got around to trying with the oh Interpreter yet.
Really it's when you go to apply something you learn what's needed!

If anyone sees something needed for complete set of functions I will be glad to try more.
« Last Edit: April 21, 2021, 08:43:01 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Hi Bplus
here I post my version of Associates Arrays  using an array bidimensional of strings  to create a dictionary of couple Key-value
The dictionary engine has its basic functions: PutDict add a couple key-value to the dictionary, GetDict returns the value of a key, ChangeValueDict  changes  the value of key, ChangeKeyValueDict  change a key of dictionary,  EraseKeyDict  cancels a couple key-value from dictionary,  FindDuplicateKey finds if a key is still in dictionary, CompactDict moves all couples key-values to the bottom of dictionary, ShowDict shows the whole or a part of the dictionary.
Code: QB64: [Select]
  1. Const False = 0, True = Not False, sValue = 2, sKey = 1
  2.  
  3. Dim Mydict(1 To 10, 1 To 2) As String
  4.  
  5. ' start of demo
  6.  
  7. If PutDict(Mydict(), "b", "battle") = True Then Print " Inserted couple key value"
  8. If PutDict(Mydict(), "c", "cheesebattle") = True Then Print " Inserted couple key value"
  9. If PutDict(Mydict(), "a", "ancient") = True Then Print " Inserted couple key value"
  10. If PutDict(Mydict(), "d", "domus") = True Then Print " Inserted couple key value"
  11. If PutDict(Mydict(), "e", "embarassing") = True Then Print " Inserted couple key value"
  12. If PutDict(Mydict(), "e", "entering") = False Then Print " Key already in use"
  13. ShowDict Mydict(), 2, 5
  14. Print " we search the key a and e"
  15. If GetDict(Mydict(), "b") <> "" Then Print GetDict(Mydict(), "b")
  16. If GetDict(Mydict(), "e") <> "" Then Print GetDict(Mydict(), "e")
  17. Print " we change key b and e"
  18. If ChangeValueDict(Mydict(), "b", "BASIC") Then Print "changement done"
  19. If ChangeValueDict(Mydict(), "e", "Elephant") Then Print "changement done"
  20. If GetDict(Mydict(), "e") <> "" Then Print GetDict(Mydict(), "e")
  21. If GetDict(Mydict(), "b") <> "" Then Print GetDict(Mydict(), "b")
  22. Print PutDict(Mydict(), "q", "proof tool")
  23. Print PutDict(Mydict(), "y", "Yes/No")
  24. ShowDict Mydict(), 0, 0
  25. Print " we erase key e"
  26. If EraseKeyDict(Mydict(), "e") = True Then Print "key passed erased"
  27. If GetDict(Mydict(), "e") <> "" Then Print GetDict(Mydict(), "e") Else Print "key passed not foud"
  28. ShowDict Mydict(), 0, 0
  29. CompactDict Mydict()
  30. Print "Dictionary compacted"
  31. ShowDict Mydict(), 1, 0
  32. ' end of demo
  33.  
  34. ' it puts a couple key-value at the first free space in the array
  35. Function PutDict (dict() As String, Keys As String, Value As String)
  36.     PutDict = False
  37.     Dim Count As Integer
  38.     If FindDuplicateKey(dict(), Keys) = True Then Exit Function
  39.     For Count = 1 To UBound(dict, sKey)
  40.         If dict(Count, sKey) = "" Then Exit For
  41.     Next
  42.     dict(Count, sKey) = Keys
  43.     dict(Count, sValue) = Value
  44.     PutDict = True
  45.  
  46. 'it returns the value present at key passed
  47. Function GetDict$ (dict() As String, Keys As String)
  48.     GetDict$ = ""
  49.     Dim Count As Integer
  50.     For Count = 1 To UBound(dict, sKey)
  51.         If dict(Count, sKey) = Keys Then
  52.             GetDict$ = dict(Count, sValue)
  53.             Exit For
  54.         End If
  55.     Next
  56.  
  57. ' it changes the value related to a key passed
  58. Function ChangeValueDict (dict() As String, Keys As String, Value As String)
  59.     ChangeValueDict = False
  60.     Dim Count As Integer
  61.     For Count = 1 To UBound(dict, sKey)
  62.         If dict(Count, sKey) = Keys Then
  63.             dict(Count, sValue) = Value
  64.             Exit For
  65.         End If
  66.     Next
  67.     ChangeValueDict = True
  68.     If dict(Count, sValue) <> Value Then ChangeValueDict = False
  69.  
  70. 'it changes a key to another
  71. Function ChangeKeyValueDict (dict() As String, Keys As String, newKey As String)
  72.     ChangeKeyValueDict = False
  73.     Dim Count As Integer
  74.     'if there isn't keys  or if there is yet newKey , it exits
  75.     If FindDuplicateKey(dict(), newKey) = True Or FindDuplicateKey(dict(), Keys) = False Then Exit Function
  76.     For Count = 1 To UBound(dict, sKey)
  77.         If dict(Count, sKey) = Keys Then
  78.             dict(Count, sKey) = newKey
  79.             Exit For
  80.         End If
  81.     Next
  82.     ChangeKeyValueDict = True
  83.     If dict(Count, sKey) <> newKey Then ChangeKeyValueDict = False
  84.  
  85. 'it erases a couple with key Keys
  86. Function EraseKeyDict (dict() As String, Keys As String)
  87.     EraseKeyDict = False
  88.     Dim Count As Integer
  89.     For Count = 1 To UBound(dict, sKey)
  90.         If dict(Count, sKey) = Keys Then
  91.             dict(Count, sValue) = ""
  92.             dict(Count, sKey) = ""
  93.             Exit For
  94.         End If
  95.     Next
  96.     EraseKeyDict = True
  97.     If (dict(Count, sKey) <> "") Or (dict(Count, sValue) <> "") Then EraseKeyDict = False
  98.  
  99. 'it shows dict from start to the end or the full dict
  100. Sub ShowDict (dict() As String, Start As Integer, Ends As Integer)
  101.     Dim Count As Integer
  102.     If Start = 0 Or Start > Ends Or Start > UBound(dict, sKey) Then Start = 1
  103.     If Ends < Start Or Ends = 0 Or Ends > UBound(dict, sKey) Then Ends = UBound(dict, sKey)
  104.     For Count = Start To Ends
  105.         Print dict(Count, sKey); " - "; dict(Count, sValue)
  106.     Next
  107.  
  108. ' it finds a key if it is yet in the dict() array
  109. Function FindDuplicateKey (dict() As String, keys As String)
  110.     Dim Count As Integer
  111.     ' it searches a duplicate if it finds exit function
  112.     For Count = 1 To UBound(dict, sKey)
  113.         If dict(Count, sKey) = keys Then
  114.             FindDuplicateKey = True
  115.             Exit Function
  116.         End If
  117.     Next
  118.  
  119. 'it shrinks the dict  moving all keys to the bottom of array
  120. Sub CompactDict (dict() As String)
  121.     Dim count As Integer, count2 As Integer
  122.     For count = 1 To UBound(dict, sKey) - 1
  123.         If dict(count, sKey) = "" Then
  124.             For count2 = count + 1 To UBound(dict, sKey)
  125.                 If dict(count2, sKey) <> "" Then Exit For
  126.             Next
  127.             If count2 <= UBound(dict, sKey) Then Swap dict(count, sKey), dict(count2, sKey): Swap dict(count, sValue), dict(count2, sValue)
  128.         End If
  129.     Next

about the UDT version I seem to understand that it is ready to be published.
Good!
Programming isn't difficult, only it's  consuming time and coffee