What is the purpose of DictionaryIndex in Swift?

In the Dictionary header documentation in Swift:

Hash-based mapping from Key instances to Value . Also collecting key-value pairs in no particular order.

Pay attention in particular to no defined ordering .

With this in mind, I had problems fully understanding these calculated variables (and the associated methods that take these types):

 // The position of the first element in a non-empty dictionary. var startIndex: DictionaryIndex<Key, Value> { get } // The collection "past the end" position. var endIndex: DictionaryIndex<Key, Value> { get } 

The "index" here is DictionaryIndex .

However, the DictionaryIndex documentation here looks circular:

Used to access key-value pairs in an instance of Dictionary<Key, Value> .

What is the purpose of DictionaryIndex ?

+6
source share
1 answer

We know that a Dictionary consists of keys and values. Each key is mapped to a value based on some internal calculations. Here's the mechanism used for this purpose, hashing .

From Wikipedia:

The hash table uses a hash function to calculate the index into an array of buckets or slots from which you can find the correct value.

Note that a Dictionary is a hash table that uses some hash function and returns an object of type DictionaryIndex - with which you can access a specific object directly in the dictionary.

Correct me if I am wrong!

+2
source

Source: https://habr.com/ru/post/987180/


All Articles