1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function _sasite_path(node) {
var start = node;
this.get_len = function(node) {
if (node == undefined) node = start;
var sasite = node.sasite;
var len = sasite.length - 1;
var last = sasite[len];
if (Array.isArray(last))
len += this.get_len(last[0]);
return len;
}
this.get_count = function(node) {
if (node == undefined) node = start;
var sasite = node.sasite;
var len = sasite.length - 1;
if (len == 0) return 0;
var last = sasite[len];
var count = 0;
if (Array.isArray(last))
for (var i = 0; i < last.length; i++)
count += this.get_count(last[i]);
return Math.max(count, 1);
}
this.get_index = function(path) {
var node = start;
var index = 0;
var last;
var sasite;
var pos = 1;
var j;
for (var i = 0; i < path.length; i++) {
sasite = path[i];
if (node.sasite.length == 1) return -1;
curr = node.sasite[pos++];
if (Array.isArray(curr)) {
last = 0;
for (j = 0; j < curr.length; j++) {
if (j != 0 && curr[j].sasite.length != 1) {
index += this.get_count(curr[last]);
last = j;
}
if (sasite.equals(curr[j].sasite[0])) {
node = curr[last];
pos = 1;
break;
}
}
if (j == curr.length) return -1;
}
else if (!sasite.equals(curr)) return -1;
}
return index;
}
}