-
Notifications
You must be signed in to change notification settings - Fork 20
/
bitset_parse.go
47 lines (39 loc) · 999 Bytes
/
bitset_parse.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
package goga
// BitsetParse - an interface to an object that is able
// to parse a bitset into an array of uint64s
type BitsetParse interface {
SetFormat([]int)
Process(*Bitset) []uint64
}
type bitsetParse struct {
expectedBitsetSize int
format []int
}
// CreateBitsetParse returns an instance of a bitset parser
func CreateBitsetParse() BitsetParse {
return &bitsetParse{}
}
func (bp *bitsetParse) SetFormat(format []int) {
bp.expectedBitsetSize = 0
for _, i := range format {
bp.expectedBitsetSize += i
}
bp.format = format
}
func (bp *bitsetParse) Process(bitset *Bitset) []uint64 {
if bitset.GetSize() != bp.expectedBitsetSize {
panic("Input format does not match bitset size")
}
ret := make([]uint64, len(bp.format))
runningBits := 0
for retIndex, numBits := range bp.format {
ret[retIndex] = 0
for i := 0; i < numBits; i++ {
if bitset.Get(i+runningBits) == 1 {
ret[retIndex] |= (1 << uint(i))
}
}
runningBits += numBits
}
return ret
}