10-08-2024, 02:27 AM
(10-07-2024, 07:18 PM)Dimster Wrote: I've just recently come across a Dictionary as an alternative to an Array. Doesn't appear that QB64PE supports Dictionaries. Does anyone who codes in languages which do use Dictionaries find them the same as using an Array or better than an Array or offering a completely different manipulation of data than an Array?Arrays and dictionary are quite different really, whether you want one or the other depends on how you intend to use it. That said I'd say dictionaries are typically more useful.
The details depend on the particular dictionary implementation, but typically a dictionary (if implemented via a hash table) will offer constant-time lookup, insert, and delete. An array in contrast offers constant-time lookup but a more expensive insert and delete. The disadvantage is that enumerating over all the entries in an dictionary is typically expensive and the entries are not always in a particular order, where-as enumerating over an array is about as fast as it gets.
If speed is not a real concern then you can use a dictionary in place of an array with no real issue. Otherwise, an array is appropriate if the data rarely gets entries added or removed and/or if you often loop over all the entries. A dictionary is better if insertions and removals are very often and you don't typically need to loop over all the entries.
Also note that the implementation backing the dictionary can greatly impact it's actual performance. A hash table is the typical implementation but that's not guaranteed, it could also be a binary search tree or just a sorted list. The latter two have the advantage of being ordered and are typically faster to enumerate, but a hash table will normally offer better performance for lookup/insert/delete. For the binary search tree, it's not significantly different, but a sorted list is basically just the same performance as an array.
For QB64-PE support, it's not built-in but you could create a dictionary yourself. I typically prefer the array of linked-lists approach as it's pretty easy to make (performance is good assuming you size the underlying array appropriately). I'm not so sure how easy it would be to make in QB64-PE though. You could also skip the linked-lists and just use the array with an approach to handle hash collisions.

