( mutable: MutableGraph<N, E, T>, f: (data: N) => Option.Option<N> )
| 2025 | * @since 3.18.0 |
| 2026 | */ |
| 2027 | export const filterMapNodes = <N, E, T extends Kind = "directed">( |
| 2028 | mutable: MutableGraph<N, E, T>, |
| 2029 | f: (data: N) => Option.Option<N> |
| 2030 | ): void => { |
| 2031 | assertMutable(mutable) |
| 2032 | const impl = graphImpl(mutable) |
| 2033 | |
| 2034 | const nodesToRemove: Array<NodeIndex> = [] |
| 2035 | |
| 2036 | // First pass: identify nodes to remove and transform data for nodes to keep |
| 2037 | for (const [index, data] of impl.nodes) { |
| 2038 | const result = f(data) |
| 2039 | if (Option.isSome(result)) { |
| 2040 | // Transform node data |
| 2041 | impl.nodes.set(index, result.value) |
| 2042 | } else { |
| 2043 | // Mark for removal |
| 2044 | nodesToRemove.push(index) |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | // Second pass: remove filtered out nodes and their edges |
| 2049 | for (const nodeIndex of nodesToRemove) { |
| 2050 | removeNode(mutable, nodeIndex) |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | /** |
| 2055 | * Filters and optionally transforms edges in a mutable graph using a predicate function. |
nothing calls this directly
no test coverage detected