Swift Collections — Extending Swift Data Structures
--
- 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
, andOrderedDictionary
.
1) Deque
Deques
(pronounced “deck”), short for double-ended queues, are similar toArray
, 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 andappend
to insert an element at the end of a deque. - Similarly, you can use
popFirst
andpopLast
to pop elements from the deque ends.
- Prepending an element is a constant time operation for
Deque
, but a linear time operation forArray
. - Accessing an element at an arbitrary offset is a constant time operation for both
Deque
andArray
.
2) OrderedSet
OrderedSet
is a powerful hybrid of anArray
and aSet
.- Like
Set
, it ensures that each of their elements is unique and provides efficient membership tests withcontains
, while preserving the same order as they were appended just likeArray
. - We can create an ordered set with any element type that conforms to the
Hashable
protocol:
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 forArray
.
3) OrderedDictionary
- An
OrderedDictionary
provides many of the same operations asDictionary
, while preserving the same order as they were appended. - It consists of an
OrderedSet
of keys and a regularArray
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.
OrderedDictionary
uses integer indices with the first element always start at0
.- To avoid ambiguity between key-based and index-based subscripts,
OrderedDictionary
doesn’t conform toCollection
directly. Instead, it provides a random-access view over its key-value pairs:
- 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