typenative 0.3.1 → 0.4.1

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.
Files changed (170) hide show
  1. package/.github/workflows/ci.yml +2 -0
  2. package/CHANGELOG.md +258 -0
  3. package/CONTRIBUTION.md +1 -1
  4. package/README.md +17 -1
  5. package/bin/cli.js +26 -11
  6. package/bin/go/bridge.js +74 -14
  7. package/bin/go/convert.js +59 -1
  8. package/bin/go/metadata.js +13 -4
  9. package/bin/go/overrides.js +4 -6
  10. package/bin/go/stdlib.generated.js +2 -0
  11. package/bin/index.js +338 -47
  12. package/bin/main.js +36 -14
  13. package/bin/node/assert.js +2 -2
  14. package/bin/node/buffer.js +9 -0
  15. package/bin/node/child_process.js +51 -3
  16. package/bin/node/cluster.js +41 -3
  17. package/bin/node/core.js +1 -1
  18. package/bin/node/crypto.js +205 -4
  19. package/bin/node/dgram.js +230 -6
  20. package/bin/node/dns.js +471 -8
  21. package/bin/node/errno_tables.js +445 -0
  22. package/bin/node/events.js +1 -1
  23. package/bin/node/fs.js +22 -15
  24. package/bin/node/fs_watch.js +407 -0
  25. package/bin/node/http.js +128 -30
  26. package/bin/node/http2.js +37 -7
  27. package/bin/node/module_v8.js +142 -0
  28. package/bin/node/net.js +9 -6
  29. package/bin/node/process.js +242 -0
  30. package/bin/node/readline.js +272 -1
  31. package/bin/node/registry.js +3 -1
  32. package/bin/node/sqlite.js +60 -12
  33. package/bin/node/stream.js +145 -10
  34. package/bin/node/test.js +311 -19
  35. package/bin/node/timers.js +1 -1
  36. package/bin/node/typed_arrays.js +608 -0
  37. package/bin/node/url.js +23 -5
  38. package/bin/node/util.js +21 -1
  39. package/bin/node/util_extra.js +6 -30
  40. package/bin/node/worker_threads.js +79 -19
  41. package/bin/node/zlib.js +131 -16
  42. package/bin/transpiler.js +4729 -616
  43. package/bin/typecheck-node.js +1 -0
  44. package/bin/version.js +1 -1
  45. package/conformance.md +1030 -877
  46. package/examples/README.md +54 -0
  47. package/examples/fibonacci/README.md +89 -0
  48. package/examples/fibonacci/bench.mjs +87 -0
  49. package/examples/fibonacci/main.ts +21 -0
  50. package/examples/hello-world/README.md +62 -0
  51. package/examples/hello-world/bench.mjs +66 -0
  52. package/examples/hello-world/main.ts +7 -0
  53. package/examples/json-server/README.md +127 -0
  54. package/examples/json-server/bench.mjs +263 -0
  55. package/examples/json-server/main.ts +67 -0
  56. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  57. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  58. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  59. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  60. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  61. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  62. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  63. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  64. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  65. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  66. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  67. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  68. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  69. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  70. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  71. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  72. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  73. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  74. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  75. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  76. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  77. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  78. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  79. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  80. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  81. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  82. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  83. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  84. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  85. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  86. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  87. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  88. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  89. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  90. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  91. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  92. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  93. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  94. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  95. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  96. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  97. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  98. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  99. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  100. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  101. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  102. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  103. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  104. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  105. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  106. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  107. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  108. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  109. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  110. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  111. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  112. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  113. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  114. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  115. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  116. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  117. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  118. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  119. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  120. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  121. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  122. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  123. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  124. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  125. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  126. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  127. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  128. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  129. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  130. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  131. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  132. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  133. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  134. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  135. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  136. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  137. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  138. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  139. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  140. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  141. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  142. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  143. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  144. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  145. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  146. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  147. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  148. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  149. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  150. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  151. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  152. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  153. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  154. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  155. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  156. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  157. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  158. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  159. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  160. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  161. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  162. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  163. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  164. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  165. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  166. package/govendor/manifest.txt +3 -1
  167. package/package.json +1 -1
  168. package/types/typenative-go.d.ts +23 -0
  169. package/types/typenative-npm.d.ts +241 -5
  170. package/types/typenative.d.ts +302 -12
@@ -0,0 +1,56 @@
1
+ package brotli
2
+
3
+ /* Copyright 2010 Google Inc. All Rights Reserved.
4
+
5
+ Distributed under MIT license.
6
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ /* Write bits into a byte array. */
10
+
11
+ type bitWriter struct {
12
+ dst []byte
13
+
14
+ // Data waiting to be written is the low nbits of bits.
15
+ bits uint64
16
+ nbits uint
17
+ }
18
+
19
+ func (w *bitWriter) writeBits(nb uint, b uint64) {
20
+ w.bits |= b << w.nbits
21
+ w.nbits += nb
22
+ if w.nbits >= 32 {
23
+ bits := w.bits
24
+ w.bits >>= 32
25
+ w.nbits -= 32
26
+ w.dst = append(w.dst,
27
+ byte(bits),
28
+ byte(bits>>8),
29
+ byte(bits>>16),
30
+ byte(bits>>24),
31
+ )
32
+ }
33
+ }
34
+
35
+ func (w *bitWriter) writeSingleBit(bit bool) {
36
+ if bit {
37
+ w.writeBits(1, 1)
38
+ } else {
39
+ w.writeBits(1, 0)
40
+ }
41
+ }
42
+
43
+ func (w *bitWriter) jumpToByteBoundary() {
44
+ dst := w.dst
45
+ for w.nbits != 0 {
46
+ dst = append(dst, byte(w.bits))
47
+ w.bits >>= 8
48
+ if w.nbits > 8 { // Avoid underflow
49
+ w.nbits -= 8
50
+ } else {
51
+ w.nbits = 0
52
+ }
53
+ }
54
+ w.bits = 0
55
+ w.dst = dst
56
+ }
@@ -0,0 +1,144 @@
1
+ package brotli
2
+
3
+ /* Copyright 2013 Google Inc. All Rights Reserved.
4
+
5
+ Distributed under MIT license.
6
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ /* Block split point selection utilities. */
10
+
11
+ type blockSplit struct {
12
+ num_types uint
13
+ num_blocks uint
14
+ types []byte
15
+ lengths []uint32
16
+ types_alloc_size uint
17
+ lengths_alloc_size uint
18
+ }
19
+
20
+ const (
21
+ kMaxLiteralHistograms uint = 100
22
+ kMaxCommandHistograms uint = 50
23
+ kLiteralBlockSwitchCost float64 = 28.1
24
+ kCommandBlockSwitchCost float64 = 13.5
25
+ kDistanceBlockSwitchCost float64 = 14.6
26
+ kLiteralStrideLength uint = 70
27
+ kCommandStrideLength uint = 40
28
+ kSymbolsPerLiteralHistogram uint = 544
29
+ kSymbolsPerCommandHistogram uint = 530
30
+ kSymbolsPerDistanceHistogram uint = 544
31
+ kMinLengthForBlockSplitting uint = 128
32
+ kIterMulForRefining uint = 2
33
+ kMinItersForRefining uint = 100
34
+ )
35
+
36
+ func countLiterals(cmds []command) uint {
37
+ var total_length uint = 0
38
+ /* Count how many we have. */
39
+
40
+ for i := range cmds {
41
+ total_length += uint(cmds[i].insert_len_)
42
+ }
43
+
44
+ return total_length
45
+ }
46
+
47
+ func copyLiteralsToByteArray(cmds []command, data []byte, offset uint, mask uint, literals []byte) {
48
+ var pos uint = 0
49
+ var from_pos uint = offset & mask
50
+ for i := range cmds {
51
+ var insert_len uint = uint(cmds[i].insert_len_)
52
+ if from_pos+insert_len > mask {
53
+ var head_size uint = mask + 1 - from_pos
54
+ copy(literals[pos:], data[from_pos:][:head_size])
55
+ from_pos = 0
56
+ pos += head_size
57
+ insert_len -= head_size
58
+ }
59
+
60
+ if insert_len > 0 {
61
+ copy(literals[pos:], data[from_pos:][:insert_len])
62
+ pos += insert_len
63
+ }
64
+
65
+ from_pos = uint((uint32(from_pos+insert_len) + commandCopyLen(&cmds[i])) & uint32(mask))
66
+ }
67
+ }
68
+
69
+ func myRand(seed *uint32) uint32 {
70
+ /* Initial seed should be 7. In this case, loop length is (1 << 29). */
71
+ *seed *= 16807
72
+
73
+ return *seed
74
+ }
75
+
76
+ func bitCost(count uint) float64 {
77
+ if count == 0 {
78
+ return -2.0
79
+ } else {
80
+ return fastLog2(count)
81
+ }
82
+ }
83
+
84
+ const histogramsPerBatch = 64
85
+
86
+ const clustersPerBatch = 16
87
+
88
+ func initBlockSplit(self *blockSplit) {
89
+ self.num_types = 0
90
+ self.num_blocks = 0
91
+ self.types = self.types[:0]
92
+ self.lengths = self.lengths[:0]
93
+ self.types_alloc_size = 0
94
+ self.lengths_alloc_size = 0
95
+ }
96
+
97
+ func splitBlock(cmds []command, data []byte, pos uint, mask uint, params *encoderParams, literal_split *blockSplit, insert_and_copy_split *blockSplit, dist_split *blockSplit) {
98
+ {
99
+ var literals_count uint = countLiterals(cmds)
100
+ var literals []byte = make([]byte, literals_count)
101
+
102
+ /* Create a continuous array of literals. */
103
+ copyLiteralsToByteArray(cmds, data, pos, mask, literals)
104
+
105
+ /* Create the block split on the array of literals.
106
+ Literal histograms have alphabet size 256. */
107
+ splitByteVectorLiteral(literals, literals_count, kSymbolsPerLiteralHistogram, kMaxLiteralHistograms, kLiteralStrideLength, kLiteralBlockSwitchCost, params, literal_split)
108
+
109
+ literals = nil
110
+ }
111
+ {
112
+ var insert_and_copy_codes []uint16 = make([]uint16, len(cmds))
113
+ /* Compute prefix codes for commands. */
114
+
115
+ for i := range cmds {
116
+ insert_and_copy_codes[i] = cmds[i].cmd_prefix_
117
+ }
118
+
119
+ /* Create the block split on the array of command prefixes. */
120
+ splitByteVectorCommand(insert_and_copy_codes, kSymbolsPerCommandHistogram, kMaxCommandHistograms, kCommandStrideLength, kCommandBlockSwitchCost, params, insert_and_copy_split)
121
+
122
+ /* TODO: reuse for distances? */
123
+
124
+ insert_and_copy_codes = nil
125
+ }
126
+ {
127
+ var distance_prefixes []uint16 = make([]uint16, len(cmds))
128
+ var j uint = 0
129
+ /* Create a continuous array of distance prefixes. */
130
+
131
+ for i := range cmds {
132
+ var cmd *command = &cmds[i]
133
+ if commandCopyLen(cmd) != 0 && cmd.cmd_prefix_ >= 128 {
134
+ distance_prefixes[j] = cmd.dist_prefix_ & 0x3FF
135
+ j++
136
+ }
137
+ }
138
+
139
+ /* Create the block split on the array of distance prefixes. */
140
+ splitByteVectorDistance(distance_prefixes, j, kSymbolsPerDistanceHistogram, kMaxCommandHistograms, kCommandStrideLength, kDistanceBlockSwitchCost, params, dist_split)
141
+
142
+ distance_prefixes = nil
143
+ }
144
+ }
@@ -0,0 +1,434 @@
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
+ func initialEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []histogramCommand) {
12
+ var seed uint32 = 7
13
+ var block_length uint = length / num_histograms
14
+ var i uint
15
+ clearHistogramsCommand(histograms, num_histograms)
16
+ for i = 0; i < num_histograms; i++ {
17
+ var pos uint = length * i / num_histograms
18
+ if i != 0 {
19
+ pos += uint(myRand(&seed) % uint32(block_length))
20
+ }
21
+
22
+ if pos+stride >= length {
23
+ pos = length - stride - 1
24
+ }
25
+
26
+ histogramAddVectorCommand(&histograms[i], data[pos:], stride)
27
+ }
28
+ }
29
+
30
+ func randomSampleCommand(seed *uint32, data []uint16, length uint, stride uint, sample *histogramCommand) {
31
+ var pos uint = 0
32
+ if stride >= length {
33
+ stride = length
34
+ } else {
35
+ pos = uint(myRand(seed) % uint32(length-stride+1))
36
+ }
37
+
38
+ histogramAddVectorCommand(sample, data[pos:], stride)
39
+ }
40
+
41
+ func refineEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []histogramCommand) {
42
+ var iters uint = kIterMulForRefining*length/stride + kMinItersForRefining
43
+ var seed uint32 = 7
44
+ var iter uint
45
+ iters = ((iters + num_histograms - 1) / num_histograms) * num_histograms
46
+ for iter = 0; iter < iters; iter++ {
47
+ var sample histogramCommand
48
+ histogramClearCommand(&sample)
49
+ randomSampleCommand(&seed, data, length, stride, &sample)
50
+ histogramAddHistogramCommand(&histograms[iter%num_histograms], &sample)
51
+ }
52
+ }
53
+
54
+ /* Assigns a block id from the range [0, num_histograms) to each data element
55
+ in data[0..length) and fills in block_id[0..length) with the assigned values.
56
+ Returns the number of blocks, i.e. one plus the number of block switches. */
57
+ func findBlocksCommand(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []histogramCommand, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint {
58
+ var data_size uint = histogramDataSizeCommand()
59
+ var bitmaplen uint = (num_histograms + 7) >> 3
60
+ var num_blocks uint = 1
61
+ var i uint
62
+ var j uint
63
+ assert(num_histograms <= 256)
64
+ if num_histograms <= 1 {
65
+ for i = 0; i < length; i++ {
66
+ block_id[i] = 0
67
+ }
68
+
69
+ return 1
70
+ }
71
+
72
+ for i := 0; i < int(data_size*num_histograms); i++ {
73
+ insert_cost[i] = 0
74
+ }
75
+ for i = 0; i < num_histograms; i++ {
76
+ insert_cost[i] = fastLog2(uint(uint32(histograms[i].total_count_)))
77
+ }
78
+
79
+ for i = data_size; i != 0; {
80
+ i--
81
+ for j = 0; j < num_histograms; j++ {
82
+ insert_cost[i*num_histograms+j] = insert_cost[j] - bitCost(uint(histograms[j].data_[i]))
83
+ }
84
+ }
85
+
86
+ for i := 0; i < int(num_histograms); i++ {
87
+ cost[i] = 0
88
+ }
89
+ for i := 0; i < int(length*bitmaplen); i++ {
90
+ switch_signal[i] = 0
91
+ }
92
+
93
+ /* After each iteration of this loop, cost[k] will contain the difference
94
+ between the minimum cost of arriving at the current byte position using
95
+ entropy code k, and the minimum cost of arriving at the current byte
96
+ position. This difference is capped at the block switch cost, and if it
97
+ reaches block switch cost, it means that when we trace back from the last
98
+ position, we need to switch here. */
99
+ for i = 0; i < length; i++ {
100
+ var byte_ix uint = i
101
+ var ix uint = byte_ix * bitmaplen
102
+ var insert_cost_ix uint = uint(data[byte_ix]) * num_histograms
103
+ var min_cost float64 = 1e99
104
+ var block_switch_cost float64 = block_switch_bitcost
105
+ var k uint
106
+ for k = 0; k < num_histograms; k++ {
107
+ /* We are coding the symbol in data[byte_ix] with entropy code k. */
108
+ cost[k] += insert_cost[insert_cost_ix+k]
109
+
110
+ if cost[k] < min_cost {
111
+ min_cost = cost[k]
112
+ block_id[byte_ix] = byte(k)
113
+ }
114
+ }
115
+
116
+ /* More blocks for the beginning. */
117
+ if byte_ix < 2000 {
118
+ block_switch_cost *= 0.77 + 0.07*float64(byte_ix)/2000
119
+ }
120
+
121
+ for k = 0; k < num_histograms; k++ {
122
+ cost[k] -= min_cost
123
+ if cost[k] >= block_switch_cost {
124
+ var mask byte = byte(1 << (k & 7))
125
+ cost[k] = block_switch_cost
126
+ assert(k>>3 < bitmaplen)
127
+ switch_signal[ix+(k>>3)] |= mask
128
+ /* Trace back from the last position and switch at the marked places. */
129
+ }
130
+ }
131
+ }
132
+ {
133
+ var byte_ix uint = length - 1
134
+ var ix uint = byte_ix * bitmaplen
135
+ var cur_id byte = block_id[byte_ix]
136
+ for byte_ix > 0 {
137
+ var mask byte = byte(1 << (cur_id & 7))
138
+ assert(uint(cur_id)>>3 < bitmaplen)
139
+ byte_ix--
140
+ ix -= bitmaplen
141
+ if switch_signal[ix+uint(cur_id>>3)]&mask != 0 {
142
+ if cur_id != block_id[byte_ix] {
143
+ cur_id = block_id[byte_ix]
144
+ num_blocks++
145
+ }
146
+ }
147
+
148
+ block_id[byte_ix] = cur_id
149
+ }
150
+ }
151
+
152
+ return num_blocks
153
+ }
154
+
155
+ var remapBlockIdsCommand_kInvalidId uint16 = 256
156
+
157
+ func remapBlockIdsCommand(block_ids []byte, length uint, new_id []uint16, num_histograms uint) uint {
158
+ var next_id uint16 = 0
159
+ var i uint
160
+ for i = 0; i < num_histograms; i++ {
161
+ new_id[i] = remapBlockIdsCommand_kInvalidId
162
+ }
163
+
164
+ for i = 0; i < length; i++ {
165
+ assert(uint(block_ids[i]) < num_histograms)
166
+ if new_id[block_ids[i]] == remapBlockIdsCommand_kInvalidId {
167
+ new_id[block_ids[i]] = next_id
168
+ next_id++
169
+ }
170
+ }
171
+
172
+ for i = 0; i < length; i++ {
173
+ block_ids[i] = byte(new_id[block_ids[i]])
174
+ assert(uint(block_ids[i]) < num_histograms)
175
+ }
176
+
177
+ assert(uint(next_id) <= num_histograms)
178
+ return uint(next_id)
179
+ }
180
+
181
+ func buildBlockHistogramsCommand(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []histogramCommand) {
182
+ var i uint
183
+ clearHistogramsCommand(histograms, num_histograms)
184
+ for i = 0; i < length; i++ {
185
+ histogramAddCommand(&histograms[block_ids[i]], uint(data[i]))
186
+ }
187
+ }
188
+
189
+ var clusterBlocksCommand_kInvalidIndex uint32 = math.MaxUint32
190
+
191
+ func clusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids []byte, split *blockSplit) {
192
+ var histogram_symbols []uint32 = make([]uint32, num_blocks)
193
+ var block_lengths []uint32 = make([]uint32, num_blocks)
194
+ var expected_num_clusters uint = clustersPerBatch * (num_blocks + histogramsPerBatch - 1) / histogramsPerBatch
195
+ var all_histograms_size uint = 0
196
+ var all_histograms_capacity uint = expected_num_clusters
197
+ var all_histograms []histogramCommand = make([]histogramCommand, all_histograms_capacity)
198
+ var cluster_size_size uint = 0
199
+ var cluster_size_capacity uint = expected_num_clusters
200
+ var cluster_size []uint32 = make([]uint32, cluster_size_capacity)
201
+ var num_clusters uint = 0
202
+ var histograms []histogramCommand = make([]histogramCommand, brotli_min_size_t(num_blocks, histogramsPerBatch))
203
+ var max_num_pairs uint = histogramsPerBatch * histogramsPerBatch / 2
204
+ var pairs_capacity uint = max_num_pairs + 1
205
+ var pairs []histogramPair = make([]histogramPair, pairs_capacity)
206
+ var pos uint = 0
207
+ var clusters []uint32
208
+ var num_final_clusters uint
209
+ var new_index []uint32
210
+ var i uint
211
+ var sizes = [histogramsPerBatch]uint32{0}
212
+ var new_clusters = [histogramsPerBatch]uint32{0}
213
+ var symbols = [histogramsPerBatch]uint32{0}
214
+ var remap = [histogramsPerBatch]uint32{0}
215
+
216
+ for i := 0; i < int(num_blocks); i++ {
217
+ block_lengths[i] = 0
218
+ }
219
+ {
220
+ var block_idx uint = 0
221
+ for i = 0; i < length; i++ {
222
+ assert(block_idx < num_blocks)
223
+ block_lengths[block_idx]++
224
+ if i+1 == length || block_ids[i] != block_ids[i+1] {
225
+ block_idx++
226
+ }
227
+ }
228
+
229
+ assert(block_idx == num_blocks)
230
+ }
231
+
232
+ for i = 0; i < num_blocks; i += histogramsPerBatch {
233
+ var num_to_combine uint = brotli_min_size_t(num_blocks-i, histogramsPerBatch)
234
+ var num_new_clusters uint
235
+ var j uint
236
+ for j = 0; j < num_to_combine; j++ {
237
+ var k uint
238
+ histogramClearCommand(&histograms[j])
239
+ for k = 0; uint32(k) < block_lengths[i+j]; k++ {
240
+ histogramAddCommand(&histograms[j], uint(data[pos]))
241
+ pos++
242
+ }
243
+
244
+ histograms[j].bit_cost_ = populationCostCommand(&histograms[j])
245
+ new_clusters[j] = uint32(j)
246
+ symbols[j] = uint32(j)
247
+ sizes[j] = 1
248
+ }
249
+
250
+ num_new_clusters = histogramCombineCommand(histograms, sizes[:], symbols[:], new_clusters[:], []histogramPair(pairs), num_to_combine, num_to_combine, histogramsPerBatch, max_num_pairs)
251
+ if all_histograms_capacity < (all_histograms_size + num_new_clusters) {
252
+ var _new_size uint
253
+ if all_histograms_capacity == 0 {
254
+ _new_size = all_histograms_size + num_new_clusters
255
+ } else {
256
+ _new_size = all_histograms_capacity
257
+ }
258
+ var new_array []histogramCommand
259
+ for _new_size < (all_histograms_size + num_new_clusters) {
260
+ _new_size *= 2
261
+ }
262
+ new_array = make([]histogramCommand, _new_size)
263
+ if all_histograms_capacity != 0 {
264
+ copy(new_array, all_histograms[:all_histograms_capacity])
265
+ }
266
+
267
+ all_histograms = new_array
268
+ all_histograms_capacity = _new_size
269
+ }
270
+
271
+ brotli_ensure_capacity_uint32_t(&cluster_size, &cluster_size_capacity, cluster_size_size+num_new_clusters)
272
+ for j = 0; j < num_new_clusters; j++ {
273
+ all_histograms[all_histograms_size] = histograms[new_clusters[j]]
274
+ all_histograms_size++
275
+ cluster_size[cluster_size_size] = sizes[new_clusters[j]]
276
+ cluster_size_size++
277
+ remap[new_clusters[j]] = uint32(j)
278
+ }
279
+
280
+ for j = 0; j < num_to_combine; j++ {
281
+ histogram_symbols[i+j] = uint32(num_clusters) + remap[symbols[j]]
282
+ }
283
+
284
+ num_clusters += num_new_clusters
285
+ assert(num_clusters == cluster_size_size)
286
+ assert(num_clusters == all_histograms_size)
287
+ }
288
+
289
+ histograms = nil
290
+
291
+ max_num_pairs = brotli_min_size_t(64*num_clusters, (num_clusters/2)*num_clusters)
292
+ if pairs_capacity < max_num_pairs+1 {
293
+ pairs = nil
294
+ pairs = make([]histogramPair, (max_num_pairs + 1))
295
+ }
296
+
297
+ clusters = make([]uint32, num_clusters)
298
+ for i = 0; i < num_clusters; i++ {
299
+ clusters[i] = uint32(i)
300
+ }
301
+
302
+ num_final_clusters = histogramCombineCommand(all_histograms, cluster_size, histogram_symbols, clusters, pairs, num_clusters, num_blocks, maxNumberOfBlockTypes, max_num_pairs)
303
+ pairs = nil
304
+ cluster_size = nil
305
+
306
+ new_index = make([]uint32, num_clusters)
307
+ for i = 0; i < num_clusters; i++ {
308
+ new_index[i] = clusterBlocksCommand_kInvalidIndex
309
+ }
310
+ pos = 0
311
+ {
312
+ var next_index uint32 = 0
313
+ for i = 0; i < num_blocks; i++ {
314
+ var histo histogramCommand
315
+ var j uint
316
+ var best_out uint32
317
+ var best_bits float64
318
+ histogramClearCommand(&histo)
319
+ for j = 0; uint32(j) < block_lengths[i]; j++ {
320
+ histogramAddCommand(&histo, uint(data[pos]))
321
+ pos++
322
+ }
323
+
324
+ if i == 0 {
325
+ best_out = histogram_symbols[0]
326
+ } else {
327
+ best_out = histogram_symbols[i-1]
328
+ }
329
+ best_bits = histogramBitCostDistanceCommand(&histo, &all_histograms[best_out])
330
+ for j = 0; j < num_final_clusters; j++ {
331
+ var cur_bits float64 = histogramBitCostDistanceCommand(&histo, &all_histograms[clusters[j]])
332
+ if cur_bits < best_bits {
333
+ best_bits = cur_bits
334
+ best_out = clusters[j]
335
+ }
336
+ }
337
+
338
+ histogram_symbols[i] = best_out
339
+ if new_index[best_out] == clusterBlocksCommand_kInvalidIndex {
340
+ new_index[best_out] = next_index
341
+ next_index++
342
+ }
343
+ }
344
+ }
345
+
346
+ clusters = nil
347
+ all_histograms = nil
348
+ brotli_ensure_capacity_uint8_t(&split.types, &split.types_alloc_size, num_blocks)
349
+ brotli_ensure_capacity_uint32_t(&split.lengths, &split.lengths_alloc_size, num_blocks)
350
+ {
351
+ var cur_length uint32 = 0
352
+ var block_idx uint = 0
353
+ var max_type byte = 0
354
+ for i = 0; i < num_blocks; i++ {
355
+ cur_length += block_lengths[i]
356
+ if i+1 == num_blocks || histogram_symbols[i] != histogram_symbols[i+1] {
357
+ var id byte = byte(new_index[histogram_symbols[i]])
358
+ split.types[block_idx] = id
359
+ split.lengths[block_idx] = cur_length
360
+ max_type = brotli_max_uint8_t(max_type, id)
361
+ cur_length = 0
362
+ block_idx++
363
+ }
364
+ }
365
+
366
+ split.num_blocks = block_idx
367
+ split.num_types = uint(max_type) + 1
368
+ }
369
+
370
+ new_index = nil
371
+ block_lengths = nil
372
+ histogram_symbols = nil
373
+ }
374
+
375
+ func splitByteVectorCommand(data []uint16, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *encoderParams, split *blockSplit) {
376
+ length := uint(len(data))
377
+ var data_size uint = histogramDataSizeCommand()
378
+ var num_histograms uint = length/literals_per_histogram + 1
379
+ var histograms []histogramCommand
380
+ if num_histograms > max_histograms {
381
+ num_histograms = max_histograms
382
+ }
383
+
384
+ if length == 0 {
385
+ split.num_types = 1
386
+ return
387
+ } else if length < kMinLengthForBlockSplitting {
388
+ brotli_ensure_capacity_uint8_t(&split.types, &split.types_alloc_size, split.num_blocks+1)
389
+ brotli_ensure_capacity_uint32_t(&split.lengths, &split.lengths_alloc_size, split.num_blocks+1)
390
+ split.num_types = 1
391
+ split.types[split.num_blocks] = 0
392
+ split.lengths[split.num_blocks] = uint32(length)
393
+ split.num_blocks++
394
+ return
395
+ }
396
+
397
+ histograms = make([]histogramCommand, num_histograms)
398
+
399
+ /* Find good entropy codes. */
400
+ initialEntropyCodesCommand(data, length, sampling_stride_length, num_histograms, histograms)
401
+
402
+ refineEntropyCodesCommand(data, length, sampling_stride_length, num_histograms, histograms)
403
+ {
404
+ var block_ids []byte = make([]byte, length)
405
+ var num_blocks uint = 0
406
+ var bitmaplen uint = (num_histograms + 7) >> 3
407
+ var insert_cost []float64 = make([]float64, (data_size * num_histograms))
408
+ var cost []float64 = make([]float64, num_histograms)
409
+ var switch_signal []byte = make([]byte, (length * bitmaplen))
410
+ var new_id []uint16 = make([]uint16, num_histograms)
411
+ var iters uint
412
+ if params.quality < hqZopflificationQuality {
413
+ iters = 3
414
+ } else {
415
+ iters = 10
416
+ }
417
+ /* Find a good path through literals with the good entropy codes. */
418
+
419
+ var i uint
420
+ for i = 0; i < iters; i++ {
421
+ num_blocks = findBlocksCommand(data, length, block_switch_cost, num_histograms, histograms, insert_cost, cost, switch_signal, block_ids)
422
+ num_histograms = remapBlockIdsCommand(block_ids, length, new_id, num_histograms)
423
+ buildBlockHistogramsCommand(data, length, block_ids, num_histograms, histograms)
424
+ }
425
+
426
+ insert_cost = nil
427
+ cost = nil
428
+ switch_signal = nil
429
+ new_id = nil
430
+ histograms = nil
431
+ clusterBlocksCommand(data, length, num_blocks, block_ids, split)
432
+ block_ids = nil
433
+ }
434
+ }