Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of a DictionaryIndex in Swift?

Tags:

ios

swift

Per the header documentation on Dictionary in Swift:

A hash-based mapping from Key to Value 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?

like image 890
JRG-Developer Avatar asked May 14 '15 06:05

JRG-Developer


1 Answers

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!

like image 107
NightFury Avatar answered Sep 17 '22 16:09

NightFury