-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (73 loc) · 1.25 KB
/
index.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var factorialStop = function(n, stop){
if(n === stop){
return 1;
}
return n * factorialStop(n - 1, stop);
};
var sdr = {};
sdr.sparsity = function(arr, n){
return arr.length / n;
};
sdr.capacity = function(n, w){
return factorialStop(n, n - w) / factorialStop(w, 1);
};
sdr.intersect = function(a, b){
var u = [];
var curv = -1;
var i = 0;
var j = 0;
while(true){
while(a[i] <= curv && (i < a.length)){
i++;
}
while(b[j] <= curv && (j < b.length)){
j++;
}
if(!(i < a.length) || !(j < b.length)){
break;
}
if(a[i] <= b[j]){
if(a[i] === b[j]){
u.push(a[i]);
}
curv = a[i];
}else{
curv = b[j];
}
}
return u;
};
sdr.union = function(a, b){
var u = [];
var curv = -1;
var i = 0;
var j = 0;
while(true){
while(a[i] <= curv && (i < a.length)){
i++;
}
while(b[j] <= curv && (j < b.length)){
j++;
}
if(!(i < a.length) || !(j < b.length)){
break;
}
if(a[i] <= b[j]){
u.push(a[i]);
curv = a[i];
}else{
u.push(b[j]);
curv = b[j];
}
}
while(i < a.length){
u.push(a[i]);
i++;
}
while(j < b.length){
u.push(b[j]);
j++;
}
return u;
};
module.exports = sdr;