( graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction )
| 1541 | * @category queries |
| 1542 | */ |
| 1543 | export const neighborsDirected = <N, E, T extends Kind = "directed">( |
| 1544 | graph: Graph<N, E, T> | MutableGraph<N, E, T>, |
| 1545 | nodeIndex: NodeIndex, |
| 1546 | direction: Direction |
| 1547 | ): Array<NodeIndex> => { |
| 1548 | const adjacencyMap = direction === "incoming" |
| 1549 | ? graph.reverseAdjacency |
| 1550 | : graph.adjacency |
| 1551 | |
| 1552 | const adjacencyList = adjacencyMap.get(nodeIndex) |
| 1553 | if (adjacencyList === undefined) { |
| 1554 | return [] |
| 1555 | } |
| 1556 | |
| 1557 | const result: Array<NodeIndex> = [] |
| 1558 | for (const edgeIndex of adjacencyList) { |
| 1559 | const edge = graph.edges.get(edgeIndex) |
| 1560 | if (edge !== undefined) { |
| 1561 | // For incoming direction, we want the source node instead of target |
| 1562 | const neighborNode = direction === "incoming" |
| 1563 | ? edge.source |
| 1564 | : edge.target |
| 1565 | result.push(neighborNode) |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | return result |
| 1570 | } |
| 1571 | |
| 1572 | // ============================================================================= |
| 1573 | // GraphViz Export |
no test coverage detected