| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016 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_DOMAIN_SECURITY_PRELOAD_GENERATOR_HUFFMAN_FREQUENCY_TRACKER_H_ |
| 6 #define NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_HUFFMAN_FREQUENCY_TRACKER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <map> |
| 11 #include <memory> |
| 12 #include <vector> |
| 13 |
| 14 namespace net { |
| 15 |
| 16 class HuffmanNode; |
| 17 |
| 18 struct HuffmanRepresentation { |
| 19 uint32_t bits; |
| 20 uint32_t number_of_bits; |
| 21 }; |
| 22 |
| 23 // A HuffmanRepresentationTable maps the original characters to their Huffman |
| 24 // representation. The Huffman representation consists of the number of bits |
| 25 // needed to represent the character and the actual bits. |
| 26 using HuffmanRepresentationTable = std::map<uint8_t, HuffmanRepresentation>; |
| 27 using HuffmanRepresentationPair = std::pair<uint8_t, HuffmanRepresentation>; |
| 28 |
| 29 // This class tracks the number of times each character is used and calculates |
| 30 // a space efficient way to represent all tracked characters by constructing a |
| 31 // Huffman tree based on the number of times each character is seen. |
| 32 class HuffmanFrequencyTracker { |
| 33 public: |
| 34 HuffmanFrequencyTracker(); |
| 35 ~HuffmanFrequencyTracker(); |
| 36 |
| 37 // Will increase the count for |character| by one indicating it has been used. |
| 38 void RecordUsage(uint8_t character); |
| 39 |
| 40 // Returns a HuffmanRepresentationTable based on the usage data collected |
| 41 // through RecordUsage(). |
| 42 HuffmanRepresentationTable ToHuffmanRepresentationTable(); |
| 43 |
| 44 // Outputs the Huffman representation as a vector of uint8_t's in a format |
| 45 // Chromium can use to reconstruct the tree. |
| 46 // |
| 47 // The nodes of the tree are pairs of uint8s. The last node in the array is |
| 48 // the root of the tree. Each pair is two uint8_t values, the first is "left" |
| 49 // and the second is "right". If a uint8_t value has the MSB set then it |
| 50 // represents a literal leaf value. Otherwise it's a pointer to the n'th |
| 51 // element of the array. |
| 52 std::vector<uint8_t> AsVector(); |
| 53 |
| 54 private: |
| 55 // Determines the Huffman representation of the characters under |node| and |
| 56 // inserts them into |table|. |bits| and |number_of_bits| are used as a |
| 57 // prefix. |
| 58 void TreeToHuffmanRepresentationTable(HuffmanNode* node, |
| 59 uint32_t bits, |
| 60 uint32_t number_of_bits, |
| 61 HuffmanRepresentationTable* table); |
| 62 |
| 63 // Converts the tree under |node| into a byte representation in |vector|. See |
| 64 // AsVector() for more information on the format. |
| 65 uint32_t WriteToVector(HuffmanNode* node, std::vector<uint8_t>* vector); |
| 66 |
| 67 // Constructs a Huffman tree based on |counts_|. |
| 68 std::unique_ptr<HuffmanNode> BuildTree(); |
| 69 |
| 70 // Holds usage information for the traced characters. Maps the character to |
| 71 // the number of these its usage has been recorded through RecordUsage(). |
| 72 std::map<uint8_t, uint32_t> counts_; |
| 73 }; |
| 74 |
| 75 } // namespace net |
| 76 |
| 77 #endif // NET_TOOLS_DOMAIN_SECURITY_PRELOAD_GENERATOR_HUFFMAN_FREQUENCY_TRACKER
_H_ |
| OLD | NEW |