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); bool zero_rs = (right_shift == 0);
right_shift -= step; 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 // 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(current_char);
} }
}
t_res.push_back(static_cast<uchar>(masks[t_n])); t_res.push_back(static_cast<uchar>(masks[t_n]));
return pack_n(it, t_input_end, t_res, t_n + 1); 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; left_shift = (left_shift - t_n) + step;
} }
t_res.push_back( t_res.push_back(
static_cast<uchar>(current_char | (*it >> left_shift & 0xFFu))); static_cast<uchar>(current_char | (*it >> left_shift & 0xFFU)));
// current_char = 0;
bool zero_rs = (right_shift == 0); bool zero_rs = (right_shift == 0);
right_shift -= step; right_shift -= step;
if (right_shift < 0) { if (right_shift < 0) {
if (!zero_rs) { 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); t_res.push_back(current_char);
} }
right_shift = 8 + right_shift; right_shift = 8 + right_shift;
} }
if (right_shift == 0) { if (right_shift == 0) {
current_char = static_cast<uchar>(*it & 0xffu); current_char = static_cast<uchar>(*it & 0xFFU);
t_res.push_back(current_char); t_res.push_back(current_char);
current_char = 0; current_char = 0;
char_touched = false; char_touched = false;
} else { } else {
current_char = static_cast<uchar>(*it << right_shift & 0xFFu); current_char = static_cast<uchar>(*it << right_shift & 0xFFU);
char_touched = true; 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, vuchar pack_16(const vuint16::const_iterator t_input_begin,
const vuint16::const_iterator t_input_end, vuchar &t_res) { const vuint16::const_iterator t_input_end, vuchar &t_res) {
std::for_each(t_input_begin, t_input_end, [&](const auto value) { 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 >> 8 & 0xFFU));
t_res.push_back(static_cast<uchar>(value & 0xFFu)); t_res.push_back(static_cast<uchar>(value & 0xFFU));
}); });
return t_res; 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 if (current_char >= max_value) { // if it is the mask
return unpack_n(it + 1, t_end, t_res, t_n + 1); 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) { if (right_shift == 0) {
++it; ++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); return std::make_pair(true, t_c);
} }
auto &e = t_dictionary[std::make_pair(t_nr_chaine, t_c)]; auto &e = t_dictionary[std::make_pair(t_nr_chaine, t_c)];
return (e != 0) ? std::make_pair(true, e) if(e != 0)
: std::make_pair(false, (e = static_cast<uint16_t>( return std::make_pair(true, e);
t_dictionary.size() + 255))); 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, ustring dico_uncompress(std::map<uint16_t, ustring> &t_dict,

View File

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