|
1 | 1 | import Foundation |
2 | 2 |
|
| 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 | +/// ``` |
3 | 14 | 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`. |
4 | 23 | public static func == (lhs: PNNode, rhs: PNNode) -> Bool { |
5 | 24 | lhs === rhs |
6 | 25 | } |
7 | 26 |
|
| 27 | + /// A unique identifier for the node. |
8 | 28 | 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. |
9 | 34 | public var dependencies: [PNNode] { |
10 | 35 | synchronization.withLock { |
11 | 36 | unsafeDependencies |
12 | 37 | } |
13 | 38 | } |
| 39 | + |
| 40 | + /// The internal storage for dependencies. |
| 41 | + /// |
| 42 | + /// This is used internally to store the actual dependencies in a thread-safe manner. |
14 | 43 | 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. |
15 | 48 | private let synchronization = NSLock() |
16 | 49 |
|
| 50 | + /// Initializes a new instance of `PNNode` with the specified identifier. |
| 51 | + /// |
| 52 | + /// - Parameter newIdentifier: A unique identifier for the node. |
17 | 53 | init(identifier newIdentifier: String) { |
18 | 54 | identifier = newIdentifier |
19 | 55 | } |
20 | 56 |
|
| 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. |
21 | 62 | public func addDependency(node: PNNode) { |
22 | 63 | synchronization.withLock { |
23 | 64 | unsafeDependencies.append(node) |
|
0 commit comments