Example #1
0
// Add appends the bits given its size to the vector.
func (v *BitVector) Add(bits uint64, size int) {
	if size <= 0 || size > 64 {
		panic("ranksel: bit size must be in range [1,64]")
	}

	// Add bits
	v.bits.Add(bits, size)
	vlength := v.bits.Len()

	// Increment popcount
	popcnt := bit.PopCount(bits)
	v.popcount += popcnt

	// Update rank sampling
	lenranks := len(v.ranks)
	overflow := vlength - (lenranks * v.opts.Sr)
	if overflow > 0 {
		v.ranks = append(v.ranks, 0)

		rank := bit.Rank(bits, size-overflow-1)
		v.ranks[lenranks] = v.popcount - popcnt + rank
	}

	// Update select sampling
	lenidx := len(v.indices)
	overflow = v.popcount - (lenidx * v.opts.Ss)
	if overflow > 0 {
		v.indices = append(v.indices, 0)

		sel := bit.Select(bits, popcnt-overflow+1)
		v.indices[lenidx] = (vlength - size + sel) & ^0x3F
	}
}
Example #2
0
// Rank1 counts the number of 1s from
// the beginning up to the ith index.
func (v *BitVector) Rank1(i int) int {
	if i >= v.bits.Len() {
		panic("ranksel: index out of range")
	}

	j := i / v.opts.Sr
	ip := (j * v.opts.Sr) >> 6
	rank := v.ranks[j]

	aidx := i & 63
	bidx := i >> 6
	vbits := v.bits.Bits()
	for _, b := range vbits[ip:bidx] {
		rank += bit.PopCount(b)
	}

	return rank + bit.Rank(vbits[bidx], aidx)
}