1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var _ = require('underscore')
|
9 | , inherits = require('inherits')
|
10 |
|
11 | var IperElement = require('./IperElement')
|
12 |
|
13 | function IperNode(graph, opts) {
|
14 | var self = this
|
15 |
|
16 | IperElement.call(this, graph)
|
17 |
|
18 | if (!_.isObject(opts))
|
19 | opts = {}
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function getDegree () {
|
34 | var degree = 0
|
35 |
|
36 | |
37 |
|
38 |
|
39 | _.each(graph.edges, function (edge) {
|
40 | _.each(edge.nodeIds, function (nodeId) {
|
41 | if (nodeId === self.id)
|
42 | degree++
|
43 | })
|
44 | })
|
45 |
|
46 | return degree
|
47 | }
|
48 |
|
49 | Object.defineProperty(this, 'degree', {get: getDegree})
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function getMaxDegree () {
|
56 | return opts.maxDegree
|
57 | }
|
58 |
|
59 | Object.defineProperty(this, 'maxDegree', {get: getMaxDegree})
|
60 |
|
61 |
|
62 | graph.nodes.push(this)
|
63 | }
|
64 |
|
65 | inherits(IperNode, IperElement)
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function getAdjacentNodeIds() {
|
76 | var id = this.id
|
77 |
|
78 | var adjacentNodeIds = []
|
79 |
|
80 |
|
81 | _.each(this.graph.edges, function (edge) {
|
82 |
|
83 | if (_.contains(edge.nodeIds, id))
|
84 |
|
85 | adjacentNodeIds.push(_.without(edge.nodeIds, id))
|
86 | })
|
87 |
|
88 | |
89 |
|
90 |
|
91 | return _.uniq(_.flatten(adjacentNodeIds))
|
92 | }
|
93 |
|
94 | IperNode.prototype.getAdjacentNodeIds = getAdjacentNodeIds
|
95 |
|
96 |
|
97 |
|
98 | module.exports = IperNode
|
99 |
|