Code
Swift Collections – A data structure implementations for the Swift

Swift Collections is an open-source package of data structure implementations for the Swift programming language.
Read more about the package, and the intent behind it, in the announcement on swift.org.
Contents
The package currently provides the following implementations:
-
Deque<Element>
, a double-ended queue backed by a ring buffer. Deques are range-replaceable, mutable, random-access collections. -
OrderedSet<Element>
, a variant of the standardSet
where the order of items is well-defined and items can be arbitrarily reordered. Uses aContiguousArray
as its backing store, augmented by a separate hash table of bit packed offsets into it. -
OrderedDictionary<Key, Value>
, an ordered variant of the standardDictionary
, providing similar benefits.
The following data structures are currently being worked on but they aren’t ready for inclusion in a tagged release:
Heap
andPriorityQueue
, min-max heaps backed by an array.SortedSet
andSortedDictionary
, sorted collections backed by in-memory persistent b-trees.HashSet
andHashMap
, persistent hashed collections implemented as Compressed Hash-Array Mapped Prefix-Trees (CHAMP).BitArray
andBitSet
, dynamic bit vectors.SparseSet
, a constant time set construct, trading off memory for speed.
Swift Collections uses the same modularization approach as Swift Numerics: it provides a standalone module for each thematic group of data structures it implements. For instance, if you only need a double-ended queue type, you can pull in only that by importing DequeModule
. OrderedSet
and OrderedDictionary
share much of the same underlying implementation, so they are provided by a single module, called OrderedCollections
. However, there is also a top-level Collections
module that gives you every collection type with a single import statement:
import Collections
var deque: Deque<String> = ["Ted", "Rebecca"]
deque.prepend("Keeley")
deque.append("Nathan")
print(deque) // ["Keeley", "Ted", "Rebecca", "Nathan"]
