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

Support multiple edge types in Graph.hasEdge #8550

Merged
merged 15 commits into from Oct 19, 2022
14 changes: 11 additions & 3 deletions packages/core/graph/src/AdjacencyList.js
Expand Up @@ -329,10 +329,18 @@ export default class AdjacencyList<TEdgeType: number = 1> {
hasEdge(
from: NodeId,
to: NodeId,
type: TEdgeType | NullEdgeType = 1,
type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
): boolean {
let hash = this.#edges.hash(from, to, type);
return this.#edges.addressOf(hash, from, to, type) !== null;
let hasEdge = (type: TEdgeType | NullEdgeType) => {
let hash = this.#edges.hash(from, to, type);
return this.#edges.addressOf(hash, from, to, type) !== null;
};

if (Array.isArray(type)) {
return type.some(hasEdge);
}

return hasEdge(type);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/graph/src/Graph.js
Expand Up @@ -104,7 +104,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
hasEdge(
from: NodeId,
to: NodeId,
type?: TEdgeType | NullEdgeType = 1,
type?: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
): boolean {
return this.adjacencyList.hasEdge(from, to, type);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/core/graph/test/AdjacencyList.test.js
Expand Up @@ -243,6 +243,21 @@ describe('AdjacencyList', () => {
AdjacencyList.prototype.hash = originalHash;
});

it('hasEdge should accept an array of edge types', () => {
let graph = new AdjacencyList();
let a = graph.addNode();
let b = graph.addNode();
let c = graph.addNode();

graph.addEdge(a, b, 1);
graph.addEdge(b, c, 2);

assert.ok(!graph.hasEdge(a, b, [2, 3]));
assert.ok(graph.hasEdge(a, b, [1, 2]));
assert.ok(!graph.hasEdge(b, c, [1, 3]));
assert.ok(graph.hasEdge(b, c, [2, 3]));
});

describe('deserialize', function () {
this.timeout(10000);

Expand Down