func (d *IntegerDecoder) Read() int64 {
	switch d.encoding {
	case intCompressedRLE:
		return ZigZagDecode(d.rleFirst) + int64(d.i)*ZigZagDecode(d.rleDelta)
	default:
		v := ZigZagDecode(d.values[d.i])
		v = v + d.prev
		d.prev = v
		return v
	}
}
Read returns the next value from the decoder.
package tsm1
import (
	"encoding/binary"
	"fmt"
	"github.com/jwilder/encoding/simple8b"
)
const (
	intUncompressed = 0
	intCompressedSimple = 1
	intCompressedRLE = 2
)
type IntegerEncoder struct {
	prev   int64
	rle    bool
	values []uint64
}
func NewIntegerEncoder(sz int) IntegerEncoder {
	return IntegerEncoder{
		rle:    true,
		values: make([]uint64, 0, sz),
	}
}
func (e *IntegerEncoder) Flush() {}
func (e *IntegerEncoder) Reset() {
	e.prev = 0
	e.rle = true
	e.values = e.values[:0]
}
func (e *IntegerEncoder) Write(v int64) {
	delta := v - e.prev
	e.prev = v
	enc := ZigZagEncode(delta)
	if len(e.values) > 1 {
		e.rle = e.rle && e.values[len(e.values)-1] == enc
	}
	e.values = append(e.values, enc)
}
func (e *IntegerEncoder) Bytes() ([]byte, error) {
	if e.rle && len(e.values) > 2 {
		return e.encodeRLE()
	}
	for _, v := range e.values {
		if v > simple8b.MaxValue {
			return e.encodeUncompressed()
		}
	}
	return e.encodePacked()
}
func (e *IntegerEncoder) encodeRLE() ([]byte, error) {
	var b [31]byte
	b[0] = byte(intCompressedRLE) << 4
	i := 1
	binary.BigEndian.PutUint64(b[i:], e.values[0])
	i += 8
	i += binary.PutUvarint(b[i:], e.values[1])
	i += binary.PutUvarint(b[i:], uint64(len(e.values)-1))
	return b[:i], nil
}
func (e *IntegerEncoder) encodePacked() ([]byte, error) {
	if len(e.values) == 0 {
		return nil, nil
	}
	encoded, err := simple8b.EncodeAll(e.values[1:])
	if err != nil {
		return nil, err
	}
	b := make([]byte, 1+(len(encoded)+1)*8)
	b[0] = byte(intCompressedSimple) << 4
	binary.BigEndian.PutUint64(b[1:9], e.values[0])
	for i, v := range encoded {
		binary.BigEndian.PutUint64(b[9+i*8:9+i*8+8], v)
	}
	return b, nil
}
func (e *IntegerEncoder) encodeUncompressed() ([]byte, error) {
	if len(e.values) == 0 {
		return nil, nil
	}
	b := make([]byte, 1+len(e.values)*8)
	b[0] = byte(intUncompressed) << 4
	for i, v := range e.values {
		binary.BigEndian.PutUint64(b[1+i*8:1+i*8+8], v)
	}
	return b, nil
}
type IntegerDecoder struct {
	values [240]uint64
	bytes  []byte
	i      int
	n      int
	prev   int64
	first  bool
	rleFirst uint64
	rleDelta uint64
	encoding byte
	err      error
}
func (d *IntegerDecoder) SetBytes(b []byte) {
	if len(b) > 0 {
		d.encoding = b[0] >> 4
		d.bytes = b[1:]
	} else {
		d.encoding = 0
		d.bytes = nil
	}
	d.i = 0
	d.n = 0
	d.prev = 0
	d.first = true
	d.rleFirst = 0
	d.rleDelta = 0
	d.err = nil
}
func (d *IntegerDecoder) Next() bool {
	if d.i >= d.n && len(d.bytes) == 0 {
		return false
	}
	d.i++
	if d.i >= d.n {
		switch d.encoding {
		case intUncompressed:
			d.decodeUncompressed()
		case intCompressedSimple:
			d.decodePacked()
		case intCompressedRLE:
			d.decodeRLE()
		default:
			d.err = fmt.Errorf("unknown encoding %v", d.encoding)
		}
	}
	return d.err == nil && d.i < d.n
}
func (d *IntegerDecoder) Error() error {
	return d.err
}