2011-10-17 3 views
0

"AA1B7B7B7B7"과 같은 시퀀스를 "# A12 # B74"와 같은 문자열로 인코딩하는 RLE 인코딩 기능을 만들었습니다.RLE : 두 기호로 인코딩

void encode(const char *input_path, const char *output_path) 
    { // Begin of SBDLib::SBIMask::encode 
    std::fstream input(input_path, std::ios_base::in | std::ios_base::binary); 
    std::fstream output(output_path, std::ios_base::out | std::ios_base::binary); 
    int size = 0; // Set size variable 
    input.seekg(0, std::ios::end); // Move to EOF 
    size = input.tellg(); // Tell position 
    input.seekg(0); // Move to the beginning 
    int i = 1; // Create encoding counter 
    int counter = 0; // Create color counter 
    int cbyte1, cbyte2; // Create current color bytes 
    int pbyte1 = 0x0; int pbyte2 = 0x0; // Create previous color bytes 
    while (((cbyte1 = input.get()) != EOF && (cbyte2 = input.get()) != EOF) 
      || input.tellg() >= size) 
    { // Begin of while 
     // If current bytes are not equal to previous bytes 
     // or cursor is at the end of the input file, write 
     // binary data to file; don't do it if previous bytes 
     // were not set from 0x0 to any other integer. 
     if (((cbyte1 != pbyte1 || cbyte2 != pbyte2) 
     || (input.tellg() == size)) 
     && (pbyte1 != 0x0 && pbyte2 != 0x0)) 
     { // Begin of main if 
      output << SEPARATOR; // Write separator to file 
      output.write(reinterpret_cast<const char*>(&pbyte1), 1); 
      output.write(reinterpret_cast<const char*>(&pbyte2), 1); 
      output << std::hex << counter; // Write separator, bytes and count 
      counter = 1; // Reset counter 
     } // End of main if 
     else counter++; // Increment counter 
     pbyte1 = cbyte1; pbyte2 = cbyte2; // Set previous bytes 
    } // End of main while 
    } // End of encode 

그러나 기능이 필요한만큼 빠릅니다. 이것은 함수의 두 번째 버전입니다. 이미 더 빨리 만들 수 있도록 개선했지만 너무 느립니다. 개선 할 아이디어가 있습니까? 나는 아이디어가 부족합니다.

답변

0

파일에서 읽는 데이터의 크기에 따라 단일 charcaters를 읽지 않는 것이 좋지만 입력 파일에서 한 번에 데이터 덩어리를 읽는 것이 좋습니다. 이것은 각 입력 문자에 대해 디스크의 입력 파일에 액세스하는 것보다 훨씬 빠릅니다.

의사 코드 예제 : 나는 큰 텍스트 파일을 읽을 필요

char dataArray[100]; 

while(!EOF) 
{ 
    input.get(&dataArray[0], 100); // read a block of data not a single charater 

    process(dataArray); // process one line 
} 
+0

; 평균 체중은 ~ 15 MiB입니다. 청크 읽기 예제 코드를 제공해 주시겠습니까? 나는 네가 의미하는 것을 이해하지 못한다. – ghostmansd

+0

내 모든 파일은 한 줄로 구성되어 있습니다. – ghostmansd

+0

다음에는 한 번에 읽을 수있는 적절한 문자 크기를 지정하십시오. http://www.cplusplus.com/reference/iostream/istream/get/ 문서를 살펴보십시오. –