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.
Files changed (148) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/README.md +17 -1
  3. package/bin/cli.js +25 -11
  4. package/bin/go/convert.js +11 -1
  5. package/bin/index.js +221 -44
  6. package/bin/main.js +35 -14
  7. package/bin/node/assert.js +2 -2
  8. package/bin/node/buffer.js +9 -0
  9. package/bin/node/dns.js +1 -1
  10. package/bin/node/events.js +1 -1
  11. package/bin/node/fs.js +1 -1
  12. package/bin/node/fs_watch.js +407 -0
  13. package/bin/node/http2.js +33 -6
  14. package/bin/node/readline.js +1 -1
  15. package/bin/node/registry.js +2 -1
  16. package/bin/node/stream.js +6 -3
  17. package/bin/node/timers.js +1 -1
  18. package/bin/node/tls.js +3 -1
  19. package/bin/node/typed_arrays.js +608 -0
  20. package/bin/node/util.js +3 -0
  21. package/bin/node/zlib.js +131 -16
  22. package/bin/transpiler.js +684 -62
  23. package/bin/version.js +1 -1
  24. package/conformance.md +16 -4
  25. package/examples/README.md +54 -0
  26. package/examples/fibonacci/README.md +89 -0
  27. package/examples/fibonacci/bench.mjs +87 -0
  28. package/examples/fibonacci/main.ts +21 -0
  29. package/examples/hello-world/README.md +62 -0
  30. package/examples/hello-world/bench.mjs +66 -0
  31. package/examples/hello-world/main.ts +7 -0
  32. package/examples/json-server/README.md +125 -0
  33. package/examples/json-server/bench.mjs +171 -0
  34. package/examples/json-server/main.ts +67 -0
  35. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  36. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  37. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  38. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  39. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  40. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  41. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  42. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  43. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  44. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  45. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  46. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  47. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  48. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  49. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  50. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  51. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  52. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  53. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  54. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  55. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  56. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  57. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  58. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  59. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  60. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  61. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  62. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  63. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  64. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  65. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  66. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  67. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  68. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  69. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  70. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  71. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  72. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  73. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  74. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  75. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  76. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  77. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  78. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  79. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  80. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  81. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  82. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  83. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  84. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  85. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  86. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  87. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  88. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  89. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  90. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  91. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  92. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  93. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  94. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  95. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  96. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  97. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  98. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  99. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  100. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  101. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  102. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  103. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  104. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  105. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  106. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  107. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  108. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  109. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  110. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  111. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  112. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  113. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  114. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  115. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  116. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  117. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  118. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  119. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  120. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  121. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  122. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  123. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  124. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  125. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  126. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  127. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  128. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  129. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  130. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  131. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  132. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  133. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  134. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  135. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  136. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  137. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  138. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  139. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  140. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  141. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  142. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  143. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  144. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  145. package/govendor/manifest.txt +3 -1
  146. package/package.json +1 -1
  147. package/types/typenative-npm.d.ts +72 -0
  148. package/types/typenative.d.ts +117 -1
@@ -0,0 +1,165 @@
1
+ package brotli
2
+
3
+ /* Copyright 2015 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
+ /* Greedy block splitter for one block category (literal, command or distance).
10
+ */
11
+ type blockSplitterCommand struct {
12
+ alphabet_size_ uint
13
+ min_block_size_ uint
14
+ split_threshold_ float64
15
+ num_blocks_ uint
16
+ split_ *blockSplit
17
+ histograms_ []histogramCommand
18
+ histograms_size_ *uint
19
+ target_block_size_ uint
20
+ block_size_ uint
21
+ curr_histogram_ix_ uint
22
+ last_histogram_ix_ [2]uint
23
+ last_entropy_ [2]float64
24
+ merge_last_count_ uint
25
+ }
26
+
27
+ func initBlockSplitterCommand(self *blockSplitterCommand, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]histogramCommand, histograms_size *uint) {
28
+ var max_num_blocks uint = num_symbols/min_block_size + 1
29
+ var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1)
30
+ /* We have to allocate one more histogram than the maximum number of block
31
+ types for the current histogram when the meta-block is too big. */
32
+ self.alphabet_size_ = alphabet_size
33
+
34
+ self.min_block_size_ = min_block_size
35
+ self.split_threshold_ = split_threshold
36
+ self.num_blocks_ = 0
37
+ self.split_ = split
38
+ self.histograms_size_ = histograms_size
39
+ self.target_block_size_ = min_block_size
40
+ self.block_size_ = 0
41
+ self.curr_histogram_ix_ = 0
42
+ self.merge_last_count_ = 0
43
+ brotli_ensure_capacity_uint8_t(&split.types, &split.types_alloc_size, max_num_blocks)
44
+ brotli_ensure_capacity_uint32_t(&split.lengths, &split.lengths_alloc_size, max_num_blocks)
45
+ self.split_.num_blocks = max_num_blocks
46
+ *histograms_size = max_num_types
47
+ if cap(*histograms) < int(*histograms_size) {
48
+ *histograms = make([]histogramCommand, (*histograms_size))
49
+ } else {
50
+ *histograms = (*histograms)[:*histograms_size]
51
+ }
52
+ self.histograms_ = *histograms
53
+
54
+ /* Clear only current histogram. */
55
+ histogramClearCommand(&self.histograms_[0])
56
+
57
+ self.last_histogram_ix_[1] = 0
58
+ self.last_histogram_ix_[0] = self.last_histogram_ix_[1]
59
+ }
60
+
61
+ /* Does either of three things:
62
+ (1) emits the current block with a new block type;
63
+ (2) emits the current block with the type of the second last block;
64
+ (3) merges the current block with the last block. */
65
+ func blockSplitterFinishBlockCommand(self *blockSplitterCommand, is_final bool) {
66
+ var split *blockSplit = self.split_
67
+ var last_entropy []float64 = self.last_entropy_[:]
68
+ var histograms []histogramCommand = self.histograms_
69
+ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_)
70
+ if self.num_blocks_ == 0 {
71
+ /* Create first block. */
72
+ split.lengths[0] = uint32(self.block_size_)
73
+
74
+ split.types[0] = 0
75
+ last_entropy[0] = bitsEntropy(histograms[0].data_[:], self.alphabet_size_)
76
+ last_entropy[1] = last_entropy[0]
77
+ self.num_blocks_++
78
+ split.num_types++
79
+ self.curr_histogram_ix_++
80
+ if self.curr_histogram_ix_ < *self.histograms_size_ {
81
+ histogramClearCommand(&histograms[self.curr_histogram_ix_])
82
+ }
83
+ self.block_size_ = 0
84
+ } else if self.block_size_ > 0 {
85
+ var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
86
+ var combined_histo [2]histogramCommand
87
+ var combined_entropy [2]float64
88
+ var diff [2]float64
89
+ var j uint
90
+ for j = 0; j < 2; j++ {
91
+ var last_histogram_ix uint = self.last_histogram_ix_[j]
92
+ combined_histo[j] = histograms[self.curr_histogram_ix_]
93
+ histogramAddHistogramCommand(&combined_histo[j], &histograms[last_histogram_ix])
94
+ combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
95
+ diff[j] = combined_entropy[j] - entropy - last_entropy[j]
96
+ }
97
+
98
+ if split.num_types < maxNumberOfBlockTypes && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ {
99
+ /* Create new block. */
100
+ split.lengths[self.num_blocks_] = uint32(self.block_size_)
101
+
102
+ split.types[self.num_blocks_] = byte(split.num_types)
103
+ self.last_histogram_ix_[1] = self.last_histogram_ix_[0]
104
+ self.last_histogram_ix_[0] = uint(byte(split.num_types))
105
+ last_entropy[1] = last_entropy[0]
106
+ last_entropy[0] = entropy
107
+ self.num_blocks_++
108
+ split.num_types++
109
+ self.curr_histogram_ix_++
110
+ if self.curr_histogram_ix_ < *self.histograms_size_ {
111
+ histogramClearCommand(&histograms[self.curr_histogram_ix_])
112
+ }
113
+ self.block_size_ = 0
114
+ self.merge_last_count_ = 0
115
+ self.target_block_size_ = self.min_block_size_
116
+ } else if diff[1] < diff[0]-20.0 {
117
+ split.lengths[self.num_blocks_] = uint32(self.block_size_)
118
+ split.types[self.num_blocks_] = split.types[self.num_blocks_-2]
119
+ /* Combine this block with second last block. */
120
+
121
+ var tmp uint = self.last_histogram_ix_[0]
122
+ self.last_histogram_ix_[0] = self.last_histogram_ix_[1]
123
+ self.last_histogram_ix_[1] = tmp
124
+ histograms[self.last_histogram_ix_[0]] = combined_histo[1]
125
+ last_entropy[1] = last_entropy[0]
126
+ last_entropy[0] = combined_entropy[1]
127
+ self.num_blocks_++
128
+ self.block_size_ = 0
129
+ histogramClearCommand(&histograms[self.curr_histogram_ix_])
130
+ self.merge_last_count_ = 0
131
+ self.target_block_size_ = self.min_block_size_
132
+ } else {
133
+ /* Combine this block with last block. */
134
+ split.lengths[self.num_blocks_-1] += uint32(self.block_size_)
135
+
136
+ histograms[self.last_histogram_ix_[0]] = combined_histo[0]
137
+ last_entropy[0] = combined_entropy[0]
138
+ if split.num_types == 1 {
139
+ last_entropy[1] = last_entropy[0]
140
+ }
141
+
142
+ self.block_size_ = 0
143
+ histogramClearCommand(&histograms[self.curr_histogram_ix_])
144
+ self.merge_last_count_++
145
+ if self.merge_last_count_ > 1 {
146
+ self.target_block_size_ += self.min_block_size_
147
+ }
148
+ }
149
+ }
150
+
151
+ if is_final {
152
+ *self.histograms_size_ = split.num_types
153
+ split.num_blocks = self.num_blocks_
154
+ }
155
+ }
156
+
157
+ /* Adds the next symbol to the current histogram. When the current histogram
158
+ reaches the target size, decides on merging the block. */
159
+ func blockSplitterAddSymbolCommand(self *blockSplitterCommand, symbol uint) {
160
+ histogramAddCommand(&self.histograms_[self.curr_histogram_ix_], symbol)
161
+ self.block_size_++
162
+ if self.block_size_ == self.target_block_size_ {
163
+ blockSplitterFinishBlockCommand(self, false) /* is_final = */
164
+ }
165
+ }
@@ -0,0 +1,165 @@
1
+ package brotli
2
+
3
+ /* Copyright 2015 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
+ /* Greedy block splitter for one block category (literal, command or distance).
10
+ */
11
+ type blockSplitterDistance struct {
12
+ alphabet_size_ uint
13
+ min_block_size_ uint
14
+ split_threshold_ float64
15
+ num_blocks_ uint
16
+ split_ *blockSplit
17
+ histograms_ []histogramDistance
18
+ histograms_size_ *uint
19
+ target_block_size_ uint
20
+ block_size_ uint
21
+ curr_histogram_ix_ uint
22
+ last_histogram_ix_ [2]uint
23
+ last_entropy_ [2]float64
24
+ merge_last_count_ uint
25
+ }
26
+
27
+ func initBlockSplitterDistance(self *blockSplitterDistance, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]histogramDistance, histograms_size *uint) {
28
+ var max_num_blocks uint = num_symbols/min_block_size + 1
29
+ var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1)
30
+ /* We have to allocate one more histogram than the maximum number of block
31
+ types for the current histogram when the meta-block is too big. */
32
+ self.alphabet_size_ = alphabet_size
33
+
34
+ self.min_block_size_ = min_block_size
35
+ self.split_threshold_ = split_threshold
36
+ self.num_blocks_ = 0
37
+ self.split_ = split
38
+ self.histograms_size_ = histograms_size
39
+ self.target_block_size_ = min_block_size
40
+ self.block_size_ = 0
41
+ self.curr_histogram_ix_ = 0
42
+ self.merge_last_count_ = 0
43
+ brotli_ensure_capacity_uint8_t(&split.types, &split.types_alloc_size, max_num_blocks)
44
+ brotli_ensure_capacity_uint32_t(&split.lengths, &split.lengths_alloc_size, max_num_blocks)
45
+ self.split_.num_blocks = max_num_blocks
46
+ *histograms_size = max_num_types
47
+ if cap(*histograms) < int(*histograms_size) {
48
+ *histograms = make([]histogramDistance, *histograms_size)
49
+ } else {
50
+ *histograms = (*histograms)[:*histograms_size]
51
+ }
52
+ self.histograms_ = *histograms
53
+
54
+ /* Clear only current histogram. */
55
+ histogramClearDistance(&self.histograms_[0])
56
+
57
+ self.last_histogram_ix_[1] = 0
58
+ self.last_histogram_ix_[0] = self.last_histogram_ix_[1]
59
+ }
60
+
61
+ /* Does either of three things:
62
+ (1) emits the current block with a new block type;
63
+ (2) emits the current block with the type of the second last block;
64
+ (3) merges the current block with the last block. */
65
+ func blockSplitterFinishBlockDistance(self *blockSplitterDistance, is_final bool) {
66
+ var split *blockSplit = self.split_
67
+ var last_entropy []float64 = self.last_entropy_[:]
68
+ var histograms []histogramDistance = self.histograms_
69
+ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_)
70
+ if self.num_blocks_ == 0 {
71
+ /* Create first block. */
72
+ split.lengths[0] = uint32(self.block_size_)
73
+
74
+ split.types[0] = 0
75
+ last_entropy[0] = bitsEntropy(histograms[0].data_[:], self.alphabet_size_)
76
+ last_entropy[1] = last_entropy[0]
77
+ self.num_blocks_++
78
+ split.num_types++
79
+ self.curr_histogram_ix_++
80
+ if self.curr_histogram_ix_ < *self.histograms_size_ {
81
+ histogramClearDistance(&histograms[self.curr_histogram_ix_])
82
+ }
83
+ self.block_size_ = 0
84
+ } else if self.block_size_ > 0 {
85
+ var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
86
+ var combined_histo [2]histogramDistance
87
+ var combined_entropy [2]float64
88
+ var diff [2]float64
89
+ var j uint
90
+ for j = 0; j < 2; j++ {
91
+ var last_histogram_ix uint = self.last_histogram_ix_[j]
92
+ combined_histo[j] = histograms[self.curr_histogram_ix_]
93
+ histogramAddHistogramDistance(&combined_histo[j], &histograms[last_histogram_ix])
94
+ combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
95
+ diff[j] = combined_entropy[j] - entropy - last_entropy[j]
96
+ }
97
+
98
+ if split.num_types < maxNumberOfBlockTypes && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ {
99
+ /* Create new block. */
100
+ split.lengths[self.num_blocks_] = uint32(self.block_size_)
101
+
102
+ split.types[self.num_blocks_] = byte(split.num_types)
103
+ self.last_histogram_ix_[1] = self.last_histogram_ix_[0]
104
+ self.last_histogram_ix_[0] = uint(byte(split.num_types))
105
+ last_entropy[1] = last_entropy[0]
106
+ last_entropy[0] = entropy
107
+ self.num_blocks_++
108
+ split.num_types++
109
+ self.curr_histogram_ix_++
110
+ if self.curr_histogram_ix_ < *self.histograms_size_ {
111
+ histogramClearDistance(&histograms[self.curr_histogram_ix_])
112
+ }
113
+ self.block_size_ = 0
114
+ self.merge_last_count_ = 0
115
+ self.target_block_size_ = self.min_block_size_
116
+ } else if diff[1] < diff[0]-20.0 {
117
+ split.lengths[self.num_blocks_] = uint32(self.block_size_)
118
+ split.types[self.num_blocks_] = split.types[self.num_blocks_-2]
119
+ /* Combine this block with second last block. */
120
+
121
+ var tmp uint = self.last_histogram_ix_[0]
122
+ self.last_histogram_ix_[0] = self.last_histogram_ix_[1]
123
+ self.last_histogram_ix_[1] = tmp
124
+ histograms[self.last_histogram_ix_[0]] = combined_histo[1]
125
+ last_entropy[1] = last_entropy[0]
126
+ last_entropy[0] = combined_entropy[1]
127
+ self.num_blocks_++
128
+ self.block_size_ = 0
129
+ histogramClearDistance(&histograms[self.curr_histogram_ix_])
130
+ self.merge_last_count_ = 0
131
+ self.target_block_size_ = self.min_block_size_
132
+ } else {
133
+ /* Combine this block with last block. */
134
+ split.lengths[self.num_blocks_-1] += uint32(self.block_size_)
135
+
136
+ histograms[self.last_histogram_ix_[0]] = combined_histo[0]
137
+ last_entropy[0] = combined_entropy[0]
138
+ if split.num_types == 1 {
139
+ last_entropy[1] = last_entropy[0]
140
+ }
141
+
142
+ self.block_size_ = 0
143
+ histogramClearDistance(&histograms[self.curr_histogram_ix_])
144
+ self.merge_last_count_++
145
+ if self.merge_last_count_ > 1 {
146
+ self.target_block_size_ += self.min_block_size_
147
+ }
148
+ }
149
+ }
150
+
151
+ if is_final {
152
+ *self.histograms_size_ = split.num_types
153
+ split.num_blocks = self.num_blocks_
154
+ }
155
+ }
156
+
157
+ /* Adds the next symbol to the current histogram. When the current histogram
158
+ reaches the target size, decides on merging the block. */
159
+ func blockSplitterAddSymbolDistance(self *blockSplitterDistance, symbol uint) {
160
+ histogramAddDistance(&self.histograms_[self.curr_histogram_ix_], symbol)
161
+ self.block_size_++
162
+ if self.block_size_ == self.target_block_size_ {
163
+ blockSplitterFinishBlockDistance(self, false) /* is_final = */
164
+ }
165
+ }
@@ -0,0 +1,165 @@
1
+ package brotli
2
+
3
+ /* Copyright 2015 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
+ /* Greedy block splitter for one block category (literal, command or distance).
10
+ */
11
+ type blockSplitterLiteral struct {
12
+ alphabet_size_ uint
13
+ min_block_size_ uint
14
+ split_threshold_ float64
15
+ num_blocks_ uint
16
+ split_ *blockSplit
17
+ histograms_ []histogramLiteral
18
+ histograms_size_ *uint
19
+ target_block_size_ uint
20
+ block_size_ uint
21
+ curr_histogram_ix_ uint
22
+ last_histogram_ix_ [2]uint
23
+ last_entropy_ [2]float64
24
+ merge_last_count_ uint
25
+ }
26
+
27
+ func initBlockSplitterLiteral(self *blockSplitterLiteral, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]histogramLiteral, histograms_size *uint) {
28
+ var max_num_blocks uint = num_symbols/min_block_size + 1
29
+ var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1)
30
+ /* We have to allocate one more histogram than the maximum number of block
31
+ types for the current histogram when the meta-block is too big. */
32
+ self.alphabet_size_ = alphabet_size
33
+
34
+ self.min_block_size_ = min_block_size
35
+ self.split_threshold_ = split_threshold
36
+ self.num_blocks_ = 0
37
+ self.split_ = split
38
+ self.histograms_size_ = histograms_size
39
+ self.target_block_size_ = min_block_size
40
+ self.block_size_ = 0
41
+ self.curr_histogram_ix_ = 0
42
+ self.merge_last_count_ = 0
43
+ brotli_ensure_capacity_uint8_t(&split.types, &split.types_alloc_size, max_num_blocks)
44
+ brotli_ensure_capacity_uint32_t(&split.lengths, &split.lengths_alloc_size, max_num_blocks)
45
+ self.split_.num_blocks = max_num_blocks
46
+ *histograms_size = max_num_types
47
+ if cap(*histograms) < int(*histograms_size) {
48
+ *histograms = make([]histogramLiteral, *histograms_size)
49
+ } else {
50
+ *histograms = (*histograms)[:*histograms_size]
51
+ }
52
+ self.histograms_ = *histograms
53
+
54
+ /* Clear only current histogram. */
55
+ histogramClearLiteral(&self.histograms_[0])
56
+
57
+ self.last_histogram_ix_[1] = 0
58
+ self.last_histogram_ix_[0] = self.last_histogram_ix_[1]
59
+ }
60
+
61
+ /* Does either of three things:
62
+ (1) emits the current block with a new block type;
63
+ (2) emits the current block with the type of the second last block;
64
+ (3) merges the current block with the last block. */
65
+ func blockSplitterFinishBlockLiteral(self *blockSplitterLiteral, is_final bool) {
66
+ var split *blockSplit = self.split_
67
+ var last_entropy []float64 = self.last_entropy_[:]
68
+ var histograms []histogramLiteral = self.histograms_
69
+ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_)
70
+ if self.num_blocks_ == 0 {
71
+ /* Create first block. */
72
+ split.lengths[0] = uint32(self.block_size_)
73
+
74
+ split.types[0] = 0
75
+ last_entropy[0] = bitsEntropy(histograms[0].data_[:], self.alphabet_size_)
76
+ last_entropy[1] = last_entropy[0]
77
+ self.num_blocks_++
78
+ split.num_types++
79
+ self.curr_histogram_ix_++
80
+ if self.curr_histogram_ix_ < *self.histograms_size_ {
81
+ histogramClearLiteral(&histograms[self.curr_histogram_ix_])
82
+ }
83
+ self.block_size_ = 0
84
+ } else if self.block_size_ > 0 {
85
+ var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_)
86
+ var combined_histo [2]histogramLiteral
87
+ var combined_entropy [2]float64
88
+ var diff [2]float64
89
+ var j uint
90
+ for j = 0; j < 2; j++ {
91
+ var last_histogram_ix uint = self.last_histogram_ix_[j]
92
+ combined_histo[j] = histograms[self.curr_histogram_ix_]
93
+ histogramAddHistogramLiteral(&combined_histo[j], &histograms[last_histogram_ix])
94
+ combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_)
95
+ diff[j] = combined_entropy[j] - entropy - last_entropy[j]
96
+ }
97
+
98
+ if split.num_types < maxNumberOfBlockTypes && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ {
99
+ /* Create new block. */
100
+ split.lengths[self.num_blocks_] = uint32(self.block_size_)
101
+
102
+ split.types[self.num_blocks_] = byte(split.num_types)
103
+ self.last_histogram_ix_[1] = self.last_histogram_ix_[0]
104
+ self.last_histogram_ix_[0] = uint(byte(split.num_types))
105
+ last_entropy[1] = last_entropy[0]
106
+ last_entropy[0] = entropy
107
+ self.num_blocks_++
108
+ split.num_types++
109
+ self.curr_histogram_ix_++
110
+ if self.curr_histogram_ix_ < *self.histograms_size_ {
111
+ histogramClearLiteral(&histograms[self.curr_histogram_ix_])
112
+ }
113
+ self.block_size_ = 0
114
+ self.merge_last_count_ = 0
115
+ self.target_block_size_ = self.min_block_size_
116
+ } else if diff[1] < diff[0]-20.0 {
117
+ split.lengths[self.num_blocks_] = uint32(self.block_size_)
118
+ split.types[self.num_blocks_] = split.types[self.num_blocks_-2]
119
+ /* Combine this block with second last block. */
120
+
121
+ var tmp uint = self.last_histogram_ix_[0]
122
+ self.last_histogram_ix_[0] = self.last_histogram_ix_[1]
123
+ self.last_histogram_ix_[1] = tmp
124
+ histograms[self.last_histogram_ix_[0]] = combined_histo[1]
125
+ last_entropy[1] = last_entropy[0]
126
+ last_entropy[0] = combined_entropy[1]
127
+ self.num_blocks_++
128
+ self.block_size_ = 0
129
+ histogramClearLiteral(&histograms[self.curr_histogram_ix_])
130
+ self.merge_last_count_ = 0
131
+ self.target_block_size_ = self.min_block_size_
132
+ } else {
133
+ /* Combine this block with last block. */
134
+ split.lengths[self.num_blocks_-1] += uint32(self.block_size_)
135
+
136
+ histograms[self.last_histogram_ix_[0]] = combined_histo[0]
137
+ last_entropy[0] = combined_entropy[0]
138
+ if split.num_types == 1 {
139
+ last_entropy[1] = last_entropy[0]
140
+ }
141
+
142
+ self.block_size_ = 0
143
+ histogramClearLiteral(&histograms[self.curr_histogram_ix_])
144
+ self.merge_last_count_++
145
+ if self.merge_last_count_ > 1 {
146
+ self.target_block_size_ += self.min_block_size_
147
+ }
148
+ }
149
+ }
150
+
151
+ if is_final {
152
+ *self.histograms_size_ = split.num_types
153
+ split.num_blocks = self.num_blocks_
154
+ }
155
+ }
156
+
157
+ /* Adds the next symbol to the current histogram. When the current histogram
158
+ reaches the target size, decides on merging the block. */
159
+ func blockSplitterAddSymbolLiteral(self *blockSplitterLiteral, symbol uint) {
160
+ histogramAddLiteral(&self.histograms_[self.curr_histogram_ix_], symbol)
161
+ self.block_size_++
162
+ if self.block_size_ == self.target_block_size_ {
163
+ blockSplitterFinishBlockLiteral(self, false) /* is_final = */
164
+ }
165
+ }
@@ -0,0 +1,37 @@
1
+ package brotli
2
+
3
+ /* Copyright 2017 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
+ /* Parameters for the Brotli encoder with chosen quality levels. */
10
+ type hasherParams struct {
11
+ type_ int
12
+ bucket_bits int
13
+ block_bits int
14
+ hash_len int
15
+ num_last_distances_to_check int
16
+ }
17
+
18
+ type distanceParams struct {
19
+ distance_postfix_bits uint32
20
+ num_direct_distance_codes uint32
21
+ alphabet_size uint32
22
+ max_distance uint
23
+ }
24
+
25
+ /* Encoding parameters */
26
+ type encoderParams struct {
27
+ mode int
28
+ quality int
29
+ lgwin uint
30
+ lgblock int
31
+ size_hint uint
32
+ disable_literal_context_modeling bool
33
+ large_window bool
34
+ hasher hasherParams
35
+ dist distanceParams
36
+ dictionary encoderDictionary
37
+ }
@@ -0,0 +1,103 @@
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
+ func brotli_min_double(a float64, b float64) float64 {
10
+ if a < b {
11
+ return a
12
+ } else {
13
+ return b
14
+ }
15
+ }
16
+
17
+ func brotli_max_double(a float64, b float64) float64 {
18
+ if a > b {
19
+ return a
20
+ } else {
21
+ return b
22
+ }
23
+ }
24
+
25
+ func brotli_min_float(a float32, b float32) float32 {
26
+ if a < b {
27
+ return a
28
+ } else {
29
+ return b
30
+ }
31
+ }
32
+
33
+ func brotli_max_float(a float32, b float32) float32 {
34
+ if a > b {
35
+ return a
36
+ } else {
37
+ return b
38
+ }
39
+ }
40
+
41
+ func brotli_min_int(a int, b int) int {
42
+ if a < b {
43
+ return a
44
+ } else {
45
+ return b
46
+ }
47
+ }
48
+
49
+ func brotli_max_int(a int, b int) int {
50
+ if a > b {
51
+ return a
52
+ } else {
53
+ return b
54
+ }
55
+ }
56
+
57
+ func brotli_min_size_t(a uint, b uint) uint {
58
+ if a < b {
59
+ return a
60
+ } else {
61
+ return b
62
+ }
63
+ }
64
+
65
+ func brotli_max_size_t(a uint, b uint) uint {
66
+ if a > b {
67
+ return a
68
+ } else {
69
+ return b
70
+ }
71
+ }
72
+
73
+ func brotli_min_uint32_t(a uint32, b uint32) uint32 {
74
+ if a < b {
75
+ return a
76
+ } else {
77
+ return b
78
+ }
79
+ }
80
+
81
+ func brotli_max_uint32_t(a uint32, b uint32) uint32 {
82
+ if a > b {
83
+ return a
84
+ } else {
85
+ return b
86
+ }
87
+ }
88
+
89
+ func brotli_min_uint8_t(a byte, b byte) byte {
90
+ if a < b {
91
+ return a
92
+ } else {
93
+ return b
94
+ }
95
+ }
96
+
97
+ func brotli_max_uint8_t(a byte, b byte) byte {
98
+ if a > b {
99
+ return a
100
+ } else {
101
+ return b
102
+ }
103
+ }
@@ -0,0 +1,30 @@
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
+ /* Functions for encoding of integers into prefix codes the amount of extra
10
+ bits, and the actual values of the extra bits. */
11
+
12
+ /* Here distance_code is an intermediate code, i.e. one of the special codes or
13
+ the actual distance increased by BROTLI_NUM_DISTANCE_SHORT_CODES - 1. */
14
+ func prefixEncodeCopyDistance(distance_code uint, num_direct_codes uint, postfix_bits uint, code *uint16, extra_bits *uint32) {
15
+ if distance_code < numDistanceShortCodes+num_direct_codes {
16
+ *code = uint16(distance_code)
17
+ *extra_bits = 0
18
+ return
19
+ } else {
20
+ var dist uint = (uint(1) << (postfix_bits + 2)) + (distance_code - numDistanceShortCodes - num_direct_codes)
21
+ var bucket uint = uint(log2FloorNonZero(dist) - 1)
22
+ var postfix_mask uint = (1 << postfix_bits) - 1
23
+ var postfix uint = dist & postfix_mask
24
+ var prefix uint = (dist >> bucket) & 1
25
+ var offset uint = (2 + prefix) << bucket
26
+ var nbits uint = bucket - postfix_bits
27
+ *code = uint16(nbits<<10 | (numDistanceShortCodes + num_direct_codes + ((2*(nbits-1) + prefix) << postfix_bits) + postfix))
28
+ *extra_bits = uint32((dist - offset) >> postfix_bits)
29
+ }
30
+ }