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