Skip to content

Commit 5ab4c23

Browse files
Merge pull request #4 from P0rc3lain/docs-update
Docs update
2 parents 2434757 + 2269396 commit 5ab4c23

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

Sources/PNDependencyGraph/PNNode.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
11
import Foundation
22

3+
/// A node in a dependency graph.
4+
///
5+
/// The `PNNode` class represents a single node within a dependency graph. Each node
6+
/// has a unique identifier and can have dependencies on other nodes.
7+
///
8+
/// Example usage:
9+
/// ```swift
10+
/// let node = PNNode(identifier: "node1")
11+
/// let dependency = PNNode(identifier: "node2")
12+
/// node.addDependency(node: dependency)
13+
/// ```
314
public final class PNNode: @unchecked Sendable, Equatable {
15+
/// Compares two `PNNode` instances for equality.
16+
///
17+
/// Two nodes are considered equal if they are the same instance (identity comparison).
18+
///
19+
/// - Parameters:
20+
/// - lhs: The left-hand side node to compare.
21+
/// - rhs: The right-hand side node to compare.
22+
/// - Returns: `true` if the nodes are identical; otherwise, `false`.
423
public static func == (lhs: PNNode, rhs: PNNode) -> Bool {
524
lhs === rhs
625
}
726

27+
/// A unique identifier for the node.
828
public let identifier: String
29+
30+
/// The dependencies of this node.
31+
///
32+
/// This property returns an array of nodes that this node depends on.
33+
/// The dependencies are thread-safe and can be accessed from multiple threads.
934
public var dependencies: [PNNode] {
1035
synchronization.withLock {
1136
unsafeDependencies
1237
}
1338
}
39+
40+
/// The internal storage for dependencies.
41+
///
42+
/// This is used internally to store the actual dependencies in a thread-safe manner.
1443
private var unsafeDependencies: [PNNode] = []
44+
45+
/// A lock used to synchronize access to dependencies.
46+
///
47+
/// This ensures that the `dependencies` property can be safely accessed from multiple threads.
1548
private let synchronization = NSLock()
1649

50+
/// Initializes a new instance of `PNNode` with the specified identifier.
51+
///
52+
/// - Parameter newIdentifier: A unique identifier for the node.
1753
init(identifier newIdentifier: String) {
1854
identifier = newIdentifier
1955
}
2056

57+
/// Adds a dependency to this node.
58+
///
59+
/// This method adds a node to the list of dependencies for this node.
60+
///
61+
/// - Parameter node: The node that this node depends on.
2162
public func addDependency(node: PNNode) {
2263
synchronization.withLock {
2364
unsafeDependencies.append(node)

Sources/PNDependencyGraph/Visitors/PNMultithreadVisitor.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
import Foundation
22

3+
/// A multithreaded visitor for traversing a compiled dependency graph.
4+
///
5+
/// The `PNMultithreadVisitor` class allows for concurrent traversal of a dependency graph,
6+
/// executing node visits in parallel using an operation queue. It ensures that the first
7+
/// node is visited synchronously while all other nodes are processed in parallel.
8+
///
9+
/// Example usage:
10+
/// ```swift
11+
/// let visitor = PNMultithreadVisitor(graph: myGraph)
12+
/// visitor.visit { node in
13+
/// print("Visiting node: \(node)")
14+
/// }
15+
/// ```
316
public final class PNMultithreadVisitor {
417
private let graph: PNCompiledGraph
518
private let queue = OperationQueue()
19+
20+
/// Initializes a new instance of `PNMultithreadVisitor` with the specified graph.
21+
///
22+
/// - Parameter newGraph: The compiled dependency graph to visit.
623
public init(graph newGraph: PNCompiledGraph) {
724
graph = newGraph
825
queue.qualityOfService = .userInitiated
926
queue.maxConcurrentOperationCount = graph.nodes.count - 1
1027
}
1128

29+
/// Traverses the dependency graph, visiting each node.
30+
///
31+
/// The first node in the graph is visited synchronously, while all other nodes
32+
/// are processed concurrently in an operation queue.
33+
///
34+
/// - Parameter completion: A closure to be called for each visited node.
1235
public func visit(completion: @Sendable @escaping (PNNode) -> Void) {
1336
guard let first = graph.nodes.first else {
1437
return

Sources/PNDependencyGraph/Visitors/PNSingleThreadVisitor.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
public final class PNSingleThreadVisitor {
2+
/// A single-threaded visitor for traversing a compiled dependency graph.
3+
///
4+
/// The `PNSingleThreadVisitor` class allows for sequential traversal of a dependency graph,
5+
/// executing node visits in a single thread. All nodes are processed in the order they appear
6+
/// in the graph.
7+
///
8+
/// Example usage:
9+
/// ```swift
10+
/// let visitor = PNSingleThreadVisitor(graph: myGraph)
11+
/// visitor.visit { node in
12+
/// print("Visiting node: \(node)")
13+
/// }
14+
/// ```
215
private let graph: PNCompiledGraph
16+
17+
/// Initializes a new instance of `PNSingleThreadVisitor` with the specified graph.
18+
///
19+
/// - Parameter graph: The compiled dependency graph to visit.
320
public init(graph: PNCompiledGraph) {
421
self.graph = graph
522
}
623

24+
/// Traverses the dependency graph, visiting each node in sequence.
25+
///
26+
/// All nodes in the graph are visited one after another in the order they appear.
27+
///
28+
/// - Parameter completion: A closure to be called for each visited node.
729
public func visit(completion: (PNNode) -> Void) {
830
for node in graph.nodes {
931
completion(node)

0 commit comments

Comments
 (0)