-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwaveletTree.go
67 lines (52 loc) · 1.78 KB
/
waveletTree.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
// 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
// Range represents a range [Bpos, Epos)
// only valid for Bpos <= Epos
type Range struct {
Bpos uint64
Epos uint64
}
type RankResult struct {
val uint64
freq uint64
}
// WaveletTree supports several range queries.
type WaveletTree interface {
// Num return the number of values in T
Num() uint64
// Dim returns (max. of T[0...Num) + 1)
Dim() uint64
// Lookup returns T[pos]
Lookup(pos uint64) uint64
// Rank returns the number of val's in T[0...pos)
Rank(pos uint64, val uint64) uint64
// Rank range returns the number of min <= c < max
// in T[ranze.Bpos, ranze.Epos)
RankRange(ranze Range, min uint64) Range
// Select returns the position of (rank+1)-th val in T
Select(rank uint64, val uint64) uint64
// LookupAndRank returns T[pos] and Rank(pos, T[pos])
// Faster than Lookup and Rank
LookupAndRank(pos uint64) (uint64, uint64)
// Quantile returns (k+1)th smallest value in T[ranze.Bpos, ranze.Epos]
Quantile(ranze Range, k uint64) uint64
// Intersect returns values that occure at least k ranges
Intersect(ranges []Range, k int) []uint64
// MarshalBinary encodes WaveletTree into a binary form and returns the result.
MarshalBinary() ([]byte, error)
// UnmarshalBinary decodes WaveletTree from a binary form generated MarshalBinary
UnmarshalBinary([]byte) error
}
// Builder builds WaveletTree from intergaer array.
// A user calls PushBack()s followed by Build().
type Builder interface {
PushBack(val uint64)
Build() WaveletTree
}
// NewWaveletReeBuilder returns Builder
func NewBuilder() Builder {
return &waveletMatrixBuilder{
vals: make([]uint64, 0),
}
}