diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/codecs/delta_codec.h | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/codecs/delta_codec.h')
-rw-r--r-- | library/cpp/codecs/delta_codec.h | 206 |
1 files changed, 103 insertions, 103 deletions
diff --git a/library/cpp/codecs/delta_codec.h b/library/cpp/codecs/delta_codec.h index 4e5dbb8f75..21325825e6 100644 --- a/library/cpp/codecs/delta_codec.h +++ b/library/cpp/codecs/delta_codec.h @@ -8,136 +8,136 @@ #include <util/string/cast.h> namespace NCodecs { - template <typename T = ui64, bool UnsignedDelta = true> - class TDeltaCodec: public ICodec { - static_assert(std::is_integral<T>::value, "expect std::is_integral<T>::value"); - - public: - using TUnsigned = std::make_unsigned_t<T>; - using TSigned = std::make_signed_t<T>; - using TDelta = std::conditional_t<UnsignedDelta, TUnsigned, TSigned>; - - private: - const TDelta MinDelta{Min<TDelta>()}; - const TDelta MaxDelta{Max<TDelta>() - 1}; - const TDelta InvalidDelta{MaxDelta + 1}; - - Y_FORCE_INLINE static TDelta AddSafe(TUnsigned a, TUnsigned b) { - return a + b; - } - - Y_FORCE_INLINE static TDelta SubSafe(TUnsigned a, TUnsigned b) { - return a - b; - } - - public: - struct TDecoder { - const TDelta InvalidDelta{Max<TDelta>()}; - - T Last = 0; - T Result = 0; - - bool First = true; - bool Invalid = false; - - Y_FORCE_INLINE bool Decode(TDelta t) { - if (Y_UNLIKELY(First)) { - First = false; - Result = Last = t; - return true; - } - - if (Y_UNLIKELY(Invalid)) { - Invalid = false; - Last = 0; - Result = t; - return true; - } - - Result = (Last += t); - Invalid = t == InvalidDelta; - - return !Invalid; + template <typename T = ui64, bool UnsignedDelta = true> + class TDeltaCodec: public ICodec { + static_assert(std::is_integral<T>::value, "expect std::is_integral<T>::value"); + + public: + using TUnsigned = std::make_unsigned_t<T>; + using TSigned = std::make_signed_t<T>; + using TDelta = std::conditional_t<UnsignedDelta, TUnsigned, TSigned>; + + private: + const TDelta MinDelta{Min<TDelta>()}; + const TDelta MaxDelta{Max<TDelta>() - 1}; + const TDelta InvalidDelta{MaxDelta + 1}; + + Y_FORCE_INLINE static TDelta AddSafe(TUnsigned a, TUnsigned b) { + return a + b; + } + + Y_FORCE_INLINE static TDelta SubSafe(TUnsigned a, TUnsigned b) { + return a - b; + } + + public: + struct TDecoder { + const TDelta InvalidDelta{Max<TDelta>()}; + + T Last = 0; + T Result = 0; + + bool First = true; + bool Invalid = false; + + Y_FORCE_INLINE bool Decode(TDelta t) { + if (Y_UNLIKELY(First)) { + First = false; + Result = Last = t; + return true; + } + + if (Y_UNLIKELY(Invalid)) { + Invalid = false; + Last = 0; + Result = t; + return true; + } + + Result = (Last += t); + Invalid = t == InvalidDelta; + + return !Invalid; } - }; + }; - public: - static TStringBuf MyName(); + public: + static TStringBuf MyName(); - TDeltaCodec() { - MyTraits.SizeOfInputElement = sizeof(T); - MyTraits.AssumesStructuredInput = true; + TDeltaCodec() { + MyTraits.SizeOfInputElement = sizeof(T); + MyTraits.AssumesStructuredInput = true; } - TString GetName() const override { + TString GetName() const override { return ToString(MyName()); - } + } - template <class TItem> - static void AppendTo(TBuffer& b, TItem t) { - b.Append((char*)&t, sizeof(t)); - } + template <class TItem> + static void AppendTo(TBuffer& b, TItem t) { + b.Append((char*)&t, sizeof(t)); + } - ui8 Encode(TStringBuf s, TBuffer& b) const override { - b.Clear(); + ui8 Encode(TStringBuf s, TBuffer& b) const override { + b.Clear(); if (s.empty()) { - return 0; - } + return 0; + } b.Reserve(s.size()); TArrayRef<const T> tin{(const T*)s.data(), s.size() / sizeof(T)}; const T* it = tin.begin(); - TDelta last = *(it++); - AppendTo(b, last); + TDelta last = *(it++); + AppendTo(b, last); - TDelta maxt = SubSafe(MaxDelta, last); - TDelta mint = AddSafe(MinDelta, last); + TDelta maxt = SubSafe(MaxDelta, last); + TDelta mint = AddSafe(MinDelta, last); for (; it != tin.end(); ++it) { - TDelta t = *it; - - if (Y_LIKELY((t >= mint) & (t <= maxt))) { - AppendTo(b, t - last); - last = t; - maxt = SubSafe(MaxDelta, last); - mint = AddSafe(MinDelta, last); - } else { - // delta overflow - AppendTo(b, InvalidDelta); - AppendTo(b, t); - last = 0; - maxt = MaxDelta; - mint = MinDelta; - } - } - - return 0; - } - - void Decode(TStringBuf s, TBuffer& b) const override { - b.Clear(); + TDelta t = *it; + + if (Y_LIKELY((t >= mint) & (t <= maxt))) { + AppendTo(b, t - last); + last = t; + maxt = SubSafe(MaxDelta, last); + mint = AddSafe(MinDelta, last); + } else { + // delta overflow + AppendTo(b, InvalidDelta); + AppendTo(b, t); + last = 0; + maxt = MaxDelta; + mint = MinDelta; + } + } + + return 0; + } + + void Decode(TStringBuf s, TBuffer& b) const override { + b.Clear(); if (s.empty()) { - return; + return; } b.Reserve(s.size()); TArrayRef<const T> tin{(const T*)s.data(), s.size() / sizeof(T)}; - TDecoder dec; + TDecoder dec; for (const T* it = tin.begin(); it != tin.end(); ++it) { - T tmp; - memcpy(&tmp, it, sizeof(tmp)); - if (dec.Decode(tmp)) { - AppendTo(b, dec.Result); - } + T tmp; + memcpy(&tmp, it, sizeof(tmp)); + if (dec.Decode(tmp)) { + AppendTo(b, dec.Result); + } } } - protected: - void DoLearn(ISequenceReader&) override { - } - }; + protected: + void DoLearn(ISequenceReader&) override { + } + }; } |