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

New: Add removal functions #96

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/models/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface IGraphSettings<N extends INodeBase, E extends IEdgeBase> {
onLoadedImages?: () => void;
onSetupData?: (data: Partial<IGraphData<N, E>>) => void;
onMergeData?: (data: Partial<IGraphData<N, E>>) => void;
onRemoveData?: (data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) => void;
onRemoveData?: (data: Partial<{ nodeIds: any[]; edgeIds: any[] }>) => void;
}

export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N, E> {
Expand Down Expand Up @@ -263,18 +263,41 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
this._settings?.onMergeData?.(data);
}

// TODO(dlozic): Add delete all mechanic.
remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) {
const nodeIds = data.nodeIds ?? [];
const edgeIds = data.edgeIds ?? [];

this._removeNodes(nodeIds);
this._removeEdges(edgeIds);
const removedData = this._removeNodes(nodeIds);
const removedEdgeIds = this._removeEdges(edgeIds);

// Merge edges removed by removing nodes and by removing edges, ensuring there are no duplicate edge IDs.
removedData.edgeIds = removedData.edgeIds.concat(
removedEdgeIds.filter((edgeId) => removedData.edgeIds.indexOf(edgeId) < 0),
);
Copy link
Author

Choose a reason for hiding this comment

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

This is the most elegant solution I've found so far, but I'm open to suggestions for any more performant/readable alternatives

Copy link
Contributor

Choose a reason for hiding this comment

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

This can be slow, so I would suggest creating utility function, e.g. dedupArrays that receive a generic array, or multiple arrays and dedups their values - using set or map.

E.g.

dedupArrays([1, 2, 3], [1, 2]) -> [1, 2, 3])
dedupArrays([1, 2, 3], [1, 2], [5, 6]) -> [1, 2, 3, 5, 6])
dedupArrays([1, 2, 3], [1, 2], [5, 6], [], [6, 7]) -> [1, 2, 3, 5, 6, 7])


this._applyEdgeOffsets();
this._applyStyle();

this._settings?.onRemoveData?.(data);
if (this._settings && this._settings.onRemoveData) {
this._settings.onRemoveData(removedData);
}
}

removeAll() {
const nodeIds = this._nodes.getAll().map((node) => node.id);
const edgeIds = this._edges.getAll().map((edge) => edge.id);

this.remove({ nodeIds, edgeIds });
}

removeAllEdges() {
const edgeIds = this._edges.getAll().map((edge) => edge.id);

this.remove({ edgeIds });
}

removeAllNodes() {
this.removeAll();
}

isEqual<T extends INodeBase, K extends IEdgeBase>(graph: Graph<T, K>): boolean {
Expand Down Expand Up @@ -474,7 +497,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
this._edges.removeMany(removedEdgeIds);
}

private _removeNodes(nodeIds: any[]) {
private _removeNodes(nodeIds: any[]): { nodeIds: any[]; edgeIds: any[] } {
Copy link
Contributor

Choose a reason for hiding this comment

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

This return type is cool. I would maybe extract it into an interface/type. Also, just for a sake of consistency, I would maybe consider returning the same type on _removeEdges - right now, nodeIds will be always empty, but in the future if we add "removeConnectingNodesfor edges, then the API still holds and can use the returned object with bothnodeIdsandedgeIds`.

const removedNodeIds: any[] = [];
const removedEdgeIds: any[] = [];

Expand All @@ -496,9 +519,11 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
}
this._nodes.removeMany(removedNodeIds);
this._edges.removeMany(removedEdgeIds);

return { nodeIds: removedNodeIds, edgeIds: removedEdgeIds };
}

private _removeEdges(edgeIds: any[]) {
private _removeEdges(edgeIds: any[]): any[] {
const removedEdgeIds: any[] = [];

for (let i = 0; i < edgeIds.length; i++) {
Expand All @@ -512,6 +537,8 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
removedEdgeIds.push(edge.id);
}
this._edges.removeMany(removedEdgeIds);

return removedEdgeIds;
}

private _applyEdgeOffsets() {
Expand Down
Loading