Chromium Code Reviews| Index: net/tools/domain_security_preload_generator/trie/trie_bit_buffer.h |
| diff --git a/net/tools/domain_security_preload_generator/trie/trie_bit_buffer.h b/net/tools/domain_security_preload_generator/trie/trie_bit_buffer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b5aea08ddd00a1fdece587b0e8649de9df148638 |
| --- /dev/null |
| +++ b/net/tools/domain_security_preload_generator/trie/trie_bit_buffer.h |
| @@ -0,0 +1,77 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_BIT_BUFFER_H_ |
| +#define NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_BIT_BUFFER_H_ |
| + |
| +#include <stdint.h> |
| + |
| +#include <vector> |
| + |
| +#include "net/tools/domain_security_preload_generator/bit_writer.h" |
| +#include "net/tools/domain_security_preload_generator/huffman/huffman_frequency_tracker.h" |
| + |
| +namespace net { |
| + |
| +// TrieBitBuffer acts as a buffer for TrieWriter. It can write bits, characters, |
| +// and positions. The characters are stored as their Huffmanrepresentation. |
|
agl
2016/12/06 18:51:36
Huffmanrepresentation -> HuffmanRepresentation
martijnc
2016/12/07 22:37:54
Done.
|
| +// Positions are references to other locations in the trie. |
| +class TrieBitBuffer { |
| + public: |
| + TrieBitBuffer(); |
| + ~TrieBitBuffer(); |
| + |
| + // Write |bit| to the buffer. |
| + void WriteBit(uint8_t bit); |
| + |
| + // Write the last |number_of_bits| from |bits| to the buffer. |
| + void WriteBits(uint32_t bits, uint8_t number_of_bits); |
| + |
| + // Write a position the the buffer. Actually stores the difference between |
| + // |position| and |last_position|. |last_position| will updated to equal the |
|
agl
2016/12/06 18:51:36
"be" after "will"
martijnc
2016/12/07 22:37:54
Done.
|
| + // input |position|. |
| + void WritePosition(int position, int* last_position); |
| + |
| + // Write the character in |byte| to the buffer using its Huffman |
| + // representation in |table|. Optionally tracks usage of the character in |
| + // |tracker|. |
| + void WriteChar(uint8_t byte, |
| + const HuffmanRepresentationTable& table, |
| + HuffmanFrequencyTracker* tracker); |
| + |
| + // Write the entire buffer to |writer|. |
|
agl
2016/12/06 18:51:36
Comment should say what the return value is.
martijnc
2016/12/07 22:37:54
Done.
|
| + uint32_t WriteToBitWriter(BitWriter& writer); |
| + |
| + // Appends the buffered bits in |current_byte_| to |elements_|. empty bits |
| + // are filled with zero's. |
| + void Close(); |
|
agl
2016/12/06 18:51:36
Likewise, I'd probably call this Flush.
martijnc
2016/12/07 22:37:54
Renamed.
|
| + |
| + private: |
| + // Represents either the last |number_of_bits| bits in |bits| or a position |
| + // (offset) in the trie. |
| + struct BitsOrPosition { |
| + uint8_t bits; |
| + uint8_t number_of_bits; |
| + int position; |
| + }; |
| + |
| + // Returns the minimum number of bits needed to represent |input|. |
| + uint8_t BitLength(int input) const; |
| + |
| + // Append a new element to |elements_|. |
| + void AppendBitsElement(uint8_t bits, uint8_t number_of_bits); |
| + void AppendPositionElement(int position); |
| + |
| + // Buffers bits until it can fill a byte. |
| + uint8_t current_byte_; |
|
agl
2016/12/06 18:51:36
I would use " = 0" syntax for these rather than se
martijnc
2016/12/07 22:37:54
Done.
|
| + |
| + // The number of bits currently in |current_byte_|. |
| + uint32_t used_; |
| + |
| + std::vector<BitsOrPosition> elements_; |
| +}; |
| + |
| +} // namespace net |
| + |
| +#endif // NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_TRIE_TRIE_BIT_BUFFER_H_ |