gem5 v24.0.0.0
Loading...
Searching...
No Matches
huffman.hh
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
29#ifndef __MEM_CACHE_COMPRESSORS_ENCODERS_HUFFMAN_HH__
30#define __MEM_CACHE_COMPRESSORS_ENCODERS_HUFFMAN_HH__
31
32#include <cassert>
33#include <cstdint>
34#include <map>
35#include <memory>
36#include <queue>
37#include <vector>
38
39#include "base/compiler.hh"
41
42namespace gem5
43{
44
45namespace compression
46{
47namespace encoder
48{
49
54class Huffman : public Base
55{
56 public:
57 Huffman(uint64_t max_code_length);
58 ~Huffman() = default;
59
66 void sample(uint64_t value, uint64_t frequency);
67
69 void generateCodeMaps();
70
71 Code encode(const uint64_t val) const override;
72 uint64_t decode(const uint64_t code) const override;
73
74 private:
76 class Node
77 {
78 private:
80 const uint64_t _frequency;
81
83 const uint64_t _value;
84
86 std::unique_ptr<Node> _left;
87
89 std::unique_ptr<Node> _right;
90
91 public:
93 Node(uint64_t value, uint64_t frequency)
94 : _frequency(frequency), _value(value), _left(), _right()
95 {
96 }
97
99 Node(Node* left, Node* right)
100 : _frequency(left->getFrequency() + right->getFrequency()),
101 _value(0), _left(left), _right(right)
102 {
103 }
104
106 uint64_t getFrequency() const { return _frequency; }
107
114 bool
115 isLeaf() const
116 {
117 return (_left == nullptr) && (_right == nullptr);
118 }
119
125 uint64_t
126 getValue() const
127 {
128 assert(isLeaf());
129 return _value;
130 }
131
132 const Node* getLeftSubTree() const { return _left.get(); }
133 const Node* getRightSubTree() const { return _right.get(); }
134 };
135
140 const unsigned maxCodeLength;
141
146 std::map<uint64_t, Code> valueToCode;
147 std::map<uint64_t, uint64_t> codeToValue;
148
154 {
155 bool
156 operator()(const Node* lhs, const Node* rhs) const
157 {
158 return lhs->getFrequency() > rhs->getFrequency();
159 }
160 };
161 std::priority_queue<Node*, std::vector<Node*>, NodeComparator> trees;
162
170 std::unique_ptr<Node> buildTree();
171
180 void generateCodes(const Node* node, const Code& current_code);
181};
182
183} // namespace encoder
184} // namespace compression
185} // namespace gem5
186
187#endif //__MEM_CACHE_COMPRESSORS_ENCODERS_HUFFMAN_HH__
Base class for encoders.
Definition base.hh:58
Node for the Huffman tree.
Definition huffman.hh:77
uint64_t getFrequency() const
Getter for the frequency counter.
Definition huffman.hh:106
const uint64_t _value
Value represented by this node, if this is a leaf node.
Definition huffman.hh:83
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
Node(Node *left, Node *right)
Initialize node as an internal node.
Definition huffman.hh:99
std::unique_ptr< Node > _right
The right tree.
Definition huffman.hh:89
Node(uint64_t value, uint64_t frequency)
Initialize node as a leaf node.
Definition huffman.hh:93
std::unique_ptr< Node > _left
The left tree.
Definition huffman.hh:86
const uint64_t _frequency
Frequency of the value represented by this node.
Definition huffman.hh:80
This encoder builds a Huffman tree using the frequency of each value to be encoded.
Definition huffman.hh:55
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
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Entries are not inserted directly into the tree.
Definition huffman.hh:154
bool operator()(const Node *lhs, const Node *rhs) const
Definition huffman.hh:156

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