Add basic Graph data structures (#330)

This very minor commit adds the basic data structures to the `Graph`
that will act as underlying storage for nodes and edges. This is similar
to how nodes and edges are stored in `Graph` v1 and v2, except much
simpler, because we now have string keys, no data stored with nodes, and
minimal data stored with edges.

Test plan: The only observable change is that the graph can now be
constructed without error. I added a unit test to verify this behavior
:) as well as that the Graph constructor properly returns an object of
type `Graph` and not an `any`.
(Context: https://github.com/facebook/flow/issues/6400)

Paired with @wchargin
This commit is contained in:
Dandelion Mané 2018-06-03 16:39:12 -07:00 committed by GitHub
parent 3acfefb904
commit 3e7776c245
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -92,8 +92,20 @@ export function fromEdgeAddress(n: EdgeAddress): string[] {
} }
export class Graph { export class Graph {
_nodes: Set<NodeAddress>;
// If `e` is an Edge in the graph, then:
// * _edges.get(e.address) `deepEquals` e
// * _inEdges.get(e.dst) `contains` e
// * _outEdges.get(e.src) `contains` e
_edges: Map<EdgeAddress, Edge>;
_inEdges: Map<NodeAddress, Edge[]>;
_outEdges: Map<NodeAddress, Edge[]>;
constructor(): void { constructor(): void {
throw new Error("constructor"); this._nodes = new Set();
this._edges = new Map();
this._inEdges = new Map();
this._outEdges = new Map();
} }
addNode(a: NodeAddress): this { addNode(a: NodeAddress): this {

View File

@ -5,6 +5,7 @@ import {
fromNodeAddress, fromNodeAddress,
toEdgeAddress, toEdgeAddress,
fromEdgeAddress, fromEdgeAddress,
Graph,
} from "./graph"; } from "./graph";
describe("core/graph", () => { describe("core/graph", () => {
@ -107,4 +108,13 @@ describe("core/graph", () => {
expect(toEdgeAddress(["foo"])).not.toEqual(toNodeAddress(["foo"])); expect(toEdgeAddress(["foo"])).not.toEqual(toNodeAddress(["foo"]));
}); });
}); });
describe("Graph class", () => {
it("can be constructed", () => {
const x = new Graph();
// Verify that `x` is not of type `any`
// $ExpectFlowError
expect(() => x.measureSpectacularity()).toThrow();
});
});
}); });