Expose `Graph.mergeManyConservative` (#209)

Summary:
This offers #205 to general users.

Test Plan:
Existing tests suffice.

wchargin-branch: merge-many-conservative
This commit is contained in:
William Chargin 2018-05-04 15:42:39 -07:00 committed by GitHub
parent e3469f157d
commit a642ed46b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View File

@ -338,6 +338,28 @@ export class Graph<NP, EP> {
);
return result;
}
/**
* Equivalent to
*
* graphs.reduce((g, h) => Graph.mergeConservative(g, h), new Graph()),
*
* but uses a mutable accumulator for improved performance.
*/
static mergeManyConservative<NP, EP>(
graphs: $ReadOnlyArray<Graph<$Subtype<NP>, $Subtype<EP>>>
): Graph<NP, EP> {
const result = new Graph();
graphs.forEach((graph) => {
graph.nodes().forEach((node) => {
result.addNode(node);
});
graph.edges().forEach((edge) => {
result.addEdge(edge);
});
});
return result;
}
}
export function edgeID(src: Address, dst: Address): string {

View File

@ -41,7 +41,7 @@ class GitGraphCreator {
const treeAndNameToSubmoduleUrls = this.treeAndNameToSubmoduleUrls(
repository
);
const graphs = [
return Graph.mergeManyConservative([
...Object.keys(repository.commits).map((hash) =>
this.commitGraph(repository.commits[hash])
),
@ -49,17 +49,7 @@ class GitGraphCreator {
this.treeGraph(repository.trees[hash], treeAndNameToSubmoduleUrls)
),
this.becomesEdges(repository),
];
const result = new Graph();
graphs.forEach((g) => {
g.nodes().forEach((node) => {
result.addNode(node);
});
g.edges().forEach((edge) => {
result.addEdge(edge);
});
});
return result;
]);
}
treeAndNameToSubmoduleUrls(repository: Repository) {