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.
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 standard
Set where the order of items is well-defined and items can be arbitrarily reordered. Uses a
ContiguousArray as its backing store, augmented by a separate hash table of bit packed offsets into it.
OrderedDictionary<Key, Value>, an ordered variant of the standard
Dictionary, providing similar benefits.
The following data structures are currently being worked on but they aren't ready for inclusion in a tagged release:
PriorityQueue, min-max heaps backed by an array.
SortedDictionary, sorted collections backed by in-memory persistent b-trees.
HashMap, persistent hashed collections implemented as Compressed Hash-Array Mapped Prefix-Trees (CHAMP).
BitSet, 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
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"]
The Swift Collections package is source stable. The version numbers follow Semantic Versioning -- source breaking changes to public API can only land in a new major version.
The public API of version 1.0 of the
swift-collections package consists of non-underscored declarations that are marked
public in the
Interfaces that aren't part of the public API may continue to change in any release, including patch releases. If you have a use case that requires using underscored APIs, please submit a Feature Request describing it! We'd like the public interface to be as useful as possible -- although preferably without compromising safety or limiting future evolution.
By "underscored declarations" we mean declarations that have a leading underscore anywhere in their fully qualified name. For instance, here are some names that wouldn't be considered part of the public API, even if they were technically marked public:
Note that contents of the
Benchmarks subdirectories aren't public API. We don't make any source compatibility promises about them -- they may change at whim, and code may be removed in any new release. Do not rely on anything about them.
Future minor versions of the package may update these rules as needed.
We'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate. Accordingly, from time to time, we expect that new versions of this package will require clients to upgrade to a more recent Swift toolchain release. (This allows the package to make use of new language/stdlib features, build on compiler bug fixes, and adopt new package manager functionality as soon as they are available.) Requiring a new Swift release will only need a minor version bump.
To use this package in a SwiftPM project, you need to set it up as a package dependency:
// swift-tools-version:5.4 import PackageDescription let package = Package( name: "MyPackage", dependencies: [ .package( url: "https://github.com/apple/swift-collections.git", .upToNextMajor(from: "1.0.0") // or `.upToNextMinor ) ], targets: [ .target( name: "MyTarget", dependencies: [ .product(name: "Collections", package: "swift-collections") ] ) ] )
We have a dedicated Swift Collections Forum where people can ask and answer questions on how to use or work on this package. It's also a great place to discuss its evolution.
If you find something that looks like a bug, please open a Bug Report! Fill out as many details as you can.
We have some basic documentation on package internals that will help you get started.
By submitting a pull request, you represent that you have the right to license your contribution to Apple and the community, and agree by submitting the patch that your contributions are licensed under the Swift License, a copy of which is provided in this repository.
Like all Swift.org projects, we would like the Swift Collections project to foster a diverse and friendly community. We expect contributors to adhere to the Swift.org Code of Conduct. A copy of this document is available in this repository.
The current code owner of this package is Karoy Lorentey (@lorentey). You can contact him on the Swift forums, or by writing an email to klorentey at apple dot com. (Please keep it related to this project.)
In case of moderation issues, you can also directly contact a member of the Swift Core Team.
|Last commit: 13 weeks ago|
This release resolves issues uncovered since 1.0.1 was tagged, including a high severity crash in
OrderedDictionary. Upgrading is recommended for all clients.
OrderedDictionarythat could result in some mutation methods corrupting shared copies of the same value, leading to subsequent crashes. (Issue #104)
Deque.append(contentsOf:)now uses an exponential storage resizing strategy, as expected. Calling it in a loop no longer results in O(n) reallocations. (Issue #113)
Thank you to everyone who contributed to this release! Your bug reports, discussions and pull requests all help improve this package.