better code quality

This commit is contained in:
Phuntsok Drak-pa 2018-11-21 01:49:30 +01:00
parent 988dbcaa87
commit fdcdc6519a
3 changed files with 17 additions and 21 deletions

View File

@ -65,14 +65,12 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin,
bool zero_rs = (right_shift == 0);
right_shift -= step;
if (right_shift < 0) { // si right_shift est inférieur à zéro
if (right_shift < 0 && !zero_rs) {
// si right_shift est inférieur à zéro
// si right_shift était différent de zéro, alors extra octet
if (!zero_rs) {
current_char =
static_cast<uchar>(masks[t_n] >> (-right_shift) & 0xffu);
current_char = static_cast<uchar>(masks[t_n] >> (-right_shift) & 0xFFU);
t_res.push_back(current_char);
}
}
t_res.push_back(static_cast<uchar>(masks[t_n]));
return pack_n(it, t_input_end, t_res, t_n + 1);
}
@ -82,25 +80,24 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin,
left_shift = (left_shift - t_n) + step;
}
t_res.push_back(
static_cast<uchar>(current_char | (*it >> left_shift & 0xFFu)));
// current_char = 0;
static_cast<uchar>(current_char | (*it >> left_shift & 0xFFU)));
bool zero_rs = (right_shift == 0);
right_shift -= step;
if (right_shift < 0) {
if (!zero_rs) {
current_char = static_cast<uchar>(*it >> (-right_shift) & 0xFFu);
current_char = static_cast<uchar>(*it >> (-right_shift) & 0xFFU);
t_res.push_back(current_char);
}
right_shift = 8 + right_shift;
}
if (right_shift == 0) {
current_char = static_cast<uchar>(*it & 0xffu);
current_char = static_cast<uchar>(*it & 0xFFU);
t_res.push_back(current_char);
current_char = 0;
char_touched = false;
} else {
current_char = static_cast<uchar>(*it << right_shift & 0xFFu);
current_char = static_cast<uchar>(*it << right_shift & 0xFFU);
char_touched = true;
}
}
@ -112,9 +109,9 @@ vuchar pack_n(const vuint16::const_iterator t_input_begin,
vuchar pack_16(const vuint16::const_iterator t_input_begin,
const vuint16::const_iterator t_input_end, vuchar &t_res) {
std::for_each(t_input_begin, t_input_end, [&](const auto value) {
t_res.push_back(static_cast<uchar>(value >> 8 & 0xFFu));
t_res.push_back(static_cast<uchar>(value & 0xFFu));
std::for_each(t_input_begin, t_input_end, [&t_res](const auto value) {
t_res.push_back(static_cast<uchar>(value >> 8 & 0xFFU));
t_res.push_back(static_cast<uchar>(value & 0xFFU));
});
return t_res;
}
@ -159,7 +156,8 @@ vuint16 unpack_n(const ustring::const_iterator t_begin,
if (current_char >= max_value) { // if it is the mask
return unpack_n(it + 1, t_end, t_res, t_n + 1);
}
t_res.push_back(current_char &= masks[t_n]);
current_char &= masks[t_n];
t_res.push_back(current_char);
if (right_shift == 0) {
++it;
}

View File

@ -27,9 +27,10 @@ std::pair<bool, uint16_t> dico(dic_comp_t &t_dictionary,
return std::make_pair(true, t_c);
}
auto &e = t_dictionary[std::make_pair(t_nr_chaine, t_c)];
return (e != 0) ? std::make_pair(true, e)
: std::make_pair(false, (e = static_cast<uint16_t>(
t_dictionary.size() + 255)));
if(e != 0)
return std::make_pair(true, e);
e = static_cast<uint16_t>(t_dictionary.size() + 255);
return std::make_pair(false, e);
}
ustring dico_uncompress(std::map<uint16_t, ustring> &t_dict,

View File

@ -36,7 +36,6 @@ vvuint16 lzw_compress(ustring &&t_text) {
vvuint16 res{};
const auto DICT_MAX = static_cast<size_t>(ipow(2, 14) - 256); /* 16 bits */
uint16_t w = 0xFFFF;
bool pushed = false;
vuint16 chunk{};
dict_t dict{};
for (const auto c : t_text) {
@ -52,11 +51,9 @@ vvuint16 lzw_compress(ustring &&t_text) {
if (const auto &[exists, pos] = dico(dict, w, static_cast<uint8_t>(c));
exists) {
w = pos;
pushed = false;
} else {
chunk.push_back(w);
w = static_cast<uint16_t>(c);
pushed = true;
}
}
if (w != 0xFFFF) {