Per the header documentation on Dictionary
in Swift:
A hash-based mapping from
Key
toValue
instances. Also a collection of key-value pairs with no defined ordering.
Note in particular- no defined ordering
.
With this in mind, I'm having trouble fully understanding these computed variables (and the related methods that take these types):
// The position of the first element in a non-empty dictionary.
var startIndex: DictionaryIndex<Key, Value> { get }
// The collection's "past the end" position.
var endIndex: DictionaryIndex<Key, Value> { get }
The "index" here is a DictionaryIndex
.
However, the documentation on DictionaryIndex
is kinda circular here:
Used to access the key-value pairs in an instance of
Dictionary<Key, Value>
.
What actually is the purpose of DictionaryIndex
?
We know that a Dictionary
is composed of keys and values. Every key is mapped to a value based on some internal calculations. Here the mechanism used for this purpose is Hashing.
From wikipedia:
A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.
Consider that a Dictionary
is a Hash Table, which uses some hash function and returns an object of type DictionaryIndex
- using which you can access particular object directly in the Dictionary.
Correct me if I am wrong!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With