Skip to content

Commit b0f956e

Browse files
Merge pull request #5 from P0rc3lain/rest-of-docs
Add docs for the rest of classes
2 parents 5ab4c23 + be4bc65 commit b0f956e

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Foundation
2+
3+
public enum PNCompilationError: Error {
4+
/// Indicates that the graph contains a cycle and cannot be compiled.
5+
case cycle
6+
}

Sources/PNDependencyGraph/PNCompiledGraph.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22

3+
/// A compiled dependency graph.
34
public final class PNCompiledGraph {
45
let nodes: [PNNode]
56
init(nodes newNodes: [PNNode]) {

Sources/PNDependencyGraph/PNDependencyGraph.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
11
import Foundation
22

3-
public enum PNCompilationError: Error {
4-
case cycle
5-
}
6-
3+
/// The main class for building and compiling dependency graphs.
4+
///
5+
/// This class provides methods to add nodes, define dependencies between them,
6+
/// and compile the graph into a topologically sorted order.
77
public final class PNGraph {
8+
/// A dictionary of all nodes in the graph, keyed by their identifiers.
89
private var nodes: [String: PNNode] = [:]
910

11+
/// Adds a new node to the graph with the specified identifier.
12+
///
13+
/// If a node with this identifier already exists, it is returned instead of creating a new one.
14+
///
15+
/// - Parameter identifier: A unique identifier for the node.
16+
/// - Returns: The node associated with the provided identifier.
1017
public func add(identifier: String) -> PNNode {
1118
if let existing = nodes[identifier] { return existing }
1219
let node = PNNode(identifier: identifier)
1320
nodes[identifier] = node
1421
return node
1522
}
1623

24+
/// Initializes an empty dependency graph.
1725
public init() {
1826

1927
}
2028

29+
/// Retrieves a node from the graph by its identifier.
30+
///
31+
/// - Parameter identifier: The identifier of the node to retrieve.
32+
/// - Returns: The node with the specified identifier, or `nil` if not found.
2133
public func get(identifier: String) -> PNNode? {
2234
nodes[identifier]
2335
}
2436

37+
/// Compiles the dependency graph into a topologically sorted order.
38+
///
39+
/// This method performs a depth-first search to detect cycles and generate an execution order.
40+
/// If a cycle is detected, it throws `PNCompilationError.cycle`.
41+
///
42+
/// - Returns: A compiled graph containing nodes in topological order.
43+
/// - Throws: `PNCompilationError.cycle` if the graph contains a cycle.
2544
public func compile() throws -> PNCompiledGraph {
2645
var visited: Set<String> = []
2746
var visiting: Set<String> = []

0 commit comments

Comments
 (0)