diff --git a/huffman/src/table.hpp b/huffman/src/table.hpp index fe10c1c..e9023ca 100644 --- a/huffman/src/table.hpp +++ b/huffman/src/table.hpp @@ -426,26 +426,25 @@ class table -> std::expected { using R = std::expected; + using D = std::iter_difference_t; while (pos != end()) { if (pos->bitsize() > c.bitsize()) { break; } - if (pos->bitsize() < c.bitsize()) { - using D = std::iter_difference_t; - assert( - pos.base()->skip() <= std::size_t{std::numeric_limits::max()}); + const auto skip = pos.base()->skip(); - pos += static_cast(pos.base()->skip()); - continue; - } + if (pos->bitsize() == c.bitsize()) { + assert(pos->value() <= c.value()); - if (static_cast(*pos) == c) { - return R{std::in_place, pos}; + if (const auto dist = c.value() - pos->value(); dist < skip) { + return R{std::in_place, pos + static_cast(dist)}; + } } - ++pos; + assert(skip <= std::size_t{std::numeric_limits::max()}); + pos += static_cast(skip); } return R{std::unexpect, pos};