@gmod/cram 4.0.3 → 4.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cram-bundle.js +1 -1
- package/dist/cramFile/file.js +43 -9
- package/dist/cramFile/file.js.map +1 -1
- package/dist/cramFile/slice/decodeRecord.js +3 -3
- package/dist/cramFile/slice/decodeRecord.js.map +1 -1
- package/dist/cramFile/util.d.ts +0 -2
- package/dist/cramFile/util.js +3 -31
- package/dist/cramFile/util.js.map +1 -1
- package/dist/htscodecs/arith_gen.d.ts +5 -7
- package/dist/htscodecs/arith_gen.js +122 -105
- package/dist/htscodecs/arith_gen.js.map +1 -1
- package/dist/htscodecs/arith_sh.d.ts +1 -8
- package/dist/htscodecs/arith_sh.js +16 -10
- package/dist/htscodecs/arith_sh.js.map +1 -1
- package/dist/htscodecs/byte_model.d.ts +1 -6
- package/dist/htscodecs/byte_model.js +25 -17
- package/dist/htscodecs/byte_model.js.map +1 -1
- package/dist/htscodecs/fqzcomp.d.ts +1 -1
- package/dist/htscodecs/fqzcomp.js +98 -77
- package/dist/htscodecs/fqzcomp.js.map +1 -1
- package/dist/htscodecs/index.d.ts +5 -5
- package/dist/htscodecs/index.js +53 -16
- package/dist/htscodecs/index.js.map +1 -1
- package/dist/htscodecs/iostream.d.ts +9 -20
- package/dist/htscodecs/iostream.js +21 -116
- package/dist/htscodecs/iostream.js.map +1 -1
- package/dist/htscodecs/rans.d.ts +1 -1
- package/dist/htscodecs/rans.js +65 -54
- package/dist/htscodecs/rans.js.map +1 -1
- package/dist/htscodecs/rans4x16.d.ts +1 -1
- package/dist/htscodecs/rans4x16.js +151 -111
- package/dist/htscodecs/rans4x16.js.map +1 -1
- package/dist/htscodecs/tok3.d.ts +1 -2
- package/dist/htscodecs/tok3.js +82 -239
- package/dist/htscodecs/tok3.js.map +1 -1
- package/dist/util.d.ts +1 -0
- package/dist/util.js +20 -0
- package/dist/util.js.map +1 -0
- package/esm/cramFile/file.js +3 -2
- package/esm/cramFile/file.js.map +1 -1
- package/esm/cramFile/slice/decodeRecord.js +3 -3
- package/esm/cramFile/slice/decodeRecord.js.map +1 -1
- package/esm/cramFile/util.d.ts +0 -2
- package/esm/cramFile/util.js +3 -29
- package/esm/cramFile/util.js.map +1 -1
- package/esm/htscodecs/arith_gen.d.ts +5 -7
- package/esm/htscodecs/arith_gen.js +108 -97
- package/esm/htscodecs/arith_gen.js.map +1 -1
- package/esm/htscodecs/arith_sh.d.ts +1 -8
- package/esm/htscodecs/arith_sh.js +14 -11
- package/esm/htscodecs/arith_sh.js.map +1 -1
- package/esm/htscodecs/byte_model.d.ts +1 -6
- package/esm/htscodecs/byte_model.js +23 -18
- package/esm/htscodecs/byte_model.js.map +1 -1
- package/esm/htscodecs/fqzcomp.d.ts +1 -1
- package/esm/htscodecs/fqzcomp.js +91 -76
- package/esm/htscodecs/fqzcomp.js.map +1 -1
- package/esm/htscodecs/index.d.ts +5 -5
- package/esm/htscodecs/index.js +14 -20
- package/esm/htscodecs/index.js.map +1 -1
- package/esm/htscodecs/iostream.d.ts +9 -20
- package/esm/htscodecs/iostream.js +19 -117
- package/esm/htscodecs/iostream.js.map +1 -1
- package/esm/htscodecs/rans.d.ts +1 -1
- package/esm/htscodecs/rans.js +61 -56
- package/esm/htscodecs/rans.js.map +1 -1
- package/esm/htscodecs/rans4x16.d.ts +1 -1
- package/esm/htscodecs/rans4x16.js +143 -109
- package/esm/htscodecs/rans4x16.js.map +1 -1
- package/esm/htscodecs/tok3.d.ts +1 -2
- package/esm/htscodecs/tok3.js +41 -237
- package/esm/htscodecs/tok3.js.map +1 -1
- package/esm/util.d.ts +1 -0
- package/esm/util.js +17 -0
- package/esm/util.js.map +1 -0
- package/package.json +2 -3
- package/src/cramFile/file.ts +3 -2
- package/src/cramFile/slice/decodeRecord.ts +3 -5
- package/src/cramFile/util.ts +6 -39
- package/src/htscodecs/{arith_gen.js → arith_gen.ts} +133 -95
- package/src/htscodecs/{arith_sh.js → arith_sh.ts} +17 -9
- package/src/htscodecs/{byte_model.js → byte_model.ts} +26 -16
- package/src/htscodecs/{fqzcomp.js → fqzcomp.ts} +108 -74
- package/src/htscodecs/{index.js → index.ts} +14 -20
- package/src/htscodecs/iostream.ts +159 -0
- package/src/htscodecs/{rans.js → rans.ts} +73 -56
- package/src/htscodecs/{rans4x16.js → rans4x16.ts} +180 -111
- package/src/htscodecs/tok3.ts +197 -0
- package/src/util.ts +16 -0
- package/src/htscodecs/iostream.js +0 -257
- package/src/htscodecs/tok3.js +0 -413
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/* eslint-disable no-var */
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
|
|
1
4
|
/*
|
|
2
5
|
* Copyright (c) 2019,2020 Genome Research Ltd.
|
|
3
6
|
* Author(s): James Bonfield
|
|
@@ -31,27 +34,12 @@
|
|
|
31
34
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32
35
|
*/
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
const IOStream = require('./iostream')
|
|
36
|
-
const ByteModel = require('./byte_model')
|
|
37
|
-
const bzip2 = require('bzip2')
|
|
37
|
+
import bzip2 from 'bzip2'
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
return sum
|
|
45
|
-
}
|
|
46
|
-
function concatUint8Array(args) {
|
|
47
|
-
const mergedArray = new Uint8Array(sum(args))
|
|
48
|
-
let offset = 0
|
|
49
|
-
for (const entry of args) {
|
|
50
|
-
mergedArray.set(entry, offset)
|
|
51
|
-
offset += entry.length
|
|
52
|
-
}
|
|
53
|
-
return mergedArray
|
|
54
|
-
}
|
|
39
|
+
import RangeCoder from './arith_sh'
|
|
40
|
+
import ByteModel from './byte_model'
|
|
41
|
+
import IOStream from './iostream'
|
|
42
|
+
import { concatUint8Array } from '../util'
|
|
55
43
|
|
|
56
44
|
const ARITH_ORDER = 1
|
|
57
45
|
const ARITH_EXT = 4
|
|
@@ -61,21 +49,26 @@ const ARITH_CAT = 32
|
|
|
61
49
|
const ARITH_RLE = 64
|
|
62
50
|
const ARITH_PACK = 128
|
|
63
51
|
|
|
64
|
-
|
|
65
|
-
|
|
52
|
+
export default class RangeCoderGen {
|
|
53
|
+
stream: IOStream
|
|
54
|
+
decode(src: Uint8Array) {
|
|
66
55
|
this.stream = new IOStream(src)
|
|
67
56
|
return this.decodeStream(this.stream)
|
|
68
57
|
}
|
|
69
58
|
|
|
70
|
-
decodeStream(stream, n_out = 0) {
|
|
71
|
-
|
|
72
|
-
if (!(flags & ARITH_NOSIZE))
|
|
73
|
-
|
|
59
|
+
decodeStream(stream: IOStream, n_out = 0) {
|
|
60
|
+
const flags = this.stream.ReadByte()
|
|
61
|
+
if (!(flags & ARITH_NOSIZE)) {
|
|
62
|
+
n_out = this.stream.ReadUint7()
|
|
63
|
+
}
|
|
64
|
+
let e_len = n_out
|
|
74
65
|
|
|
75
|
-
|
|
66
|
+
const order = flags & ARITH_ORDER
|
|
76
67
|
|
|
77
68
|
// 4-way recursion
|
|
78
|
-
if (flags & ARITH_STRIPE)
|
|
69
|
+
if (flags & ARITH_STRIPE) {
|
|
70
|
+
return this.decodeStripe(this.stream, n_out)
|
|
71
|
+
}
|
|
79
72
|
|
|
80
73
|
// Meta data
|
|
81
74
|
if (flags & ARITH_PACK) {
|
|
@@ -84,7 +77,9 @@ module.exports = class RangeCoderGen {
|
|
|
84
77
|
}
|
|
85
78
|
|
|
86
79
|
// NOP, useful for tiny blocks
|
|
87
|
-
if (flags & ARITH_CAT)
|
|
80
|
+
if (flags & ARITH_CAT) {
|
|
81
|
+
var data = this.decodeCat(this.stream, e_len)
|
|
82
|
+
}
|
|
88
83
|
// Entropy decode
|
|
89
84
|
else if (flags & ARITH_EXT) {
|
|
90
85
|
var data = this.decodeExt(this.stream, e_len)
|
|
@@ -99,46 +94,55 @@ module.exports = class RangeCoderGen {
|
|
|
99
94
|
}
|
|
100
95
|
|
|
101
96
|
// Transforms
|
|
102
|
-
if (flags & ARITH_PACK)
|
|
97
|
+
if (flags & ARITH_PACK) {
|
|
98
|
+
data = this.decodePack(data, P, n_out)
|
|
99
|
+
}
|
|
103
100
|
|
|
104
101
|
return data
|
|
105
102
|
}
|
|
106
103
|
|
|
107
|
-
|
|
104
|
+
// ----------------------------------------------------------------------
|
|
108
105
|
// Order-0 codec
|
|
109
106
|
decode0(stream, n_out) {
|
|
110
|
-
|
|
107
|
+
const output = new Uint8Array(n_out)
|
|
111
108
|
|
|
112
|
-
|
|
113
|
-
if (max_sym == 0)
|
|
109
|
+
let max_sym = stream.ReadByte()
|
|
110
|
+
if (max_sym == 0) {
|
|
111
|
+
max_sym = 256
|
|
112
|
+
}
|
|
114
113
|
|
|
115
|
-
|
|
114
|
+
const byte_model = new ByteModel(max_sym)
|
|
116
115
|
|
|
117
|
-
|
|
116
|
+
const rc = new RangeCoder(stream)
|
|
118
117
|
rc.RangeStartDecode(stream)
|
|
119
118
|
|
|
120
|
-
for (
|
|
119
|
+
for (let i = 0; i < n_out; i++) {
|
|
121
120
|
output[i] = byte_model.ModelDecode(stream, rc)
|
|
121
|
+
}
|
|
122
122
|
|
|
123
123
|
return output
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
// ----------------------------------------------------------------------
|
|
127
127
|
// Order-1 codec
|
|
128
128
|
|
|
129
129
|
decode1(stream, n_out) {
|
|
130
|
-
|
|
130
|
+
const output = new Uint8Array(n_out)
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
if (max_sym == 0)
|
|
132
|
+
let max_sym = stream.ReadByte()
|
|
133
|
+
if (max_sym == 0) {
|
|
134
|
+
max_sym = 256
|
|
135
|
+
}
|
|
134
136
|
|
|
135
|
-
|
|
136
|
-
for (var i = 0; i < max_sym; i++)
|
|
137
|
+
const byte_model = new Array(max_sym)
|
|
138
|
+
for (var i = 0; i < max_sym; i++) {
|
|
139
|
+
byte_model[i] = new ByteModel(max_sym)
|
|
140
|
+
}
|
|
137
141
|
|
|
138
|
-
|
|
142
|
+
const rc = new RangeCoder(stream)
|
|
139
143
|
rc.RangeStartDecode(stream)
|
|
140
144
|
|
|
141
|
-
|
|
145
|
+
let last = 0
|
|
142
146
|
for (var i = 0; i < n_out; i++) {
|
|
143
147
|
output[i] = byte_model[last].ModelDecode(stream, rc)
|
|
144
148
|
last = output[i]
|
|
@@ -147,7 +151,7 @@ module.exports = class RangeCoderGen {
|
|
|
147
151
|
return output
|
|
148
152
|
}
|
|
149
153
|
|
|
150
|
-
|
|
154
|
+
// ----------------------------------------------------------------------
|
|
151
155
|
// External codec
|
|
152
156
|
decodeExt(stream, n_out) {
|
|
153
157
|
const bits = bzip2.array(stream.buf.slice(stream.pos))
|
|
@@ -164,114 +168,138 @@ module.exports = class RangeCoderGen {
|
|
|
164
168
|
return concatUint8Array(chunks)
|
|
165
169
|
}
|
|
166
170
|
|
|
167
|
-
|
|
171
|
+
// ----------------------------------------------------------------------
|
|
168
172
|
// Order-0 RLE codec
|
|
169
173
|
decodeRLE0(stream, n_out) {
|
|
170
|
-
|
|
174
|
+
const output = new Uint8Array(n_out)
|
|
171
175
|
|
|
172
|
-
|
|
173
|
-
if (max_sym == 0)
|
|
176
|
+
let max_sym = stream.ReadByte()
|
|
177
|
+
if (max_sym == 0) {
|
|
178
|
+
max_sym = 256
|
|
179
|
+
}
|
|
174
180
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
for (var i = 0; i <= 257; i++)
|
|
181
|
+
const model_lit = new ByteModel(max_sym)
|
|
182
|
+
const model_run = new Array(258)
|
|
183
|
+
for (var i = 0; i <= 257; i++) {
|
|
184
|
+
model_run[i] = new ByteModel(4)
|
|
185
|
+
}
|
|
178
186
|
|
|
179
|
-
|
|
187
|
+
const rc = new RangeCoder(stream)
|
|
180
188
|
rc.RangeStartDecode(stream)
|
|
181
189
|
|
|
182
190
|
var i = 0
|
|
183
191
|
while (i < n_out) {
|
|
184
192
|
output[i] = model_lit.ModelDecode(stream, rc)
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
193
|
+
let part = model_run[output[i]].ModelDecode(stream, rc)
|
|
194
|
+
let run = part
|
|
195
|
+
let rctx = 256
|
|
188
196
|
while (part == 3) {
|
|
189
197
|
part = model_run[rctx].ModelDecode(stream, rc)
|
|
190
198
|
rctx = 257
|
|
191
199
|
run += part
|
|
192
200
|
}
|
|
193
|
-
for (
|
|
201
|
+
for (let j = 1; j <= run; j++) {
|
|
202
|
+
output[i + j] = output[i]
|
|
203
|
+
}
|
|
194
204
|
i += run + 1
|
|
195
205
|
}
|
|
196
206
|
|
|
197
207
|
return output
|
|
198
208
|
}
|
|
199
209
|
|
|
200
|
-
|
|
210
|
+
// ----------------------------------------------------------------------
|
|
201
211
|
// Order-1 RLE codec
|
|
202
212
|
|
|
203
213
|
decodeRLE1(stream, n_out) {
|
|
204
|
-
|
|
214
|
+
const output = new Uint8Array(n_out)
|
|
205
215
|
|
|
206
|
-
|
|
207
|
-
if (max_sym == 0)
|
|
216
|
+
let max_sym = stream.ReadByte()
|
|
217
|
+
if (max_sym == 0) {
|
|
218
|
+
max_sym = 256
|
|
219
|
+
}
|
|
208
220
|
|
|
209
|
-
|
|
210
|
-
for (var i = 0; i < max_sym; i++)
|
|
221
|
+
const model_lit = new Array(max_sym)
|
|
222
|
+
for (var i = 0; i < max_sym; i++) {
|
|
223
|
+
model_lit[i] = new ByteModel(max_sym)
|
|
224
|
+
}
|
|
211
225
|
|
|
212
|
-
|
|
213
|
-
for (var i = 0; i <= 257; i++)
|
|
226
|
+
const model_run = new Array(258)
|
|
227
|
+
for (var i = 0; i <= 257; i++) {
|
|
228
|
+
model_run[i] = new ByteModel(4)
|
|
229
|
+
}
|
|
214
230
|
|
|
215
|
-
|
|
231
|
+
const rc = new RangeCoder(stream)
|
|
216
232
|
rc.RangeStartDecode(stream)
|
|
217
233
|
|
|
218
|
-
|
|
234
|
+
let last = 0
|
|
219
235
|
var i = 0
|
|
220
236
|
while (i < n_out) {
|
|
221
237
|
output[i] = model_lit[last].ModelDecode(stream, rc)
|
|
222
238
|
last = output[i]
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
239
|
+
let part = model_run[output[i]].ModelDecode(stream, rc)
|
|
240
|
+
let run = part
|
|
241
|
+
let rctx = 256
|
|
226
242
|
while (part == 3) {
|
|
227
243
|
part = model_run[rctx].ModelDecode(stream, rc)
|
|
228
244
|
rctx = 257
|
|
229
245
|
run += part
|
|
230
246
|
}
|
|
231
|
-
for (
|
|
247
|
+
for (let j = 1; j <= run; j++) {
|
|
248
|
+
output[i + j] = output[i]
|
|
249
|
+
}
|
|
232
250
|
i += run + 1
|
|
233
251
|
}
|
|
234
252
|
|
|
235
253
|
return output
|
|
236
254
|
}
|
|
237
255
|
|
|
238
|
-
|
|
256
|
+
// ----------------------------------------------------------------------
|
|
239
257
|
// Pack method
|
|
240
258
|
decodePackMeta(stream) {
|
|
241
259
|
this.nsym = stream.ReadByte()
|
|
242
260
|
|
|
243
|
-
|
|
244
|
-
for (
|
|
261
|
+
const M = new Array(this.nsym)
|
|
262
|
+
for (let i = 0; i < this.nsym; i++) {
|
|
263
|
+
M[i] = stream.ReadByte()
|
|
264
|
+
}
|
|
245
265
|
|
|
246
|
-
|
|
266
|
+
const e_len = stream.ReadUint7() // Could be derived data from nsym and n_out
|
|
247
267
|
|
|
248
268
|
return [M, e_len]
|
|
249
269
|
}
|
|
250
270
|
|
|
251
271
|
decodePack(data, M, len) {
|
|
252
|
-
|
|
272
|
+
const out = new Uint8Array(len)
|
|
253
273
|
|
|
254
274
|
if (this.nsym <= 1) {
|
|
255
275
|
// Constant value
|
|
256
|
-
for (var i = 0; i < len; i++)
|
|
276
|
+
for (var i = 0; i < len; i++) {
|
|
277
|
+
out[i] = M[0]
|
|
278
|
+
}
|
|
257
279
|
} else if (this.nsym <= 2) {
|
|
258
280
|
// 1 bit per value
|
|
259
281
|
for (var i = 0, j = 0; i < len; i++) {
|
|
260
|
-
if (i % 8 == 0)
|
|
282
|
+
if (i % 8 == 0) {
|
|
283
|
+
var v = data[j++]
|
|
284
|
+
}
|
|
261
285
|
out[i] = M[v & 1]
|
|
262
286
|
v >>= 1
|
|
263
287
|
}
|
|
264
288
|
} else if (this.nsym <= 4) {
|
|
265
289
|
// 2 bits per value
|
|
266
290
|
for (var i = 0, j = 0; i < len; i++) {
|
|
267
|
-
if (i % 4 == 0)
|
|
291
|
+
if (i % 4 == 0) {
|
|
292
|
+
var v = data[j++]
|
|
293
|
+
}
|
|
268
294
|
out[i] = M[v & 3]
|
|
269
295
|
v >>= 2
|
|
270
296
|
}
|
|
271
297
|
} else if (this.nsym <= 16) {
|
|
272
298
|
// 4 bits per value
|
|
273
299
|
for (var i = 0, j = 0; i < len; i++) {
|
|
274
|
-
if (i % 2 == 0)
|
|
300
|
+
if (i % 2 == 0) {
|
|
301
|
+
var v = data[j++]
|
|
302
|
+
}
|
|
275
303
|
out[i] = M[v & 15]
|
|
276
304
|
v >>= 4
|
|
277
305
|
}
|
|
@@ -285,14 +313,20 @@ module.exports = class RangeCoderGen {
|
|
|
285
313
|
|
|
286
314
|
// Compute M array and return meta-data stream
|
|
287
315
|
packMeta(src) {
|
|
288
|
-
|
|
316
|
+
const stream = new IOStream('', 0, 1024)
|
|
289
317
|
|
|
290
318
|
// Count symbols
|
|
291
|
-
|
|
292
|
-
for (var i = 0; i < src.length; i++)
|
|
319
|
+
const M = new Array(256)
|
|
320
|
+
for (var i = 0; i < src.length; i++) {
|
|
321
|
+
M[src[i]] = 1
|
|
322
|
+
}
|
|
293
323
|
|
|
294
324
|
// Write Map
|
|
295
|
-
for (var nsym = 0, i = 0; i < 256; i++)
|
|
325
|
+
for (var nsym = 0, i = 0; i < 256; i++) {
|
|
326
|
+
if (M[i]) {
|
|
327
|
+
M[i] = ++nsym
|
|
328
|
+
}
|
|
329
|
+
} // map to 1..N
|
|
296
330
|
stream.WriteByte(nsym)
|
|
297
331
|
|
|
298
332
|
// FIXME: add check for nsym > 16?
|
|
@@ -308,24 +342,26 @@ module.exports = class RangeCoderGen {
|
|
|
308
342
|
}
|
|
309
343
|
|
|
310
344
|
decodeStripe(stream, len) {
|
|
311
|
-
|
|
345
|
+
const N = stream.ReadByte()
|
|
312
346
|
|
|
313
347
|
// Retrieve lengths
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
for (var j = 0; j < N; j++)
|
|
348
|
+
const clen = new Array(N)
|
|
349
|
+
const ulen = new Array(N)
|
|
350
|
+
for (var j = 0; j < N; j++) {
|
|
351
|
+
clen[j] = stream.ReadUint7()
|
|
352
|
+
}
|
|
317
353
|
|
|
318
354
|
// Decode streams
|
|
319
|
-
|
|
355
|
+
const T = new Array(N)
|
|
320
356
|
for (var j = 0; j < N; j++) {
|
|
321
357
|
ulen[j] = Math.floor(len / N) + (len % N > j)
|
|
322
358
|
T[j] = this.decodeStream(stream, ulen[j])
|
|
323
359
|
}
|
|
324
360
|
|
|
325
361
|
// Transpose
|
|
326
|
-
|
|
362
|
+
const out = new Uint8Array(len)
|
|
327
363
|
for (var j = 0; j < N; j++) {
|
|
328
|
-
for (
|
|
364
|
+
for (let i = 0; i < ulen[j]; i++) {
|
|
329
365
|
out[i * N + j] = T[j][i]
|
|
330
366
|
}
|
|
331
367
|
}
|
|
@@ -333,11 +369,13 @@ module.exports = class RangeCoderGen {
|
|
|
333
369
|
return out
|
|
334
370
|
}
|
|
335
371
|
|
|
336
|
-
|
|
372
|
+
// ----------------------------------------------------------------------
|
|
337
373
|
// Cat method
|
|
338
374
|
decodeCat(stream, len) {
|
|
339
|
-
|
|
340
|
-
for (
|
|
375
|
+
const out = new Uint8Array(len)
|
|
376
|
+
for (let i = 0; i < len; i++) {
|
|
377
|
+
out[i] = stream.ReadByte()
|
|
378
|
+
}
|
|
341
379
|
|
|
342
380
|
return out
|
|
343
381
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
1
3
|
/*
|
|
2
4
|
* Copyright (c) 2019 Genome Research Ltd.
|
|
3
5
|
* Author(s): James Bonfield
|
|
@@ -39,9 +41,9 @@
|
|
|
39
41
|
// Arith_sh.js 6.7s decode (32-bit with carries)
|
|
40
42
|
// Arith.js 317.0s decode (64-bit no carries); int64 crippling it.
|
|
41
43
|
|
|
42
|
-
|
|
44
|
+
// ----------------------------------------------------------------------
|
|
43
45
|
// Arithmetic (range) coder
|
|
44
|
-
|
|
46
|
+
export default class RangeCoder {
|
|
45
47
|
constructor(src) {
|
|
46
48
|
this.low = 0
|
|
47
49
|
this.range = 0xffffffff
|
|
@@ -52,23 +54,25 @@ module.exports = class RangeCoder {
|
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
RangeStartDecode(src) {
|
|
55
|
-
for (
|
|
57
|
+
for (let i = 0; i < 5; i++) {
|
|
58
|
+
this.code = (this.code << 8) + src.ReadByte()
|
|
59
|
+
}
|
|
56
60
|
this.code &= 0xffffffff
|
|
57
61
|
this.code >>>= 0 // force to be +ve int
|
|
58
62
|
}
|
|
59
63
|
|
|
60
64
|
RangeGetFrequency(tot_freq) {
|
|
61
65
|
this.range = Math.floor(this.range / tot_freq)
|
|
62
|
-
//return this.code / this.range;
|
|
66
|
+
// return this.code / this.range;
|
|
63
67
|
return Math.floor(this.code / this.range)
|
|
64
68
|
|
|
65
69
|
// Conceptual scenario; return freq only and don't modify range yet
|
|
66
|
-
//return Math.floor(this.code / (Math.floor(this.range / tot_freq)));
|
|
70
|
+
// return Math.floor(this.code / (Math.floor(this.range / tot_freq)));
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
RangeDecode(src, sym_low, sym_freq, tot_freq) {
|
|
70
74
|
// Conceptually we divide range here, but in practice we cached it earlier
|
|
71
|
-
//this.range = Math.floor(this.range / tot_freq);
|
|
75
|
+
// this.range = Math.floor(this.range / tot_freq);
|
|
72
76
|
|
|
73
77
|
this.code -= sym_low * this.range
|
|
74
78
|
this.range *= sym_freq
|
|
@@ -111,7 +115,7 @@ module.exports = class RangeCoder {
|
|
|
111
115
|
}
|
|
112
116
|
|
|
113
117
|
RangeEncode(dst, sym_low, sym_freq, tot_freq) {
|
|
114
|
-
|
|
118
|
+
const old_low = this.low
|
|
115
119
|
this.range = Math.floor(this.range / tot_freq)
|
|
116
120
|
this.low += sym_low * this.range
|
|
117
121
|
this.low >>>= 0 // Truncate to +ve int so we can spot overflow
|
|
@@ -121,7 +125,9 @@ module.exports = class RangeCoder {
|
|
|
121
125
|
// NB: can this.low < old_low occur twice before range < (1<<24)?
|
|
122
126
|
// We claim not, but prove it!
|
|
123
127
|
if (this.low < old_low) {
|
|
124
|
-
if (this.carry != 0)
|
|
128
|
+
if (this.carry != 0) {
|
|
129
|
+
console.log('ERROR: Multiple carry')
|
|
130
|
+
}
|
|
125
131
|
this.carry = 1
|
|
126
132
|
}
|
|
127
133
|
|
|
@@ -133,6 +139,8 @@ module.exports = class RangeCoder {
|
|
|
133
139
|
}
|
|
134
140
|
|
|
135
141
|
RangeFinishEncode(dst) {
|
|
136
|
-
for (
|
|
142
|
+
for (let i = 0; i < 5; i++) {
|
|
143
|
+
this.RangeShiftLow(dst)
|
|
144
|
+
}
|
|
137
145
|
}
|
|
138
146
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/* eslint-disable no-var */
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
|
|
1
4
|
/*
|
|
2
5
|
* Copyright (c) 2019 Genome Research Ltd.
|
|
3
6
|
* Author(s): James Bonfield
|
|
@@ -38,14 +41,14 @@
|
|
|
38
41
|
const MAX_FREQ = (1 << 16) - 17
|
|
39
42
|
const STEP = 16
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
export default class ByteModel {
|
|
42
45
|
constructor(max_sym = 256) {
|
|
43
46
|
this.total_freq = max_sym
|
|
44
47
|
this.max_sym = max_sym - 1
|
|
45
|
-
this.S =
|
|
46
|
-
this.F =
|
|
48
|
+
this.S = []
|
|
49
|
+
this.F = []
|
|
47
50
|
|
|
48
|
-
for (
|
|
51
|
+
for (let i = 0; i <= this.max_sym; i++) {
|
|
49
52
|
this.S[i] = i
|
|
50
53
|
this.F[i] = 1
|
|
51
54
|
}
|
|
@@ -53,12 +56,14 @@ module.exports = class ByteModel {
|
|
|
53
56
|
|
|
54
57
|
ModelDecode(src, rc) {
|
|
55
58
|
// Find symbol
|
|
56
|
-
|
|
59
|
+
const freq = rc.RangeGetFrequency(this.total_freq)
|
|
57
60
|
|
|
58
61
|
// Linear scan to find cumulative frequency 'freq'
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
while (acc + this.F[x] <= freq)
|
|
62
|
+
let acc = 0
|
|
63
|
+
let x = 0
|
|
64
|
+
while (acc + this.F[x] <= freq) {
|
|
65
|
+
acc += this.F[x++]
|
|
66
|
+
}
|
|
62
67
|
|
|
63
68
|
// for (var acc = 0; (acc += this.F[x]) <= freq; x++)
|
|
64
69
|
// ;
|
|
@@ -70,12 +75,14 @@ module.exports = class ByteModel {
|
|
|
70
75
|
// Update model
|
|
71
76
|
this.F[x] += STEP
|
|
72
77
|
this.total_freq += STEP
|
|
73
|
-
if (this.total_freq > MAX_FREQ)
|
|
78
|
+
if (this.total_freq > MAX_FREQ) {
|
|
79
|
+
this.ModelRenormalise()
|
|
80
|
+
}
|
|
74
81
|
|
|
75
82
|
// Keep symbols approximately frequency sorted
|
|
76
|
-
|
|
83
|
+
const sym = this.S[x]
|
|
77
84
|
if (x > 0 && this.F[x] > this.F[x - 1]) {
|
|
78
|
-
|
|
85
|
+
let tmp = this.F[x]
|
|
79
86
|
this.F[x] = this.F[x - 1]
|
|
80
87
|
this.F[x - 1] = tmp
|
|
81
88
|
|
|
@@ -90,7 +97,7 @@ module.exports = class ByteModel {
|
|
|
90
97
|
ModelRenormalise() {
|
|
91
98
|
// Halve all the frequencies, being careful not to hit zero
|
|
92
99
|
this.total_freq = 0
|
|
93
|
-
for (
|
|
100
|
+
for (let i = 0; i <= this.max_sym; i++) {
|
|
94
101
|
this.F[i] -= Math.floor(this.F[i] / 2)
|
|
95
102
|
this.total_freq += this.F[i]
|
|
96
103
|
}
|
|
@@ -98,8 +105,10 @@ module.exports = class ByteModel {
|
|
|
98
105
|
|
|
99
106
|
ModelEncode(dst, rc, sym) {
|
|
100
107
|
// Find cumulative frequency
|
|
101
|
-
|
|
102
|
-
for (var x = 0; this.S[x] != sym; x++)
|
|
108
|
+
let acc = 0
|
|
109
|
+
for (var x = 0; this.S[x] != sym; x++) {
|
|
110
|
+
acc += this.F[x]
|
|
111
|
+
}
|
|
103
112
|
|
|
104
113
|
// Encode
|
|
105
114
|
rc.RangeEncode(dst, acc, this.F[x], this.total_freq)
|
|
@@ -107,14 +116,15 @@ module.exports = class ByteModel {
|
|
|
107
116
|
// Update model
|
|
108
117
|
this.F[x] += STEP
|
|
109
118
|
this.total_freq += STEP
|
|
110
|
-
if (this.total_freq > MAX_FREQ)
|
|
119
|
+
if (this.total_freq > MAX_FREQ) {
|
|
111
120
|
// FIXME x2
|
|
112
121
|
this.ModelRenormalise()
|
|
122
|
+
}
|
|
113
123
|
|
|
114
124
|
// Keep symbols approximately frequency sorted
|
|
115
125
|
var sym = this.S[x]
|
|
116
126
|
if (x > 0 && this.F[x] > this.F[x - 1]) {
|
|
117
|
-
|
|
127
|
+
let tmp = this.F[x]
|
|
118
128
|
this.F[x] = this.F[x - 1]
|
|
119
129
|
this.F[x - 1] = tmp
|
|
120
130
|
|