-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> { | ||
|
@@ -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), | ||
); | ||
|
||
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 { | ||
|
@@ -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[] } { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
const removedNodeIds: any[] = []; | ||
const removedEdgeIds: any[] = []; | ||
|
||
|
@@ -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++) { | ||
|
@@ -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() { | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.