2019-09-20 19:28:22 +00:00
|
|
|
package rlepluslazy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Version = 0
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrWrongVersion = errors.New("invalid RLE+ version")
|
|
|
|
ErrDecode = fmt.Errorf("invalid encoding for RLE+ version %d", Version)
|
|
|
|
)
|
|
|
|
|
|
|
|
type RLE struct {
|
2019-09-23 17:58:58 +00:00
|
|
|
buf []byte
|
2019-09-20 19:28:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 14:47:36 +00:00
|
|
|
func FromBuf(buf []byte) (RLE, error) {
|
|
|
|
rle := RLE{buf: buf}
|
2019-09-20 19:28:22 +00:00
|
|
|
|
2019-09-23 17:58:58 +00:00
|
|
|
if len(buf) > 0 && buf[0]&3 != Version {
|
2019-12-07 14:47:36 +00:00
|
|
|
return RLE{}, xerrors.Errorf("could not create RLE+ for a buffer: %w", ErrWrongVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := rle.Count()
|
|
|
|
if err != nil {
|
|
|
|
return RLE{}, err
|
2019-09-20 19:28:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:58:58 +00:00
|
|
|
return rle, nil
|
2019-09-20 19:28:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:58:58 +00:00
|
|
|
func (rle *RLE) RunIterator() (RunIterator, error) {
|
2019-09-24 01:11:56 +00:00
|
|
|
source, err := DecodeRLE(rle.buf)
|
|
|
|
return source, err
|
|
|
|
}
|
|
|
|
|
2019-12-07 14:47:36 +00:00
|
|
|
func (rle *RLE) Count() (uint64, error) {
|
|
|
|
it, err := rle.RunIterator()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return Count(it)
|
|
|
|
}
|