diff options
author | Ruslan Kovalev <ruslan.a.kovalev@gmail.com> | 2022-02-10 16:46:44 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:44 +0300 |
commit | 59e19371de37995fcb36beb16cd6ec030af960bc (patch) | |
tree | fa68e36093ebff8b805462e9e6d331fe9d348214 /library/cpp/bit_io | |
parent | 89db6fe2fe2c32d2a832ddfeb04e8d078e301084 (diff) | |
download | ydb-59e19371de37995fcb36beb16cd6ec030af960bc.tar.gz |
Restoring authorship annotation for Ruslan Kovalev <ruslan.a.kovalev@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/bit_io')
-rw-r--r-- | library/cpp/bit_io/bitinout_ut.cpp | 340 | ||||
-rw-r--r-- | library/cpp/bit_io/bitinput.h | 62 | ||||
-rw-r--r-- | library/cpp/bit_io/bitinput_impl.h | 42 | ||||
-rw-r--r-- | library/cpp/bit_io/bitoutput.h | 62 | ||||
-rw-r--r-- | library/cpp/bit_io/ut/ya.make | 2 |
5 files changed, 254 insertions, 254 deletions
diff --git a/library/cpp/bit_io/bitinout_ut.cpp b/library/cpp/bit_io/bitinout_ut.cpp index 23a1ddf344..375e89abde 100644 --- a/library/cpp/bit_io/bitinout_ut.cpp +++ b/library/cpp/bit_io/bitinout_ut.cpp @@ -1,19 +1,19 @@ -#include "bitinput.h" -#include "bitoutput.h" - +#include "bitinput.h" +#include "bitoutput.h" + #include <library/cpp/testing/unittest/registar.h> - + #include <util/stream/buffer.h> -#include <util/generic/buffer.h> - -namespace NBitIO { - static const char BITS_REF[] = +#include <util/generic/buffer.h> + +namespace NBitIO { + static const char BITS_REF[] = "00100010 01000000 00000000 00100111 11011111 01100111 11010101 00010100 " "00100010 01100011 11100011 00110000 11011011 11011111 01001100 00110101 " "10011110 01011111 01010000 00000110 00011011 00100110 00010100 01110011 " "00001010 10101010 10101010 10101010 10101010 10101010 10101010 10101010 " "10110101 01010101 01010101 01010101 01010101 01010101 01010101 01010101 " - "01000000"; + "01000000"; inline ui64 Bits(ui64 bytes) { return bytes << 3ULL; @@ -43,237 +43,237 @@ namespace NBitIO { inline TString PrintBits(T t, ui32 bits = Bits(sizeof(T))) { return PrintBits((char*)&t, ((char*)&t) + BytesUp(bits)); } -} - -class TBitIOTest: public TTestBase { - UNIT_TEST_SUITE(TBitIOTest); +} + +class TBitIOTest: public TTestBase { + UNIT_TEST_SUITE(TBitIOTest); UNIT_TEST(TestBitIO) - UNIT_TEST_SUITE_END(); - -private: + UNIT_TEST_SUITE_END(); + +private: using TBi = NBitIO::TBitInput; using TVec = TVector<char>; - + void static CheckBits(const TVec& v, const TString& ref, const TString& rem) { - UNIT_ASSERT_VALUES_EQUAL_C(NBitIO::PrintBits(v.begin(), v.end()), ref, rem); - } - - void DoRead(TBi& b, ui32& t) { + UNIT_ASSERT_VALUES_EQUAL_C(NBitIO::PrintBits(v.begin(), v.end()), ref, rem); + } + + void DoRead(TBi& b, ui32& t) { b.Read(t, 1, 0); // 1 - b.ReadK<3>(t, 1); // 4 + b.ReadK<3>(t, 1); // 4 b.Read(t, 5, 4); // 9 - b.ReadK<14>(t, 9); // 23 + b.ReadK<14>(t, 9); // 23 b.Read(t, 1, 23); // 24 - b.ReadK<5>(t, 24); // 29 + b.ReadK<5>(t, 24); // 29 b.Read(t, 3, 29); // 32 - } - - template <typename TBo> - void DoWrite(TBo& b, ui32 t) { - b.Write(t, 1, 0); //1 - b.Write(t, 3, 1); //4 - b.Write(t, 5, 4); //9 - b.Write(t, 14, 9); //23 - b.Write(t, 1, 23); //24 - b.Write(t, 5, 24); //29 + } + + template <typename TBo> + void DoWrite(TBo& b, ui32 t) { + b.Write(t, 1, 0); //1 + b.Write(t, 3, 1); //4 + b.Write(t, 5, 4); //9 + b.Write(t, 14, 9); //23 + b.Write(t, 1, 23); //24 + b.Write(t, 5, 24); //29 b.Write(t, 3, 29); //32 - } - - template <typename TBo> + } + + template <typename TBo> void DoWrite1(TBo& out, const TString& rem) { - out.Write(0x0C, 3); + out.Write(0x0C, 3); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 1u, (rem + ", " + ToString(__LINE__))); - out.Write(0x18, 4); + out.Write(0x18, 4); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 1u, (rem + ", " + ToString(__LINE__))); - out.Write(0x0C, 3); + out.Write(0x0C, 3); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 2u, (rem + ", " + ToString(__LINE__))); - out.Write(0x30000, 17); + out.Write(0x30000, 17); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 4u, (rem + ", " + ToString(__LINE__))); - out.Write(0x0C, 3); + out.Write(0x0C, 3); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 4u, (rem + ", " + ToString(__LINE__))); - } - - template <typename TBo> + } + + template <typename TBo> void DoWrite2(TBo& out, const TString& rem) { - out.Write(0x0C, 3); + out.Write(0x0C, 3); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 8u, (rem + ", " + ToString(__LINE__))); - - out.Write(0x42, 7); + + out.Write(0x42, 7); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 9u, (rem + ", " + ToString(__LINE__))); - - DoWrite(out, 1637415112); + + DoWrite(out, 1637415112); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 13u, (rem + ", " + ToString(__LINE__))); - - DoWrite(out, 897998715); + + DoWrite(out, 897998715); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 17u, (rem + ", " + ToString(__LINE__))); - - DoWrite(out, 201416527); + + DoWrite(out, 201416527); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 21u, (rem + ", " + ToString(__LINE__))); - - DoWrite(out, 432344219); + + DoWrite(out, 432344219); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 25u, (rem + ", " + ToString(__LINE__))); - - out.Write(0xAAAAAAAAAAAAAAAAULL, 64); + + out.Write(0xAAAAAAAAAAAAAAAAULL, 64); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 33u, (rem + ", " + ToString(__LINE__))); - - out.Write(0x5555555555555555ULL, 64); + + out.Write(0x5555555555555555ULL, 64); UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 41u, (rem + ", " + ToString(__LINE__))); - } - + } + void DoBitOutput(NBitIO::TBitOutputYVector& out, const TString& rem) { - DoWrite1(out, rem); - - out.WriteWords<8>(0xabcdef); - UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 8u, (rem + ", " + ToString(__LINE__))); - - DoWrite2(out, rem); - } - + DoWrite1(out, rem); + + out.WriteWords<8>(0xabcdef); + UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 8u, (rem + ", " + ToString(__LINE__))); + + DoWrite2(out, rem); + } + void DoBitOutput(NBitIO::TBitOutputArray& out, const TString& rem) { - DoWrite1(out, rem); - - out.WriteWords<8>(0xabcdef); - UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 8u, (rem + ", " + ToString(__LINE__))); - - DoWrite2(out, rem); - } - + DoWrite1(out, rem); + + out.WriteWords<8>(0xabcdef); + UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 8u, (rem + ", " + ToString(__LINE__))); + + DoWrite2(out, rem); + } + void DoBitInput(TBi& in, const TString& rem) { - UNIT_ASSERT(!in.Eof()); - - { - ui64 val; - - val = 0; + UNIT_ASSERT(!in.Eof()); + + { + ui64 val; + + val = 0; UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 0u, (rem + ": " + NBitIO::PrintBits(val))); - + UNIT_ASSERT_C(in.Read(val, 3), (rem + ": " + NBitIO::PrintBits(val)).data()); - + UNIT_ASSERT_VALUES_EQUAL_C(val, 0x4u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 1u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data()); - - val = 0; + + val = 0; UNIT_ASSERT_C(in.Read(val, 4), (rem + ": " + NBitIO::PrintBits(val)).data()); - + UNIT_ASSERT_VALUES_EQUAL_C(val, 0x8u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 1u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data()); - - val = 0; + + val = 0; UNIT_ASSERT_C(in.Read(val, 3), (rem + ": " + NBitIO::PrintBits(val)).data()); - + UNIT_ASSERT_VALUES_EQUAL_C(val, 0x4u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 2u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data()); - - val = 0; + + val = 0; UNIT_ASSERT_C(in.Read(val, 17), (rem + ": " + NBitIO::PrintBits(val)).data()); - + UNIT_ASSERT_VALUES_EQUAL_C(val, 0x10000u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 4u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data()); - + UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 4u, (rem + ": " + NBitIO::PrintBits(val))); - - { - ui32 rt = 0; - in.ReadRandom(30, rt, 10, 20); + + { + ui32 rt = 0; + in.ReadRandom(30, rt, 10, 20); UNIT_ASSERT_STRINGS_EQUAL(NBitIO::PrintBits(rt).data(), "00000000 00000000 00001111 01111100"); - } - val = 0; + } + val = 0; UNIT_ASSERT_C(in.Read(val, 3), (rem + ": " + NBitIO::PrintBits(val)).data()); - + UNIT_ASSERT_VALUES_EQUAL_C(val, 0x4u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 4u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data()); - - val = 0; + + val = 0; UNIT_ASSERT_C(in.ReadWords<8>(val), (rem + ": " + NBitIO::PrintBits(val)).data()); - + UNIT_ASSERT_VALUES_EQUAL_C(val, 0xabcdefU, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 8u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data()); - - val = 0; + + val = 0; UNIT_ASSERT_C(in.Read(val, 3), (rem + ", " + ToString(__LINE__)).data()); - + UNIT_ASSERT_VALUES_EQUAL_C(val, 0x4u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 8u, (rem + ": " + NBitIO::PrintBits(val))); UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data()); - - val = 0; - in.Read(val, 7); + + val = 0; + in.Read(val, 7); UNIT_ASSERT_VALUES_EQUAL_C(val, 0x42u, (rem + ": " + NBitIO::PrintBits(val))); - } - - { - ui32 v = 0; - - DoRead(in, v); + } + + { + ui32 v = 0; + + DoRead(in, v); UNIT_ASSERT_VALUES_EQUAL_C(v, 1637415112ul, (rem + ": " + NBitIO::PrintBits(v))); - DoRead(in, v); + DoRead(in, v); UNIT_ASSERT_VALUES_EQUAL_C(v, 897998715u, (rem + ": " + NBitIO::PrintBits(v))); - DoRead(in, v); + DoRead(in, v); UNIT_ASSERT_VALUES_EQUAL_C(v, 201416527u, (rem + ": " + NBitIO::PrintBits(v))); - DoRead(in, v); + DoRead(in, v); UNIT_ASSERT_VALUES_EQUAL_C(v, 432344219u, (rem + ": " + NBitIO::PrintBits(v))); - + UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 25u, (rem + ": " + NBitIO::PrintBits(v))); - } - - { - ui64 v8 = 0; - in.ReadSafe(v8, 64); - + } + + { + ui64 v8 = 0; + in.ReadSafe(v8, 64); + UNIT_ASSERT_VALUES_EQUAL_C(v8, 0xAAAAAAAAAAAAAAAAULL, (rem + ": " + NBitIO::PrintBits(v8))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 33u, (rem + ": " + NBitIO::PrintBits(v8))); - - v8 = 0; - in.ReadK<64>(v8); - + + v8 = 0; + in.ReadK<64>(v8); + UNIT_ASSERT_VALUES_EQUAL_C(v8, 0x5555555555555555ULL, (rem + ": " + NBitIO::PrintBits(v8))); UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 41u, (rem + ": " + NBitIO::PrintBits(v8))); - } - - ui32 v = 0; + } + + ui32 v = 0; UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data()); UNIT_ASSERT_C(in.Read(v, 5), (rem + ", " + ToString(__LINE__)).data()); UNIT_ASSERT_C(in.Eof(), (rem + ", " + ToString(__LINE__)).data()); - } - - void TestBitIO() { - { - TVec vec; - - { - NBitIO::TBitOutputYVector out(&vec); - DoBitOutput(out, ToString(__LINE__)); - } - + } + + void TestBitIO() { + { + TVec vec; + + { + NBitIO::TBitOutputYVector out(&vec); + DoBitOutput(out, ToString(__LINE__)); + } + CheckBits(vec, NBitIO::BITS_REF, ToString(__LINE__).data()); - - { - TBi in(vec); - DoBitInput(in, ToString(__LINE__)); - } - } - { - TVec vec; - vec.resize(41, 0); - { - NBitIO::TBitOutputArray out(vec.begin(), vec.size()); - DoBitOutput(out, ToString(__LINE__)); - } - + + { + TBi in(vec); + DoBitInput(in, ToString(__LINE__)); + } + } + { + TVec vec; + vec.resize(41, 0); + { + NBitIO::TBitOutputArray out(vec.begin(), vec.size()); + DoBitOutput(out, ToString(__LINE__)); + } + CheckBits(vec, NBitIO::BITS_REF, ToString(__LINE__).data()); - - { - TBi in(vec); - DoBitInput(in, ToString(__LINE__)); - } - } - } -}; - -UNIT_TEST_SUITE_REGISTRATION(TBitIOTest); + + { + TBi in(vec); + DoBitInput(in, ToString(__LINE__)); + } + } + } +}; + +UNIT_TEST_SUITE_REGISTRATION(TBitIOTest); diff --git a/library/cpp/bit_io/bitinput.h b/library/cpp/bit_io/bitinput.h index 85711eb7f9..397b311783 100644 --- a/library/cpp/bit_io/bitinput.h +++ b/library/cpp/bit_io/bitinput.h @@ -1,16 +1,16 @@ #pragma once -#include "bitinput_impl.h" - +#include "bitinput_impl.h" + #include <util/system/yassert.h> -#include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> #include <iterator> -namespace NBitIO { +namespace NBitIO { // Based on junk/solar/codecs/bitstream.h - + class TBitInput: protected TBitInputImpl { public: template <typename TVec> @@ -18,27 +18,27 @@ namespace NBitIO { : TBitInputImpl(std::begin(vec), std::end(vec)) { } - + TBitInput(const char* start, const char* end) : TBitInputImpl(start, end) { } - + bool Eof() const { return EofImpl(); } - + ui64 GetOffset() const { ui64 bo = BitOffset(); return bo / 8 + !!(bo % 8); } - - using TBitInputImpl::GetBitLength; - + + using TBitInputImpl::GetBitLength; + ui64 GetBitOffset() const { return BitOffset() % 8; } - + public: // Read with static number of bits. // Preserves what's in result. @@ -49,7 +49,7 @@ namespace NBitIO { CopyToResultK<bits>(result, r64, skipbits); return ret; } - + // Read with static number of bits. // Zeroes other bits in result. template <ui64 bits, typename T> @@ -59,42 +59,42 @@ namespace NBitIO { result = r; return res; } - + // Shortcut to impl. template <ui64 bits> Y_FORCE_INLINE bool ReadK(ui64& result) { if (bits <= 56) return ReadKImpl<bits>(result); - + ui64 r1 = 0ULL; ui64 r2 = 0ULL; - + bool ret1 = ReadKImpl<56ULL>(r1); bool ret2 = ReadKImpl<(bits > 56ULL ? bits - 56ULL : 0) /*or else we get negative param in template*/>(r2); - + result = (r2 << 56ULL) | r1; - + return ret1 & ret2; } - + // It's safe to read up to 64 bits. // Zeroes other bits in result. template <typename T> Y_FORCE_INLINE bool ReadSafe(T& result, ui64 bits) { if (bits <= 56ULL) return Read(result, bits); - + ui64 r1 = 0ULL; ui64 r2 = 0ULL; - + bool ret1 = ReadKImpl<56ULL>(r1); bool ret2 = ReadImpl(r2, bits - 56ULL); - + result = (r2 << 56ULL) | r1; - + return ret1 & ret2; } - + // It's safe to read up to 64 bits. // Preserves what's in result. template <typename T> @@ -104,7 +104,7 @@ namespace NBitIO { CopyToResult(result, r64, bits, skipbits); return ret; } - + // Do not try to read more than 56 bits at once. Split in two reads or use ReadSafe. // Zeroes other bits in result. template <typename T> @@ -114,7 +114,7 @@ namespace NBitIO { result = r64; return ret; } - + // Shortcut to impl. Y_FORCE_INLINE bool Read(ui64& result, ui64 bits) { return ReadImpl(result, bits); @@ -129,7 +129,7 @@ namespace NBitIO { CopyToResult(result, r64, bits, skipbits); return ret; } - + // Unsigned wordwise read. Underlying data is splitted in "words" of "bits(data) + 1(flag)" bits. // Like this: (unsigned char)0x2E<3> (0010 1110) <=> 1110 0101 // fddd fddd @@ -142,13 +142,13 @@ namespace NBitIO { return retCode; } - + // Shortcut to impl. template <ui64 bits> Y_FORCE_INLINE bool ReadWords(ui64& result) { return ReadWordsImpl<bits>(result); } - + Y_FORCE_INLINE bool Back(int bits) { return Seek(BitOffset() - bits); } @@ -168,4 +168,4 @@ namespace NBitIO { return ret; } }; -} +} diff --git a/library/cpp/bit_io/bitinput_impl.h b/library/cpp/bit_io/bitinput_impl.h index b13fbef101..d1c05f9be2 100644 --- a/library/cpp/bit_io/bitinput_impl.h +++ b/library/cpp/bit_io/bitinput_impl.h @@ -1,9 +1,9 @@ -#pragma once - +#pragma once + #include <util/generic/bitops.h> #include <util/system/unaligned_mem.h> -namespace NBitIO { +namespace NBitIO { class TBitInputImpl { i64 RealStart; i64 Start; @@ -12,7 +12,7 @@ namespace NBitIO { const ui32 FakeStart; char Fake[16]; const i64 FStart; - + public: TBitInputImpl(const char* start, const char* end) : RealStart((i64)start) @@ -25,11 +25,11 @@ namespace NBitIO { memcpy(Fake, (const char*)(RealStart + (FakeStart >> 3)), (Length - FakeStart) >> 3); Start = FakeStart ? RealStart : FStart; } - - ui64 GetBitLength() const { - return Length; - } - + + ui64 GetBitLength() const { + return Length; + } + protected: template <ui32 bits> Y_FORCE_INLINE bool ReadKImpl(ui64& result) { @@ -45,7 +45,7 @@ namespace NBitIO { Start = FStart; return true; } - + Y_FORCE_INLINE bool ReadImpl(ui64& result, ui32 bits) { result = (ReadUnaligned<ui64>((const void*)(Start + (BOffset >> 3))) >> (BOffset & 7)) & MaskLowerBits(bits); BOffset += bits; @@ -57,13 +57,13 @@ namespace NBitIO { return false; } Start = FStart; - return true; - } - + return true; + } + Y_FORCE_INLINE bool EofImpl() const { return BOffset >= Length; } - + Y_FORCE_INLINE ui64 BitOffset() const { return BOffset; } @@ -81,30 +81,30 @@ namespace NBitIO { Y_FORCE_INLINE static void CopyToResultK(T& result, ui64 r64, ui64 skipbits) { result = (result & ~(Mask64(bits) << skipbits)) | (r64 << skipbits); } - + template <typename T> Y_FORCE_INLINE static void CopyToResult(T& result, ui64 r64, ui64 bits, ui64 skipbits) { result = (result & InverseMaskLowerBits(bits, skipbits)) | (r64 << skipbits); } - + public: template <ui64 bits> Y_FORCE_INLINE bool ReadWordsImpl(ui64& data) { data = 0; - + const ui64 haveMore = NthBit64(bits); const ui64 mask = Mask64(bits); ui64 current = 0; ui64 byteNo = 0; - + do { if (!ReadKImpl<bits + 1>(current)) return false; - + data |= (current & mask) << (byteNo++ * bits); } while (current & haveMore); - + return true; } }; -} +} diff --git a/library/cpp/bit_io/bitoutput.h b/library/cpp/bit_io/bitoutput.h index 2b886c1f02..c54a9a7835 100644 --- a/library/cpp/bit_io/bitoutput.h +++ b/library/cpp/bit_io/bitoutput.h @@ -1,19 +1,19 @@ #pragma once #include <library/cpp/deprecated/accessors/accessors.h> - + #include <util/stream/output.h> #include <util/system/yassert.h> #include <util/generic/bitops.h> -#include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> -namespace NBitIO { +namespace NBitIO { // Based on junk/solar/codecs/bitstream.h - + // Almost all code is hard tuned for sequential write performance. // Use tools/bursttrie/benchmarks/bitstreams_benchmark to check your changes. - + inline constexpr ui64 BytesUp(ui64 bits) { return (bits + 7ULL) >> 3ULL; } @@ -25,7 +25,7 @@ namespace NBitIO { ui64 FreeBits; ui64 Active; ui64 Offset; - + public: TBitOutputBase(TStorage* storage) : Storage(storage) @@ -38,15 +38,15 @@ namespace NBitIO { ui64 GetOffset() const { return Offset + BytesUp(64ULL - FreeBits); } - + ui64 GetBitOffset() const { return (64ULL - FreeBits) & 7ULL; } - + ui64 GetByteReminder() const { return FreeBits & 7ULL; } - + public: // interface @@ -58,14 +58,14 @@ namespace NBitIO { Active |= (data & MaskLowerBits(FreeBits)) << (64ULL - FreeBits); data >>= FreeBits; - + FreeBits = 0ULL; } Flush(); } - Active |= bits ? ((data & MaskLowerBits(bits)) << (64ULL - FreeBits)) : 0; + Active |= bits ? ((data & MaskLowerBits(bits)) << (64ULL - FreeBits)) : 0; FreeBits -= bits; } @@ -73,7 +73,7 @@ namespace NBitIO { Y_FORCE_INLINE void Write(ui64 data, ui64 bits, ui64 skipbits) { Write(data >> skipbits, bits); } - + // Unsigned wordwise write. Underlying data is splitted in "words" of "bits(data) + 1(flag)" bits. // Like this: (unsigned char)0x2E<3> (0000 0010 1110) <=> 1110 0101 // fddd fddd @@ -81,32 +81,32 @@ namespace NBitIO { Y_FORCE_INLINE void WriteWords(ui64 data) { do { ui64 part = data; - + data >>= bits; part |= FastZeroIfFalse(data, NthBit64(bits)); Write(part, bits + 1ULL); } while (data); } - + Y_FORCE_INLINE ui64 /* padded bits */ Flush() { const ui64 ubytes = 8ULL - (FreeBits >> 3ULL); - + if (ubytes) { Active <<= FreeBits; Active >>= FreeBits; - + Storage->WriteData((const char*)&Active, (const char*)&Active + ubytes); Offset += ubytes; } - + const ui64 padded = FreeBits & 7; FreeBits = 64ULL; Active = 0ULL; - + return padded; } - + virtual ~TBitOutputBase() { Flush(); } @@ -116,22 +116,22 @@ namespace NBitIO { return -i64(cond) & iftrue; } }; - + template <typename TVec> class TBitOutputVectorImpl { TVec* Data; - + public: void WriteData(const char* begin, const char* end) { NAccessors::Append(*Data, begin, end); } - + TBitOutputVectorImpl(TVec* data) : Data(data) { } }; - + template <typename TVec> struct TBitOutputVector: public TBitOutputVectorImpl<TVec>, public TBitOutputBase<TBitOutputVectorImpl<TVec>> { inline TBitOutputVector(TVec* data) @@ -144,7 +144,7 @@ namespace NBitIO { class TBitOutputArrayImpl { char* Data; size_t Left; - + public: void WriteData(const char* begin, const char* end) { size_t sz = end - begin; @@ -153,14 +153,14 @@ namespace NBitIO { Data += sz; Left -= sz; } - + TBitOutputArrayImpl(char* begin, size_t len) : Data(begin) , Left(len) { } }; - + struct TBitOutputArray: public TBitOutputArrayImpl, public TBitOutputBase<TBitOutputArrayImpl> { inline TBitOutputArray(char* begin, size_t len) : TBitOutputArrayImpl(begin, len) @@ -170,15 +170,15 @@ namespace NBitIO { }; using TBitOutputYVector = TBitOutputVector<TVector<char>>; - + class TBitOutputStreamImpl { IOutputStream* Out; - + public: void WriteData(const char* begin, const char* end) { Out->Write(begin, end - begin); } - + TBitOutputStreamImpl(IOutputStream* out) : Out(out) { @@ -192,4 +192,4 @@ namespace NBitIO { { } }; -} +} diff --git a/library/cpp/bit_io/ut/ya.make b/library/cpp/bit_io/ut/ya.make index 07ee5b4997..71d3da93c3 100644 --- a/library/cpp/bit_io/ut/ya.make +++ b/library/cpp/bit_io/ut/ya.make @@ -1,7 +1,7 @@ UNITTEST_FOR(library/cpp/bit_io) OWNER( - velavokr + velavokr g:util ) |