From 3e7776c245edd2a54136e98eda35be0a1b6b23db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Sun, 3 Jun 2018 16:39:12 -0700 Subject: [PATCH] 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 --- src/v3/core/graph.js | 14 +++++++++++++- src/v3/core/graph.test.js | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/v3/core/graph.js b/src/v3/core/graph.js index a5a4015..89c43c6 100644 --- a/src/v3/core/graph.js +++ b/src/v3/core/graph.js @@ -92,8 +92,20 @@ export function fromEdgeAddress(n: EdgeAddress): string[] { } export class Graph { + _nodes: Set; + // 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; + _inEdges: Map; + _outEdges: Map; + 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 { diff --git a/src/v3/core/graph.test.js b/src/v3/core/graph.test.js index aba9d53..1c2d376 100644 --- a/src/v3/core/graph.test.js +++ b/src/v3/core/graph.test.js @@ -5,6 +5,7 @@ import { fromNodeAddress, toEdgeAddress, fromEdgeAddress, + Graph, } from "./graph"; describe("core/graph", () => { @@ -107,4 +108,13 @@ describe("core/graph", () => { 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(); + }); + }); });