File: jetset.go

package info (click to toggle)
golang-github-pointlander-jetset 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 64 kB
  • sloc: makefile: 2
file content (297 lines) | stat: -rw-r--r-- 6,895 bytes parent folder | download | duplicates (2)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2015 The jetset Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package jetset

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"github.com/pointlander/compress"
)

type Set struct {
	data []byte
	length int
}

func (set Set) String() string {
	buffer, in, output := bytes.NewBuffer(set.data), make(chan []byte, 1), make([]byte, set.length)
	in <- output
	close(in)
	compress.BijectiveBurrowsWheelerDecoder(in).MoveToFrontRunLengthDecoder().AdaptiveDecoder().Decode(buffer)

	codes, code, reader, space := "[", uint64(0), bytes.NewReader(output), ""
	next := func() error {
		return binary.Read(reader, binary.BigEndian, &code)
	}
	for err := next(); err == nil; err = next() {
		codes += space + fmt.Sprintf("%v", code)
		space = " "
	}
	return codes + "]"
}

type decompressor struct {
	buffer *bytes.Reader
	adding bool
	current, count, offset uint64
}

func newDecompressor(set Set) *decompressor {
	if len(set.data) == 0 {
		return &decompressor{buffer: bytes.NewReader(nil)}
	}
	buffer, in, output := bytes.NewBuffer(set.data), make(chan []byte, 1), make([]byte, set.length)
	in <- output
	close(in)
	compress.BijectiveBurrowsWheelerDecoder(in).MoveToFrontRunLengthDecoder().AdaptiveDecoder().Decode(buffer)
	return &decompressor{buffer: bytes.NewReader(output)}
}

func (d *decompressor) decompress() (uint64, bool) {
	if d.adding {
		if d.count > 0 {
			d.count--
			d.current += d.offset
			return d.current, true
		} else {
			var code uint64
			err := binary.Read(d.buffer, binary.BigEndian, &code)
			if err == nil {
				if code == 0 {
					err := binary.Read(d.buffer, binary.BigEndian, &d.count)
					if err != nil {
						panic(err)
					}

					err = binary.Read(d.buffer, binary.BigEndian, &d.offset)
					if err != nil {
						panic(err)
					}

					d.count--
					d.current += d.offset
					return d.current, true
				} else {
					d.current += code
					return d.current, true
				}
			} else {
				return 0, false
			}
		}
	} else {
		err := binary.Read(d.buffer, binary.BigEndian, &d.current)
		if err == nil {
			d.adding = true
			return d.current, true
		} else {
			return 0, false
		}
	}
}

type compressor struct {
	buffer *bytes.Buffer
	writing bool
	current, count, offset uint64
}

func newCompressor() *compressor {
	return &compressor{buffer: &bytes.Buffer{}}
}

func (c *compressor) compress(e uint64) {
	if c.writing {
		offset := e - c.current
		if c.offset == offset {
			c.count++
		} else if c.count > 0 {
			binary.Write(c.buffer, binary.BigEndian, uint64(0))
			binary.Write(c.buffer, binary.BigEndian, c.count + 1)
			binary.Write(c.buffer, binary.BigEndian, c.offset)
			c.count, c.offset = 0, offset
		} else {
			binary.Write(c.buffer, binary.BigEndian, c.offset)
			c.offset = offset
		}
	} else {
		c.writing, c.offset = true, e
	}

	c.current = e
}

func (c *compressor) close() Set {
	if c.writing {
		if c.count > 0 {
			binary.Write(c.buffer, binary.BigEndian, uint64(0))
			binary.Write(c.buffer, binary.BigEndian, c.count + 1)
			binary.Write(c.buffer, binary.BigEndian, c.offset)
		} else {
			binary.Write(c.buffer, binary.BigEndian, c.offset)
		}
		buffer, in := &bytes.Buffer{}, make(chan []byte, 1)
		in <- c.buffer.Bytes()
		close(in)
		compress.BijectiveBurrowsWheelerCoder(in).MoveToFrontRunLengthCoder().AdaptiveCoder().Code(buffer)
		return Set{data: buffer.Bytes(), length: c.buffer.Len()}
	}
	return Set{}
}

func (s Set) Copy() Set {
	cp := make([]byte, len(s.data))
	copy(cp, s.data)
	return Set{data: cp, length: s.length}
}

func (s Set) Add(e uint64) Set {
	comp, decomp, found := newCompressor(), newDecompressor(s), false
	d, status := decomp.decompress()
	if !status {
		comp.compress(e)
		return comp.close()
	}
	for status {
 		if !found && e == d {
			found = true
		} else if !found && e < d {
			comp.compress(e)
			found = true
		}
		comp.compress(d)
		d, status = decomp.decompress()
	}
	if !found {
		comp.compress(e)
	}
	return comp.close()
}

func (a Set) AddRange(begin, end uint64) Set {
	comp, decomp_a, d_b := newCompressor(), newDecompressor(a), begin
	d_a, status_a := decomp_a.decompress()
	for status_a && d_b <= end {
		if d_a == d_b {
			comp.compress(d_a)
			d_a, status_a = decomp_a.decompress()
			d_b++
		} else if d_a < d_b {
			comp.compress(d_a)
			d_a, status_a = decomp_a.decompress()
		} else {
			comp.compress(d_b)
			d_b++
		}
	}
	for status_a {
		comp.compress(d_a)
		d_a, status_a = decomp_a.decompress()
	}
	for d_b <= end {
		comp.compress(d_b)
		d_b++
	}
	return comp.close()
}

func (s Set) Has(e uint64) bool {
	decomp := newDecompressor(s)
	for item, status := decomp.decompress(); status; item, status = decomp.decompress() {
		if item == e {
			return true
		}
	}
	return false
}

func (s Set) Complement(max uint64) Set {
	comp, decomp, i := newCompressor(), newDecompressor(s), uint64(0)
	d, status := decomp.decompress()
	for i <= max && status {
		if i < d {
			comp.compress(i)
		} else if i == d {
			d, status = decomp.decompress()
		}
		i++
	}
	for i <= max {
		comp.compress(i)
		i++
	}
	return comp.close()
}

func (a Set) Union(b Set) Set {
	comp, decomp_a, decomp_b := newCompressor(), newDecompressor(a), newDecompressor(b)
	d_a, status_a := decomp_a.decompress()
	d_b, status_b := decomp_b.decompress()
	for status_a && status_b {
		if d_a == d_b {
			comp.compress(d_a)
			d_a, status_a = decomp_a.decompress()
			d_b, status_b = decomp_b.decompress()
		} else if d_a < d_b {
			comp.compress(d_a)
			d_a, status_a = decomp_a.decompress()
		} else {
			comp.compress(d_b)
			d_b, status_b = decomp_b.decompress()
		}
	}
	for status_a {
		comp.compress(d_a)
		d_a, status_a = decomp_a.decompress()
	}
	for status_b {
		comp.compress(d_b)
		d_b, status_b = decomp_b.decompress()
	}
	return comp.close()
}

func (a Set) Intersection(b Set) Set {
	comp, decomp_a, decomp_b := newCompressor(), newDecompressor(a), newDecompressor(b)
	d_a, status_a := decomp_a.decompress()
	d_b, status_b := decomp_b.decompress()
	for status_a && status_b {
		if d_a == d_b {
			comp.compress(d_a)
			d_a, status_a = decomp_a.decompress()
			d_b, status_b = decomp_b.decompress()
		} else if d_a < d_b {
			d_a, status_a = decomp_a.decompress()
		} else {
			d_b, status_b = decomp_b.decompress()
		}
	}
	return comp.close()
}

func (a Set) Intersects(b Set) bool {
	decomp_a, decomp_b := newDecompressor(a), newDecompressor(b)
	d_a, status_a := decomp_a.decompress()
	d_b, status_b := decomp_b.decompress()
	for status_a && status_b {
		if d_a == d_b {
			return true
		} else if d_a < d_b {
			d_a, status_a = decomp_a.decompress()
		} else {
			d_b, status_b = decomp_b.decompress()
		}
	}
	return false
}

func (s Set) Len() int {
	length, decomp := 0, newDecompressor(s)
	for _, status := decomp.decompress(); status; _, status = decomp.decompress() {
		length++
	}
	return length
}