클래스에서 우리는 RLE에 대해 이야기했고 우리 교수는 다음 코드를 보여주었습니다. 나는 그것을 이해하려고 노력했지만, 나는 그것을 이해하지 못했습니다. 누군가가 내게이 예제에서 RLE가 어떻게 작동하는지 설명 할 수 있다면 매우 감사 할 것입니다. 데이터 압축 방법을 이해하고 있지만 프로그램 구현을 이해하지 못합니다. 의견에는 내 질문이 있습니다.런 길이 인코딩 (정수)
// Example implementation of a simple variant of // run-length encoding and decoding of a byte sequence
#include <iostream>
#include <cassert>
// PRE: 0 <= value <= 255
// POST: returns true if value is first byte of a tuple, otherwise false
bool is_tuple_start(const unsigned int value)
{
assert(0 <= value && value <= 255);
return value >= 128; //Why is it: value>=128 for first Byte of tuple?
}
// PRE: 1 <= runlength <= 127 //Why must runlength be in this range?
// POST: returns encoded runlength byte
unsigned int make_tuple_start(const unsigned int run_length)
{
assert(1 <= run_length && run_length <= 127);
return run_length + 128; //Why do I add 128?
}
// PRE: n/a
// POST: returns true if value equals the maximal run-length
bool is_max_runlength(const unsigned int value)
{
return value == 127; //same question: why is max. range 127?
}
// PRE: 128 <= value <= 255 //Why this range for value?
// POST: returns runlength of tuple
unsigned int get_runlength(const unsigned int value)
{
assert(128 <= value && value <= 255);
return value - 128; //Why -128?
}
// PRE: n/a
// POST: outputs value and adds a newline
void out_byte(const unsigned int value)
{
std::cout << value << "\n";
}
// PRE: 1 <= runlength <= 127 and 0 <= value <= 255
// POST: outputs run length encoded bytes of tuple
void output(const unsigned int run_length, const unsigned int value)
{
assert(1 <= run_length && run_length <= 127);
assert(0 <= value && value <= 255); //Why is value now between 0 and 255?
if (run_length == 1 && !is_tuple_start(value))
{
out_byte(value);
}
else
{
out_byte(make_tuple_start(run_length));
out_byte(value);
}
}
// PRE: n/a
// POST: returns true if 0 <= value <= 255, otherwise false
bool is_byte(const int value)
{
return 0 <= value && value <= 255;
}
// PRE: n/a
// POST: outputs error if value does not indicate end of sequence
void check_end_of_sequence(const int value)
{
if (value != -1)
{
std::cout << "error\n";
}
}
// PRE: n/a
// POST: reads byte sequence and outputs encoded bytes
void encode()
{
std::cout << "--- encoding: enter byte sequence, terminate with -1\n";
int value;
std::cin >> value;
if (is_byte(value))
{
int prev_value = value; //When/Where does value Change?
unsigned int run_length = 1;
while(true)
{
// read next byte, stop if invalid or end of sequence
std::cin >> value;
if (!is_byte(value))
{ break; }
// output if value has changed or maximal runlength is reached
// otherwise increase length of current run
if (value != prev_value || is_max_runlength(run_length))
{
output(run_length, prev_value);
run_length = 1;
prev_value = value;
}
else { ++run_length; }
}
output(run_length, prev_value);
}
// output "error" if sequence terminated incorrectly
check_end_of_sequence(value);
}
// PRE: n/a
// POST: reads byte sequence and outputs decoded bytes
void decode()
{
std::cout << "--- decoding: enter byte sequence, terminate with -1\n";
int value;
while(true) {
// read next byte, stop if invalid or end of sequence
std::cin >> value; //is value only a Byte? Or the whole sequence?
if (!is_byte(value))
{ break; }
// if this is a tuple output read next byte, otherwise output directly
if (is_tuple_start(value))
{
unsigned int run_length = get_runlength(value);
// next must be a valid byte, otherwise this is an error
std::cin >> value;
if (!is_byte(value))
{
value = 0;
// trigger error in case value = -1
break;
}
// output uncompressed tuple
for(int i = 0; i < run_length; ++i)
{
out_byte(value);
}
}
else { out_byte(value); }
}
// output "error" if sequence terminated incorrectly
check_end_of_sequence(value);
}
int main(const int argc, const char* argv[])
{
std::cout << "--- select mode: 0 = encode/1 = decode\n";
unsigned int mode;
std::cin >> mode;
if (mode == 0)
{
encode();
}
else if (mode == 1)
{
decode();
}
else
{
std::cout << "--- unknown mode, must be 0 (encode) or 1 (decode)\n";
}
}
내 질문에 대한 답변을 얻고 코드가 기본적으로 복사하여 붙여 넣을 수 있기를 바랍니다. 반복되지 않는 값이 단순히 저장하는 동안
<length> <value>
:이 인코딩 작동
입력 예 : 인코딩 : 0 42 42 85 85 85 172 172 172 13 13 42 -1 및 디코드 : 1 2 42 4 85 3 172 2 13 1 42 -1 – Viviane
질문에 추가하십시오 , 논평이 아니라. – Barmar