-
Notifications
You must be signed in to change notification settings - Fork 9
/
waveletMatrix.go
229 lines (211 loc) · 4.98 KB
/
waveletMatrix.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Package wavelettree provides a wavelet tree
// supporting many range-query problems, including rank/select,
// range min/max query, most frequent and percentile query for general array.
package wavelettree
import (
"github.com/hillbig/rsdic"
"github.com/ugorji/go/codec"
)
func New() WaveletTree {
return &waveletMatrix{
layers: make([]rsdic.RSDic, 0),
dim: 0,
num: 0,
blen: 0}
}
type waveletMatrix struct {
layers []rsdic.RSDic
dim uint64
num uint64
blen uint64 // =len(layers)
}
func (wm waveletMatrix) Num() uint64 {
return wm.num
}
func (wm waveletMatrix) Dim() uint64 {
return wm.dim
}
func (wm waveletMatrix) Lookup(pos uint64) uint64 {
val := uint64(0)
for depth := 0; depth < len(wm.layers); depth++ {
val <<= 1
rsd := wm.layers[depth]
if !rsd.Bit(pos) {
pos = rsd.Rank(pos, false)
} else {
val |= 1
pos = rsd.ZeroNum() + rsd.Rank(pos, true)
}
}
return val
}
func (wm waveletMatrix) Rank(pos uint64, val uint64) uint64 {
ranze := wm.RankRange(Range{0, pos}, val)
return ranze.Epos - ranze.Bpos
}
func (wm waveletMatrix) RankRange(ranze Range, val uint64) Range {
for depth := uint64(0); depth < wm.blen; depth++ {
bit := getMSB(val, depth, wm.blen)
rsd := wm.layers[depth]
ranze.Bpos = rsd.Rank(ranze.Bpos, bit)
ranze.Epos = rsd.Rank(ranze.Epos, bit)
if bit {
ranze.Bpos += rsd.ZeroNum()
ranze.Epos += rsd.ZeroNum()
}
}
return ranze
}
func (wm waveletMatrix) Select(rank uint64, val uint64) uint64 {
return wm.selectHelper(rank, val, 0, 0)
}
func (wm waveletMatrix) selectHelper(rank uint64, val uint64, pos uint64, depth uint64) uint64 {
if depth == wm.blen {
return pos + rank
}
bit := getMSB(val, depth, wm.blen)
rsd := wm.layers[depth]
pos = rsd.Rank(pos, bit)
if !bit {
rank = wm.selectHelper(rank, val, pos, depth+1)
return rsd.Select(rank, false)
} else {
zeroNum := rsd.ZeroNum()
pos += zeroNum
rank = wm.selectHelper(rank, val, pos, depth+1)
return rsd.Select(rank-zeroNum, true)
}
}
func (wm waveletMatrix) LookupAndRank(pos uint64) (uint64, uint64) {
val := uint64(0)
bpos := uint64(0)
epos := uint64(pos)
for depth := uint64(0); depth < wm.blen; depth++ {
rsd := wm.layers[depth]
bit := rsd.Bit(epos)
bpos = rsd.Rank(bpos, bit)
epos = rsd.Rank(epos, bit)
val <<= 1
if bit {
bpos += rsd.ZeroNum()
epos += rsd.ZeroNum()
val |= 1
}
}
return val, epos - bpos
}
func (wm waveletMatrix) Quantile(ranze Range, k uint64) uint64 {
val := uint64(0)
bpos, epos := ranze.Bpos, ranze.Epos
for depth := 0; depth < len(wm.layers); depth++ {
val <<= 1
rsd := wm.layers[depth]
nzBpos := rsd.Rank(bpos, false)
nzEpos := rsd.Rank(epos, false)
nz := nzEpos - nzBpos
if k < nz {
bpos = nzBpos
epos = nzEpos
} else {
k -= nz
val |= 1
bpos = rsd.ZeroNum() + bpos - nzBpos
epos = rsd.ZeroNum() + epos - nzEpos
}
}
return val
}
func (wm waveletMatrix) Intersect(ranges []Range, k int) []uint64 {
return wm.intersectHelper(ranges, k, 0, 0)
}
func (wm waveletMatrix) intersectHelper(ranges []Range, k int, depth uint64, prefix uint64) []uint64 {
if depth == wm.blen {
ret := make([]uint64, 1)
ret[0] = prefix
return ret
}
rsd := wm.layers[depth]
zeroRanges := make([]Range, 0)
oneRanges := make([]Range, 0)
for _, ranze := range ranges {
bpos, epos := ranze.Bpos, ranze.Epos
nzBpos := rsd.Rank(bpos, false)
nzEpos := rsd.Rank(epos, false)
noBpos := bpos - nzBpos + rsd.ZeroNum()
noEpos := epos - nzEpos + rsd.ZeroNum()
if nzEpos-nzBpos > 0 {
zeroRanges = append(zeroRanges, Range{nzBpos, nzEpos})
}
if noEpos-noBpos > 0 {
oneRanges = append(oneRanges, Range{noBpos, noEpos})
}
}
ret := make([]uint64, 0)
if len(zeroRanges) >= k {
ret = append(ret, wm.intersectHelper(zeroRanges, k, depth+1, prefix<<1)...)
}
if len(oneRanges) >= k {
ret = append(ret, wm.intersectHelper(oneRanges, k, depth+1, (prefix<<1)|1)...)
}
return ret
}
func (wm waveletMatrix) MarshalBinary() (out []byte, err error) {
var bh codec.MsgpackHandle
enc := codec.NewEncoderBytes(&out, &bh)
err = enc.Encode(len(wm.layers))
if err != nil {
return
}
for i := 0; i < len(wm.layers); i++ {
err = enc.Encode(wm.layers[i])
if err != nil {
return
}
}
err = enc.Encode(wm.dim)
if err != nil {
return
}
err = enc.Encode(wm.num)
if err != nil {
return
}
err = enc.Encode(wm.blen)
if err != nil {
return
}
return
}
func (wm *waveletMatrix) UnmarshalBinary(in []byte) (err error) {
var bh codec.MsgpackHandle
dec := codec.NewDecoderBytes(in, &bh)
layerNum := 0
err = dec.Decode(&layerNum)
if err != nil {
return
}
wm.layers = make([]rsdic.RSDic, layerNum)
for i := 0; i < layerNum; i++ {
wm.layers[i] = rsdic.New()
err = dec.Decode(&wm.layers[i])
if err != nil {
return
}
}
err = dec.Decode(&wm.dim)
if err != nil {
return
}
err = dec.Decode(&wm.num)
if err != nil {
return
}
err = dec.Decode(&wm.blen)
if err != nil {
return
}
return
}
func getMSB(x uint64, pos uint64, blen uint64) bool {
return ((x >> (blen - pos - 1)) & 1) == 1
}