Initial compresison.
This commit is contained in:
parent
4fce4fc614
commit
1ee31596fb
11 changed files with 195 additions and 1 deletions
15
src/compression/CMakeLists.txt
Normal file
15
src/compression/CMakeLists.txt
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
list(APPEND compression_LIB_INCLUDES
|
||||
StreamCompressor.cpp
|
||||
HuffmanEncoder.cpp
|
||||
)
|
||||
|
||||
add_library(compression SHARED ${compression_LIB_INCLUDES})
|
||||
|
||||
target_include_directories(compression PUBLIC
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
)
|
||||
|
||||
target_link_libraries(compression PUBLIC core)
|
||||
set_property(TARGET compression PROPERTY FOLDER src)
|
||||
set_target_properties( compression PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
|
77
src/compression/HuffmanEncoder.cpp
Normal file
77
src/compression/HuffmanEncoder.cpp
Normal file
|
@ -0,0 +1,77 @@
|
|||
#include "HuffmanEncoder.h"
|
||||
|
||||
#include "Tree.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <queue>
|
||||
#include <tuple>
|
||||
#include <iostream>
|
||||
|
||||
void HuffmanEncoder::encode(const HuffmanEncoder::DataStream& stream)
|
||||
{
|
||||
std::unordered_map<unsigned char, unsigned> counts;
|
||||
for (auto c : stream)
|
||||
{
|
||||
counts[c]++;
|
||||
}
|
||||
|
||||
using CountPair = std::pair<unsigned char, unsigned>;
|
||||
auto cmp = [](CountPair left, CountPair right)
|
||||
{
|
||||
return left.second > right.second;
|
||||
};
|
||||
std::priority_queue<CountPair, std::vector<CountPair>, decltype(cmp)> q(cmp);
|
||||
for (const auto& entry : counts)
|
||||
{
|
||||
q.push({entry.first, entry.second});
|
||||
}
|
||||
|
||||
NodePtr<CountPair> lastNode;
|
||||
while(!q.empty())
|
||||
{
|
||||
const auto charData = q.top();
|
||||
auto characterNode = std::make_unique<Node<CountPair> >(charData);
|
||||
q.pop();
|
||||
|
||||
if (!lastNode)
|
||||
{
|
||||
const auto rightCharData = q.top();
|
||||
auto rightCharacterNode = std::make_unique<Node<CountPair> >(rightCharData);
|
||||
q.pop();
|
||||
|
||||
const auto sum = charData.second + rightCharData.second;
|
||||
CountPair data{0, sum};
|
||||
auto midNode = std::make_unique<Node<CountPair> >(data);
|
||||
|
||||
midNode->addChild(std::move(characterNode));
|
||||
midNode->addChild(std::move(rightCharacterNode));
|
||||
lastNode = std::move(midNode);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto sum = lastNode->getData().second;
|
||||
CountPair data{0, sum};
|
||||
auto midNode = std::make_unique<Node<CountPair> >(data);
|
||||
|
||||
if (charData.second < lastNode->getData().second)
|
||||
{
|
||||
midNode->addChild(std::move(lastNode));
|
||||
midNode->addChild(std::move(characterNode));
|
||||
}
|
||||
else
|
||||
{
|
||||
midNode->addChild(std::move(characterNode));
|
||||
midNode->addChild(std::move(lastNode));
|
||||
}
|
||||
lastNode = std::move(midNode);
|
||||
}
|
||||
}
|
||||
|
||||
Tree<CountPair> tree;
|
||||
tree.addRootNode(std::move(lastNode));
|
||||
|
||||
//using TableEntry = std::tuple<>
|
||||
|
||||
|
||||
std::cout << "********" << std::endl;
|
||||
}
|
11
src/compression/HuffmanEncoder.h
Normal file
11
src/compression/HuffmanEncoder.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
class HuffmanEncoder
|
||||
{
|
||||
using DataStream = std::vector<unsigned char>;
|
||||
|
||||
public:
|
||||
void encode(const DataStream& stream);
|
||||
};
|
0
src/compression/StreamCompressor.cpp
Normal file
0
src/compression/StreamCompressor.cpp
Normal file
6
src/compression/StreamCompressor.h
Normal file
6
src/compression/StreamCompressor.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
class StreamCompressor
|
||||
{
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue