Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow the hillbig/rsdic interface change #2

Closed
wants to merge 10 commits into from
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: go

go:
- 1.4
- 1.5

before_script:
- lscpu

script:
- go test -v -bench . -benchmem
51 changes: 42 additions & 9 deletions waveletMatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,55 @@ func (wm waveletMatrix) Lookup(pos uint64) uint64 {
}

func (wm waveletMatrix) Rank(pos uint64, val uint64) uint64 {
ranze := wm.RankRange(Range{0, pos}, val)
return ranze.Epos - ranze.Bpos
return wm.RangedRankOp(Range{0, pos}, val, OpEqual) // Works but disabled for now to keep test cov.
// ranze := wm.RankRange(Range{0, pos}, val)
// return ranze.Epos - ranze.Bpos
}

func (wm waveletMatrix) RankRange(ranze Range, val uint64) Range {
func (wm waveletMatrix) RankLessThan(pos uint64, val uint64) (rankLessThan uint64) {
return wm.RangedRankOp(Range{0, pos}, val, OpLessThan)
}

func (wm waveletMatrix) RankMoreThan(pos uint64, val uint64) (rankLessThan uint64) {
return wm.RangedRankOp(Range{0, pos}, val, OpMoreThan)
}

func (wm waveletMatrix) RangedRankOp(ranze Range, val uint64, op int) uint64 {
rankLessThan := uint64(0)
rankMoreThan := uint64(0)
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()
if op == OpLessThan {
rankLessThan += rsd.Rank(ranze.Epos, false) - rsd.Rank(ranze.Bpos, false)
}
ranze.Bpos = rsd.ZeroNum() + rsd.Rank(ranze.Bpos, bit)
ranze.Epos = rsd.ZeroNum() + rsd.Rank(ranze.Epos, bit)
} else {
if op == OpMoreThan {
rankMoreThan += rsd.Rank(ranze.Epos, true) - rsd.Rank(ranze.Bpos, true)
}
ranze.Bpos = rsd.Rank(ranze.Bpos, bit)
ranze.Epos = rsd.Rank(ranze.Epos, bit)
}
}
return ranze
switch op {
case OpEqual:
return ranze.Epos - ranze.Bpos
case OpLessThan:
return rankLessThan
case OpMoreThan:
return rankMoreThan
default:
return 0
}
}

func (wm waveletMatrix) RangedRankRange(ranze Range, valueRange Range) uint64 {
end := wm.RangedRankOp(ranze, valueRange.Epos, OpLessThan)
beg := wm.RangedRankOp(ranze, valueRange.Bpos, OpLessThan)
return end - beg
}

func (wm waveletMatrix) Select(rank uint64, val uint64) uint64 {
Expand Down Expand Up @@ -203,7 +236,7 @@ func (wm *waveletMatrix) UnmarshalBinary(in []byte) (err error) {
}
wm.layers = make([]rsdic.RSDic, layerNum)
for i := 0; i < layerNum; i++ {
wm.layers[i] = rsdic.New()
wm.layers[i] = *rsdic.New()
err = dec.Decode(&wm.layers[i])
if err != nil {
return
Expand Down
4 changes: 2 additions & 2 deletions waveletMatrixBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func (wmb *waveletMatrixBuilder) Build() WaveletTree {
filter(ones, blen-depth-1, &nextZeros, &nextOnes, rsd)
zeros = nextZeros
ones = nextOnes
layers[depth] = rsd
layers[depth] = *rsd
}
return &waveletMatrix{layers, dim, uint64(len(wmb.vals)), blen}
}

func filter(vals []uint64, depth uint64, nextZeros *[]uint64, nextOnes *[]uint64, rsd rsdic.RSDic) {
func filter(vals []uint64, depth uint64, nextZeros *[]uint64, nextOnes *[]uint64, rsd *rsdic.RSDic) {
for _, val := range vals {
bit := ((val >> depth) & 1) == 1
rsd.PushBack(bit)
Expand Down
Loading