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

fix: getNodeIdsConnected should remove duplicate values #8054

Merged
merged 3 commits into from May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions packages/core/graph/src/AdjacencyList.js
Expand Up @@ -440,19 +440,19 @@ export default class AdjacencyList<TEdgeType: number = 1> {
? type.includes(this.#nodes.typeOf(node))
: type === this.#nodes.typeOf(node));

let nodes = [];
let nodes = new Set<NodeId>();
ahaoboy marked this conversation as resolved.
Show resolved Hide resolved
let node = this.#nodes.head(from);
while (node !== null) {
if (matches(node)) {
let edge = this.#nodes.firstOut(node);
while (edge !== null) {
nodes.push(this.#edges.to(edge));
nodes.add(this.#edges.to(edge));
ahaoboy marked this conversation as resolved.
Show resolved Hide resolved
edge = this.#edges.nextOut(edge);
}
}
node = this.#nodes.next(node);
}
return nodes;
return [...nodes];
ahaoboy marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -472,19 +472,19 @@ export default class AdjacencyList<TEdgeType: number = 1> {
? type.includes(this.#nodes.typeOf(node))
: type === this.#nodes.typeOf(node));

let nodes = [];
let nodes = new Set<NodeId>();
let node = this.#nodes.head(to);
while (node !== null) {
if (matches(node)) {
let edge = this.#nodes.firstIn(node);
while (edge !== null) {
nodes.push(this.#edges.from(edge));
nodes.add(this.#edges.from(edge));
edge = this.#edges.nextIn(edge);
}
}
node = this.#nodes.next(node);
}
return nodes;
return [...nodes];
}

inspect(): any {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/graph/test/AdjacencyList.test.js
Expand Up @@ -57,6 +57,18 @@ describe('AdjacencyList', () => {
assert.deepEqual(graph.getNodeIdsConnectedTo(node1), [0, 2, 4, 5, 6]);
});

it('getNodeIdsConnectedTo and getNodeIdsConnectedFrom should remove duplicate values', () => {
let graph = new AdjacencyList();
let a = graph.addNode();
let b = graph.addNode();
let c = graph.addNode();
graph.addEdge(a, b);
graph.addEdge(a, c);
graph.addEdge(a, b, 2);
assert.deepEqual(graph.getNodeIdsConnectedFrom(a, -1), [b, c]);
assert.deepEqual(graph.getNodeIdsConnectedTo(b, -1), [a]);
});

it('removeEdge should remove an edge of a specific type from the graph', () => {
let graph = new AdjacencyList();
let a = graph.addNode();
Expand Down