Sese Framework
x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Huffman.h
浏览该文件的文档.
1
// Copyright 2024 libsese
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
26
#pragma once
27
28
#include <cstdint>
29
#include <string>
30
#include <optional>
31
#include <vector>
32
33
namespace
sese::net::http
{
34
36
class
huffman_node_t
{
37
protected
:
38
huffman_node_t
*
m_left
;
39
huffman_node_t
*
m_right
;
40
int16_t
m_code
;
41
42
public
:
43
explicit
huffman_node_t
(
huffman_node_t
*l =
nullptr
,
huffman_node_t
*r =
nullptr
, int16_t
c
= -1) noexcept;
44
45
virtual ~
huffman_node_t
() {
46
m_left
=
nullptr
;
47
m_right
=
nullptr
;
48
m_code
= 0;
49
}
50
51
[[nodiscard]] int16_t
code
()
const
{
return
m_code
; }
52
53
void
code
(int16_t
c
) {
m_code
=
c
; }
54
55
huffman_node_t
*
left
() {
return
m_left
; }
56
57
void
left
(
huffman_node_t
*l) {
m_left
= l; }
58
59
huffman_node_t
*
right
() {
return
m_right
; }
60
61
void
right
(
huffman_node_t
*r) {
m_right
= r; }
62
};
63
65
class
huffman_tree_t
{
66
protected
:
67
huffman_node_t
*
m_root
;
68
69
void
delete_node
(
huffman_node_t
*n)
noexcept
;
70
71
public
:
72
huffman_tree_t
() noexcept;
73
74
virtual ~
huffman_tree_t
() noexcept;
75
76
std::optional<std::
string
>
decode
(const
char
*src,
size_t
len) const;
77
};
78
80
class
huffman_encoder_t
{
81
private
:
82
uint8_t
m_byte
;
83
uint8_t
m_count
;
84
85
protected
:
86
inline
bool
write_bit(uint8_t bit)
noexcept
;
87
88
public
:
89
huffman_encoder_t
() noexcept;
90
91
virtual ~
huffman_encoder_t
() noexcept = default;
92
93
std::vector<uint8_t>
encode
(const std::vector<uint8_t> &src) noexcept;
94
95
std::vector<uint8_t>
encode
(const std::
string
&src) noexcept;
96
97
std::vector<uint8_t>
encode
(const
char
*ptr) noexcept;
98
};
99
100
using
HuffmanEncoder
=
huffman_encoder_t
;
101
using
HuffmanDecoder
=
huffman_tree_t
;
102
103
}
// namespace sese::net::http
sese
net
http
Huffman.h
生成于 2024年 十一月 4日 星期一 09:58:01 , 为 Sese Framework使用
1.11.0