Swift Collections — Extending Swift Data Structures

Milan Panchal
Geek Culture
Published in
3 min readMay 8, 2021

--

Swift Collections — Deque | OrderedSet | OrderedDictionary
  • Swift Collections, a new open-source package focused on extending the set of available Swift data structures.
  • The package currently provides the implementations of the most frequently requested data structures: Deque, OrderedSet, and OrderedDictionary.

1) Deque

  • Deques (pronounced “deck”), short for double-ended queues, are similar to Array, but support efficient insertions and removals at both ends. This makes deques a great choice whenever we need a FIFO queue.
  • You can use the prepend method to insert a new element at the beginning and append to insert an element at the end of a deque.
  • Similarly, you can use popFirst and popLast to pop elements from the deque ends.
Swift Collection — Deque Example
  • Prepending an element is a constant time operation for Deque, but a linear time operation for Array.
  • Accessing an element at an arbitrary offset is a constant time operation for both Deque and Array.

2) OrderedSet

  • OrderedSet is a powerful hybrid of an Array and a Set.
  • Like Set, it ensures that each of their elements is unique and provides efficient membership tests with contains, while preserving the same order as they were appended just like Array.
  • We can create an ordered set with any element type that conforms to the Hashable protocol:
Swift Collection — OrderedSet Example
  • Appending an element, which includes ensuring it’s unique, is a constant time operation for OrderedSet.
  • Membership testing is a constant time operation for OrderedSet, but a linear time operation for Array.

3) OrderedDictionary

  • An OrderedDictionary provides many of the same operations as Dictionary, while preserving the same order as they were appended.
  • It consists of an OrderedSet of keys and a regular Array of elements, and both can be efficiently accessed using the .keys and .elements properties.

We can create an ordered dictionary with any key type that conforms to the Hashable protocol. If a new entry is added, it gets appended to the end of the dictionary.

Swift Collection — OrderedDictionary Example
  • OrderedDictionary uses integer indices with the first element always start at 0.
  • To avoid ambiguity between key-based and index-based subscripts, OrderedDictionary doesn’t conform to Collection directly. Instead, it provides a random-access view over its key-value pairs:
Swift Collection — OrderedDictionary key-based & index-based subscripts
  • Inserting a new key-value pair into an OrderedDictionary appends it in constant time.
  • Looking up a value for a key is a constant time operation for OrderedDictionary.

Contributing to Swift Collections

Swift Collections is available on GitHub, and contributions are welcome, although they should meet stringent criteria as to reliability, performance, and memory usage, says Karoy Lorentey

Questions?

Please feel free to comment below, if you have any questions.

If you like this article, feel free to share it with your friends and leave me a comment. Also, click on the 👏 clap button below to show how much you like the article.

Thanks for reading! 👨🏼‍💻

You can find me on:

Twitter | LinkedIn | GitHub | Medium | HackerRank | LeetCode | Stack Overflow

--

--

Milan Panchal
Geek Culture

iOS Team Lead ǁ Swift ǁ Objective-C ǁ Lifelong Learner ǁ Blogger — Join Medium from the following link: https://medium.com/@milanpanchal24/membership