@@ -18,7 +18,7 @@ if (typeof define !== 'function') {
18
18
}
19
19
20
20
define ( function ( require , exports ) {
21
- var numset = require ( './numset ' ) ;
21
+ const { nd2str } = require ( './graph ' ) ;
22
22
23
23
exports . reachability = function ( graph , nodePred ) {
24
24
let enum_nodes = new Array ( ) ;
@@ -27,9 +27,11 @@ define(function (require, exports) {
27
27
28
28
let n = nodes . length ;
29
29
30
+ const str2rid = { } ;
31
+
30
32
for ( let i = 0 ; i < n ; i ++ ) {
31
33
enum_nodes [ i ] = nodes [ i ] ;
32
- nodes [ i ] . reachability_id = i ;
34
+ str2rid [ nd2str ( nodes [ i ] ) ] = i ;
33
35
}
34
36
35
37
let visited = new Array ( n ) . fill ( 0 ) ,
@@ -44,18 +46,14 @@ define(function (require, exports) {
44
46
t . push ( new Set ( ) ) ;
45
47
}
46
48
47
- function add ( a , b ) {
48
- return a + b ;
49
- }
50
-
51
49
function visit1 ( i ) {
52
50
visited [ i ] = 1 ;
53
51
54
52
if ( ! nodePred || nodePred ( enum_nodes [ i ] ) ) {
55
53
let succ = graph . succ ( enum_nodes [ i ] )
56
54
57
55
for ( let j = 0 ; j < succ . length ; j ++ ) {
58
- let index = succ [ j ] . reachability_id ;
56
+ let index = str2rid [ nd2str ( succ [ j ] ) ] ;
59
57
if ( nodePred && ! nodePred ( succ [ j ] ) )
60
58
continue ;
61
59
if ( m [ i ] . has ( index ) || t [ i ] . has ( index ) )
@@ -103,7 +101,7 @@ define(function (require, exports) {
103
101
let succ = graph . succ ( enum_nodes [ i ] )
104
102
105
103
for ( let j = 0 ; j < succ . length ; j ++ ) {
106
- let index = succ [ j ] . reachability_id ;
104
+ let index = str2rid [ nd2str ( succ [ j ] ) ] ;
107
105
if ( nodePred && ! nodePred ( succ [ j ] ) )
108
106
return ;
109
107
if ( visited2 [ index ] == 0 && t [ index ] . size !== 0 )
@@ -116,17 +114,17 @@ define(function (require, exports) {
116
114
}
117
115
return {
118
116
getReachable : function ( src ) {
119
- var src_id = src . reachability_id ;
120
- if ( src_id === undefined ) {
117
+ const nodeStr = nd2str ( src ) ;
118
+ if ( ! ( nodeStr in str2rid ) ) {
121
119
enum_nodes . push ( src ) ;
122
120
visited . push ( 0 ) ;
123
121
visited2 . push ( 0 ) ;
124
122
popped . push ( 0 ) ;
125
123
m . push ( new Set ( ) ) ;
126
124
t . push ( new Set ( ) ) ;
127
- src . reachability_id = enum_nodes . length - 1 ;
128
- src_id = src . reachability_id ;
125
+ str2rid [ nodeStr ] = enum_nodes . length - 1 ;
129
126
}
127
+ const src_id = str2rid [ nodeStr ] ;
130
128
131
129
if ( visited [ src_id ] == 0 )
132
130
visit1 ( src_id ) ;
@@ -143,17 +141,18 @@ define(function (require, exports) {
143
141
return ret ;
144
142
} ,
145
143
iterReachable : function ( src , cb ) {
146
- var src_id = src . reachability_id ;
147
- if ( src_id === undefined ) {
144
+ const nodeStr = nd2str ( src ) ;
145
+ if ( ! ( nodeStr in str2rid ) ) {
148
146
enum_nodes . push ( src ) ;
149
147
visited . push ( 0 ) ;
150
148
visited2 . push ( 0 ) ;
151
149
popped . push ( 0 ) ;
152
150
m . push ( new Set ( ) ) ;
153
151
t . push ( new Set ( ) ) ;
154
- src . reachability_id = enum_nodes . length - 1 ;
155
- src_id = src . reachability_id ;
152
+ str2rid [ nodeStr ] = enum_nodes . length - 1 ;
156
153
}
154
+ const src_id = str2rid [ nodeStr ] ;
155
+
157
156
if ( visited [ src_id ] == 0 )
158
157
visit1 ( src_id ) ;
159
158
@@ -165,8 +164,8 @@ define(function (require, exports) {
165
164
cb ( enum_nodes [ elem ] ) ;
166
165
} ,
167
166
reaches : function ( src , dest ) {
168
- var src_id = src . reachability_id ,
169
- dest_id = dest . reachability_id ;
167
+ const src_id = str2rid [ nd2str ( src ) ] ;
168
+ const dest_id = str2rid [ nd2str ( dest ) ] ;
170
169
171
170
if ( visited [ src_id ] == 0 )
172
171
visit1 ( src_id ) ;
@@ -175,7 +174,7 @@ define(function (require, exports) {
175
174
for ( let elem of t [ src_id ] . values ( ) )
176
175
tc . add ( elem ) ;
177
176
178
- return tc . has ( des_id ) ;
177
+ return tc . has ( dest_id ) ;
179
178
}
180
179
} ;
181
180
} ;
0 commit comments