Skip to content

Commit

Permalink
change nodeid back to number only
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHung committed Nov 3, 2023
1 parent 44b5c39 commit a7116ce
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/DependencyGraph/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {SimpleCellRange} from '../AbsoluteCellRange'
import {TopSort, TopSortResult} from './TopSort'
import {ProcessableValue} from './ProcessableValue'

export type NodeId = string | number
export type NodeId = number
export type NodeAndId<Node> = { node: Node, id: NodeId }
export type DependencyQuery<Node> = (vertex: Node) => [(SimpleCellAddress | SimpleCellRange), Node][]

Expand Down Expand Up @@ -288,7 +288,7 @@ export class Graph<Node> {
operatingFunction: (node: Node) => boolean,
onCycle: (node: Node) => void
): TopSortResult<Node> {
const topSortAlgorithm = new TopSort<Node>(this.nodes, this.edges)
const topSortAlgorithm = new TopSort<Node, NodeId>(this.nodes, this.edges)
const modifiedNodesIds = modifiedNodes.map(node => this.getNodeId(node)).filter(id => id !== undefined) as NodeId[]
return topSortAlgorithm.getTopSortedWithSccSubgraphFrom(modifiedNodesIds, operatingFunction, onCycle)
}
Expand Down Expand Up @@ -396,7 +396,7 @@ export class Graph<Node> {
* Doesn't work if overlap between nodeId and node.
*/
private getNodeIdIfNode(node: Node | NodeId): NodeId | undefined {
return (typeof node === 'number' || typeof node === 'string') ? node : this.nodesIds.get(node)
return typeof node === 'number' ? node : this.nodesIds.get(node)
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/DependencyGraph/TopSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ enum NodeVisitStatus {
POPPED,
}

type id = string | number

/**
* An algorithm class. Provides an iterative implementation of Tarjan's algorithm for finding strongly connected components
*/
export class TopSort<T> {
export class TopSort<T, id> {
private entranceTime: Map<id, number> = new Map()
private low: Map<id, number> = new Map()
private parent: Map<id, id> = new Map()
Expand Down Expand Up @@ -60,7 +58,7 @@ export class TopSort<T> {
private getAdjacentNodeIds(id: id) {
const edges = this.edges.get(id)
if (edges === undefined) {
throw new Error(`Edge set missing for node ${id}: ${Array.from(this.nodes.keys())}`)
throw new Error(`Edge set missing for node ${id}`)
}
return new Set(Array.from(edges.values()).filter(id => this.nodes.has(id)))
}
Expand Down

0 comments on commit a7116ce

Please sign in to comment.