- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在阅读有关此算法的信息...我编写了一个要压缩的类,但我还没有编写解压缩类的代码...
你觉得这段代码怎么样?
我想我遇到了一个问题......我的编码是:“位置|长度”,但我相信这种方法会让我在解压时遇到问题,因为我不知道位置和长度的数量是否是2、3、4 位数字...:S
一些建议将被接受...:D
我们会接受任何建议。
主文件:
#include <iostream>
#include "Compressor.h"
int main() {
Compressor c( "/home/facu/text.txt", 3);
std::cout << c.get_TEXT_FILE() << std::endl;
std::cout << c.get_TEXT_ENCONDED() << std::endl;
c.save_file_encoded();
return 0;
}
头文件:
#ifndef _Compressor_H_
#define _Compressor_H_
#include <utility>
#include <string>
typedef unsigned int T_UI;
class Compressor
{
public:
//Constructor
Compressor( const std::string &PATH, const T_UI minbytes = 3 );
/** GET BUFFERS **/
std::string get_TEXT_FILE() const;
std::string get_TEXT_ENCONDED() const;
/** END GET BUFFERS **/
void save_file_encoded();
private:
/** BUFFERS **/
std::string TEXT_FILE; // contains the text from an archive
std::string TEXT_ENCODED; // contains the text encoded
std::string W_buffer; // contains the string to analyze
std::string W_inspection; // contains the string where will search matches
/** END BUFFERS **/
T_UI size_of_minbytes;
T_UI size_w_insp; // The size of window inspection
T_UI actual_byte;
std::pair< T_UI, T_UI> v_codes; // Values to code text
// Utilitaries functions
void change_size_insp(){ size_w_insp = TEXT_FILE.length() ; }
bool inspection_empty() const;
std::string convert_pair() const;
// Encode algorythm
void lz77_encode();
};
#endif
执行文件:
#include <iostream>
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <string>
#include <cstdlib>
#include <sstream>
#include "Compressor.h"
Compressor::Compressor(const std::string& PATH, const T_UI minbytes)
{
std::string buffer = "";
TEXT_FILE = "";
ifstream input_text( PATH.c_str(), std::ios::in );
if( !input_text )
{
std::cerr << "Can't open the text file";
std::exit( 1 );
}
while( !input_text.eof() )
{
std::getline( input_text, buffer );
TEXT_FILE += buffer;
TEXT_FILE += "\n";
buffer.clear();
}
input_text.close();
change_size_insp();
size_of_minbytes = minbytes;
TEXT_ENCODED = "";
W_buffer = "";
W_inspection = "";
v_codes.first = 0;
v_codes.second = 0;
actual_byte = 0;
lz77_encode();
}
std::string Compressor::get_TEXT_FILE() const
{
return TEXT_FILE;
}
std::string Compressor::get_TEXT_ENCONDED() const
{
return TEXT_ENCODED;
}
bool Compressor::inspection_empty() const
{
return ( size_w_insp != 0 );
}
std::string Compressor::convert_pair() const
{
std::stringstream out;
out << v_codes.first;
out << "|";
out << v_codes.second;
return out.str();
}
void Compressor::save_file_encoded()
{
std::string path("/home/facu/encoded.txt");
ofstream out_txt( path.c_str(),std::ios::out );
out_txt << TEXT_ENCODED << "\n";
out_txt.close();
}
void Compressor::lz77_encode()
{
while( inspection_empty() )
{
W_buffer = TEXT_FILE.substr( actual_byte, 1);
if( W_inspection.find( W_buffer ) == W_inspection.npos )
{
// Cant find any byte from buffer
TEXT_ENCODED += W_buffer;
W_inspection += W_buffer;
W_buffer.clear();
++actual_byte;
--size_w_insp;
}
else
{
// We founded any byte from buffer in inspection
v_codes.first = W_inspection.find( W_buffer );
v_codes.second = 1;
while( W_inspection.find( W_buffer ) != W_inspection.npos )
{
++actual_byte;
--size_w_insp;
v_codes.second++;
W_inspection += TEXT_FILE[actual_byte - 1];
W_buffer += TEXT_FILE[actual_byte];
}
++actual_byte;
--size_w_insp;
if( v_codes.second > size_of_minbytes )
TEXT_ENCODED += convert_pair();
else
TEXT_ENCODED += W_buffer;
W_buffer.clear();
}
}
}
谢谢!
我在编写解压缩类的代码:)
最佳答案
我一般建议先写解压器,再写压缩器与之匹配。
我建议先让压缩器和相应的解压缩器处理固定大小的拷贝项目,然后才——如果需要——调整它们以生成/使用可变大小的拷贝项目。
许多类似 LZ77 的算法在压缩文件中使用固定大小来表示位置和长度;通常1个16进制的长度和3个16进制的位置,共2个字节。
“|”位置和复制长度之间是不必要的。
如果你真的想实现原始的 LZ77 算法,您的压缩算法需要始终发出固定长度的复制长度(即使它为零)、固定长度的位置(当长度为零时,您也可以在此处粘贴零)和固定长度的文字值(value)。
一些类似 LZ77 的文件格式分为“项目”,这些项目要么是固定长度的复制长度、位置对,要么是一个或多个文字值。如果你走那条路,压缩器必须首先以某种方式告诉解压缩器即将到来的项目是代表文字值还是复制长度,位置对。许多方法之一是保留一个特殊的“0”位置值,而不是像所有其他位置值一样指示输出解压缩流中的某个位置,而是指示输入压缩文件中接下来的几个文字值。
几乎所有类似 LZ77 的算法都存储明文中从当前位置向后的“偏移量”,而不是从明文开头向前的“位置”。例如,“1”表示最近解码的明文字节,而不是第一个解码的明文字节。
当压缩文件包含一系列整数时,解码器如何判断一个整数在哪里结束,下一个整数从哪里开始?有 3 个流行的答案:
https://en.wikibooks.org/wiki/Data_Compression
Jacob Ziv 和 Abraham Lempel; A Universal Algorithm for Sequential Data Compression , IEEE Transactions on Information Theory,23(3),pp.337-343,1977 年 5 月。
关于c++ - LZ77 - 算法 - 分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5172588/
我正在使用 crc32(initial_crc_value,data,data_length); 生成一个 crc32 值。如果我没有在链接器选项中使用 -lz,我将收到链接器错误 "undefine
我正在尝试弄清楚如何证明用于压缩的 Lempel ZIV 77 算法确实提供了最佳压缩。 我找到了以下信息: So how well does the Lempel-Ziv algorithm wor
LZ-string 的哪种类型的 mysql 数据压缩结果?在我的数据库中,此变量为 MEDIUMTEXT 类型和排序规则 UTF8bin。使用post方法(asp.net core web serv
对于 xcode 中的其他链接器标志,-lz 意味着什么。我经常看到它与 -Objc 一起使用,或者它也可以独立使用吗?它的用途是什么?我正在使用 xcode 7.0.0 最佳答案 @Mozilla
我有兴趣实现用于压缩 ECG 信号的 LZ 算法,并希望优化与微 Controller 相关的代码。 这样压缩和解压缩 ECG 信号的熵效率更高,花费的时间更少。我完全不知道我是如何实现这一目标的。我
我正在尝试使用 quickfix 库编译代码: g++ -o main tradeclient.cpp Application.cpp -std=c++11 -fexceptions -finline
error: Ld /Users/mymac/Library/Developer/Xcode/DerivedData/Fitness-drawjjdksjmgttbwvhjuzicescoz/Buil
这让我发疯,当我尝试在模拟器上编译时,一切正常,但在设备上我遇到了这个错误: ld: library not found for -lz Command /Developer/Platforms/iP
我正在尝试在 Ubuntu 10.04 下编译 Android 源代码。我收到一条错误消息, /usr/bin/ld: cannot find -lz 你能告诉我如何解决它吗? cannot find
您好,我想在我的项目中使用 Google Maps API,我已经添加了 libz.dylib。当我在其他链接器标志中编写 -ObjC 以链接 Google Maps API 时,它工作正常但是当我删
我需要计算二进制字符串的 LZ 复杂度。 LZ 复杂度是从头到尾查看流时遇到的差异子串的数量。例如: s = 1001111011000010 在不同子串中标记序列复杂度c(s) = 6:s = 1/
在 ubuntu-13.04 上,使用 linux 发行版提供的 GCC-4.7.3 从共享库构建可执行文件时出现错误。 我猜问题出在libpng和zlib之间(前者使用后者),但我不知道为什么。 首
我知道这些是一些常见/基本的库,但它们到底是什么意思? 例如,我知道 -lm是一些数学库,但这是标准数学库还是什么? -lz压缩?什么压缩? 我不知道 -lrt 是什么是。 这些是什么东西? 数学库。
我想安装 uwsgi 但我无法安装。它是 Fedora 18 64 位系统。 python 是 2.7 版,yum install zlib 说:Package zlib-1.2.7-9.fc18.x
如果我解压缩 initrd.lz 然后重新打包而不做任何更改,然后将它替换为 Casper 目录中的新 initrd.lz for live CD。它是结构化的。在打包 initrd 时,我收到如下警
我按此顺序链接 CodeBlocks 中的库, -lz -L/usr/local/lib -L/usr/local/include -pthread -lswscale -lavutil -lavco
我不明白我的错误: LdLibrary/Developer/Xcode/DerivedData/Test-aywxyvnakaqhmwfbwellynwqmoik/Build/Products/Deb
我正在使用 lz-string.js 的 LZString.compressToBase64 函数并且需要在服务器端对数据进行解压/压缩。 明显的解决方案似乎是 lz_string_csharp但我很
尝试为 iOS 5 编译软件时,XCode 4.2 抛出错误: ld: library not found for -lz.1.2.3 我发现这篇文章告诉我要替换 1.2.3。与 1.2.5 http
我正在尝试从源代码“openssl-1.0.1j”进行编译,但失败并显示消息: “/usr/bin/ld: 找不到 -lz” 树莓派上的环境是 debian wheezy。 我的文件树: ├── zl
我是一名优秀的程序员,十分优秀!