UNPKG

1.22 kBJavaScriptView Raw
1var getIncidentEdgeIds = require('./getIncidentEdgeIds')
2var getOrphanEdgeIds = require('./getOrphanEdgeIds')
3var uniqueId = require('lodash.uniqueid')
4
5/**
6 * Hypergraph
7 * @class
8 *
9 * http://en.wikipedia.org/wiki/Hypergraph
10 *
11 * @class
12 * @param {Object} graph
13 */
14
15class Graph {
16 constructor () {
17 var arg = arguments[0] || {}
18
19 this.edges = arg.edges || {}
20 this.nodes = arg.nodes || {}
21 }
22
23 /**
24 *
25 * @param {Array} nodeIds
26 * @returns {String} id
27 */
28
29 addEdge (nodeIds) {
30 var id = uniqueId()
31
32 this.edges[id] = nodeIds
33
34 return id
35 }
36
37 /**
38 *
39 * @param {Any} data
40 * @returns {String} id
41 */
42
43 addNode (data) {
44 var id = uniqueId()
45
46 this.nodes[id] = data
47
48 return id
49 }
50
51 /**
52 *
53 * @param {String} id
54 * @returns
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 * @param {String} id
68 * @returns {Any} data
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
85module.exports = Graph