typenative 0.3.0 → 0.4.0
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/CHANGELOG.md +71 -0
- package/README.md +17 -1
- package/bin/cli.js +25 -11
- package/bin/go/convert.js +11 -1
- package/bin/index.js +221 -44
- package/bin/main.js +35 -14
- package/bin/node/assert.js +2 -2
- package/bin/node/buffer.js +9 -0
- package/bin/node/dns.js +1 -1
- package/bin/node/events.js +1 -1
- package/bin/node/fs.js +1 -1
- package/bin/node/fs_watch.js +407 -0
- package/bin/node/http2.js +33 -6
- package/bin/node/readline.js +1 -1
- package/bin/node/registry.js +2 -1
- package/bin/node/stream.js +6 -3
- package/bin/node/timers.js +1 -1
- package/bin/node/tls.js +3 -1
- package/bin/node/typed_arrays.js +608 -0
- package/bin/node/util.js +3 -0
- package/bin/node/zlib.js +131 -16
- package/bin/transpiler.js +684 -62
- package/bin/version.js +1 -1
- package/conformance.md +16 -4
- package/examples/README.md +54 -0
- package/examples/fibonacci/README.md +89 -0
- package/examples/fibonacci/bench.mjs +87 -0
- package/examples/fibonacci/main.ts +21 -0
- package/examples/hello-world/README.md +62 -0
- package/examples/hello-world/bench.mjs +66 -0
- package/examples/hello-world/main.ts +7 -0
- package/examples/json-server/README.md +125 -0
- package/examples/json-server/bench.mjs +171 -0
- package/examples/json-server/main.ts +67 -0
- package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
- package/govendor/github.com/andybalholm/brotli/README.md +19 -0
- package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
- package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
- package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
- package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
- package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
- package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
- package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
- package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
- package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
- package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
- package/govendor/github.com/andybalholm/brotli/command.go +254 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
- package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
- package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
- package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
- package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
- package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
- package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
- package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
- package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
- package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
- package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
- package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
- package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
- package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
- package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
- package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
- package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
- package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
- package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
- package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
- package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
- package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
- package/govendor/github.com/andybalholm/brotli/http.go +201 -0
- package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
- package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
- package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
- package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
- package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
- package/govendor/github.com/andybalholm/brotli/params.go +37 -0
- package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
- package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
- package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
- package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
- package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
- package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
- package/govendor/github.com/andybalholm/brotli/state.go +313 -0
- package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
- package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
- package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
- package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
- package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
- package/govendor/github.com/andybalholm/brotli/util.go +7 -0
- package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
- package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
- package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
- package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
- package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
- package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
- package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
- package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
- package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
- package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
- package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
- package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +1 -1
- package/types/typenative-npm.d.ts +72 -0
- package/types/typenative.d.ts +117 -1
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
package brotli
|
|
2
|
+
|
|
3
|
+
import "math"
|
|
4
|
+
|
|
5
|
+
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
6
|
+
|
|
7
|
+
Distributed under MIT license.
|
|
8
|
+
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/* Computes the bit cost reduction by combining out[idx1] and out[idx2] and if
|
|
12
|
+
it is below a threshold, stores the pair (idx1, idx2) in the *pairs queue. */
|
|
13
|
+
func compareAndPushToQueueLiteral(out []histogramLiteral, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) {
|
|
14
|
+
var is_good_pair bool = false
|
|
15
|
+
var p histogramPair
|
|
16
|
+
p.idx2 = 0
|
|
17
|
+
p.idx1 = p.idx2
|
|
18
|
+
p.cost_combo = 0
|
|
19
|
+
p.cost_diff = p.cost_combo
|
|
20
|
+
if idx1 == idx2 {
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if idx2 < idx1 {
|
|
25
|
+
var t uint32 = idx2
|
|
26
|
+
idx2 = idx1
|
|
27
|
+
idx1 = t
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
p.idx1 = idx1
|
|
31
|
+
p.idx2 = idx2
|
|
32
|
+
p.cost_diff = 0.5 * clusterCostDiff(uint(cluster_size[idx1]), uint(cluster_size[idx2]))
|
|
33
|
+
p.cost_diff -= out[idx1].bit_cost_
|
|
34
|
+
p.cost_diff -= out[idx2].bit_cost_
|
|
35
|
+
|
|
36
|
+
if out[idx1].total_count_ == 0 {
|
|
37
|
+
p.cost_combo = out[idx2].bit_cost_
|
|
38
|
+
is_good_pair = true
|
|
39
|
+
} else if out[idx2].total_count_ == 0 {
|
|
40
|
+
p.cost_combo = out[idx1].bit_cost_
|
|
41
|
+
is_good_pair = true
|
|
42
|
+
} else {
|
|
43
|
+
var threshold float64
|
|
44
|
+
if *num_pairs == 0 {
|
|
45
|
+
threshold = 1e99
|
|
46
|
+
} else {
|
|
47
|
+
threshold = brotli_max_double(0.0, pairs[0].cost_diff)
|
|
48
|
+
}
|
|
49
|
+
var combo histogramLiteral = out[idx1]
|
|
50
|
+
var cost_combo float64
|
|
51
|
+
histogramAddHistogramLiteral(&combo, &out[idx2])
|
|
52
|
+
cost_combo = populationCostLiteral(&combo)
|
|
53
|
+
if cost_combo < threshold-p.cost_diff {
|
|
54
|
+
p.cost_combo = cost_combo
|
|
55
|
+
is_good_pair = true
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if is_good_pair {
|
|
60
|
+
p.cost_diff += p.cost_combo
|
|
61
|
+
if *num_pairs > 0 && histogramPairIsLess(&pairs[0], &p) {
|
|
62
|
+
/* Replace the top of the queue if needed. */
|
|
63
|
+
if *num_pairs < max_num_pairs {
|
|
64
|
+
pairs[*num_pairs] = pairs[0]
|
|
65
|
+
(*num_pairs)++
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
pairs[0] = p
|
|
69
|
+
} else if *num_pairs < max_num_pairs {
|
|
70
|
+
pairs[*num_pairs] = p
|
|
71
|
+
(*num_pairs)++
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func histogramCombineLiteral(out []histogramLiteral, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint {
|
|
77
|
+
var cost_diff_threshold float64 = 0.0
|
|
78
|
+
var min_cluster_size uint = 1
|
|
79
|
+
var num_pairs uint = 0
|
|
80
|
+
{
|
|
81
|
+
/* We maintain a vector of histogram pairs, with the property that the pair
|
|
82
|
+
with the maximum bit cost reduction is the first. */
|
|
83
|
+
var idx1 uint
|
|
84
|
+
for idx1 = 0; idx1 < num_clusters; idx1++ {
|
|
85
|
+
var idx2 uint
|
|
86
|
+
for idx2 = idx1 + 1; idx2 < num_clusters; idx2++ {
|
|
87
|
+
compareAndPushToQueueLiteral(out, cluster_size, clusters[idx1], clusters[idx2], max_num_pairs, pairs[0:], &num_pairs)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for num_clusters > min_cluster_size {
|
|
93
|
+
var best_idx1 uint32
|
|
94
|
+
var best_idx2 uint32
|
|
95
|
+
var i uint
|
|
96
|
+
if pairs[0].cost_diff >= cost_diff_threshold {
|
|
97
|
+
cost_diff_threshold = 1e99
|
|
98
|
+
min_cluster_size = max_clusters
|
|
99
|
+
continue
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* Take the best pair from the top of heap. */
|
|
103
|
+
best_idx1 = pairs[0].idx1
|
|
104
|
+
|
|
105
|
+
best_idx2 = pairs[0].idx2
|
|
106
|
+
histogramAddHistogramLiteral(&out[best_idx1], &out[best_idx2])
|
|
107
|
+
out[best_idx1].bit_cost_ = pairs[0].cost_combo
|
|
108
|
+
cluster_size[best_idx1] += cluster_size[best_idx2]
|
|
109
|
+
for i = 0; i < symbols_size; i++ {
|
|
110
|
+
if symbols[i] == best_idx2 {
|
|
111
|
+
symbols[i] = best_idx1
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for i = 0; i < num_clusters; i++ {
|
|
116
|
+
if clusters[i] == best_idx2 {
|
|
117
|
+
copy(clusters[i:], clusters[i+1:][:num_clusters-i-1])
|
|
118
|
+
break
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
num_clusters--
|
|
123
|
+
{
|
|
124
|
+
/* Remove pairs intersecting the just combined best pair. */
|
|
125
|
+
var copy_to_idx uint = 0
|
|
126
|
+
for i = 0; i < num_pairs; i++ {
|
|
127
|
+
var p *histogramPair = &pairs[i]
|
|
128
|
+
if p.idx1 == best_idx1 || p.idx2 == best_idx1 || p.idx1 == best_idx2 || p.idx2 == best_idx2 {
|
|
129
|
+
/* Remove invalid pair from the queue. */
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if histogramPairIsLess(&pairs[0], p) {
|
|
134
|
+
/* Replace the top of the queue if needed. */
|
|
135
|
+
var front histogramPair = pairs[0]
|
|
136
|
+
pairs[0] = *p
|
|
137
|
+
pairs[copy_to_idx] = front
|
|
138
|
+
} else {
|
|
139
|
+
pairs[copy_to_idx] = *p
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
copy_to_idx++
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
num_pairs = copy_to_idx
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/* Push new pairs formed with the combined histogram to the heap. */
|
|
149
|
+
for i = 0; i < num_clusters; i++ {
|
|
150
|
+
compareAndPushToQueueLiteral(out, cluster_size, best_idx1, clusters[i], max_num_pairs, pairs[0:], &num_pairs)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return num_clusters
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/* What is the bit cost of moving histogram from cur_symbol to candidate. */
|
|
158
|
+
func histogramBitCostDistanceLiteral(histogram *histogramLiteral, candidate *histogramLiteral) float64 {
|
|
159
|
+
if histogram.total_count_ == 0 {
|
|
160
|
+
return 0.0
|
|
161
|
+
} else {
|
|
162
|
+
var tmp histogramLiteral = *histogram
|
|
163
|
+
histogramAddHistogramLiteral(&tmp, candidate)
|
|
164
|
+
return populationCostLiteral(&tmp) - candidate.bit_cost_
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/* Find the best 'out' histogram for each of the 'in' histograms.
|
|
169
|
+
When called, clusters[0..num_clusters) contains the unique values from
|
|
170
|
+
symbols[0..in_size), but this property is not preserved in this function.
|
|
171
|
+
Note: we assume that out[]->bit_cost_ is already up-to-date. */
|
|
172
|
+
func histogramRemapLiteral(in []histogramLiteral, in_size uint, clusters []uint32, num_clusters uint, out []histogramLiteral, symbols []uint32) {
|
|
173
|
+
var i uint
|
|
174
|
+
for i = 0; i < in_size; i++ {
|
|
175
|
+
var best_out uint32
|
|
176
|
+
if i == 0 {
|
|
177
|
+
best_out = symbols[0]
|
|
178
|
+
} else {
|
|
179
|
+
best_out = symbols[i-1]
|
|
180
|
+
}
|
|
181
|
+
var best_bits float64 = histogramBitCostDistanceLiteral(&in[i], &out[best_out])
|
|
182
|
+
var j uint
|
|
183
|
+
for j = 0; j < num_clusters; j++ {
|
|
184
|
+
var cur_bits float64 = histogramBitCostDistanceLiteral(&in[i], &out[clusters[j]])
|
|
185
|
+
if cur_bits < best_bits {
|
|
186
|
+
best_bits = cur_bits
|
|
187
|
+
best_out = clusters[j]
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
symbols[i] = best_out
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/* Recompute each out based on raw and symbols. */
|
|
195
|
+
for i = 0; i < num_clusters; i++ {
|
|
196
|
+
histogramClearLiteral(&out[clusters[i]])
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
for i = 0; i < in_size; i++ {
|
|
200
|
+
histogramAddHistogramLiteral(&out[symbols[i]], &in[i])
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/* Reorders elements of the out[0..length) array and changes values in
|
|
205
|
+
symbols[0..length) array in the following way:
|
|
206
|
+
* when called, symbols[] contains indexes into out[], and has N unique
|
|
207
|
+
values (possibly N < length)
|
|
208
|
+
* on return, symbols'[i] = f(symbols[i]) and
|
|
209
|
+
out'[symbols'[i]] = out[symbols[i]], for each 0 <= i < length,
|
|
210
|
+
where f is a bijection between the range of symbols[] and [0..N), and
|
|
211
|
+
the first occurrences of values in symbols'[i] come in consecutive
|
|
212
|
+
increasing order.
|
|
213
|
+
Returns N, the number of unique values in symbols[]. */
|
|
214
|
+
|
|
215
|
+
var histogramReindexLiteral_kInvalidIndex uint32 = math.MaxUint32
|
|
216
|
+
|
|
217
|
+
func histogramReindexLiteral(out []histogramLiteral, symbols []uint32, length uint) uint {
|
|
218
|
+
var new_index []uint32 = make([]uint32, length)
|
|
219
|
+
var next_index uint32
|
|
220
|
+
var tmp []histogramLiteral
|
|
221
|
+
var i uint
|
|
222
|
+
for i = 0; i < length; i++ {
|
|
223
|
+
new_index[i] = histogramReindexLiteral_kInvalidIndex
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
next_index = 0
|
|
227
|
+
for i = 0; i < length; i++ {
|
|
228
|
+
if new_index[symbols[i]] == histogramReindexLiteral_kInvalidIndex {
|
|
229
|
+
new_index[symbols[i]] = next_index
|
|
230
|
+
next_index++
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/* TODO: by using idea of "cycle-sort" we can avoid allocation of
|
|
235
|
+
tmp and reduce the number of copying by the factor of 2. */
|
|
236
|
+
tmp = make([]histogramLiteral, next_index)
|
|
237
|
+
|
|
238
|
+
next_index = 0
|
|
239
|
+
for i = 0; i < length; i++ {
|
|
240
|
+
if new_index[symbols[i]] == next_index {
|
|
241
|
+
tmp[next_index] = out[symbols[i]]
|
|
242
|
+
next_index++
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
symbols[i] = new_index[symbols[i]]
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
new_index = nil
|
|
249
|
+
for i = 0; uint32(i) < next_index; i++ {
|
|
250
|
+
out[i] = tmp[i]
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
tmp = nil
|
|
254
|
+
return uint(next_index)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
func clusterHistogramsLiteral(in []histogramLiteral, in_size uint, max_histograms uint, out []histogramLiteral, out_size *uint, histogram_symbols []uint32) {
|
|
258
|
+
var cluster_size []uint32 = make([]uint32, in_size)
|
|
259
|
+
var clusters []uint32 = make([]uint32, in_size)
|
|
260
|
+
var num_clusters uint = 0
|
|
261
|
+
var max_input_histograms uint = 64
|
|
262
|
+
var pairs_capacity uint = max_input_histograms * max_input_histograms / 2
|
|
263
|
+
var pairs []histogramPair = make([]histogramPair, (pairs_capacity + 1))
|
|
264
|
+
var i uint
|
|
265
|
+
|
|
266
|
+
/* For the first pass of clustering, we allow all pairs. */
|
|
267
|
+
for i = 0; i < in_size; i++ {
|
|
268
|
+
cluster_size[i] = 1
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
for i = 0; i < in_size; i++ {
|
|
272
|
+
out[i] = in[i]
|
|
273
|
+
out[i].bit_cost_ = populationCostLiteral(&in[i])
|
|
274
|
+
histogram_symbols[i] = uint32(i)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
for i = 0; i < in_size; i += max_input_histograms {
|
|
278
|
+
var num_to_combine uint = brotli_min_size_t(in_size-i, max_input_histograms)
|
|
279
|
+
var num_new_clusters uint
|
|
280
|
+
var j uint
|
|
281
|
+
for j = 0; j < num_to_combine; j++ {
|
|
282
|
+
clusters[num_clusters+j] = uint32(i + j)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
num_new_clusters = histogramCombineLiteral(out, cluster_size, histogram_symbols[i:], clusters[num_clusters:], pairs, num_to_combine, num_to_combine, max_histograms, pairs_capacity)
|
|
286
|
+
num_clusters += num_new_clusters
|
|
287
|
+
}
|
|
288
|
+
{
|
|
289
|
+
/* For the second pass, we limit the total number of histogram pairs.
|
|
290
|
+
After this limit is reached, we only keep searching for the best pair. */
|
|
291
|
+
var max_num_pairs uint = brotli_min_size_t(64*num_clusters, (num_clusters/2)*num_clusters)
|
|
292
|
+
if pairs_capacity < (max_num_pairs + 1) {
|
|
293
|
+
var _new_size uint
|
|
294
|
+
if pairs_capacity == 0 {
|
|
295
|
+
_new_size = max_num_pairs + 1
|
|
296
|
+
} else {
|
|
297
|
+
_new_size = pairs_capacity
|
|
298
|
+
}
|
|
299
|
+
var new_array []histogramPair
|
|
300
|
+
for _new_size < (max_num_pairs + 1) {
|
|
301
|
+
_new_size *= 2
|
|
302
|
+
}
|
|
303
|
+
new_array = make([]histogramPair, _new_size)
|
|
304
|
+
if pairs_capacity != 0 {
|
|
305
|
+
copy(new_array, pairs[:pairs_capacity])
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
pairs = new_array
|
|
309
|
+
pairs_capacity = _new_size
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/* Collapse similar histograms. */
|
|
313
|
+
num_clusters = histogramCombineLiteral(out, cluster_size, histogram_symbols, clusters, pairs, num_clusters, in_size, max_histograms, max_num_pairs)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
pairs = nil
|
|
317
|
+
cluster_size = nil
|
|
318
|
+
|
|
319
|
+
/* Find the optimal map from original histograms to the final ones. */
|
|
320
|
+
histogramRemapLiteral(in, in_size, clusters, num_clusters, out, histogram_symbols)
|
|
321
|
+
|
|
322
|
+
clusters = nil
|
|
323
|
+
|
|
324
|
+
/* Convert the context map to a canonical form. */
|
|
325
|
+
*out_size = histogramReindexLiteral(out, histogram_symbols, in_size)
|
|
326
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
package brotli
|
|
2
|
+
|
|
3
|
+
var kInsBase = []uint32{
|
|
4
|
+
0,
|
|
5
|
+
1,
|
|
6
|
+
2,
|
|
7
|
+
3,
|
|
8
|
+
4,
|
|
9
|
+
5,
|
|
10
|
+
6,
|
|
11
|
+
8,
|
|
12
|
+
10,
|
|
13
|
+
14,
|
|
14
|
+
18,
|
|
15
|
+
26,
|
|
16
|
+
34,
|
|
17
|
+
50,
|
|
18
|
+
66,
|
|
19
|
+
98,
|
|
20
|
+
130,
|
|
21
|
+
194,
|
|
22
|
+
322,
|
|
23
|
+
578,
|
|
24
|
+
1090,
|
|
25
|
+
2114,
|
|
26
|
+
6210,
|
|
27
|
+
22594,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var kInsExtra = []uint32{
|
|
31
|
+
0,
|
|
32
|
+
0,
|
|
33
|
+
0,
|
|
34
|
+
0,
|
|
35
|
+
0,
|
|
36
|
+
0,
|
|
37
|
+
1,
|
|
38
|
+
1,
|
|
39
|
+
2,
|
|
40
|
+
2,
|
|
41
|
+
3,
|
|
42
|
+
3,
|
|
43
|
+
4,
|
|
44
|
+
4,
|
|
45
|
+
5,
|
|
46
|
+
5,
|
|
47
|
+
6,
|
|
48
|
+
7,
|
|
49
|
+
8,
|
|
50
|
+
9,
|
|
51
|
+
10,
|
|
52
|
+
12,
|
|
53
|
+
14,
|
|
54
|
+
24,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
var kCopyBase = []uint32{
|
|
58
|
+
2,
|
|
59
|
+
3,
|
|
60
|
+
4,
|
|
61
|
+
5,
|
|
62
|
+
6,
|
|
63
|
+
7,
|
|
64
|
+
8,
|
|
65
|
+
9,
|
|
66
|
+
10,
|
|
67
|
+
12,
|
|
68
|
+
14,
|
|
69
|
+
18,
|
|
70
|
+
22,
|
|
71
|
+
30,
|
|
72
|
+
38,
|
|
73
|
+
54,
|
|
74
|
+
70,
|
|
75
|
+
102,
|
|
76
|
+
134,
|
|
77
|
+
198,
|
|
78
|
+
326,
|
|
79
|
+
582,
|
|
80
|
+
1094,
|
|
81
|
+
2118,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
var kCopyExtra = []uint32{
|
|
85
|
+
0,
|
|
86
|
+
0,
|
|
87
|
+
0,
|
|
88
|
+
0,
|
|
89
|
+
0,
|
|
90
|
+
0,
|
|
91
|
+
0,
|
|
92
|
+
0,
|
|
93
|
+
1,
|
|
94
|
+
1,
|
|
95
|
+
2,
|
|
96
|
+
2,
|
|
97
|
+
3,
|
|
98
|
+
3,
|
|
99
|
+
4,
|
|
100
|
+
4,
|
|
101
|
+
5,
|
|
102
|
+
5,
|
|
103
|
+
6,
|
|
104
|
+
7,
|
|
105
|
+
8,
|
|
106
|
+
9,
|
|
107
|
+
10,
|
|
108
|
+
24,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
func getInsertLengthCode(insertlen uint) uint16 {
|
|
112
|
+
if insertlen < 6 {
|
|
113
|
+
return uint16(insertlen)
|
|
114
|
+
} else if insertlen < 130 {
|
|
115
|
+
var nbits uint32 = log2FloorNonZero(insertlen-2) - 1
|
|
116
|
+
return uint16((nbits << 1) + uint32((insertlen-2)>>nbits) + 2)
|
|
117
|
+
} else if insertlen < 2114 {
|
|
118
|
+
return uint16(log2FloorNonZero(insertlen-66) + 10)
|
|
119
|
+
} else if insertlen < 6210 {
|
|
120
|
+
return 21
|
|
121
|
+
} else if insertlen < 22594 {
|
|
122
|
+
return 22
|
|
123
|
+
} else {
|
|
124
|
+
return 23
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
func getCopyLengthCode(copylen uint) uint16 {
|
|
129
|
+
if copylen < 10 {
|
|
130
|
+
return uint16(copylen - 2)
|
|
131
|
+
} else if copylen < 134 {
|
|
132
|
+
var nbits uint32 = log2FloorNonZero(copylen-6) - 1
|
|
133
|
+
return uint16((nbits << 1) + uint32((copylen-6)>>nbits) + 4)
|
|
134
|
+
} else if copylen < 2118 {
|
|
135
|
+
return uint16(log2FloorNonZero(copylen-70) + 12)
|
|
136
|
+
} else {
|
|
137
|
+
return 23
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
func combineLengthCodes(inscode uint16, copycode uint16, use_last_distance bool) uint16 {
|
|
142
|
+
var bits64 uint16 = uint16(copycode&0x7 | (inscode&0x7)<<3)
|
|
143
|
+
if use_last_distance && inscode < 8 && copycode < 16 {
|
|
144
|
+
if copycode < 8 {
|
|
145
|
+
return bits64
|
|
146
|
+
} else {
|
|
147
|
+
return bits64 | 64
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
/* Specification: 5 Encoding of ... (last table) */
|
|
151
|
+
/* offset = 2 * index, where index is in range [0..8] */
|
|
152
|
+
var offset uint32 = 2 * ((uint32(copycode) >> 3) + 3*(uint32(inscode)>>3))
|
|
153
|
+
|
|
154
|
+
/* All values in specification are K * 64,
|
|
155
|
+
where K = [2, 3, 6, 4, 5, 8, 7, 9, 10],
|
|
156
|
+
i + 1 = [1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
157
|
+
K - i - 1 = [1, 1, 3, 0, 0, 2, 0, 1, 2] = D.
|
|
158
|
+
All values in D require only 2 bits to encode.
|
|
159
|
+
Magic constant is shifted 6 bits left, to avoid final multiplication. */
|
|
160
|
+
offset = (offset << 5) + 0x40 + ((0x520D40 >> offset) & 0xC0)
|
|
161
|
+
|
|
162
|
+
return uint16(offset | uint32(bits64))
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
func getLengthCode(insertlen uint, copylen uint, use_last_distance bool, code *uint16) {
|
|
167
|
+
var inscode uint16 = getInsertLengthCode(insertlen)
|
|
168
|
+
var copycode uint16 = getCopyLengthCode(copylen)
|
|
169
|
+
*code = combineLengthCodes(inscode, copycode, use_last_distance)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
func getInsertBase(inscode uint16) uint32 {
|
|
173
|
+
return kInsBase[inscode]
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
func getInsertExtra(inscode uint16) uint32 {
|
|
177
|
+
return kInsExtra[inscode]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
func getCopyBase(copycode uint16) uint32 {
|
|
181
|
+
return kCopyBase[copycode]
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
func getCopyExtra(copycode uint16) uint32 {
|
|
185
|
+
return kCopyExtra[copycode]
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
type command struct {
|
|
189
|
+
insert_len_ uint32
|
|
190
|
+
copy_len_ uint32
|
|
191
|
+
dist_extra_ uint32
|
|
192
|
+
cmd_prefix_ uint16
|
|
193
|
+
dist_prefix_ uint16
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */
|
|
197
|
+
func makeCommand(dist *distanceParams, insertlen uint, copylen uint, copylen_code_delta int, distance_code uint) (cmd command) {
|
|
198
|
+
/* Don't rely on signed int representation, use honest casts. */
|
|
199
|
+
var delta uint32 = uint32(byte(int8(copylen_code_delta)))
|
|
200
|
+
cmd.insert_len_ = uint32(insertlen)
|
|
201
|
+
cmd.copy_len_ = uint32(uint32(copylen) | delta<<25)
|
|
202
|
+
|
|
203
|
+
/* The distance prefix and extra bits are stored in this Command as if
|
|
204
|
+
npostfix and ndirect were 0, they are only recomputed later after the
|
|
205
|
+
clustering if needed. */
|
|
206
|
+
prefixEncodeCopyDistance(distance_code, uint(dist.num_direct_distance_codes), uint(dist.distance_postfix_bits), &cmd.dist_prefix_, &cmd.dist_extra_)
|
|
207
|
+
getLengthCode(insertlen, uint(int(copylen)+copylen_code_delta), (cmd.dist_prefix_&0x3FF == 0), &cmd.cmd_prefix_)
|
|
208
|
+
|
|
209
|
+
return cmd
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
func makeInsertCommand(insertlen uint) (cmd command) {
|
|
213
|
+
cmd.insert_len_ = uint32(insertlen)
|
|
214
|
+
cmd.copy_len_ = 4 << 25
|
|
215
|
+
cmd.dist_extra_ = 0
|
|
216
|
+
cmd.dist_prefix_ = numDistanceShortCodes
|
|
217
|
+
getLengthCode(insertlen, 4, false, &cmd.cmd_prefix_)
|
|
218
|
+
return cmd
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
func commandRestoreDistanceCode(self *command, dist *distanceParams) uint32 {
|
|
222
|
+
if uint32(self.dist_prefix_&0x3FF) < numDistanceShortCodes+dist.num_direct_distance_codes {
|
|
223
|
+
return uint32(self.dist_prefix_) & 0x3FF
|
|
224
|
+
} else {
|
|
225
|
+
var dcode uint32 = uint32(self.dist_prefix_) & 0x3FF
|
|
226
|
+
var nbits uint32 = uint32(self.dist_prefix_) >> 10
|
|
227
|
+
var extra uint32 = self.dist_extra_
|
|
228
|
+
var postfix_mask uint32 = (1 << dist.distance_postfix_bits) - 1
|
|
229
|
+
var hcode uint32 = (dcode - dist.num_direct_distance_codes - numDistanceShortCodes) >> dist.distance_postfix_bits
|
|
230
|
+
var lcode uint32 = (dcode - dist.num_direct_distance_codes - numDistanceShortCodes) & postfix_mask
|
|
231
|
+
var offset uint32 = ((2 + (hcode & 1)) << nbits) - 4
|
|
232
|
+
return ((offset + extra) << dist.distance_postfix_bits) + lcode + dist.num_direct_distance_codes + numDistanceShortCodes
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
func commandDistanceContext(self *command) uint32 {
|
|
237
|
+
var r uint32 = uint32(self.cmd_prefix_) >> 6
|
|
238
|
+
var c uint32 = uint32(self.cmd_prefix_) & 7
|
|
239
|
+
if (r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2) {
|
|
240
|
+
return c
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return 3
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
func commandCopyLen(self *command) uint32 {
|
|
247
|
+
return self.copy_len_ & 0x1FFFFFF
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
func commandCopyLenCode(self *command) uint32 {
|
|
251
|
+
var modifier uint32 = self.copy_len_ >> 25
|
|
252
|
+
var delta int32 = int32(int8(byte(modifier | (modifier&0x40)<<1)))
|
|
253
|
+
return uint32(int32(self.copy_len_&0x1FFFFFF) + delta)
|
|
254
|
+
}
|