Swiftpack.co -  lukaskubanek/OrderedDictionary as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
lukaskubanek/OrderedDictionary
Ordered dictionary data structure implementation in Swift
.package(url: "https://github.com/lukaskubanek/OrderedDictionary.git", from: "v4.0.0")

OrderedDictionary

Swift 5.0 Swift Package Manager Carthage License: MIT Twitter: @lukaskubanek

OrderedDictionary is a lightweight implementation of an ordered dictionary data structure in Swift.

The OrderedDictionary<Key: Hashable, Value> struct is a generic collection that combines the features of the Dictionary and 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.

Internally, 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.

Requirements

  • Swift 5.0 or later
  • Xcode 11 or later
  • iOS 8 or later / macOS 10.10 or later

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.

Installation

Swift Package Manager

To install OrderedDictionary using the Swift Package Manager, add it as a dependency into your Package.swift file:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/lukaskubanek/OrderedDictionary.git", from: "4.0.0")
    ],
    ...
)

Carthage

To install OrderedDictionary using Carthage, add it as a dependency into your Cartfile:

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.

Git Submodules

You can also install OrderedDictionary via Git submodules and integrate the project OrderedDictionary.xcodeproj from the submodule directly into your Xcode workspace.

Note About CocoaPods

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).

Usage & Docs

For the explanation of the API of OrderedDictionary and examples on how to use this data structure, please refer to the documentation comments or the comprehensive test suite.

Changelog

To view the changelog, refer to GitHub's releases page. If you're upgrading from version 3.x, you might want to check out the list of changes made in version 4.0.

Credits

OrderedDictionary was built by @lukaskubanek, the founder and developer of Diagrams, a native diagram editor for Mac. This data structure is being used extensively for powering Diagrams' data model.

GitHub

link
Stars: 180
Last commit: 7 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Related Packages

Release Notes

4.0.0: MutableCollection & RandomAccessCollection
16 weeks 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.

  • OrderedDictionary now 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.
  • OrderedDictionary now conforms to RandomAccessCollection instead of BidirectionalCollection only, making the index-based traversal more efficient. [#64]
  • Re-introduced a dedicated slice type OrderedDictionarySlice<Key, Value> and added support for replacing a certain range of the OrderedDictionary with different key-value pairs.
  • Introduced OrderedDictionary.filter(_:) that actually returns an OrderedDictionary instead of an array of key-value pairs. [#66]
  • Changed the type of OrderedDictionary.orderedKeys from 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]
  • The type of OrderedDictionary.orderedValues is still a lazily evaluated collection, but instead of the top-level OrderedDictionaryValues<Key, Value> type alias, it now uses an inner type alias OrderedDictionary.LazyValues.
  • Deprecated 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 (Element).
  • Improved performance of in-place sorting via OrderedDictionary.sort(by:).
  • Added conformance to Hashable.
  • Dropped support for Swift 4.2, OrderedDictionary now requires Swift ≥5.0.
  • Tweaked precondition messages and added invariant assertions.
  • Revamped the whole test suite.
  • Removed example playgrounds and moved examples directly to documentation comments.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API