Connect with us

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 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:

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 DequeModuleOrderedSet 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"]
Swift Collections on GitHub: https://github.com/apple/swift-collections
Platform: iOS/Swift
⭐️: 2.3K
Advertisement

Trending