-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
85 lines (81 loc) · 1.37 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
80
81
82
83
84
85
"use strict"
var pool = require("typedarray-pool")
var inverse = require("invert-permutation")
function rank(permutation) {
var n = permutation.length
switch(n) {
case 0:
case 1:
return 0
case 2:
return permutation[1]
default:
break
}
var p = pool.mallocUint32(n)
var pinv = pool.mallocUint32(n)
var r = 0, s, t, i
inverse(permutation, pinv)
for(i=0; i<n; ++i) {
p[i] = permutation[i]
}
for(i=n-1; i>0; --i) {
t = pinv[i]
s = p[i]
p[i] = p[t]
p[t] = s
pinv[i] = pinv[s]
pinv[s] = t
r = (r + s) * i
}
pool.freeUint32(pinv)
pool.freeUint32(p)
return r
}
function unrank(n, r, p) {
switch(n) {
case 0:
if(p) { return p }
return []
case 1:
if(p) {
p[0] = 0
return p
} else {
return [0]
}
case 2:
if(p) {
if(r) {
p[0] = 0
p[1] = 1
} else {
p[0] = 1
p[1] = 0
}
return p
} else {
return r ? [0,1] : [1,0]
}
default:
break
}
p = p || new Array(n)
var s, t, i, nf=1
p[0] = 0
for(i=1; i<n; ++i) {
p[i] = i
nf = (nf*i)|0
}
for(i=n-1; i>0; --i) {
s = (r / nf)|0
r = (r - s * nf)|0
nf = (nf / i)|0
t = p[i]|0
p[i] = p[s]|0
p[s] = t|0
}
return p
}
exports.rank = rank
exports.unrank = unrank