-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncrlite.go
255 lines (201 loc) · 4.7 KB
/
ncrlite.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package ncrlite
import (
"errors"
"io"
"math/bits"
"slices"
)
// Writes a compressed version of set to w.
//
// Assumes no duplicates in set. Forgets about the order.
func Compress(w io.Writer, set []uint64) error {
slices.Sort(set)
return CompressSorted(w, set)
}
// Writes a compressed version of set to w.
//
// Assumes set is sorted and has no duplictes.
func CompressSorted(w io.Writer, set []uint64) error {
bw := newBitWriter(w)
bw.WriteUvarint(uint64(len(set)))
if err := bw.Err(); err != nil {
return err
}
if len(set) == 0 {
return bw.Close()
}
if len(set) == 1 {
bw.WriteUvarint(set[0])
return bw.Close()
}
// Compute deltas
ds := make([]uint64, len(set))
// None of the other deltas can be zero, so add one. As set contains
// at least two element, set[0] can't be 2⁶⁴-1, so there is no overflow.
ds[0] = set[0] + 1
for i := 0; i < len(ds)-1; i++ {
if set[i+1] <= set[i] {
panic("set has duplicates or is not sorted")
}
ds[i+1] = set[i+1] - set[i]
}
// Compute bitlength counts of deltas
freq := []int{}
for i := 0; i < len(ds); i++ {
bn := bits.Len64(ds[i]) - 1
for bn >= len(freq) {
freq = append(freq, 0)
}
freq[bn]++
}
// Compute Huffman code for the bitlengths
code := buildHuffmanCode(freq)
// Pack Huffman code
code.Pack(bw)
if err := bw.Err(); err != nil {
return err
}
// Pack each delta
for _, d := range ds {
bn := bits.Len64(d) - 1
bw.WriteBits(uint64(code[bn].code), int(code[bn].length))
bw.WriteBits(d^(1<<bn), bn)
}
// End with single byte so that when reading we can
// peek efficiently without hitting EOF.
bw.WriteBits(0xaa, 8)
return bw.Close()
}
// Decompresses a set of uint64s from r.
//
// The returned slice will be sorted.
func Decompress(r io.Reader) ([]uint64, error) {
d, err := NewDecompressor(r)
if err != nil {
return nil, err
}
ret := make([]uint64, d.Remaining())
err = d.Read(ret)
if err != nil {
return nil, err
}
return ret, nil
}
type Decompressor struct {
br *bitReader
size uint64
remaining uint64
l io.Writer
tree htLut // Huffman tree
prev uint64 // last value emitted
started bool // true if a value has been emitted
}
// Returns the number of uint64 remaining to be decompressed.
func (d *Decompressor) Remaining() uint64 {
return d.remaining
}
var ErrNoMore = errors.New("Reading beyond end of set")
// Return the total number of bytes read so far.
func (d *Decompressor) BytesRead() int {
return d.br.total
}
// Do the actual reading after having accounted for all error conditions
// and corner cases.
func (d *Decompressor) read(set []uint64) {
for i := 0; i < len(set); i++ {
// Read codeword for length
node := 0
var entry htLutEntry
for {
code := d.br.PeekByte()
entry = d.tree[node+int(code)]
if entry.skip != 0 {
break
}
d.br.SkipBits(8)
node = entry.next
}
d.br.SkipBits(entry.skip)
delta := d.br.ReadBits(entry.value) | (1 << entry.value)
val := d.prev + delta
if !d.started {
val-- // we shifted the first value so it can't be zero as delta
d.started = true
}
d.prev = val
set[i] = val
}
}
// Fill set with decompressed uint64s.
func (d *Decompressor) Read(set []uint64) error {
if len(set) == 0 {
return nil
}
if d.size == 0 {
return ErrNoMore
}
if d.size == 1 {
if d.remaining == 0 {
return ErrNoMore
}
set[0] = d.br.ReadUvarint()
if err := d.br.Err(); err != nil {
return err
}
d.remaining = 0
if len(set) > 1 {
return ErrNoMore
}
return nil
}
if d.remaining < uint64(len(set)) {
return ErrNoMore
}
if d.tree == nil {
for i := 0; i < len(set); i++ {
val := d.prev + 1
if !d.started {
val-- // we shifted the first value so it can't be zero as delta
d.started = true
}
d.prev = val
set[i] = val
}
} else {
d.read(set)
}
d.remaining -= uint64(len(set))
if d.remaining == 0 {
if d.br.ReadBits(8) != 0xaa {
return errors.New("Incorrect endmarker")
}
}
return d.br.Err()
}
// Returns a new Decompressor that reads a set of uint64s from r incrementally.
func NewDecompressor(r io.Reader) (*Decompressor, error) {
return NewDecompressorWithLogging(r, nil)
}
// Returns a new Decompressor that reads a set of uint64s from r incrementally.
//
// Logs information about the compressed format to l.
func NewDecompressorWithLogging(r io.Reader, l io.Writer) (*Decompressor, error) {
br := newBitReader(r)
d := &Decompressor{br: br}
// Read size of set
d.size = br.ReadUvarint()
if err := br.Err(); err != nil {
return nil, err
}
d.remaining = d.size
if d.size <= 1 {
return d, nil
}
// Read Huffman code
var err error
d.tree, err = unpackHuffmanTree(br, l)
if err != nil {
return nil, err
}
return d, nil
}