Skip to content

Commit c598c42

Browse files
committed
Optimized MTF (Encode/Decode in-place instead of allocating new memory)
1 parent 0d768ad commit c598c42

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Compressors/MTF.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@ class MTF {
3737

3838
generateSymbols();
3939

40-
std::string encoded;
41-
for (uint8_t symbol : toBeEncoded) {
40+
for (auto& symbol : toBeEncoded) {
4241
auto ptr = getIndexOfValue(symbol);
43-
encoded += ptr.index;
42+
symbol = ptr.index; // Encode in-place
4443
symbolsList.erase(ptr.iterator);
45-
symbolsList.push_front(symbol);
44+
symbolsList.push_front(ptr.value);
4645
}
4746

4847
remove(outputFileName.c_str()); // Remove Output File If Exists
49-
BinaryIO::write(outputFileName, encoded);
48+
BinaryIO::write(outputFileName, toBeEncoded);
5049

5150
}
5251

@@ -56,16 +55,16 @@ class MTF {
5655

5756
generateSymbols();
5857

59-
std::string decoded;
60-
for (uint8_t index : toBeDecoded) {
58+
for (auto& byte : toBeDecoded) {
59+
uint8_t index = byte;
6160
auto ptr = getValueOfIndex(index);
62-
decoded += ptr.value;
61+
byte = ptr.value; // Decode in-place
6362
symbolsList.erase(ptr.iterator);
6463
symbolsList.push_front(ptr.value);
6564
}
6665

6766
remove(outputFileName.c_str()); // Remove Output File If Exists
68-
BinaryIO::write(outputFileName, decoded);
67+
BinaryIO::write(outputFileName, toBeDecoded);
6968

7069
}
7170

0 commit comments

Comments
 (0)