OrderedDictionary is a lightweight implementation of an ordered dictionary data structure in Swift.
OrderedDictionary<Key: Hashable, Value> struct is a generic collection that combines the features of the
Array data structures from the Swift standard library. Like
Dictionary, it stores key-value pairs with each key being unique and maps each key to an associated value. Like
Array, it stores those pairs sorted and accessible by a zero-based integer index.
OrderedDictionary provides similar APIs to collections available in the Swift standard library like accessing contents by keys or indices, inserting and removing elements, iterating, sorting, transforming, filtering, etc.
OrderedDictionary uses backing storage composed of a
Dictionary for storing the key-value pairs and an
Array for managing the ordered keys. This is certainly not the most performant implementation one can achieve, but it gets its job done while reusing most functionality from the Swift standard library.
For support of older Swift versions, please refer to older versions of this library. For Swift 4.2, use version 3.x, and for Swift 4.0-4.1, use version 2.x.
The requirements for Xcode and OS versions only apply when the library is integrated as a framework or via the Xcode project.
To install OrderedDictionary using the Swift Package Manager, add it as a dependency into your
let package = Package( ... dependencies: [ .package(url: "https://github.com/lukaskubanek/OrderedDictionary.git", from: "4.0.0") ], ... )
To install OrderedDictionary using Carthage, add it as a dependency into your
github "lukaskubanek/OrderedDictionary" ~> 4.0
Then drag either the
OrderedDictionary.xcodeproj or the
OrderedDictionary.framework into your Xcode project/workspace and link your target against the
OrderedDictionary.framework. Make sure that the framework gets copied to your application bundle.
You can also install OrderedDictionary via Git submodules and integrate the project
OrderedDictionary.xcodeproj from the submodule directly into your Xcode workspace.
Although there has been a high demand for CocoaPods support, this distribution method won't be officially supported by this library. If you really want to integrate this library via CocoaPods, you can create and maintain a custom podspec (see the last section of this post).
|Last commit: 1 week ago|
This is a major overhaul of
OrderedDictionary's API that contains breaking changes. If you run into any issues upgrading from previous versions, please let me know.
OrderedDictionarynow conforms to
MutableCollection, which comes with the support for directly setting key-value pairs through a subscript as well as reordering of the whole collection or its slices.
OrderedDictionarynow conforms to
BidirectionalCollectiononly, making the index-based traversal more efficient. [#64]
OrderedDictionarySlice<Key, Value>and added support for replacing a certain range of the
OrderedDictionarywith different key-value pairs.
OrderedDictionary.filter(_:)that actually returns an
OrderedDictionaryinstead of an array of key-value pairs. [#66]
OrderedDictionary.orderedKeysfrom a lazily evaluated collection type type-aliased with
OrderedDictionaryKeys<Key, Value>to a
[Key]array. The property directly exposes the internal backing storage, which makes accessing keys more comfortable and prevents unnecessary type conversions. [#67]
OrderedDictionary.orderedValuesis still a lazily evaluated collection, but instead of the top-level
OrderedDictionaryValues<Key, Value>type alias, it now uses an inner type alias
OrderedDictionary.moveElement(forKey:to:)as it was based on a very specific move logic which turned out not to be generally applicable.
OrderedDictionary.update(_:at:)now returns a non-optional key-value pair (