2026-02-10 14:02:11 +00:00
|
|
|
|
package enc
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-07 21:27:18 +03:00
|
|
|
|
"log"
|
2026-02-10 14:02:11 +00:00
|
|
|
|
"math"
|
|
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
bin "gordenko.dev/dima/bin/little"
|
|
|
|
|
|
"gordenko.dev/dima/qb"
|
2026-02-10 14:02:11 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
// COMPRESSOR REVERSE
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
Формат:
|
|
|
|
|
|
base value (var u64)
|
|
|
|
|
|
delta (var u64)
|
|
|
|
|
|
q (qty of previous deltas; msb=0 - series, msb=1 - non series, low 7 bits - qty 1..128)
|
|
|
|
|
|
delta
|
|
|
|
|
|
delta
|
|
|
|
|
|
delta
|
|
|
|
|
|
q
|
|
|
|
|
|
|
|
|
|
|
|
Дельта рахується від base value.
|
|
|
|
|
|
Декодування у зворотньому порядку.
|
|
|
|
|
|
|
|
|
|
|
|
Після base value слідують run або literal блоки.
|
|
|
|
|
|
Run блок - це delta + header byte в кінці.
|
|
|
|
|
|
Literal блок - це від одної до N дельт + header byte в кінці.
|
|
|
|
|
|
Спочатку створюється literal блок.
|
|
|
|
|
|
Якщо для останної дельти додається дублікат, literal блок модифікується -
|
|
|
|
|
|
лічильник зменшується до 1. А остання дельта переміщюється в новий run блок.
|
|
|
|
|
|
Причому лічильник 0 - означає 2 елементи. Приклад:
|
|
|
|
|
|
До:
|
|
|
|
|
|
v1 v2 v3 h-byte(literal, 3) <- v3
|
|
|
|
|
|
Після:
|
|
|
|
|
|
v1 v2 h-byte(literal, 2) v3 h-byte(run, 2)
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
type CumulativeDeltaCompressor struct {
|
|
|
|
|
|
buf []byte
|
|
|
|
|
|
coef float64
|
|
|
|
|
|
pos int
|
|
|
|
|
|
baseValue float64
|
|
|
|
|
|
lastDelta uint64
|
|
|
|
|
|
h byte
|
|
|
|
|
|
state *CumulativeDeltaCapturedState
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
// Після відновлення із снапшота
|
|
|
|
|
|
func NewCumulativeDeltaCompressor(buf []byte, payloadSize int, fracDigits byte) *CumulativeDeltaCompressor {
|
2026-02-10 14:02:11 +00:00
|
|
|
|
var coef float64 = 1
|
|
|
|
|
|
if fracDigits > 0 {
|
|
|
|
|
|
coef = math.Pow(10, float64(fracDigits))
|
|
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
s := &CumulativeDeltaCompressor{
|
2026-02-10 14:02:11 +00:00
|
|
|
|
buf: buf,
|
2026-06-07 21:27:18 +03:00
|
|
|
|
pos: payloadSize, // перший вільний байт
|
2026-02-10 14:02:11 +00:00
|
|
|
|
coef: coef,
|
|
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
if payloadSize > 0 {
|
|
|
|
|
|
u64, _, err := bin.GetVarUint64(buf)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatalf("bug: get base value: %s", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
s.baseValue = float64(u64) / s.coef
|
|
|
|
|
|
s.pos--
|
|
|
|
|
|
s.h = s.buf[s.pos]
|
|
|
|
|
|
var n int
|
|
|
|
|
|
s.lastDelta, n, err = bin.ReverseGetVarUint64(s.buf[:s.pos])
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatalf("bug: get last delta: %s", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
s.pos -= n
|
|
|
|
|
|
}
|
|
|
|
|
|
return s
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
// func (s *CumulativeDeltaCompressor) Size() int {
|
|
|
|
|
|
// return s.pos
|
|
|
|
|
|
// }
|
2026-02-10 14:02:11 +00:00
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
func (s *CumulativeDeltaCompressor) Evaluate(value float64) (compressionWay int, requiredSpace int) {
|
|
|
|
|
|
var (
|
|
|
|
|
|
delta = uint64((value-s.baseValue)*s.coef + eps)
|
|
|
|
|
|
)
|
|
|
|
|
|
if s.pos > 0 {
|
|
|
|
|
|
if s.h < 128 {
|
|
|
|
|
|
// run
|
|
|
|
|
|
if delta == s.lastDelta && s.h < 127 {
|
|
|
|
|
|
compressionWay = incrementRun
|
|
|
|
|
|
requiredSpace += bin.CountVarUint64(uint64(s.lastDelta)) + // current delta
|
|
|
|
|
|
hSize
|
|
|
|
|
|
} else {
|
|
|
|
|
|
compressionWay = endSeries
|
|
|
|
|
|
requiredSpace += bin.CountVarUint64(uint64(s.lastDelta)) + // previous delta
|
|
|
|
|
|
hSize + // h - end of run
|
|
|
|
|
|
bin.CountVarUint64(uint64(delta)) + // new literal
|
|
|
|
|
|
hSize // new h
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// literal
|
|
|
|
|
|
if delta != s.lastDelta {
|
|
|
|
|
|
if s.h < 255 {
|
|
|
|
|
|
compressionWay = incrementLiteral
|
|
|
|
|
|
requiredSpace += bin.CountVarUint64(uint64(s.lastDelta)) + // previous delta
|
|
|
|
|
|
bin.CountVarUint64(uint64(delta)) + // new delta
|
|
|
|
|
|
hSize
|
|
|
|
|
|
} else {
|
|
|
|
|
|
compressionWay = endSeries
|
|
|
|
|
|
requiredSpace += bin.CountVarUint64(uint64(s.lastDelta)) + // previous delta
|
|
|
|
|
|
hSize + // h - end of literal
|
|
|
|
|
|
bin.CountVarUint64(uint64(delta)) + // new literal
|
|
|
|
|
|
hSize // new h
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
compressionWay = startRun
|
|
|
|
|
|
if s.h > 128 {
|
|
|
|
|
|
requiredSpace += hSize // h - end of literal
|
|
|
|
|
|
}
|
|
|
|
|
|
requiredSpace += bin.CountVarUint64(uint64(delta)) + // new delta
|
|
|
|
|
|
hSize // new h
|
|
|
|
|
|
}
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
} else {
|
|
|
|
|
|
// encode base value
|
|
|
|
|
|
compressionWay = addBaseValue
|
|
|
|
|
|
requiredSpace = bin.CountVarUint64(uint64(value*s.coef)) + // base value
|
|
|
|
|
|
bin.CountVarUint64(uint64(delta)) + // new delta
|
|
|
|
|
|
+hSize // new h
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) Compress(compressionWay int, value float64) {
|
|
|
|
|
|
delta := uint64((value-s.baseValue)*s.coef + eps)
|
|
|
|
|
|
switch compressionWay {
|
|
|
|
|
|
case incrementRun:
|
|
|
|
|
|
s.h++
|
|
|
|
|
|
case incrementLiteral:
|
|
|
|
|
|
// write previous delta and increment counter
|
|
|
|
|
|
n, _ := bin.ReversePutVarUint64(s.buf[s.pos:], uint64(s.lastDelta))
|
|
|
|
|
|
s.pos += n
|
|
|
|
|
|
s.lastDelta = delta
|
|
|
|
|
|
s.h++
|
|
|
|
|
|
case endSeries:
|
|
|
|
|
|
n, _ := bin.ReversePutVarUint64(s.buf[s.pos:], uint64(s.lastDelta))
|
|
|
|
|
|
s.pos += n
|
|
|
|
|
|
// write h - end of run
|
|
|
|
|
|
s.buf[s.pos] = s.h
|
|
|
|
|
|
s.pos++
|
|
|
|
|
|
// start new literal (length=1)
|
|
|
|
|
|
s.lastDelta = delta
|
|
|
|
|
|
s.h = 128
|
|
|
|
|
|
case startRun:
|
|
|
|
|
|
if s.h > 128 {
|
|
|
|
|
|
// write h - end of literal (because length > 1)
|
|
|
|
|
|
s.h--
|
|
|
|
|
|
s.buf[s.pos] = s.h
|
|
|
|
|
|
s.pos++
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
// start new run (length=2)
|
|
|
|
|
|
s.h = 0
|
|
|
|
|
|
case addBaseValue:
|
|
|
|
|
|
// start new literal (length=1)
|
|
|
|
|
|
n, _ := bin.PutVarUint64(s.buf[s.pos:], uint64(value*s.coef))
|
|
|
|
|
|
s.pos += n
|
|
|
|
|
|
s.baseValue = value
|
|
|
|
|
|
// start new literal (length=1)
|
|
|
|
|
|
s.lastDelta = 0
|
|
|
|
|
|
s.h = 128
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
}
|
2026-02-10 14:02:11 +00:00
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
func (s *CumulativeDeltaCompressor) DeleteLast() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type CumulativeDeltaCapturedState struct {
|
|
|
|
|
|
H byte
|
|
|
|
|
|
LastDelta uint64
|
|
|
|
|
|
Payload []byte
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) CaptureState() {
|
|
|
|
|
|
if s.state != nil {
|
|
|
|
|
|
qb.Abort(qb.RepeatableLock, nil)
|
|
|
|
|
|
}
|
|
|
|
|
|
// позиція посувається вліво, отже може перескочити на попередній chunk
|
|
|
|
|
|
s.state = &CumulativeDeltaCapturedState{
|
|
|
|
|
|
H: s.h,
|
|
|
|
|
|
LastDelta: s.lastDelta,
|
|
|
|
|
|
Payload: s.buf[:s.pos],
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// fix - повернути в Pool буфери
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) ForgetCapturedState() {
|
|
|
|
|
|
s.state = nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) Offset() int {
|
|
|
|
|
|
// if s.state != nil {
|
|
|
|
|
|
// return s.state.Pos
|
|
|
|
|
|
// }
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Snapshot - для створення снапшота.
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) Snapshot() (left []byte, right []byte) {
|
|
|
|
|
|
if s.state == nil {
|
|
|
|
|
|
left = s.buf[:s.pos]
|
|
|
|
|
|
right = s.encodeTail(s.lastDelta, s.h)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
left = s.state.Payload
|
|
|
|
|
|
right = s.encodeTail(s.state.LastDelta, s.state.H)
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) encodeTail(lastDelta uint64, h byte) []byte {
|
|
|
|
|
|
tail := make([]byte, 10) // max var uint64 + h
|
|
|
|
|
|
n, _ := bin.PutVarUint64(tail, lastDelta)
|
|
|
|
|
|
tail[n] = h
|
|
|
|
|
|
return tail[:n+1]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) Rotate(newbuf []byte) (payloadSize int) {
|
|
|
|
|
|
n, _ := bin.PutVarUint64(s.buf[s.pos:], s.lastDelta)
|
|
|
|
|
|
s.pos += n
|
|
|
|
|
|
s.buf[s.pos] = s.h
|
|
|
|
|
|
s.pos++
|
|
|
|
|
|
payloadSize = s.pos
|
|
|
|
|
|
// УВАГА!
|
|
|
|
|
|
// state не чіпаємо
|
|
|
|
|
|
s.buf = newbuf
|
|
|
|
|
|
s.pos = 0
|
|
|
|
|
|
s.baseValue = 0
|
|
|
|
|
|
s.lastDelta = 0
|
|
|
|
|
|
s.h = 0
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) CreateDecompressor() qb.ValueDecompressor {
|
|
|
|
|
|
var (
|
|
|
|
|
|
h = s.h
|
|
|
|
|
|
lastDelta = s.lastDelta
|
|
|
|
|
|
payload = s.buf[:s.pos]
|
|
|
|
|
|
)
|
|
|
|
|
|
if s.state != nil {
|
|
|
|
|
|
h = s.state.H
|
|
|
|
|
|
lastDelta = s.state.LastDelta
|
|
|
|
|
|
payload = s.state.Payload
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
return NewCumulativeDeltaDecompressorFromState(CumulativeDeltaDecompressorFromStateOptions{
|
|
|
|
|
|
Coef: s.coef,
|
|
|
|
|
|
H: h,
|
|
|
|
|
|
LastDelta: lastDelta,
|
|
|
|
|
|
Payload: payload,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaCompressor) Chunks() []byte {
|
|
|
|
|
|
return s.buf
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
// DECOMPRESSOR
|
|
|
|
|
|
|
|
|
|
|
|
type CumulativeDeltaDecompressor struct {
|
|
|
|
|
|
buf []byte
|
|
|
|
|
|
coef float64
|
|
|
|
|
|
pos int
|
|
|
|
|
|
bound int
|
|
|
|
|
|
baseValue float64
|
|
|
|
|
|
lastValue float64
|
|
|
|
|
|
isRun bool
|
|
|
|
|
|
pending int
|
|
|
|
|
|
done bool
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewCumulativeDeltaDecompressor(buf []byte, fracDigits byte) *CumulativeDeltaDecompressor {
|
|
|
|
|
|
var coef float64 = 1
|
|
|
|
|
|
if fracDigits > 0 {
|
|
|
|
|
|
coef = math.Pow(10, float64(fracDigits))
|
|
|
|
|
|
}
|
|
|
|
|
|
u64, n, err := bin.GetVarUint64(buf)
|
2026-02-10 14:02:11 +00:00
|
|
|
|
if err != nil {
|
2026-06-07 21:27:18 +03:00
|
|
|
|
log.Fatalf("bug: get base value: %s", err)
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
//fmt.Printf("baseValue: %.2f, bound: %d, pos: %d\n", float64(u64)/coef, n, size)
|
|
|
|
|
|
s := &CumulativeDeltaDecompressor{
|
|
|
|
|
|
buf: buf,
|
|
|
|
|
|
coef: coef,
|
|
|
|
|
|
pos: len(buf), // first free
|
|
|
|
|
|
baseValue: float64(u64) / coef,
|
|
|
|
|
|
bound: n,
|
|
|
|
|
|
}
|
|
|
|
|
|
// читаю заголовок наступної серії
|
|
|
|
|
|
s.readHeader()
|
|
|
|
|
|
s.readValue()
|
|
|
|
|
|
return s
|
|
|
|
|
|
}
|
2026-02-10 14:02:11 +00:00
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
type CumulativeDeltaDecompressorFromStateOptions struct {
|
|
|
|
|
|
Coef float64
|
|
|
|
|
|
H byte
|
|
|
|
|
|
LastDelta uint64
|
|
|
|
|
|
Payload []byte
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewCumulativeDeltaDecompressorFromState(opt CumulativeDeltaDecompressorFromStateOptions) *CumulativeDeltaDecompressor {
|
|
|
|
|
|
u64, n, err := bin.GetVarUint64(opt.Payload)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatalf("bug: get base value: %s", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
//fmt.Printf("baseValue: %.2f, bound: %d, pos: %d\n", float64(u64)/opt.Coef, n, size)
|
|
|
|
|
|
s := &CumulativeDeltaDecompressor{
|
|
|
|
|
|
buf: opt.Payload,
|
|
|
|
|
|
coef: opt.Coef,
|
|
|
|
|
|
pos: len(opt.Payload), // first free
|
|
|
|
|
|
baseValue: float64(u64) / opt.Coef,
|
|
|
|
|
|
bound: n,
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
s.lastValue = s.baseValue + float64(opt.LastDelta)/s.coef
|
|
|
|
|
|
s.decodeHeaderByte(opt.H)
|
|
|
|
|
|
//fmt.Printf("payload: %d\n", opt.Payload)
|
|
|
|
|
|
//fmt.Printf("restore from state: bound=%d, isRun=%t, pending=%d, baseValue=%v, lastValue=%v\n",
|
|
|
|
|
|
// n, s.isRun, s.pending, s.baseValue, s.lastValue)
|
|
|
|
|
|
return s
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaDecompressor) NextValue() (value float64, done bool) {
|
|
|
|
|
|
//fmt.Printf("NextValue(): bound: %d, pos: %d, pending: %d\n", s.bound, s.pos, s.pending)
|
|
|
|
|
|
if s.done {
|
|
|
|
|
|
return 0, true
|
|
|
|
|
|
}
|
|
|
|
|
|
// метод працює як do while - спочатку значення, а потім перевірка умови
|
|
|
|
|
|
value = s.lastValue
|
|
|
|
|
|
s.pending--
|
|
|
|
|
|
if s.pending > 0 {
|
|
|
|
|
|
// якщо в серії залишаються елементи
|
|
|
|
|
|
if !s.isRun {
|
|
|
|
|
|
s.readValue()
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if s.pos > s.bound {
|
|
|
|
|
|
// в серії більше немає елементів, отже перевіряє чи є ще дані в буфері.
|
|
|
|
|
|
// дані є - читаю заголовок наступної серії
|
|
|
|
|
|
s.readHeader()
|
|
|
|
|
|
s.readValue()
|
2026-02-10 14:02:11 +00:00
|
|
|
|
} else {
|
2026-06-07 21:27:18 +03:00
|
|
|
|
s.done = true
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
// серія завершена - перевіряю чи є ще серії
|
|
|
|
|
|
return value, false
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
|
func (s *CumulativeDeltaDecompressor) readHeader() {
|
|
|
|
|
|
//fmt.Println("read from pos:", s.pos)
|
2026-02-10 14:02:11 +00:00
|
|
|
|
s.pos--
|
2026-06-07 21:27:18 +03:00
|
|
|
|
h := s.buf[s.pos]
|
|
|
|
|
|
s.decodeHeaderByte(h)
|
|
|
|
|
|
|
|
|
|
|
|
// fmt.Println("h:", h)
|
|
|
|
|
|
// fmt.Println("isRun:", s.isRun)
|
|
|
|
|
|
// fmt.Println("pending:", s.pending)
|
|
|
|
|
|
}
|
|
|
|
|
|
func (s *CumulativeDeltaDecompressor) readValue() {
|
|
|
|
|
|
u64, n, err := bin.ReverseGetVarUint64(s.buf[:s.pos])
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
// fmt.Println()
|
|
|
|
|
|
// fmt.Println("read from pos:", s.pos)
|
|
|
|
|
|
// fmt.Println("read delta:", u64)
|
|
|
|
|
|
// fmt.Println("read delta n:", n)
|
|
|
|
|
|
// fmt.Println()
|
|
|
|
|
|
s.pos -= n
|
|
|
|
|
|
s.lastValue = s.baseValue + float64(u64)/s.coef
|
|
|
|
|
|
//fmt.Println(s.baseValue, float64(u64)/s.coef)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *CumulativeDeltaDecompressor) decodeHeaderByte(h byte) {
|
|
|
|
|
|
s.isRun = h < 128
|
|
|
|
|
|
if s.isRun {
|
|
|
|
|
|
s.pending = int(h) + 2
|
2026-02-10 14:02:11 +00:00
|
|
|
|
} else {
|
2026-06-07 21:27:18 +03:00
|
|
|
|
s.pending = int(h&127) + 1
|
2026-02-10 14:02:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-07 21:27:18 +03:00
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
// func (s *CumulativeDeltaCompressor) Append(value float64) {
|
|
|
|
|
|
// if s.pos == 0 {
|
|
|
|
|
|
// // base value
|
|
|
|
|
|
// n, _ := bin.PutVarUint64(s.buf[s.pos:], uint64(value*s.coef))
|
|
|
|
|
|
// s.pos += n
|
|
|
|
|
|
// s.baseValue = value
|
|
|
|
|
|
// s.appendNewLiteral(0)
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// delta := uint64((value-s.baseValue)*s.coef + eps)
|
|
|
|
|
|
// if delta == s.lastDelta {
|
|
|
|
|
|
// if s.h < 128 {
|
|
|
|
|
|
// // run блок - отже треба збільшити лічильник
|
|
|
|
|
|
// if s.h < 127 {
|
|
|
|
|
|
// // increase counter
|
|
|
|
|
|
// s.h++
|
|
|
|
|
|
// s.buf[s.pos-1] = s.h
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// // не можу збільшити - буде переповнення. Додаю новий literal блок
|
|
|
|
|
|
// // counter overflow
|
|
|
|
|
|
// s.appendNewLiteral(delta)
|
|
|
|
|
|
// }
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// // literal блок.
|
|
|
|
|
|
// // Якщо в ньому лише одне значення - перетворюю його на run блок.
|
|
|
|
|
|
// // Інакше забираю останнє значення із literal блока і додаю новий run блок.
|
|
|
|
|
|
// q := s.h & 127
|
|
|
|
|
|
// if q == 0 { // 1 кодується як 0
|
|
|
|
|
|
// s.convertLiteralToRun()
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// // забираю останнє значення із literal блоку щоб зробити run блок
|
|
|
|
|
|
// s.convertLastFromLiteralToRun()
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// if s.h < 127 { // fix 128?
|
|
|
|
|
|
// // end of run
|
|
|
|
|
|
// s.appendNewLiteral(delta)
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// if s.h < 255 {
|
|
|
|
|
|
// // encode value from pos - 1, then append h byte
|
|
|
|
|
|
// s.appendDeltaToLiteral(delta)
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// // overflowed - encode new
|
|
|
|
|
|
// s.appendNewLiteral(delta)
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|