1 | var getIncidentEdgeIds = require('./getIncidentEdgeIds')
|
2 | var getOrphanEdgeIds = require('./getOrphanEdgeIds')
|
3 | var uniqueId = require('lodash.uniqueid')
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | class Graph {
|
16 | constructor () {
|
17 | var arg = arguments[0] || {}
|
18 |
|
19 | this.edges = arg.edges || {}
|
20 | this.nodes = arg.nodes || {}
|
21 | }
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | addEdge (nodeIds) {
|
30 | var id = uniqueId()
|
31 |
|
32 | this.edges[id] = nodeIds
|
33 |
|
34 | return id
|
35 | }
|
36 |
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | addNode (data) {
|
44 | var id = uniqueId()
|
45 |
|
46 | this.nodes[id] = data
|
47 |
|
48 | return id
|
49 | }
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | delEdge (id) {
|
58 | var nodeIds = this.edges[id]
|
59 |
|
60 | delete this.edges[id]
|
61 |
|
62 | return nodeIds
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | delNode (id) {
|
72 | let data = this.nodes[id]
|
73 | delete this.nodes[id]
|
74 |
|
75 | let incidentEdgeIds = getIncidentEdgeIds(this.edges, id)
|
76 |
|
77 | for (let edgeId in incidentEdgeIds) {
|
78 | this.delEdge(edgeId)
|
79 | }
|
80 |
|
81 | return data
|
82 | }
|
83 | }
|
84 |
|
85 | module.exports = Graph
|