2019-09-23 15:50:41 +00:00
|
|
|
package rlepluslazy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
)
|
|
|
|
|
|
|
|
func EncodeRuns(rit RunIterator, buf []byte) ([]byte, error) {
|
2019-09-23 17:58:58 +00:00
|
|
|
bv := writeBitvec(buf)
|
|
|
|
bv.Put(0, 2)
|
2019-09-23 15:50:41 +00:00
|
|
|
|
|
|
|
first := true
|
|
|
|
varBuf := make([]byte, binary.MaxVarintLen64)
|
|
|
|
|
|
|
|
for rit.HasNext() {
|
|
|
|
run, err := rit.NextRun()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if first {
|
|
|
|
if run.Val {
|
2019-09-23 17:58:58 +00:00
|
|
|
bv.Put(1, 1)
|
2019-09-23 15:50:41 +00:00
|
|
|
} else {
|
2019-09-23 17:58:58 +00:00
|
|
|
bv.Put(0, 1)
|
2019-09-23 15:50:41 +00:00
|
|
|
}
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case run.Len == 1:
|
2019-09-23 17:58:58 +00:00
|
|
|
bv.Put(1, 1)
|
2019-09-23 15:50:41 +00:00
|
|
|
case run.Len < 16:
|
2019-09-23 17:58:58 +00:00
|
|
|
bv.Put(2, 2)
|
|
|
|
bv.Put(byte(run.Len), 4)
|
2019-09-23 15:50:41 +00:00
|
|
|
case run.Len >= 16:
|
2019-09-23 17:58:58 +00:00
|
|
|
bv.Put(0, 2)
|
2019-09-23 15:50:41 +00:00
|
|
|
numBytes := binary.PutUvarint(varBuf, run.Len)
|
|
|
|
for i := 0; i < numBytes; i++ {
|
2019-09-23 17:58:58 +00:00
|
|
|
bv.Put(varBuf[i], 8)
|
2019-09-23 15:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if first {
|
2019-09-23 17:58:58 +00:00
|
|
|
bv.Put(0, 1)
|
2019-09-23 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:58:58 +00:00
|
|
|
return bv.Out(), nil
|
2019-09-23 15:50:41 +00:00
|
|
|
|
|
|
|
}
|