Bit-unpacking FIXEDDDDDDDDDDDD

This commit is contained in:
Phuntsok Drak-pa 2018-06-11 00:58:01 +02:00
parent 5eb33fb04f
commit e54e5fa07d
2 changed files with 35 additions and 32 deletions

View File

@ -3,6 +3,13 @@
#include <cmath>
#include <algorithm>
#ifdef Debug
#include <cstdio>
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<uint16_t>(*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());

View File

@ -5,17 +5,9 @@
#include "common.hh"
#ifdef Debug
constexpr bool debug_mode = true;
#include <cstdio>
#else
constexpr bool debug_mode = false;
#endif
using std::uint8_t;
using std::uint16_t;
using dic_comp_t = std::map<std::pair<uint16_t, uint8_t>, uint16_t>;
// using dic_un_t = std::map<std::uint16_t, std::unique_ptr<std::pair<uint16_t, uint8_t>>>;
using ustring = std::basic_string<unsigned char>;
int ipow(int base, int exp) {
@ -68,34 +60,23 @@ std::pair<bool, uint16_t> dico(dic_comp_t &t_dictionary,
ustring dico_uncompress(std::map<uint16_t, ustring> &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<unsigned char>(t_code)}
: t_dict[t_code];
// Si le code n'existe pas
if(e.empty()) {
e = (t_old < 256) ? ustring{static_cast<unsigned char>(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<unsigned char>(t_old)};
} else {
str = t_dict[t_old];
str += str[0];
}
t_dict[static_cast<uint16_t>(t_dict.size() + 255)] = std::move(str);
return e;
}
// auto str = t_dict[t_old];
auto str = (t_old < 256) ? ustring{static_cast<unsigned char>(t_old)}
: t_dict[t_old];
str += str[0];
t_dict[static_cast<uint16_t>(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<unsigned char>(t_old)}
: t_dict[t_old];
e += e[0];
t_dict[t_code] = e;
return e;
}