Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
huffman.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <Huffman.H>
41
42#include <tpl_binNodeUtils.H>
43
44#include <random>
45#include <sstream>
46#include <string>
47#include <vector>
48
49using namespace Aleph;
50using namespace std;
51
52namespace {
53
54vector<char> to_c_buffer(const string &s)
55{
57 buf.reserve(s.size() + 1);
58 buf.insert(buf.end(), s.begin(), s.end());
59 buf.push_back('\0');
60 return buf;
61}
62
63string decode_bits(BinNode<string> *root, const string &end_symbol, BitArray &bits)
64{
66 ostringstream out;
67 decoder.decode(bits, out);
68 return out.str();
69}
70
72{
73 const string input = "hello world\nthis\tis a test\n";
74 auto buf = to_c_buffer(input);
75
77 BitArray bits;
78
79 encoder.read_input(buf.data(), true);
80 encoder.encode(buf.data(), bits);
81
82 EXPECT_EQ(encoder.get_end_of_stream(), string(""));
83 EXPECT_GT(bits.size(), 0u);
84
85 EXPECT_EQ(decode_bits(encoder.get_root(), encoder.get_end_of_stream(), bits), input);
86
87 destroyRec(encoder.get_root());
88 destroyRec(encoder.get_freq_root());
89}
90
92{
93 const string input = "A";
94 istringstream in1(input);
95
97 encoder.read_input(in1, false);
98
99 istringstream in2(input);
100 BitArray bits;
101 encoder.encode(in2, bits);
102
103 EXPECT_EQ(decode_bits(encoder.get_root(), encoder.get_end_of_stream(), bits), input);
104
105 destroyRec(encoder.get_root());
106}
107
109{
110 const string input = "a b\nc\tdd\n";
111 auto buf = to_c_buffer(input);
112
114 encoder.read_input(buf.data(), false);
115
117 encoder.encode(buf.data(), bits1);
118
119 ostringstream out;
120 encoder.save_tree(out);
121
123 istringstream in(out.str());
124 loaded.load_tree(in);
125
126 EXPECT_TRUE(areEquivalents(encoder.get_root(), loaded.get_root()));
127 EXPECT_EQ(loaded.get_end_of_stream(), encoder.get_end_of_stream());
128
130 loaded.encode(buf.data(), bits2);
132 EXPECT_EQ(decode_bits(loaded.get_root(), loaded.get_end_of_stream(), bits2), input);
133
134 destroyRec(encoder.get_root());
135 destroyRec(loaded.get_root());
136}
137
139{
140 const string input = "aba";
141 auto buf = to_c_buffer(input);
142
144 encoder.read_input(buf.data(), false);
145
146 BitArray bits;
147 encoder.encode(buf.data(), bits);
148
149 BitArray extended = bits;
150 extended.push(1);
151 extended.push(0);
152 extended.push(1);
153
154 EXPECT_EQ(decode_bits(encoder.get_root(), encoder.get_end_of_stream(), extended), input);
155
156 destroyRec(encoder.get_root());
157}
158
160{
161 mt19937 rng(123456u);
163 const string alphabet = "abcde fghij\n\t";
165
166 for (int iter = 0; iter < 50; ++iter)
167 {
168 const int len = len_dist(rng);
169 string input;
170 input.reserve(static_cast<size_t>(len));
171 for (int i = 0; i < len; ++i)
172 input.push_back(alphabet[pick(rng)]);
173
174 auto buf = to_c_buffer(input);
175
177 encoder.read_input(buf.data(), false);
178
179 BitArray bits;
180 encoder.encode(buf.data(), bits);
181
182 EXPECT_EQ(decode_bits(encoder.get_root(), encoder.get_end_of_stream(), bits), input);
183
184 destroyRec(encoder.get_root());
185 }
186}
187
189{
191 BitArray bits;
192 char input[] = "x";
193 EXPECT_THROW(encoder.encode(input, bits), std::domain_error);
194}
195
197{
199 encoder.set_end_of_stream("END");
200 EXPECT_THROW(encoder.set_end_of_stream("OTHER"), std::domain_error);
201}
202
204{
206 EXPECT_THROW((void) encoder.generate_huffman_tree(false), std::domain_error);
207}
208
209} // namespace
210
Huffman coding for data compression.
Node for binary search tree.
Contiguous array of bits.
Definition bitArray.H:189
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Definition bitArray.H:334
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T & push(const T &item)
Definition htlist.H:1523
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
iterator end() noexcept
Return an STL-compatible end iterator.
#define TEST(name)
static mt19937 rng
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4060
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool areEquivalents(Node *t1, Node *t2, Equal &op) noexcept
Return true if trees are equivalents.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
static StlIterator begin(SetType &s)
Create an iterator positioned at the first element of the container.
static StlIterator end(SetType &s)
Create an end iterator for the container.
Utility functions for binary tree operations.