Files
swift-algorithms/Sources/Algorithms/Keyed.swift
T
Nate Cook 0bd7f4dcb1 Migrate to typed errors across the board
Along with converting to typed errors, this also bumps the swiftlang
version in Package.swift to 6.0 (to support typed errors).
2024-12-22 15:58:42 -06:00

69 lines
2.9 KiB
Swift
Executable File

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Algorithms open source project
//
// Copyright (c) 2020 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
//
//===----------------------------------------------------------------------===//
extension Sequence {
/// Creates a new Dictionary from the elements of `self`, keyed by the
/// results returned by the given `keyForValue` closure.
///
/// If the key derived for a new element collides with an existing key from a previous element,
/// the latest value will be kept.
///
/// - Parameters:
/// - keyForValue: A closure that returns a key for each element in `self`.
@inlinable
public func keyed<Key, E: Error>(
by keyForValue: (Element) throws(E) -> Key
) throws(E) -> [Key: Element] {
// An anonymous closure is inferred as `throws(any Error)`, so we give it
// the correct type here.
let resolve: (Key, Element, Element) throws(E) -> Element = { _, _, new in new }
return try self.keyed(by: keyForValue, resolvingConflictsWith: resolve)
}
/// Creates a new Dictionary from the elements of `self`, keyed by the
/// results returned by the given `keyForValue` closure. As the dictionary is
/// built, the initializer calls the `resolve` closure with the current and
/// new values for any duplicate keys. Pass a closure as `resolve` that
/// returns the value to use in the resulting dictionary: The closure can
/// choose between the two values, combine them to produce a new value, or
/// even throw an error.
///
/// - Parameters:
/// - keyForValue: A closure that returns a key for each element in `self`.
/// - resolve: A closure that is called with the values for any duplicate
/// keys that are encountered. The closure returns the desired value for
/// the final dictionary.
@inlinable
public func keyed<Key, E: Error>(
by keyForValue: (Element) throws(E) -> Key,
resolvingConflictsWith resolve: (Key, Element, Element) throws(E) -> Element
) throws(E) -> [Key: Element] {
var result = [Key: Element]()
for element in self {
let key = try keyForValue(element)
if let oldValue = result.updateValue(element, forKey: key) {
let valueToKeep = try resolve(key, oldValue, element)
// This causes a second look-up for the same key. The standard library can avoid that
// by calling `mutatingFind` to get access to the bucket where the value will end up,
// and updating in place.
// Swift Algorithms doesn't have access to that API, so we make do.
// When this gets merged into the standard library, we should optimize this.
result[key] = valueToKeep
}
}
return result
}
}