Files
swift-collections/Sources/SortedCollections/SortedDictionary/SortedDictionary.swift
T
Vihan Bhargava 98f278545d Continue applying refactors and optimizations.
Key Changes:
 - _BTree.init() dynamically computes capacity from stride
 - _BTree.UnsafePath uses Unmanaged +0 references.

Minor Changes:
 - Fix _BTree.Iterator bug on single-leaf trees.
2021-08-09 19:13:50 -07:00

177 lines
6.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
/// A collection which maintains key-value pairs in ascending sorted order.
///
/// A sorted dictionary is a type of tree, providing efficient read and write operations
/// to the entries it contains. Each entry in the sorted dictionary is identified using a
/// key, which is a comparable type such as a string or number. You can use that key
/// to retrieve the corresponding value.
public struct SortedDictionary<Key: Comparable, Value> {
/// An element of the sorted dictionary. A key-value tuple.
public typealias Element = (key: Key, value: Value)
@usableFromInline
internal typealias _Tree = _BTree<Key, Value>
@usableFromInline
internal var _root: _Tree
/// Creates an empty dictionary.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public init() {
self._root = _Tree()
}
/// Creates a dictionary rooted at a given BTree.
@inlinable
internal init(_rootedAt tree: _Tree) {
self._root = tree
}
}
// MARK: Accessing Keys and Values
extension SortedDictionary {
/// A read-only collection view for the keys contained in this dictionary, as
/// an `SortedSet`.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var keys: Keys { Keys(_base: self) }
/// A mutable collection view containing the values in this dictionary.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var values: Values {
// TODO: consider implementing a _read accessor.
get {
Values(_base: self)
}
_modify {
// TODO: avoid allocating a new node
var values = Values(_base: SortedDictionary())
swap(&self, &values._base)
defer { self = values._base }
yield &values
}
}
}
// MARK: Mutations
extension SortedDictionary {
/// Updates the value stored in the dictionary for the given key, or
/// adds a new key-value pair if the key does not exist.
///
/// Use this method instead of key-based subscripting when you need to
/// know whether the new value supplants the value of an existing key. If
/// the value of an existing key is updated, `updateValue(_:forKey:)`
/// returns the original value.
///
/// - Parameters:
/// - value: The new value to add to the dictionary.
/// - key: The key to associate with value. If key already exists in the
/// dictionary, value replaces the existing associated value. If key
/// isn’t already a key of the dictionary, the `(key, value)` pair
/// is added.
/// - Returns: The value that was replaced, or nil if a new key-value
/// pair was added.
/// - Complexity: O(`log n`) where `n` is the number of key-value pairs in the
/// dictionary.
@inlinable
@discardableResult
public mutating func updateValue(_ value: Value, forKey key: Key) -> Value? {
self._root.updateAnyValue(value, forKey: key)?.value
}
}
// MARK: Removing Keys and Values
extension SortedDictionary {
/// Removes the given key and its associated value from the sorted dictionary.
///
/// Calling this method invalidates any existing indices for use with this sorted dictionary.
///
/// - Parameter key: The key to remove along with its associated value.
/// - Returns: The value that was removed, or `nil` if the key was not present
/// in the dictionary.
/// - Complexity: O(`log n`) where `n` is the number of key-value pairs in the
/// dictionary.
@inlinable
@inline(__always)
public mutating func removeValue(forKey key: Key) -> Value? {
return self._root.removeAnyElement(forKey: key)?.value
}
}
// MARK: Transforming a Dictionary
extension SortedDictionary {
/// Returns a new dictionary containing the keys of this dictionary with the values
/// transformed by the given closure.
///
/// - Parameter transform: A closure that transforms a value. transform accepts
/// each value of the dictionary in order as its parameter and returns a transformed
/// value of the same or of a different type.
/// - Returns: A sorted dictionary containing the keys and transformed values of
/// this dictionary.
/// - Complexity: O(`n`)
@inlinable
@inline(__always)
public func mapValues<T>(
_ transform: (Value) throws -> T
) rethrows -> SortedDictionary<Key, T> {
// TODO: Optimize to reuse dictionary structure.
// TODO: optimize to use identify fastest iteration method
// TODO: optimize CoW checks
var newDictionary = SortedDictionary<Key, T>()
for (key, value) in self {
newDictionary.updateValue(try transform(value), forKey: key)
}
return newDictionary
}
/// Returns a new dictionary containing only the key-value pairs that have non-nil values
/// as the result of transformation by the given closure.
///
/// Use this method to receive a dictionary with non-optional values when your transformation
/// produces optional values.
///
/// - Parameter transform: A closure that transforms a value. `transform` accepts
/// each value of the dictionary as its parameter and returns an optional transformed
/// value of the same or of a different type.
/// - Returns: A sorted dictionary containing the keys and non-nil transformed values
/// of this dictionary.
/// - Complexity: O(`n`)
func compactMapValues<T>(
_ transform: (Value) throws -> T?
) rethrows -> SortedDictionary<Key, T> {
// TODO: Optimize to reuse dictionary structure.
// TODO: optimize to use identify fastest iteration method
// TODO: optimize CoW checks
var newDictionary = SortedDictionary<Key, T>()
for (key, value) in self {
if let value = try transform(value) {
newDictionary.updateValue(value, forKey: key)
}
}
return newDictionary
}
}