From e54e5fa07d47bff24bebb9131e6a4b761d3144f4 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Mon, 11 Jun 2018 00:58:01 +0200 Subject: [PATCH] Bit-unpacking FIXEDDDDDDDDDDDD --- src/bitpack.cc | 24 +++++++++++++++++++++++- src/common.cc | 43 ++++++++++++------------------------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/bitpack.cc b/src/bitpack.cc index 4ebed43..f548cfd 100644 --- a/src/bitpack.cc +++ b/src/bitpack.cc @@ -3,6 +3,13 @@ #include #include +#ifdef Debug +#include +constexpr bool debug = true; +#else +constexpr bool debug = false; +#endif + using std::uint16_t; using std::vector; using uchar = unsigned char; @@ -95,7 +102,7 @@ pack_n(const vuint16::const_iterator t_input_begin, } /////////////////////////////////////////////////////////////////////////////// -// unpacking // +// unpacking // /////////////////////////////////////////////////////////////////////////////// uint16_t mask_n(int t_nb_bits) { @@ -127,6 +134,9 @@ uint16_t mask_n(int t_nb_bits) { left_shift = ((left_shift += step) >= t_n) ? (left_shift - t_n) + step : left_shift; current_char = static_cast(*it << left_shift); + if constexpr(debug) { + std::printf("left:\t%d\t", left_shift); + } // right bits bool zero_rs = right_shift; right_shift -= step; @@ -134,12 +144,24 @@ uint16_t mask_n(int t_nb_bits) { // optional middle bits before right bits if (zero_rs) { current_char |= *(++it) << std::abs(right_shift); + if constexpr(debug) { + std::printf("middle:\t%d\t", std::abs(right_shift)); + } } right_shift = 8 - std::abs(right_shift); } current_char |= *(++it) >> right_shift; + if constexpr(debug) { + std::printf("right\t%d\t", right_shift); + } // char made! ret.push_back(current_char &= mask); + if constexpr(debug) { + std::printf("value:\t%d\n", current_char); + } + if(right_shift == 0) { + ++it; + } if (current_char >= max_value) { const auto next_vec = unpack_n(it, t_end, t_n + 1); ret.insert(ret.end(), next_vec.begin(), next_vec.end()); diff --git a/src/common.cc b/src/common.cc index 1198876..092f394 100644 --- a/src/common.cc +++ b/src/common.cc @@ -5,17 +5,9 @@ #include "common.hh" -#ifdef Debug -constexpr bool debug_mode = true; -#include -#else -constexpr bool debug_mode = false; -#endif - using std::uint8_t; using std::uint16_t; using dic_comp_t = std::map, uint16_t>; -// using dic_un_t = std::map>>; using ustring = std::basic_string; int ipow(int base, int exp) { @@ -68,34 +60,23 @@ std::pair dico(dic_comp_t &t_dictionary, ustring dico_uncompress(std::map &t_dict, const uint16_t t_code, const uint16_t t_old) { - if constexpr(debug_mode) { - std::printf("Code: %d\tOld code: %d\n", t_code, t_old); - } - auto e = (t_code < 256) ? ustring{static_cast(t_code)} : t_dict[t_code]; - - // Si le code n'existe pas - - if(e.empty()) { - e = (t_old < 256) ? ustring{static_cast(t_old)} - : t_dict[t_old]; - const auto temp = e[0]; - e += temp; - t_dict[t_code] = std::move(e); - if constexpr(debug_mode) { - std::printf("String: %s\n", e.c_str()); + if (!e.empty()) { + ustring str; + if(t_old < 256) { + str = ustring{static_cast(t_old)}; + } else { + str = t_dict[t_old]; + str += str[0]; } + t_dict[static_cast(t_dict.size() + 255)] = std::move(str); return e; } - // auto str = t_dict[t_old]; - auto str = (t_old < 256) ? ustring{static_cast(t_old)} - : t_dict[t_old]; - str += str[0]; - t_dict[static_cast(t_dict.size() + 255)] = std::move(str); - if constexpr(debug_mode) { - std::printf("String: %s\n", e.c_str()); - } + e = (t_old < 256) ? ustring{static_cast(t_old)} + : t_dict[t_old]; + e += e[0]; + t_dict[t_code] = e; return e; }