- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
代码已更新为使用 unique_ptr 和命名空间。注意:我试图在命名空间 huffman 中实现匿名命名空间,但它不允许将文件分成 .cpp 和 .h。欢迎对当前代码提出任何批评。请随意使用 MIT 协议(protocol)中规定的代码。
源代码.cpp:
/*
#######################################################################################################################################
Copyright 2017 Daniel Rossinsky
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#######################################################################################################################################
*/
#include"Huffman.h"
int main(int argc, char *argv[]) {
if (argc < 4) std::cout << "Too few arguments\n";
else if (argc == 4) {
if (*argv[1] == 'c') Huffman::compress(argv[2], argv[3], argv[3]);
else if (*argv[1] == 'd') {
std::string temp{ argv[2] };
std::size_t pathEnd{ temp.find_last_of("/\\") };
Huffman::decompress(argv[2], argv[3], temp.substr(0, pathEnd + 1));
}//end of else if
else std::cout << "Unknown command\n";
}//end of else if
else std::cout << "Too much arguments\n";
return 0;
//Huffman::compress("C:/Users/User/Desktop/test.txt", "C:/Users/User/Desktop/", "C:/Users/User/Desktop/");
//Huffman::decompress("C:/Users/User/Desktop/testCompressed.bin", "C:/Users/User/Desktop/testKey.bin", "C:/Users/User/Desktop/");
}
/*
cmd example:
-----------
compress:
syntax: huffman.exe c filePath dest
example: C:/Users/User/Desktop/huffman.exe c C:/Users/User/Desktop/test.txt C:/Users/User/Desktop/
decompress:
syntax: huffman.exe d filePath keyPath
example: C:/Users/User/Desktop/huffman.exe d C:/Users/User/Desktop/testCompressed.bin C:/Users/User/Desktop/testKey.bin
NOTE:
-----
You can use the commented code in main instead
*/
哈夫曼.h:
#ifndef HUFFMAN
#define HUFFMAN
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<deque>
#include<memory>
namespace Huffman {
namespace inner {
struct node;
/*type aliases*/
using Table = std::map<char, std::size_t>;
using Cypher = std::map<char, std::vector<bool> >;
using smartNode = std::unique_ptr<node>;
/*type aliases*/
struct node {
smartNode m_left;
smartNode m_right;
std::size_t m_frequency{};
char m_data{};
node() = default;
node(smartNode left, smartNode right) :
m_left{ std::move(left) }, m_right{ std::move(right) } {
m_frequency = m_left->m_frequency + m_right->m_frequency;
}
};
struct functor {
bool operator()(smartNode const& first, smartNode const& second) const
{
return first->m_frequency > second->m_frequency;
}
};
/*shared functions*/
smartNode makeTree(std::deque<smartNode>& nodeData);
void readFile(const std::string& filePath, std::string& fileContent);
std::deque<smartNode> storeFreqTable(const Table& table);
/*shared functions*/
/*compressor related functions*/
void setNameAndExten(const std::string& filePath, std::string& fileName, std::string& fileExten);
void UpdateFreqTable(Table& freqTable, const std::string& fileContent);
void encode(smartNode const &root, Cypher& key, std::vector<bool>& code);
void createBinaryFile(const std::string& filePath,
const std::string& fileName,
const std::string& fileContent,
Cypher& key,
std::vector<bool>& code);
void createKey(const std::string& filePath,
const Table& freqTable,
const std::string& fileName,
const std::string& fileExten);
/*compressor related functions*/
/*decompressor related functions*/
void readKey(Table& freqTable,
std::string& fileExten,
const std::string keyPath,
std::string& fileContent);
std::size_t decodedContentSize(const Table& freqTable);
void decode(const std::string& filePath,
std::string& decodedContent,
smartNode root,
std::string& fileName,
std::string& fileContent);
void createFile(const std::string& decodedContent,
const std::string& locToDecompress,
const std::string& fileName,
const std::string& fileExten);
/*decompressor related functions*/
}//end of inner namespace
void compress(const std::string& filePath, const std::string& locToCreateKey, const std::string& locToCompress);
void decompress(const std::string& filePath, const std::string& keyPath, const std::string& locToDecompress);
}//end of Huffman namespace
#endif
哈夫曼.cpp:
#include"Huffman.h"
#include<fstream>
#include<sstream>
#include<algorithm>
#include<cstdlib>
/*----------------SHARED_FUNCTIONS_START----------------*/
Huffman::inner::smartNode Huffman::inner::makeTree(std::deque<smartNode>& nodeData) {
while (nodeData.size() > 1) {
std::sort(nodeData.begin(), nodeData.end(), functor());
smartNode leftSon{ std::move(nodeData.back()) };
nodeData.pop_back();
smartNode rightSon{ std::move(nodeData.back()) };
nodeData.pop_back();
smartNode parent = std::make_unique<node>(std::move(leftSon), std::move(rightSon));
nodeData.emplace_back(std::move(parent));
}//end of while loop
return std::move(nodeData.front());
}
void Huffman::inner::readFile(const std::string& filePath, std::string& fileContent) {
std::ifstream inFile(filePath, std::ios::binary);
if (inFile.is_open()) {
auto const start_pos{ inFile.tellg() };
inFile.ignore(std::numeric_limits<std::streamsize>::max());
std::streamsize char_count{ inFile.gcount() };
inFile.seekg(start_pos);
fileContent = std::string(static_cast<std::size_t>(char_count), '0');
inFile.read(&fileContent[0], static_cast<std::streamsize> (fileContent.size()));
inFile.close();
}//end of if
else {
std::cout << "Unable to open file\n";
std::exit(EXIT_FAILURE);
}//end of else
}
std::deque<Huffman::inner::smartNode> Huffman::inner::storeFreqTable(const Table& table) {
std::deque<smartNode> nodeData;
for (const auto& index : table) {
smartNode leaf = std::make_unique<node>();
leaf->m_data = index.first;
leaf->m_frequency = index.second;
nodeData.emplace_back(std::move(leaf));
}//end of for loop
return nodeData;
}
/*-----------------SHARED_FUNCTIONS_END-----------------*/
/*-----------------COMPRESSOR_FUNCTIONS_START-----------------*/
void Huffman::inner::setNameAndExten(const std::string& filePath,
std::string& fileName,
std::string& fileExten) {
std::size_t foundName{ filePath.find_last_of("/\\") };
std::size_t foundExten{ filePath.find_last_of('.') };
fileName = filePath.substr(foundName + 1, foundExten - foundName - 1);
fileExten = filePath.substr(foundExten);
}
void Huffman::inner::UpdateFreqTable(Table& freqTable, const std::string& fileContent) {
for (const auto& data : fileContent) {
++freqTable[data];
}//end of for loop
}
void Huffman::inner::encode(smartNode const &root,
Cypher& key,
std::vector<bool>& code) {
if (root->m_left != nullptr) {
code.emplace_back(false);
encode(std::move(root->m_left), key, code);
}//end of if
if (root->m_right != nullptr) {
code.emplace_back(true);
encode(std::move(root->m_right), key, code);
}//end of if
if (root->m_data) key[root->m_data] = code;
if (!code.empty()) code.pop_back();
}
void Huffman::inner::createBinaryFile(const std::string& filePath,
const std::string& fileName,
const std::string& fileContent,
Cypher& key,
std::vector<bool>& code) {
int offSet{}; int tempBuff{}; int inBuff{};
std::ofstream outFile(filePath + fileName + "Compressed.bin", std::ios::binary);
if (outFile.is_open()) {
for (const auto& data : fileContent) {
tempBuff = data;
code = key[static_cast<char>(tempBuff)];
for (const auto& index : code) {
inBuff |= index << (7 - offSet);
++offSet;
if (offSet == 8) {
offSet = 0;
outFile.put(static_cast<char>(inBuff));
inBuff = 0;
}//end of if
}//end of for loop
}//end of for loop
outFile.close();
}//end of if
else {
std::cout << "Unable to open file\n";
std::exit(EXIT_FAILURE);
}//end of else
}
void Huffman::inner::createKey(const std::string& filePath,
const Table& freqTable,
const std::string& fileName,
const std::string& fileExten) {
std::ofstream outFile(filePath + fileName + "Key.bin", std::ios::binary);
if (outFile.is_open()) {
auto&& index{ freqTable.begin() };
do {
outFile.put(index->first);
outFile.put(' ');
outFile << std::to_string(index->second);
++index;
if (index != freqTable.end()) outFile.put(' ');
} while (index != freqTable.end());
outFile << fileExten;
outFile.close();
}//end of if
else {
std::cout << "Unable to open file\n";
std::exit(EXIT_FAILURE);
}//end of else
}
/*------------------COMPRESSOR_FUNCTIONS_END------------------*/
/*-----------------DECOMPRESSOR_FUNCTIONS_START-----------------*/
void Huffman::inner::readKey(Table& freqTable,
std::string& fileExten,
const std::string keyPath,
std::string& fileContent) {
char buffer{};
std::string freq{};
readFile(keyPath, fileContent);
for (std::size_t index{}; index < fileContent.length(); ++index) {
buffer = fileContent[index];
index += 2;
do {
freq += fileContent[index];
++index;
} while ((fileContent[index] != ' ') && (fileContent[index] != '.'));
if (fileContent[index] == '.') {
fileExten = fileContent.substr(index, (fileContent.length() - 1));
index = fileContent.length();
}//end of if
else {
freqTable[buffer] = static_cast<unsigned int>(std::stoi(freq));
freq.clear();
}//end of else
}//end of for
freqTable[buffer] = static_cast<unsigned int>(std::stoi(freq));
fileContent.clear();
fileContent.shrink_to_fit();
}
std::size_t Huffman::inner::decodedContentSize(const Table& freqTable) {
std::size_t size{};
for (const auto& index : freqTable) size += index.second;
return size;
}
void Huffman::inner::decode(const std::string& filePath,
std::string& decodedContent,
smartNode root,
std::string& fileName,
std::string& fileContent) {
node* temp = root.get();
int offSet{}; int inBuff{};
std::size_t foundName{ filePath.find_last_of("/\\") };
fileName = filePath.substr(foundName + 1, filePath.find_last_of('C') - foundName - 1);
readFile(filePath, fileContent);
for (const auto& data : fileContent) {
inBuff = data;
while (offSet < 8) {
if (inBuff & (1 << (7 - offSet))) temp = temp->m_right.get();
else temp = temp->m_left.get();
if (temp->m_data) {
decodedContent += temp->m_data;
temp = root.get();
}//end of if
++offSet;
}//end of while
offSet = 0;
}//end of for
}
void Huffman::inner::createFile(const std::string& decodedContent,
const std::string& locToDecompress,
const std::string& fileName,
const std::string& fileExten) {
std::ofstream outFile(locToDecompress + fileName + fileExten, std::ios::binary);
if (outFile.is_open()) {
outFile.write(&decodedContent[0], static_cast<std::streamsize>(decodedContent.size()));
outFile.close();
}//end of if
else {
std::cout << "Unable to open file\n";
std::exit(EXIT_FAILURE);
}//end of else
}
/*------------------DECOMPRESSOR_FUNCTIONS_END------------------*/
void Huffman::compress(const std::string& filePath,
const std::string& locToCreateKey,
const std::string& locToCompress) {
std::string fileName;
std::string fileExten;
Huffman::inner::setNameAndExten(filePath, fileName, fileExten);
std::string fileContent;
Huffman::inner::readFile(filePath, fileContent);
Huffman::inner::Table freqTable;
Huffman::inner::UpdateFreqTable(freqTable, fileContent);
Huffman::inner::smartNode root = Huffman::inner::makeTree(Huffman::inner::storeFreqTable(freqTable));
Huffman::inner::Cypher key;
std::vector<bool> code;
encode(root, key, code);
Huffman::inner::createBinaryFile(locToCompress, fileName, fileContent, key, code);
Huffman::inner::createKey(locToCreateKey, freqTable, fileName, fileExten);
}
void Huffman::decompress(const std::string& filePath,
const std::string& keyPath,
const std::string& locToDecompress) {
Huffman::inner::Table freqTable;
std::string fileExten;
std::string fileContent;
Huffman::inner::readKey(freqTable, fileExten, keyPath, fileContent);
Huffman::inner::smartNode root = Huffman::inner::makeTree(Huffman::inner::storeFreqTable(freqTable));
std::string fileName;
std::string decodedContent;
decodedContent.reserve(Huffman::inner::decodedContentSize(freqTable));
decode(filePath, decodedContent, std::move(root), fileName, fileContent);
Huffman::inner::createFile(decodedContent, locToDecompress, fileName, fileExten);
}
最佳答案
编辑:固定功能:
decode(const std::string& filePath,
std::string& decodedContent,
smartNode root,
std::string& fileName,
std::string& fileContent) {
node* temp = root.get();
int offSet{}; int inBuff{};
std::size_t foundName{ filePath.find_last_of("/\\") };
fileName = filePath.substr(foundName + 1, filePath.find_last_of('C') - foundName - 1);
readFile(filePath, fileContent);
for (const auto& data : fileContent) {
inBuff = data;
while (offSet < 8) {
if (inBuff & (1 << (7 - offSet))) temp = temp->m_right.get();
else temp = temp->m_left.get();
if (temp->m_data) {
decodedContent += temp->m_data;
temp = root.get();
}//end of if
++offSet;
}//end of while
offSet = 0;
}//end of for
}
关于c++ - 哈夫曼压缩器/解压器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912472/
我希望通过扫描线为 x 的每个值找到 y 的值来绘制椭圆。 对于普通椭圆,公式很容易找到:y = Sqrt[b^2 - (b^2 x^2)/a^2] 但是当椭圆的轴旋转时,我一直无法弄清楚如何计算 y
假设我有这个矩阵: 1 1 1 | 1 0 0 1 | 1 这个系统显然有无限的解决方案。 x1 = -x2 x3 = 1 x1 依赖于 x2,x2 是免费的,但我感兴趣的是 x3。是否有一种算法可以
我正在考虑使用神经网络在我正在构建的太空射击游戏中为我的敌人提供动力,我想知道;当网络没有一个明确的好的输出集时,你如何训练神经网络? 最佳答案 我目前正在研究神经网络,如果没有明确定义的输入和输出编
我需要一个针对受限资源环境(例如具有以下特征的二进制(十六进制数据)嵌入式系统)进行优化的快速解压缩例程: 数据面向 8 位(字节)(数据总线为 8 位宽)。 字节值的范围并不统一为 0 - 0xFF
PHP代码: $txt="John has cat and dog."; //plain text $txt=base64_encode($txt); //base64 encode $txt=gzd
程序从用户那里接收到一个正数k,并且应该检查方程有多少解 3*x+5*y=k 在许多解决方案的情况下,该函数采用所有解决方案中 |x-y| 的较大绝对值。如果只有一种解决方案,它会打印出来。例如: 如
我必须求解以下微分方程: 或 如果没有 F_1 术语,代码就很简单。但我无法用包含 F_1 项来解决它,尽管我知道解决方案应该看起来像阻尼谐振。 from scipy.integrate import
我知道这个问题是前缀和的变体,我只是在设置它时遇到了一些困难。 最佳答案 定义: P[i] = A[i+1] + A[i+2] + ... + A[n] Q[i] = A[1] + ... + A[i
在许多在线示例中,文件在 Java 中使用编码缓冲区进行(解)压缩。然而,对于 NIO,无需选择一个好的缓冲区大小。我找到了文件和套接字的示例,但是是否有用于压缩输入的 NIO channel (例如
我有一个形式为 A*x = B 的方程组,其中 [A] 是一个三对角系数矩阵。使用 Numpy 求解器 numpy.linalg.solve 我可以求解 x 的方程组。 请参阅下面的示例,了解我如何开
我试图回答这个问题,只使用递归(动态编程) http://en.wikipedia.org/wiki/Longest_increasing_subsequence 从这篇文章中,我意识到最有效的现有解
解决此问题的方法是,按照我发帖的其中一项建议,将DLL添加到GAC中。正如我在我的一份答复中所指出的那样,在需要运行此过程的环境中,可伸缩性将不可用。因此,不能选择简单的解决方案。为了解决这个问题,我
是否有专门描述 AAC-LC 标准的规范,以及实现编解码器的现实目标,而不是通用编解码器,而是针对特定 AAC-LC 格式,具有预定义的 channel 数和采样率? 是否有一些针对 AAC-LC 的
我想使用通用的“p”来定义多路复用器将有多少输出。输入和所有输出均为 1 位。输出、控制和输入可以很简单,例如: signal control : std_logic_vector(log 2 p
我正在尝试在 javascript 中使用一些三 Angular 函数来定位一些菱形 div,但似乎我的逻辑在某处失败了。 你可以看到我尝试了这个公式:pos + trig * dimension。我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
我一直在考虑这两个 JSON 库: 谷歌 Gson JSON.Simple XStream Google Gson 非常棒,它可以序列化具有无参数构造函数的类对象。 JSON.Simple 非常简洁,
使用 Gekko 拟合数据的数值 ODE 解。 嗨,大家好! 我想知道是否可以使用 GEKKO 拟合 ODE 的系数。 我尝试复制 example given here 失败. 这是我想出的(但有缺陷
众所周知,ASCII使用7位来编码字符,所以用来表示文本的字节数总是小于文本字母的长度 例如: StringBuilder text = new StringBuilder(); In
我找到了一个 link其中显示了一个示例,当线性方程组有无限多个解时,Matlab mldivide 运算符 (\) 给出“特殊”解。 例如: A = [1 2 0; 0 4 3]; b = [8;
我是一名优秀的程序员,十分优秀!