Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change edge types to numbers #6126

Merged
merged 16 commits into from Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
187 changes: 136 additions & 51 deletions packages/core/core/src/BundleGraph.js

Large diffs are not rendered by default.

39 changes: 22 additions & 17 deletions packages/core/core/src/Graph.js
Expand Up @@ -6,18 +6,19 @@ import type {TraversalActions, GraphVisitor} from '@parcel/types';
import assert from 'assert';
import nullthrows from 'nullthrows';

export type GraphOpts<TNode, TEdgeType: string | null = null> = {|
type NullEdgeType = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should not support the null edge type at all, and just have users/extenders always specify the types of edges the way they do nodes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export type GraphOpts<TNode, TEdgeType: number = 0> = {|
nodes?: Map<NodeId, TNode>,
edges?: AdjacencyListMap<TEdgeType | null>,
edges?: AdjacencyListMap<TEdgeType | NullEdgeType>,
rootNodeId?: ?NodeId,
|};

export const ALL_EDGE_TYPES = '@@all_edge_types';

export default class Graph<TNode: Node, TEdgeType: string | null = null> {
export default class Graph<TNode: Node, TEdgeType: number = 0> {
nodes: Map<NodeId, TNode>;
inboundEdges: AdjacencyList<TEdgeType | null>;
outboundEdges: AdjacencyList<TEdgeType | null>;
inboundEdges: AdjacencyList<TEdgeType | NullEdgeType>;
outboundEdges: AdjacencyList<TEdgeType | NullEdgeType>;
rootNodeId: ?NodeId;

constructor(opts: GraphOpts<TNode, TEdgeType> = ({}: any)) {
Expand Down Expand Up @@ -60,7 +61,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {

// Returns a list of all edges in the graph. This can be large, so iterating
// the complete list can be costly in large graphs. Used when merging graphs.
getAllEdges(): Array<Edge<TEdgeType | null>> {
getAllEdges(): Array<Edge<TEdgeType | NullEdgeType>> {
let edges = [];
for (let [from, edgeList] of this.outboundEdges.getListMap()) {
for (let [type, toNodes] of edgeList) {
Expand Down Expand Up @@ -98,7 +99,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
return this.rootNodeId ? this.getNode(this.rootNodeId) : null;
}

addEdge(from: NodeId, to: NodeId, type: TEdgeType | null = null): void {
addEdge(from: NodeId, to: NodeId, type: TEdgeType | NullEdgeType = 0): void {
if (!this.getNode(from)) {
throw new Error(`"from" node '${from}' not found`);
}
Expand All @@ -111,13 +112,17 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
this.inboundEdges.addEdge(to, from, type);
}

hasEdge(from: NodeId, to: NodeId, type?: TEdgeType | null = null): boolean {
hasEdge(
from: NodeId,
to: NodeId,
type?: TEdgeType | NullEdgeType = 0,
): boolean {
return this.outboundEdges.hasEdge(from, to, type);
}

getNodesConnectedTo(
node: TNode,
type: TEdgeType | null | Array<TEdgeType | null> = null,
type: TEdgeType | NullEdgeType | Array<TEdgeType> = 0,
): Array<TNode> {
assertHasNode(this, node);

Expand Down Expand Up @@ -150,7 +155,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {

getNodesConnectedFrom(
node: TNode,
type: TEdgeType | null | Array<TEdgeType | null> = null,
type: TEdgeType | NullEdgeType | Array<TEdgeType> = 0,
): Array<TNode> {
assertHasNode(this, node);

Expand Down Expand Up @@ -215,7 +220,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
this.removeNode(node);
}

removeEdges(node: TNode, type: TEdgeType | null = null) {
removeEdges(node: TNode, type: TEdgeType | NullEdgeType = 0) {
assertHasNode(this, node);

for (let to of this.outboundEdges.getEdges(node.id, type)) {
Expand All @@ -227,7 +232,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
removeEdge(
from: NodeId,
to: NodeId,
type: TEdgeType | null = null,
type: TEdgeType | NullEdgeType = 0,
removeOrphans: boolean = true,
) {
if (!this.outboundEdges.hasEdge(from, to, type)) {
Expand Down Expand Up @@ -291,7 +296,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
replaceNode(
fromNode: TNode,
toNode: TNode,
type: TEdgeType | null = null,
type: TEdgeType | NullEdgeType = 0,
): void {
assertHasNode(this, fromNode);

Expand All @@ -310,7 +315,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
fromNode: TNode,
toNodes: $ReadOnlyArray<TNode>,
replaceFilter?: null | (TNode => boolean),
type?: TEdgeType | null = null,
type?: TEdgeType | NullEdgeType = 0,
): void {
assertHasNode(this, fromNode);

Expand Down Expand Up @@ -339,7 +344,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
traverse<TContext>(
visit: GraphVisitor<TNode, TContext>,
startNode: ?TNode,
type: TEdgeType | null | Array<TEdgeType | null> = null,
type: TEdgeType | NullEdgeType | Array<TEdgeType> = 0,
thebriando marked this conversation as resolved.
Show resolved Hide resolved
): ?TContext {
return this.dfs({
visit,
Expand All @@ -352,15 +357,15 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
filter: (TNode, TraversalActions) => ?TValue,
visit: GraphVisitor<TValue, TContext>,
startNode: ?TNode,
type?: TEdgeType | null | Array<TEdgeType | null>,
type?: TEdgeType | Array<TEdgeType>,
): ?TContext {
return this.traverse(mapVisitor(filter, visit), startNode, type);
}

traverseAncestors<TContext>(
startNode: TNode,
visit: GraphVisitor<TNode, TContext>,
type: TEdgeType | null | Array<TEdgeType | null> = null,
type: TEdgeType | NullEdgeType | Array<TEdgeType> = 0,
): ?TContext {
return this.dfs({
visit,
Expand Down