diff options
| author | Anton Samokhvalov <[email protected]> | 2022-02-10 16:45:17 +0300 | 
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:45:17 +0300 | 
| commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
| tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/bit_io | |
| parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
Restoring authorship annotation for Anton Samokhvalov <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/bit_io')
| -rw-r--r-- | library/cpp/bit_io/bitinout_ut.cpp | 26 | ||||
| -rw-r--r-- | library/cpp/bit_io/bitinput.cpp | 2 | ||||
| -rw-r--r-- | library/cpp/bit_io/bitinput.h | 240 | ||||
| -rw-r--r-- | library/cpp/bit_io/bitinput_impl.cpp | 2 | ||||
| -rw-r--r-- | library/cpp/bit_io/bitinput_impl.h | 142 | ||||
| -rw-r--r-- | library/cpp/bit_io/bitoutput.cpp | 2 | ||||
| -rw-r--r-- | library/cpp/bit_io/bitoutput.h | 282 | ||||
| -rw-r--r-- | library/cpp/bit_io/ut/ya.make | 18 | ||||
| -rw-r--r-- | library/cpp/bit_io/ya.make | 32 | 
9 files changed, 373 insertions, 373 deletions
diff --git a/library/cpp/bit_io/bitinout_ut.cpp b/library/cpp/bit_io/bitinout_ut.cpp index da71d680703..23a1ddf344f 100644 --- a/library/cpp/bit_io/bitinout_ut.cpp +++ b/library/cpp/bit_io/bitinout_ut.cpp @@ -3,16 +3,16 @@  #include <library/cpp/testing/unittest/registar.h> -#include <util/stream/buffer.h>  +#include <util/stream/buffer.h>  #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 "  +        "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";      inline ui64 Bits(ui64 bytes) { @@ -47,7 +47,7 @@ namespace NBitIO {  class TBitIOTest: public TTestBase {      UNIT_TEST_SUITE(TBitIOTest); -    UNIT_TEST(TestBitIO)  +    UNIT_TEST(TestBitIO)      UNIT_TEST_SUITE_END();  private: @@ -59,13 +59,13 @@ private:      }      void DoRead(TBi& b, ui32& t) { -        b.Read(t, 1, 0);   // 1  +        b.Read(t, 1, 0);   // 1          b.ReadK<3>(t, 1);  // 4 -        b.Read(t, 5, 4);   // 9  +        b.Read(t, 5, 4);   // 9          b.ReadK<14>(t, 9); // 23 -        b.Read(t, 1, 23);  // 24  +        b.Read(t, 1, 23);  // 24          b.ReadK<5>(t, 24); // 29 -        b.Read(t, 3, 29);  // 32  +        b.Read(t, 3, 29);  // 32      }      template <typename TBo> @@ -76,7 +76,7 @@ private:          b.Write(t, 14, 9); //23          b.Write(t, 1, 23); //24          b.Write(t, 5, 24); //29 -        b.Write(t, 3, 29); //32  +        b.Write(t, 3, 29); //32      }      template <typename TBo> @@ -213,7 +213,7 @@ private:              DoRead(in, v);              UNIT_ASSERT_VALUES_EQUAL_C(v, 1637415112ul, (rem + ": " + NBitIO::PrintBits(v)));              DoRead(in, v); -            UNIT_ASSERT_VALUES_EQUAL_C(v, 897998715u, (rem + ": " + NBitIO::PrintBits(v)));  +            UNIT_ASSERT_VALUES_EQUAL_C(v, 897998715u, (rem + ": " + NBitIO::PrintBits(v)));              DoRead(in, v);              UNIT_ASSERT_VALUES_EQUAL_C(v, 201416527u, (rem + ": " + NBitIO::PrintBits(v)));              DoRead(in, v); diff --git a/library/cpp/bit_io/bitinput.cpp b/library/cpp/bit_io/bitinput.cpp index a453450fff9..d7c37f06fcc 100644 --- a/library/cpp/bit_io/bitinput.cpp +++ b/library/cpp/bit_io/bitinput.cpp @@ -1 +1 @@ -#include "bitinput.h"  +#include "bitinput.h" diff --git a/library/cpp/bit_io/bitinput.h b/library/cpp/bit_io/bitinput.h index ffd9e48f7b2..85711eb7f9a 100644 --- a/library/cpp/bit_io/bitinput.h +++ b/library/cpp/bit_io/bitinput.h @@ -2,170 +2,170 @@  #include "bitinput_impl.h" -#include <util/system/yassert.h>  +#include <util/system/yassert.h>  #include <util/generic/vector.h>  #include <util/generic/yexception.h>  #include <iterator>  namespace NBitIO { -    // Based on junk/solar/codecs/bitstream.h  +    // Based on junk/solar/codecs/bitstream.h -    class TBitInput: protected  TBitInputImpl {  -    public:  -        template <typename TVec>  -        explicit TBitInput(const TVec& vec)  +    class TBitInput: protected  TBitInputImpl { +    public: +        template <typename TVec> +        explicit TBitInput(const TVec& vec)              : TBitInputImpl(std::begin(vec), std::end(vec)) -        {  -        }  +        { +        } -        TBitInput(const char* start, const char* end)  -            : TBitInputImpl(start, end)  -        {  -        }  +        TBitInput(const char* start, const char* end) +            : TBitInputImpl(start, end) +        { +        } -        bool Eof() const {  -            return EofImpl();  -        }  +        bool Eof() const { +            return EofImpl(); +        } -        ui64 GetOffset() const {  -            ui64 bo = BitOffset();  -            return bo / 8 + !!(bo % 8);  -        }  +        ui64 GetOffset() const { +            ui64 bo = BitOffset(); +            return bo / 8 + !!(bo % 8); +        }          using TBitInputImpl::GetBitLength; -        ui64 GetBitOffset() const {  -            return BitOffset() % 8;  -        }  +        ui64 GetBitOffset() const { +            return BitOffset() % 8; +        } -    public:  -        // Read with static number of bits.  -        // Preserves what's in result.  -        template <ui64 bits, typename T>  +    public: +        // Read with static number of bits. +        // Preserves what's in result. +        template <ui64 bits, typename T>          Y_FORCE_INLINE bool ReadK(T& result, ui64 skipbits) { -            ui64 r64 = 0;  -            bool ret = bits <= 56 ? ReadKImpl<bits>(r64) : ReadSafe(r64, bits);  -            CopyToResultK<bits>(result, r64, skipbits);  -            return ret;  -        }  - -        // Read with static number of bits.  -        // Zeroes other bits in result.  -        template <ui64 bits, typename T>  +            ui64 r64 = 0; +            bool ret = bits <= 56 ? ReadKImpl<bits>(r64) : ReadSafe(r64, bits); +            CopyToResultK<bits>(result, r64, skipbits); +            return ret; +        } + +        // Read with static number of bits. +        // Zeroes other bits in result. +        template <ui64 bits, typename T>          Y_FORCE_INLINE bool ReadK(T& result) { -            ui64 r = 0;  -            bool res = ReadK<bits>(r);  -            result = r;  -            return res;  -        }  - -        // Shortcut to impl.  -        template <ui64 bits>  +            ui64 r = 0; +            bool res = ReadK<bits>(r); +            result = r; +            return res; +        } + +        // Shortcut to impl. +        template <ui64 bits>          Y_FORCE_INLINE bool ReadK(ui64& result) { -            if (bits <= 56)  -                return ReadKImpl<bits>(result);  +            if (bits <= 56) +                return ReadKImpl<bits>(result); -            ui64 r1 = 0ULL;  -            ui64 r2 = 0ULL;  +            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);  +            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;  +            result = (r2 << 56ULL) | r1; -            return ret1 & ret2;  -        }  +            return ret1 & ret2; +        } -        // It's safe to read up to 64 bits.  -        // Zeroes other bits in result.  -        template <typename T>  +        // 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);  +            if (bits <= 56ULL) +                return Read(result, bits); -            ui64 r1 = 0ULL;  -            ui64 r2 = 0ULL;  +            ui64 r1 = 0ULL; +            ui64 r2 = 0ULL; -            bool ret1 = ReadKImpl<56ULL>(r1);  -            bool ret2 = ReadImpl(r2, bits - 56ULL);  +            bool ret1 = ReadKImpl<56ULL>(r1); +            bool ret2 = ReadImpl(r2, bits - 56ULL); -            result = (r2 << 56ULL) | r1;  +            result = (r2 << 56ULL) | r1; -            return ret1 & ret2;  -        }  +            return ret1 & ret2; +        } -        // It's safe to read up to 64 bits.  -        // Preserves what's in result.  -        template <typename T>  +        // It's safe to read up to 64 bits. +        // Preserves what's in result. +        template <typename T>          Y_FORCE_INLINE bool ReadSafe(T& result, ui64 bits, ui64 skipbits) { -            ui64 r64 = 0;  -            bool ret = ReadSafe(r64, bits);  -            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>  +            ui64 r64 = 0; +            bool ret = ReadSafe(r64, bits); +            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>          Y_FORCE_INLINE bool Read(T& result, ui64 bits) { -            ui64 r64 = 0;  -            bool ret = ReadImpl(r64, bits);  -            result = r64;  -            return ret;  -        }  +            ui64 r64 = 0; +            bool ret = ReadImpl(r64, bits); +            result = r64; +            return ret; +        } -        // Shortcut to impl.  +        // Shortcut to impl.          Y_FORCE_INLINE bool Read(ui64& result, ui64 bits) { -            return ReadImpl(result, bits);  -        }  +            return ReadImpl(result, bits); +        } -        // Do not try to read more than 56 bits at once. Split in two reads or use ReadSafe.  -        // Preserves what's in result.  -        template <typename T>  +        // Do not try to read more than 56 bits at once. Split in two reads or use ReadSafe. +        // Preserves what's in result. +        template <typename T>          Y_FORCE_INLINE bool Read(T& result, ui64 bits, ui64 skipbits) { -            ui64 r64 = 0;  -            bool ret = ReadImpl(r64, bits);  -            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  -        template <ui64 bits, typename T>  +            ui64 r64 = 0; +            bool ret = ReadImpl(r64, bits); +            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 +        template <ui64 bits, typename T>          Y_FORCE_INLINE bool ReadWords(T& result) { -            ui64 r64 = 0;  +            ui64 r64 = 0; -            bool retCode = ReadWordsImpl<bits>(r64);  -            result = r64;  +            bool retCode = ReadWordsImpl<bits>(r64); +            result = r64; -            return retCode;  -        }  +            return retCode; +        } -        // Shortcut to impl.  -        template <ui64 bits>  +        // Shortcut to impl. +        template <ui64 bits>          Y_FORCE_INLINE bool ReadWords(ui64& result) { -            return ReadWordsImpl<bits>(result);  -        }  +            return ReadWordsImpl<bits>(result); +        }          Y_FORCE_INLINE bool Back(int bits) { -            return Seek(BitOffset() - bits);  -        }  +            return Seek(BitOffset() - bits); +        }          Y_FORCE_INLINE bool Seek(int bitoffset) { -            return TBitInputImpl::Seek(bitoffset);  -        }  +            return TBitInputImpl::Seek(bitoffset); +        } -        // A way to read a portion of bits at random location.  -        // Didn't want to complicate sequential read, neither to copypaste.  -        template <typename T>  +        // A way to read a portion of bits at random location. +        // Didn't want to complicate sequential read, neither to copypaste. +        template <typename T>          Y_FORCE_INLINE bool ReadRandom(ui64 bitoffset, T& result, ui64 bits, ui64 skipbits) { -            const ui64 curr = BitOffset();  -            Seek(bitoffset);  -            bool ret = ReadSafe<T>(result, bits, skipbits);  -            Seek(curr);  -            return ret;  -        }  -    };  +            const ui64 curr = BitOffset(); +            Seek(bitoffset); +            bool ret = ReadSafe<T>(result, bits, skipbits); +            Seek(curr); +            return ret; +        } +    };  } diff --git a/library/cpp/bit_io/bitinput_impl.cpp b/library/cpp/bit_io/bitinput_impl.cpp index e9ec98eeff3..81c897f882e 100644 --- a/library/cpp/bit_io/bitinput_impl.cpp +++ b/library/cpp/bit_io/bitinput_impl.cpp @@ -1 +1 @@ -#include "bitinput_impl.h"  +#include "bitinput_impl.h" diff --git a/library/cpp/bit_io/bitinput_impl.h b/library/cpp/bit_io/bitinput_impl.h index aa46b6e31b5..b13fbef1012 100644 --- a/library/cpp/bit_io/bitinput_impl.h +++ b/library/cpp/bit_io/bitinput_impl.h @@ -1,110 +1,110 @@  #pragma once  #include <util/generic/bitops.h> -#include <util/system/unaligned_mem.h>  +#include <util/system/unaligned_mem.h>  namespace NBitIO { -    class TBitInputImpl {  -        i64 RealStart;  -        i64 Start;  -        ui64 Length;  -        ui64 BOffset;  -        const ui32 FakeStart;  -        char Fake[16];  -        const i64 FStart;  - -    public:  -        TBitInputImpl(const char* start, const char* end)  -            : RealStart((i64)start)  -            , Start((i64)start)  -            , Length((end - start) << 3)  -            , BOffset(0)  -            , FakeStart(Length > 64 ? Length - 64 : 0)  -            , FStart((i64)Fake - (FakeStart >> 3))  -        {  -            memcpy(Fake, (const char*)(RealStart + (FakeStart >> 3)), (Length - FakeStart) >> 3);  -            Start = FakeStart ? RealStart : FStart;  -        }  +    class TBitInputImpl { +        i64 RealStart; +        i64 Start; +        ui64 Length; +        ui64 BOffset; +        const ui32 FakeStart; +        char Fake[16]; +        const i64 FStart; + +    public: +        TBitInputImpl(const char* start, const char* end) +            : RealStart((i64)start) +            , Start((i64)start) +            , Length((end - start) << 3) +            , BOffset(0) +            , FakeStart(Length > 64 ? Length - 64 : 0) +            , FStart((i64)Fake - (FakeStart >> 3)) +        { +            memcpy(Fake, (const char*)(RealStart + (FakeStart >> 3)), (Length - FakeStart) >> 3); +            Start = FakeStart ? RealStart : FStart; +        }          ui64 GetBitLength() const {              return Length;          } -    protected:  -        template <ui32 bits>  +    protected: +        template <ui32 bits>          Y_FORCE_INLINE bool ReadKImpl(ui64& result) {              result = (ReadUnaligned<ui64>((const void*)(Start + (BOffset >> 3))) >> (BOffset & 7)) & Mask64(bits); -            BOffset += bits;  -            if (BOffset < FakeStart)  -                return true;  -            if (BOffset > Length) {  -                result = 0;  -                BOffset -= bits;  -                return false;  -            }  -            Start = FStart;  +            BOffset += bits; +            if (BOffset < FakeStart) +                return true; +            if (BOffset > Length) { +                result = 0; +                BOffset -= bits; +                return false; +            } +            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;  -            if (BOffset < FakeStart)  -                return true;  +            result = (ReadUnaligned<ui64>((const void*)(Start + (BOffset >> 3))) >> (BOffset & 7)) & MaskLowerBits(bits); +            BOffset += bits; +            if (BOffset < FakeStart) +                return true;              if (Y_UNLIKELY(BOffset > Length)) { -                result = 0;  -                BOffset -= bits;  -                return false;  -            }  -            Start = FStart;  +                result = 0; +                BOffset -= bits; +                return false; +            } +            Start = FStart;              return true;          }          Y_FORCE_INLINE bool EofImpl() const { -            return BOffset >= Length;  -        }  +            return BOffset >= Length; +        }          Y_FORCE_INLINE ui64 BitOffset() const { -            return BOffset;  -        }  +            return BOffset; +        }          Y_FORCE_INLINE bool Seek(i64 offset) { -            if (offset < 0 || offset > (i64)Length)  -                return false;  -            BOffset = offset;  -            Start = BOffset < FakeStart ? RealStart : FStart;  -            return true;  -        }  - -    protected:  -        template <ui64 bits, typename T>  +            if (offset < 0 || offset > (i64)Length) +                return false; +            BOffset = offset; +            Start = BOffset < FakeStart ? RealStart : FStart; +            return true; +        } + +    protected: +        template <ui64 bits, typename T>          Y_FORCE_INLINE static void CopyToResultK(T& result, ui64 r64, ui64 skipbits) {              result = (result & ~(Mask64(bits) << skipbits)) | (r64 << skipbits); -        }  +        } -        template <typename T>  +        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>  +    public: +        template <ui64 bits>          Y_FORCE_INLINE bool ReadWordsImpl(ui64& data) { -            data = 0;  +            data = 0;              const ui64 haveMore = NthBit64(bits);              const ui64 mask = Mask64(bits); -            ui64 current = 0;  -            ui64 byteNo = 0;  +            ui64 current = 0; +            ui64 byteNo = 0; -            do {  -                if (!ReadKImpl<bits + 1>(current))  -                    return false;  +            do { +                if (!ReadKImpl<bits + 1>(current)) +                    return false; -                data |= (current & mask) << (byteNo++ * bits);  -            } while (current & haveMore);  +                data |= (current & mask) << (byteNo++ * bits); +            } while (current & haveMore); -            return true;  -        }  -    };  +            return true; +        } +    };  } diff --git a/library/cpp/bit_io/bitoutput.cpp b/library/cpp/bit_io/bitoutput.cpp index a28265aa0b4..d6c1c095dae 100644 --- a/library/cpp/bit_io/bitoutput.cpp +++ b/library/cpp/bit_io/bitoutput.cpp @@ -1 +1 @@ -#include "bitoutput.h"  +#include "bitoutput.h" diff --git a/library/cpp/bit_io/bitoutput.h b/library/cpp/bit_io/bitoutput.h index 0a6869b5d09..2b886c1f026 100644 --- a/library/cpp/bit_io/bitoutput.h +++ b/library/cpp/bit_io/bitoutput.h @@ -2,194 +2,194 @@  #include <library/cpp/deprecated/accessors/accessors.h> -#include <util/stream/output.h>  -#include <util/system/yassert.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>  namespace NBitIO { -    // Based on junk/solar/codecs/bitstream.h  +    // 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.  +    // 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;      } -    template <typename TStorage>  -    class TBitOutputBase {  -    protected:  -        TStorage* Storage;  -        ui64 FreeBits;  -        ui64 Active;  -        ui64 Offset;  - -    public:  -        TBitOutputBase(TStorage* storage)  -            : Storage(storage)  -            , FreeBits(64)  -            , Active()  -            , Offset()  -        {  -        }  - -        ui64 GetOffset() const {  +    template <typename TStorage> +    class TBitOutputBase { +    protected: +        TStorage* Storage; +        ui64 FreeBits; +        ui64 Active; +        ui64 Offset; + +    public: +        TBitOutputBase(TStorage* storage) +            : Storage(storage) +            , FreeBits(64) +            , Active() +            , Offset() +        { +        } + +        ui64 GetOffset() const {              return Offset + BytesUp(64ULL - FreeBits); -        }  +        } -        ui64 GetBitOffset() const {  -            return (64ULL - FreeBits) & 7ULL;  -        }  +        ui64 GetBitOffset() const { +            return (64ULL - FreeBits) & 7ULL; +        } -        ui64 GetByteReminder() const {  -            return FreeBits & 7ULL;  -        }  +        ui64 GetByteReminder() const { +            return FreeBits & 7ULL; +        } -    public:  -        // interface  +    public: +        // interface -        // Write "bits" lower bits.  +        // Write "bits" lower bits.          Y_FORCE_INLINE void Write(ui64 data, ui64 bits) { -            if (FreeBits < bits) {  -                if (FreeBits) {  -                    bits -= FreeBits;  - -                    Active |= (data & MaskLowerBits(FreeBits)) << (64ULL - FreeBits);  -                    data >>= FreeBits;  - -                    FreeBits = 0ULL;  -                }  -  -                Flush();  -            }  -  +            if (FreeBits < bits) { +                if (FreeBits) { +                    bits -= FreeBits; + +                    Active |= (data & MaskLowerBits(FreeBits)) << (64ULL - FreeBits); +                    data >>= FreeBits; + +                    FreeBits = 0ULL; +                } + +                Flush(); +            } +              Active |= bits ? ((data & MaskLowerBits(bits)) << (64ULL - FreeBits)) : 0; -            FreeBits -= bits;  +            FreeBits -= bits;          } -        // Write "bits" lower bits starting from "skipbits" bit.  +        // Write "bits" lower bits starting from "skipbits" bit.          Y_FORCE_INLINE void Write(ui64 data, ui64 bits, ui64 skipbits) { -            Write(data >> skipbits, bits);  -        }  +            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  -        template <ui64 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 +        template <ui64 bits>          Y_FORCE_INLINE void WriteWords(ui64 data) { -            do {  -                ui64 part = data;  +            do { +                ui64 part = data; -                data >>= bits;  +                data >>= bits;                  part |= FastZeroIfFalse(data, NthBit64(bits)); -                Write(part, bits + 1ULL);  -            } while (data);  -        }  +                Write(part, bits + 1ULL); +            } while (data); +        }          Y_FORCE_INLINE ui64 /* padded bits */ Flush() { -            const ui64 ubytes = 8ULL - (FreeBits >> 3ULL);  +            const ui64 ubytes = 8ULL - (FreeBits >> 3ULL); -            if (ubytes) {  -                Active <<= FreeBits;  -                Active >>= FreeBits;  +            if (ubytes) { +                Active <<= FreeBits; +                Active >>= FreeBits; -                Storage->WriteData((const char*)&Active, (const char*)&Active + ubytes);  -                Offset += ubytes;  -            }  +                Storage->WriteData((const char*)&Active, (const char*)&Active + ubytes); +                Offset += ubytes; +            } -            const ui64 padded = FreeBits & 7;  +            const ui64 padded = FreeBits & 7; -            FreeBits = 64ULL;  -            Active = 0ULL;  +            FreeBits = 64ULL; +            Active = 0ULL; -            return padded;  -        }  +            return padded; +        } -        virtual ~TBitOutputBase() {  -            Flush();  -        }  +        virtual ~TBitOutputBase() { +            Flush(); +        }      private:          static Y_FORCE_INLINE ui64 FastZeroIfFalse(bool cond, ui64 iftrue) {              return -i64(cond) & iftrue;          } -    };  +    }; -    template <typename TVec>  -    class TBitOutputVectorImpl {  -        TVec* Data;  +    template <typename TVec> +    class TBitOutputVectorImpl { +        TVec* Data; -    public:  -        void WriteData(const char* begin, const char* end) {  +    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)  -            : TBitOutputVectorImpl<TVec>(data)  -            , TBitOutputBase<TBitOutputVectorImpl<TVec>>(this)  -        {  -        }  -    };  -  -    class TBitOutputArrayImpl {  -        char* Data;  -        size_t Left;  - -    public:  -        void WriteData(const char* begin, const char* end) {  -            size_t sz = end - begin;  +        } + +        TBitOutputVectorImpl(TVec* data) +            : Data(data) +        { +        } +    }; + +    template <typename TVec> +    struct TBitOutputVector: public TBitOutputVectorImpl<TVec>, public TBitOutputBase<TBitOutputVectorImpl<TVec>> { +        inline TBitOutputVector(TVec* data) +            : TBitOutputVectorImpl<TVec>(data) +            , TBitOutputBase<TBitOutputVectorImpl<TVec>>(this) +        { +        } +    }; + +    class TBitOutputArrayImpl { +        char* Data; +        size_t Left; + +    public: +        void WriteData(const char* begin, const char* end) { +            size_t sz = end - begin;              Y_VERIFY(sz <= Left, " "); -            memcpy(Data, begin, sz);  -            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)  -            , TBitOutputBase<TBitOutputArrayImpl>(this)  -        {  -        }  -    };  -  +            memcpy(Data, begin, sz); +            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) +            , TBitOutputBase<TBitOutputArrayImpl>(this) +        { +        } +    }; +      using TBitOutputYVector = TBitOutputVector<TVector<char>>; -    class TBitOutputStreamImpl {  +    class TBitOutputStreamImpl {          IOutputStream* Out; -    public:  -        void WriteData(const char* begin, const char* end) {  -            Out->Write(begin, end - begin);  -        }  +    public: +        void WriteData(const char* begin, const char* end) { +            Out->Write(begin, end - begin); +        }          TBitOutputStreamImpl(IOutputStream* out) -            : Out(out)  -        {  -        }  -    };  -  -    struct TBitOutputStream: public TBitOutputStreamImpl, public TBitOutputBase<TBitOutputStreamImpl> {  +            : Out(out) +        { +        } +    }; + +    struct TBitOutputStream: public TBitOutputStreamImpl, public TBitOutputBase<TBitOutputStreamImpl> {          inline TBitOutputStream(IOutputStream* out) -            : TBitOutputStreamImpl(out)  -            , TBitOutputBase<TBitOutputStreamImpl>(this)  -        {  -        }  -    };  +            : TBitOutputStreamImpl(out) +            , TBitOutputBase<TBitOutputStreamImpl>(this) +        { +        } +    };  } diff --git a/library/cpp/bit_io/ut/ya.make b/library/cpp/bit_io/ut/ya.make index 758fccad62a..07ee5b4997c 100644 --- a/library/cpp/bit_io/ut/ya.make +++ b/library/cpp/bit_io/ut/ya.make @@ -1,12 +1,12 @@  UNITTEST_FOR(library/cpp/bit_io) -  -OWNER(  + +OWNER(      velavokr      g:util -)  -  -SRCS(  -    bitinout_ut.cpp  -)  -  -END()  +) + +SRCS( +    bitinout_ut.cpp +) + +END() diff --git a/library/cpp/bit_io/ya.make b/library/cpp/bit_io/ya.make index cfc83f145e8..df1de81ff9e 100644 --- a/library/cpp/bit_io/ya.make +++ b/library/cpp/bit_io/ya.make @@ -1,18 +1,18 @@ -LIBRARY()  -  -OWNER(  -    velavokr  +LIBRARY() + +OWNER( +    velavokr      g:util -)  -  -PEERDIR(  +) + +PEERDIR(      library/cpp/deprecated/accessors -)  -  -SRCS(  -    bitinput.cpp  -    bitinput_impl.cpp  -    bitoutput.cpp  -)  -  -END()  +) + +SRCS( +    bitinput.cpp +    bitinput_impl.cpp +    bitoutput.cpp +) + +END()  | 
