gem5 v24.0.0.0
Loading...
Searching...
No Matches
huffman.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019, 2020 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
30
31#include <cassert>
32
33#include "base/logging.hh"
34
35namespace gem5
36{
37
38namespace compression
39{
40namespace encoder
41{
42
43Huffman::Huffman(uint64_t max_code_length)
44 : Base(), maxCodeLength(max_code_length)
45{
47 "Code length cannot surpass its underlying container");
48}
49
50void
51Huffman::sample(uint64_t value, uint64_t frequency)
52{
53 if (frequency != 0) {
54 trees.push(new Node(value, frequency));
55 }
56}
57
58std::unique_ptr<Huffman::Node>
60{
61 // Construct tree by assigning left and right nodes. The left path leads
62 // to the most frequent values
63 while (trees.size() > 1) {
64 Node* left = trees.top();
65 trees.pop();
66
67 Node* right = trees.top();
68 trees.pop();
69
70 Node* parent = new Node(left, right);
71 trees.push(parent);
72 }
73
74 // All queue entries have been merged into a single entry containing
75 // the tree
76 Node* root = trees.top();
77 trees.pop();
78 return std::unique_ptr<Node>(root);
79}
80
81void
83{
84 valueToCode.clear();
85 codeToValue.clear();
86 generateCodes(buildTree().get(), Code());
87}
88
89void
90Huffman::generateCodes(const Node* node, const Code& current_code)
91{
92 // Drop all entries with length greater than maxCodeLength
93 if (current_code.length > maxCodeLength) {
94 return;
95 }
96
97 if (node->isLeaf()) {
98 valueToCode[node->getValue()] = current_code;
99 codeToValue[current_code.code] = node->getValue();
100 } else {
101 Code right_code = current_code;
102 right_code.code = (right_code.code << 1) + 1;
103 right_code.length++;
104 generateCodes(node->getRightSubTree(), right_code);
105
106 Code left_code = current_code;
107 left_code.code = left_code.code << 1;
108 left_code.length++;
109 generateCodes(node->getLeftSubTree(), left_code);
110 }
111}
112
113Code
114Huffman::encode(const uint64_t val) const
115{
116 auto it = valueToCode.find(val);
117 if (it == valueToCode.end()) {
118 // If the value is unknown, generate a dummy code with invalid
119 // length to let the caller know the encoding is invalid
120 Code dummy_code;
121 dummy_code.code = 0;
122 dummy_code.length = 65;
123 return dummy_code;
124 } else {
125 return it->second;
126 }
127}
128
129uint64_t
130Huffman::decode(const uint64_t code) const
131{
132 // A code that does not exist cannot be decoded
133 auto it = codeToValue.find(code);
134 assert(it != codeToValue.end());
135 return it->second;
136}
137
138} // namespace encoder
139} // namespace compression
140} // namespace gem5
Base class for encoders.
Definition base.hh:58
Node for the Huffman tree.
Definition huffman.hh:77
bool isLeaf() const
Determine if the node is a leaf node by checking if it does not have sub-trees.
Definition huffman.hh:115
uint64_t getValue() const
Get the leaf's value.
Definition huffman.hh:126
std::map< uint64_t, Code > valueToCode
Table containing the codewords and their respective lengths.
Definition huffman.hh:146
void sample(uint64_t value, uint64_t frequency)
Inserts the value-frequency pair in the tree.
Definition huffman.cc:51
Code encode(const uint64_t val) const override
The function responsible for the generation of the alternative value.
Definition huffman.cc:114
void generateCodes(const Node *node, const Code &current_code)
Recursive function that generates the huffman codes based on the tree provided.
Definition huffman.cc:90
void generateCodeMaps()
Generation of the code maps.
Definition huffman.cc:82
const unsigned maxCodeLength
Maximum number of bits in a codeword.
Definition huffman.hh:140
uint64_t decode(const uint64_t code) const override
Decode a value.
Definition huffman.cc:130
std::map< uint64_t, uint64_t > codeToValue
Definition huffman.hh:147
std::unique_ptr< Node > buildTree()
Build a Huffman tree using the values and their respective frequencies, which have been informed thro...
Definition huffman.cc:59
Huffman(uint64_t max_code_length)
Definition huffman.cc:43
std::priority_queue< Node *, std::vector< Node * >, NodeComparator > trees
Definition huffman.hh:161
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
unsigned length
Number of bits in the code.
Definition base.hh:49
uint64_t code
Only the LSB of the code are relevant.
Definition base.hh:47

Generated on Tue Jun 18 2024 16:24:04 for gem5 by doxygen 1.11.0