나는이 알고리즘에 대해 읽고 있었다 ... 그리고 난 아직 압축 해제 클래스를 코딩하지 않은 압축하는 클래스를 코딩?LZ77 - 알고리즘 - 당신이 코드에 대해 어떻게 생각하십니까</p> <p>... 해상도
나는 문제가 생겼다고 생각한다. 나의 성문화는 다음과 같다. "position | length"하지만 위치와 길이의 숫자가 맞는지 알지 못하기 때문에이 방법으로 문제가 풀릴 것이라고 믿는다. ... 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();
}
}
}
감사합니다!
임은 descompressing 클래스를 코딩 :
'위치 | 길이 | - 또는 길이를 고정 너비 필드로 만드시겠습니까? –
그래, 나도 그걸 생각하고 있었어. 또한 가장 좋은 방법은 파일에 구조가있는 "aleatory file"을 사용하는 것이라고 생각한다. – fpointbin