Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions Sources/Testing/Support/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,36 +389,34 @@ extension Graph {
/// The recursive implementation of `forEach(_:)`.
///
/// - Parameters:
/// - keyPath: The key path to use for the root node when passing it to
/// `body`.
/// - keyPath: The key path of the current node, passed to `body`.
/// - body: A closure that is invoked once per element in the graph. The key
/// path and leaf value of each node are passed to the closure.
///
/// - Throws: Whatever is thrown by `body`.
private func _forEach<E>(keyPath: [K], _ body: (Element) throws(E) -> Void) throws(E) {
private func _forEach<E>(keyPath: inout [K], _ body: (Element) throws(E) -> Void) throws(E) {
try body((keyPath, value))
for (key, child) in children {
var childKeyPath = keyPath
childKeyPath.append(key)
try child._forEach(keyPath: childKeyPath, body)
keyPath.append(key)
try child._forEach(keyPath: &keyPath, body)
keyPath.removeLast()
}
}

/// The recursive implementation of `forEach(_:)`.
///
/// - Parameters:
/// - keyPath: The key path to use for the root node when passing it to
/// `body`.
/// - keyPath: The key path of the current node, passed to `body`.
/// - body: A closure that is invoked once per element in the graph. The
/// key path and leaf value of each node are passed to the closure.
///
/// - Throws: Whatever is thrown by `body`.
private func _forEach<E>(keyPath: [K], _ body: (Element) async throws(E) -> Void) async throws(E) {
private func _forEach<E>(keyPath: inout [K], _ body: (Element) async throws(E) -> Void) async throws(E) {
try await body((keyPath, value))
for (key, child) in children {
var childKeyPath = keyPath
childKeyPath.append(key)
try await child._forEach(keyPath: childKeyPath, body)
keyPath.append(key)
try await child._forEach(keyPath: &keyPath, body)
keyPath.removeLast()
}
}

Expand All @@ -432,7 +430,8 @@ extension Graph {
///
/// This function iterates depth-first.
func forEach<E>(_ body: (Element) throws(E) -> Void) throws(E) {
try _forEach(keyPath: []) { (element) throws(E) in
var keyPath: [K] = []
try _forEach(keyPath: &keyPath) { (element) throws(E) in
try body(element)
}
}
Expand All @@ -447,7 +446,8 @@ extension Graph {
///
/// This function iterates depth-first.
func forEach<E>(_ body: (Element) async throws(E) -> Void) async throws(E) {
try await _forEach(keyPath: []) { (element) async throws(E) in
var keyPath: [K] = []
try await _forEach(keyPath: &keyPath) { (element) async throws(E) in
try await body(element)
}
}
Expand Down Expand Up @@ -546,16 +546,16 @@ extension Graph {
///
/// This function iterates depth-first.
func compactMapValues<U, E>(_ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph<K, U>? {
try _compactMapValues(keyPath: []) { (element) throws(E) in
var keyPath: [K] = []
return try _compactMapValues(keyPath: &keyPath) { (element) throws(E) in
try transform(element)
}
}

/// The recursive implementation of `compactMapValues(_:)`.
///
/// - Parameters:
/// - keyPath: The key path to use for the root node when passing it to
/// `transform`.
/// - keyPath: The key path of the current node, passed to `transform`.
/// - transform: A closure that is invoked once per element in the graph.
/// The key path and leaf value of each node are passed to this closure.
/// The result of the closure is a tuple containing the new value and
Expand All @@ -565,21 +565,21 @@ extension Graph {
/// child nodes are omitted from the new graph.
///
/// - Throws: Whatever is thrown by `transform`.
private func _compactMapValues<U, E>(keyPath: [K], _ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph<K, U>? {
private func _compactMapValues<U, E>(keyPath: inout [K], _ transform: (Element) throws(E) -> (U, recursivelyApply: Bool)?) throws(E) -> Graph<K, U>? {
guard let (newValue, recursivelyApply) = try transform((keyPath, value)) else {
return nil
}

var newChildren = [K: Graph<K,U>]()
newChildren.reserveCapacity(children.count)
for (key, child) in children {
var childKeyPath = keyPath
childKeyPath.append(key)
keyPath.append(key)
defer { keyPath.removeLast() }

if recursivelyApply {
newChildren[key] = child._compactMapValues(keyPath: childKeyPath) { _ in (newValue, true) }
newChildren[key] = child._compactMapValues(keyPath: &keyPath) { _ in (newValue, true) }
} else {
newChildren[key] = try child._compactMapValues(keyPath: childKeyPath, transform)
newChildren[key] = try child._compactMapValues(keyPath: &keyPath, transform)
}
}

Expand All @@ -606,16 +606,16 @@ extension Graph {
///
/// This function iterates depth-first.
func compactMapValues<U, E>(_ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph<K, U>? {
try await _compactMapValues(keyPath: []) { (element) async throws(E) in
var keyPath: [K] = []
return try await _compactMapValues(keyPath: &keyPath) { (element) async throws(E) in
try await transform(element)
}
}

/// The recursive implementation of `compactMapValues(_:)`.
///
/// - Parameters:
/// - keyPath: The key path to use for the root node when passing it to
/// `transform`.
/// - keyPath: The key path of the current node, passed to `transform`.
/// - transform: A closure that is invoked once per element in the graph.
/// The key path and leaf value of each node are passed to this closure.
/// The result of the closure is a tuple containing the new value and
Expand All @@ -625,21 +625,21 @@ extension Graph {
/// child nodes are omitted from the new graph.
///
/// - Throws: Whatever is thrown by `transform`.
private func _compactMapValues<U, E>(keyPath: [K], _ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph<K, U>? {
private func _compactMapValues<U, E>(keyPath: inout [K], _ transform: (Element) async throws(E) -> (U, recursivelyApply: Bool)?) async throws(E) -> Graph<K, U>? {
guard let (newValue, recursivelyApply) = try await transform((keyPath, value)) else {
return nil
}

var newChildren = [K: Graph<K,U>]()
newChildren.reserveCapacity(children.count)
for (key, child) in children {
var childKeyPath = keyPath
childKeyPath.append(key)
keyPath.append(key)
defer { keyPath.removeLast() }

if recursivelyApply {
newChildren[key] = child._compactMapValues(keyPath: childKeyPath) { _ in (newValue, true) }
newChildren[key] = child._compactMapValues(keyPath: &keyPath) { _ in (newValue, true) }
} else {
newChildren[key] = try await child._compactMapValues(keyPath: childKeyPath, transform)
newChildren[key] = try await child._compactMapValues(keyPath: &keyPath, transform)
}
}

Expand Down
Loading