aboutsummaryrefslogtreecommitdiffstats
path: root/consteval_huffman.hpp
diff options
context:
space:
mode:
authorclyne <clyne@bitgloo.com>2020-12-27 19:03:37 -0500
committerGitHub <noreply@github.com>2020-12-27 19:03:37 -0500
commit4a81241a82a26c1695150ab661db568993e7fef6 (patch)
tree42647a4034558462e96129170b91471fd9218f36 /consteval_huffman.hpp
parentcfbe244e902093fae5250f6369af1fd9772763ca (diff)
Optimized decoder increment
Diffstat (limited to 'consteval_huffman.hpp')
-rw-r--r--consteval_huffman.hpp20
1 files changed, 13 insertions, 7 deletions
diff --git a/consteval_huffman.hpp b/consteval_huffman.hpp
index 5978e75..a1517ec 100644
--- a/consteval_huffman.hpp
+++ b/consteval_huffman.hpp
@@ -9,6 +9,7 @@
#include <algorithm>
#include <span>
+#include <string>
/**
* Compresses the given data string using Huffman coding, providing a
@@ -244,7 +245,7 @@ public:
if constexpr (bytes_saved() > 0) {
const auto [size_bytes, last_bits] = m_data->compressed_size_info();
m_pos = size_bytes - 1;
- m_bit = 8 - last_bits;
+ m_bit = 1 << (7 - last_bits);
} else {
m_pos = data_length + 1;
}
@@ -253,7 +254,7 @@ public:
}
bool operator==(const decode_info& other) const {
- return m_data == other.m_data && m_bit == other.m_bit && m_pos == other.m_pos;
+ return m_bit == other.m_bit && m_pos == other.m_pos;
}
auto operator*() const {
return m_current;
@@ -272,12 +273,17 @@ public:
void get_next() {
if constexpr (bytes_saved() > 0) {
auto *node = m_data->decode_tree;
+ auto pos = m_pos;
+ auto bit = m_bit;
do {
- bool bit = m_data->compressed_data[m_pos] & (1 << (m_bit - 1));
- if (--m_bit == 0)
- m_bit = 8, m_pos++;
- node += 3 * node[bit ? 2 : 1];
+ auto idx = (m_data->compressed_data[pos] & bit) ? 2u : 1u;
+ node += node[idx] * 3u;
+ bit >>= 1;
+ if (!bit)
+ bit = 0x80, pos++;
} while (node[1] != 0);
+ m_pos = pos;
+ m_bit = bit;
m_current = *node;
} else {
m_current = data[m_pos++];
@@ -286,7 +292,7 @@ public:
const huffman_compress<data> *m_data = nullptr;
size_t m_pos = 0;
- unsigned char m_bit = 8;
+ unsigned char m_bit = 0x80;
int m_current = -1;
friend class huffman_compress;