1 // Copyright 2018 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_TOOLS_HUFFMAN_TRIE_TRIE_TRIE_WRITER_H_ 6 #define NET_TOOLS_HUFFMAN_TRIE_TRIE_TRIE_WRITER_H_ 7 8 #include <vector> 9 10 #include "net/tools/huffman_trie/bit_writer.h" 11 #include "net/tools/huffman_trie/huffman/huffman_builder.h" 12 #include "net/tools/huffman_trie/trie_entry.h" 13 14 namespace net::huffman_trie { 15 16 enum : uint8_t { kTerminalValue = 0, kEndOfTableValue = 127 }; 17 18 class TrieWriter { 19 public: 20 TrieWriter(const HuffmanRepresentationTable& huffman_table, 21 HuffmanBuilder* huffman_builder); 22 ~TrieWriter(); 23 24 // Constructs a trie containing all |entries|. The output is written to 25 // |buffer_| and |*position| is set to the position of the trie root. Returns 26 // true on success and false on failure. 27 bool WriteEntries(const TrieEntries& entries, uint32_t* position); 28 29 // Returns the position |buffer_| is currently at. The returned value 30 // represents the number of bits. 31 uint32_t position() const; 32 33 // Flushes |buffer_|. 34 void Flush(); 35 36 // Returns the trie bytes. Call Flush() first to ensure the buffer is 37 // complete. bytes()38 const std::vector<uint8_t>& bytes() const { return buffer_.bytes(); } 39 40 protected: huffman_table()41 const HuffmanRepresentationTable& huffman_table() const { 42 return huffman_table_; 43 } 44 huffman_builder()45 HuffmanBuilder* huffman_builder() { return huffman_builder_; } 46 47 private: 48 bool WriteDispatchTables(ReversedEntries::iterator start, 49 ReversedEntries::iterator end, 50 uint32_t* position); 51 52 BitWriter buffer_; 53 const HuffmanRepresentationTable& huffman_table_; 54 HuffmanBuilder* huffman_builder_; 55 }; 56 57 } // namespace net::huffman_trie 58 59 #endif // NET_TOOLS_HUFFMAN_TRIE_TRIE_TRIE_WRITER_H_ 60