diff options
author | pkalinnikov <pkalinnikov@yandex-team.ru> | 2022-02-10 16:50:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:15 +0300 |
commit | d507a9366b2ab84411afe63fea9fba5498891e1b (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp | |
parent | 9e33e026829d561d6fd46d72b88c367952e08075 (diff) | |
download | ydb-d507a9366b2ab84411afe63fea9fba5498891e1b.tar.gz |
Restoring authorship annotation for <pkalinnikov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/actors/core/actorid.cpp | 6 | ||||
-rw-r--r-- | library/cpp/containers/bitseq/bititerator.h | 258 | ||||
-rw-r--r-- | library/cpp/containers/bitseq/bititerator_ut.cpp | 196 | ||||
-rw-r--r-- | library/cpp/containers/bitseq/bitvector.h | 266 | ||||
-rw-r--r-- | library/cpp/containers/bitseq/bitvector_ut.cpp | 118 | ||||
-rw-r--r-- | library/cpp/containers/bitseq/traits.h | 46 | ||||
-rw-r--r-- | library/cpp/containers/bitseq/ut/ya.make | 16 | ||||
-rw-r--r-- | library/cpp/containers/bitseq/ya.make | 8 | ||||
-rw-r--r-- | library/cpp/packedtypes/longs.h | 16 | ||||
-rw-r--r-- | library/cpp/packedtypes/longs_ut.cpp | 86 | ||||
-rw-r--r-- | library/cpp/packedtypes/ut/ya.make | 2 | ||||
-rw-r--r-- | library/cpp/packedtypes/ya.make | 2 | ||||
-rw-r--r-- | library/cpp/packedtypes/zigzag.h | 24 | ||||
-rw-r--r-- | library/cpp/packedtypes/zigzag_ut.cpp | 70 |
14 files changed, 557 insertions, 557 deletions
diff --git a/library/cpp/actors/core/actorid.cpp b/library/cpp/actors/core/actorid.cpp index 8de83ebc0d..ccda035eac 100644 --- a/library/cpp/actors/core/actorid.cpp +++ b/library/cpp/actors/core/actorid.cpp @@ -1,6 +1,6 @@ #include "actorid.h" #include <util/string/builder.h> -#include <util/string/cast.h> +#include <util/string/cast.h> namespace NActors { void TActorId::Out(IOutputStream& o) const { @@ -26,9 +26,9 @@ namespace NActors { semicolons[1] = str.find(':', semicolons[0] + 1); if (semicolons[1] == TStringBuf::npos) return false; - + bool success = TryFromString(buf + 1, semicolons[0] - 1, Raw.N.NodeId) && TryFromString(buf + semicolons[0] + 1, semicolons[1] - semicolons[0] - 1, Raw.N.LocalId) && TryFromString(buf + semicolons[1] + 1, sz - semicolons[1] - 2, Raw.N.Hint); - + return success; } } diff --git a/library/cpp/containers/bitseq/bititerator.h b/library/cpp/containers/bitseq/bititerator.h index 616c9bbac4..52dadd3798 100644 --- a/library/cpp/containers/bitseq/bititerator.h +++ b/library/cpp/containers/bitseq/bititerator.h @@ -1,138 +1,138 @@ -#pragma once - -#include "traits.h" - +#pragma once + +#include "traits.h" + #include <library/cpp/pop_count/popcount.h> -template <typename T> -class TBitIterator { -public: - using TWord = T; - using TTraits = TBitSeqTraits<TWord>; - -public: - TBitIterator(const T* data = nullptr) - : Current(0) - , Mask(0) - , Data(data) +template <typename T> +class TBitIterator { +public: + using TWord = T; + using TTraits = TBitSeqTraits<TWord>; + +public: + TBitIterator(const T* data = nullptr) + : Current(0) + , Mask(0) + , Data(data) { } - - /// Get the word next to the one we are currenlty iterating over. - const TWord* NextWord() const { - return Data; - } - - /// Get the next bit without moving the iterator. - bool Peek() const { - return Mask ? (Current & Mask) : (*Data & 1); - } - - /// Get the next bit and move forward. - /// TODO: Implement inversed iteration as well. - bool Next() { - if (!Mask) { - Current = *Data++; - Mask = 1; - } - const bool bit = Current & Mask; - Mask <<= 1; - return bit; - } - - /// Get the next count bits without moving the iterator. - TWord Peek(ui8 count) const { - if (!count) - return 0; + + /// Get the word next to the one we are currenlty iterating over. + const TWord* NextWord() const { + return Data; + } + + /// Get the next bit without moving the iterator. + bool Peek() const { + return Mask ? (Current & Mask) : (*Data & 1); + } + + /// Get the next bit and move forward. + /// TODO: Implement inversed iteration as well. + bool Next() { + if (!Mask) { + Current = *Data++; + Mask = 1; + } + const bool bit = Current & Mask; + Mask <<= 1; + return bit; + } + + /// Get the next count bits without moving the iterator. + TWord Peek(ui8 count) const { + if (!count) + return 0; Y_VERIFY_DEBUG(count <= TTraits::NumBits); - - if (!Mask) - return *Data & TTraits::ElemMask(count); - + + if (!Mask) + return *Data & TTraits::ElemMask(count); + auto usedBits = (size_t)PopCount(Mask - 1); - TWord result = Current >> usedBits; - auto leftInCurrent = TTraits::NumBits - usedBits; - if (count <= leftInCurrent) - return result & TTraits::ElemMask(count); - - count -= leftInCurrent; - result |= (*Data & TTraits::ElemMask(count)) << leftInCurrent; - return result; - } - - /// Get the next count bits and move forward by count bits. - TWord Read(ui8 count) { - if (!count) - return 0; + TWord result = Current >> usedBits; + auto leftInCurrent = TTraits::NumBits - usedBits; + if (count <= leftInCurrent) + return result & TTraits::ElemMask(count); + + count -= leftInCurrent; + result |= (*Data & TTraits::ElemMask(count)) << leftInCurrent; + return result; + } + + /// Get the next count bits and move forward by count bits. + TWord Read(ui8 count) { + if (!count) + return 0; Y_VERIFY_DEBUG(count <= TTraits::NumBits); - - if (!Mask) { - Current = *Data++; - Mask = 1 << count; - return Current & TTraits::ElemMask(count); - } - + + if (!Mask) { + Current = *Data++; + Mask = 1 << count; + return Current & TTraits::ElemMask(count); + } + auto usedBits = (size_t)PopCount(Mask - 1); - TWord result = Current >> usedBits; - auto leftInCurrent = TTraits::NumBits - usedBits; - if (count < leftInCurrent) { - Mask <<= count; - return result & TTraits::ElemMask(count); - } - - count -= leftInCurrent; - if (count) { - Current = *Data++; - Mask = 1 << count; - result |= (Current & TTraits::ElemMask(count)) << leftInCurrent; - } else { - Mask = 0; - } - - return result; - } - - /// Move the iterator forward by count bits. - void Forward(int count) { - if (!count) - return; - + TWord result = Current >> usedBits; + auto leftInCurrent = TTraits::NumBits - usedBits; + if (count < leftInCurrent) { + Mask <<= count; + return result & TTraits::ElemMask(count); + } + + count -= leftInCurrent; + if (count) { + Current = *Data++; + Mask = 1 << count; + result |= (Current & TTraits::ElemMask(count)) << leftInCurrent; + } else { + Mask = 0; + } + + return result; + } + + /// Move the iterator forward by count bits. + void Forward(int count) { + if (!count) + return; + int leftInCurrent = (size_t)PopCount(~(Mask - 1)); - if (count < leftInCurrent) { - Mask <<= count; - return; - } - - count -= leftInCurrent; - Data += count >> TTraits::DivShift; - auto remainder = count & TTraits::ModMask; - - if (remainder) { - Current = *Data++; - Mask = 1 << remainder; - } else { - Current = 0; - Mask = 0; - } - } - - /// Skip trailing bits of the current word and move by count words forward. - void Align(int count = 0) { - Current = 0; - if (Mask) - Mask = 0; - Data += count; - } - - /// Initialize the iterator. - void Reset(const TWord* data) { - Current = 0; - Mask = 0; - Data = data; - } - -private: - TWord Current; - TWord Mask; - const TWord* Data; -}; + if (count < leftInCurrent) { + Mask <<= count; + return; + } + + count -= leftInCurrent; + Data += count >> TTraits::DivShift; + auto remainder = count & TTraits::ModMask; + + if (remainder) { + Current = *Data++; + Mask = 1 << remainder; + } else { + Current = 0; + Mask = 0; + } + } + + /// Skip trailing bits of the current word and move by count words forward. + void Align(int count = 0) { + Current = 0; + if (Mask) + Mask = 0; + Data += count; + } + + /// Initialize the iterator. + void Reset(const TWord* data) { + Current = 0; + Mask = 0; + Data = data; + } + +private: + TWord Current; + TWord Mask; + const TWord* Data; +}; diff --git a/library/cpp/containers/bitseq/bititerator_ut.cpp b/library/cpp/containers/bitseq/bititerator_ut.cpp index 5f34e0de84..ed0925866f 100644 --- a/library/cpp/containers/bitseq/bititerator_ut.cpp +++ b/library/cpp/containers/bitseq/bititerator_ut.cpp @@ -1,109 +1,109 @@ -#include "bititerator.h" - +#include "bititerator.h" + #include <library/cpp/testing/unittest/registar.h> -#include <util/generic/vector.h> - +#include <util/generic/vector.h> + Y_UNIT_TEST_SUITE(TBitIteratorTest) { TVector<ui16> GenWords() { TVector<ui16> words(1, 0); - for (ui16 word = 1; word; ++word) - words.push_back(word); - return words; - } - - template <typename TWord> + for (ui16 word = 1; word; ++word) + words.push_back(word); + return words; + } + + template <typename TWord> void AssertPeekRead(TBitIterator<TWord> & iter, ui8 count, TWord expected) { - auto peek = iter.Peek(count); - auto read = iter.Read(count); - UNIT_ASSERT_EQUAL(peek, read); - UNIT_ASSERT_EQUAL(peek, expected); - } - + auto peek = iter.Peek(count); + auto read = iter.Read(count); + UNIT_ASSERT_EQUAL(peek, read); + UNIT_ASSERT_EQUAL(peek, expected); + } + Y_UNIT_TEST(TestNextAndPeek) { - const auto& words = GenWords(); - - TBitIterator<ui16> iter(words.data()); - ui16 word = 0; - for (int i = 0; i != (1 << 16); ++i, ++word) { - for (int bit = 0; bit != 16; ++bit) { - auto peek = iter.Peek(); - auto next = iter.Next(); - UNIT_ASSERT_EQUAL(peek, next); - UNIT_ASSERT_EQUAL(peek, (word >> bit) & 1); - } - UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + i + 1); - } - - UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + words.size()); - } - + const auto& words = GenWords(); + + TBitIterator<ui16> iter(words.data()); + ui16 word = 0; + for (int i = 0; i != (1 << 16); ++i, ++word) { + for (int bit = 0; bit != 16; ++bit) { + auto peek = iter.Peek(); + auto next = iter.Next(); + UNIT_ASSERT_EQUAL(peek, next); + UNIT_ASSERT_EQUAL(peek, (word >> bit) & 1); + } + UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + i + 1); + } + + UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + words.size()); + } + Y_UNIT_TEST(TestAlignedReadAndPeek) { - const auto& words = GenWords(); - - TBitIterator<ui16> iter(words.data()); - ui16 word = 0; - for (int i = 0; i != (1 << 16); ++i, ++word) { - AssertPeekRead(iter, 16, word); - UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + i + 1); - } - - UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + words.size()); - } - + const auto& words = GenWords(); + + TBitIterator<ui16> iter(words.data()); + ui16 word = 0; + for (int i = 0; i != (1 << 16); ++i, ++word) { + AssertPeekRead(iter, 16, word); + UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + i + 1); + } + + UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + words.size()); + } + Y_UNIT_TEST(TestForward) { TVector<ui32> words; - words.push_back((1 << 10) | (1 << 20) | (1 << 25)); - words.push_back(1 | (1 << 5) | (1 << 6) | (1 << 30)); - for (int i = 0; i < 3; ++i) - words.push_back(0); - words.push_back(1 << 10); - - TBitIterator<ui32> iter(words.data()); - UNIT_ASSERT(!iter.Next()); - UNIT_ASSERT(!iter.Next()); - UNIT_ASSERT(!iter.Next()); - iter.Forward(6); - UNIT_ASSERT(!iter.Next()); - UNIT_ASSERT(iter.Next()); - UNIT_ASSERT(!iter.Next()); - iter.Forward(8); - UNIT_ASSERT(iter.Next()); - iter.Forward(4); - UNIT_ASSERT(iter.Next()); - iter.Forward(5); - UNIT_ASSERT(!iter.Next()); - UNIT_ASSERT(iter.Next()); - iter.Forward(4); - UNIT_ASSERT(iter.Next()); - - iter.Reset(words.data()); - iter.Forward(38); - UNIT_ASSERT(iter.Next()); - UNIT_ASSERT(!iter.Next()); - UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + 2); - - iter.Forward(24 + 32 * 3 + 9); - UNIT_ASSERT(!iter.Next()); - UNIT_ASSERT(iter.Next()); - UNIT_ASSERT(!iter.Next()); - UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + 6); - } - + words.push_back((1 << 10) | (1 << 20) | (1 << 25)); + words.push_back(1 | (1 << 5) | (1 << 6) | (1 << 30)); + for (int i = 0; i < 3; ++i) + words.push_back(0); + words.push_back(1 << 10); + + TBitIterator<ui32> iter(words.data()); + UNIT_ASSERT(!iter.Next()); + UNIT_ASSERT(!iter.Next()); + UNIT_ASSERT(!iter.Next()); + iter.Forward(6); + UNIT_ASSERT(!iter.Next()); + UNIT_ASSERT(iter.Next()); + UNIT_ASSERT(!iter.Next()); + iter.Forward(8); + UNIT_ASSERT(iter.Next()); + iter.Forward(4); + UNIT_ASSERT(iter.Next()); + iter.Forward(5); + UNIT_ASSERT(!iter.Next()); + UNIT_ASSERT(iter.Next()); + iter.Forward(4); + UNIT_ASSERT(iter.Next()); + + iter.Reset(words.data()); + iter.Forward(38); + UNIT_ASSERT(iter.Next()); + UNIT_ASSERT(!iter.Next()); + UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + 2); + + iter.Forward(24 + 32 * 3 + 9); + UNIT_ASSERT(!iter.Next()); + UNIT_ASSERT(iter.Next()); + UNIT_ASSERT(!iter.Next()); + UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + 6); + } + Y_UNIT_TEST(TestUnalignedReadAndPeek) { TVector<ui32> words; - words.push_back((1 << 10) | (1 << 20) | (1 << 25)); - words.push_back(1 | (1 << 5) | (1 << 6) | (1 << 30)); - for (int i = 0; i < 5; ++i) - words.push_back(1 | (1 << 10)); - - TBitIterator<ui32> iter(words.data()); - AssertPeekRead(iter, 5, ui32(0)); - AssertPeekRead(iter, 7, ui32(1 << 5)); - AssertPeekRead(iter, 21, ui32((1 << 8) | (1 << 13) | (1 << 20))); - AssertPeekRead(iter, 32, (words[1] >> 1) | (1 << 31)); - iter.Forward(8); - UNIT_ASSERT(!iter.Next()); - UNIT_ASSERT(iter.Next()); - UNIT_ASSERT(!iter.Next()); - } -} + words.push_back((1 << 10) | (1 << 20) | (1 << 25)); + words.push_back(1 | (1 << 5) | (1 << 6) | (1 << 30)); + for (int i = 0; i < 5; ++i) + words.push_back(1 | (1 << 10)); + + TBitIterator<ui32> iter(words.data()); + AssertPeekRead(iter, 5, ui32(0)); + AssertPeekRead(iter, 7, ui32(1 << 5)); + AssertPeekRead(iter, 21, ui32((1 << 8) | (1 << 13) | (1 << 20))); + AssertPeekRead(iter, 32, (words[1] >> 1) | (1 << 31)); + iter.Forward(8); + UNIT_ASSERT(!iter.Next()); + UNIT_ASSERT(iter.Next()); + UNIT_ASSERT(!iter.Next()); + } +} diff --git a/library/cpp/containers/bitseq/bitvector.h b/library/cpp/containers/bitseq/bitvector.h index 87ed1560b8..3f8fd81ee5 100644 --- a/library/cpp/containers/bitseq/bitvector.h +++ b/library/cpp/containers/bitseq/bitvector.h @@ -1,158 +1,158 @@ -#pragma once - -#include "traits.h" - +#pragma once + +#include "traits.h" + #include <library/cpp/pop_count/popcount.h> -#include <util/generic/vector.h> -#include <util/ysaveload.h> - -template <typename T> +#include <util/generic/vector.h> +#include <util/ysaveload.h> + +template <typename T> class TReadonlyBitVector; template <typename T> -class TBitVector { -public: - using TWord = T; - using TTraits = TBitSeqTraits<TWord>; - -private: +class TBitVector { +public: + using TWord = T; + using TTraits = TBitSeqTraits<TWord>; + +private: friend class TReadonlyBitVector<T>; - ui64 Size_; + ui64 Size_; TVector<TWord> Data_; - -public: - TBitVector() - : Size_(0) - , Data_(0) - { - } - - TBitVector(ui64 size) - : Size_(size) - , Data_(static_cast<size_t>((Size_ + TTraits::ModMask) >> TTraits::DivShift), 0) - { - } - + +public: + TBitVector() + : Size_(0) + , Data_(0) + { + } + + TBitVector(ui64 size) + : Size_(size) + , Data_(static_cast<size_t>((Size_ + TTraits::ModMask) >> TTraits::DivShift), 0) + { + } + virtual ~TBitVector() = default; - - void Clear() { - Size_ = 0; - Data_.clear(); - } - - void Resize(ui64 size) { - Size_ = size; - Data_.resize((Size_ + TTraits::ModMask) >> TTraits::DivShift); - } - - void Swap(TBitVector& other) { - DoSwap(Size_, other.Size_); - DoSwap(Data_, other.Data_); - } - - bool Set(ui64 pos) { + + void Clear() { + Size_ = 0; + Data_.clear(); + } + + void Resize(ui64 size) { + Size_ = size; + Data_.resize((Size_ + TTraits::ModMask) >> TTraits::DivShift); + } + + void Swap(TBitVector& other) { + DoSwap(Size_, other.Size_); + DoSwap(Data_, other.Data_); + } + + bool Set(ui64 pos) { Y_ASSERT(pos < Size_); - TWord& val = Data_[pos >> TTraits::DivShift]; - if (val & TTraits::BitMask(pos & TTraits::ModMask)) - return false; - val |= TTraits::BitMask(pos & TTraits::ModMask); - return true; - } - - bool Test(ui64 pos) const { + TWord& val = Data_[pos >> TTraits::DivShift]; + if (val & TTraits::BitMask(pos & TTraits::ModMask)) + return false; + val |= TTraits::BitMask(pos & TTraits::ModMask); + return true; + } + + bool Test(ui64 pos) const { return TTraits::Test(Data(), pos, Size_); - } - - void Reset(ui64 pos) { + } + + void Reset(ui64 pos) { Y_ASSERT(pos < Size_); - Data_[pos >> TTraits::DivShift] &= ~TTraits::BitMask(pos & TTraits::ModMask); - } - - TWord Get(ui64 pos, ui8 width, TWord mask) const { + Data_[pos >> TTraits::DivShift] &= ~TTraits::BitMask(pos & TTraits::ModMask); + } + + TWord Get(ui64 pos, ui8 width, TWord mask) const { return TTraits::Get(Data(), pos, width, mask, Size_); - } - - TWord Get(ui64 pos, ui8 width) const { - return Get(pos, width, TTraits::ElemMask(width)); - } - - void Set(ui64 pos, TWord value, ui8 width, TWord mask) { + } + + TWord Get(ui64 pos, ui8 width) const { + return Get(pos, width, TTraits::ElemMask(width)); + } + + void Set(ui64 pos, TWord value, ui8 width, TWord mask) { if (!width) return; Y_ASSERT((pos + width) <= Size_); - size_t word = pos >> TTraits::DivShift; - TWord shift1 = pos & TTraits::ModMask; - TWord shift2 = TTraits::NumBits - shift1; - Data_[word] &= ~(mask << shift1); - Data_[word] |= (value & mask) << shift1; - if (shift2 < width) { + size_t word = pos >> TTraits::DivShift; + TWord shift1 = pos & TTraits::ModMask; + TWord shift2 = TTraits::NumBits - shift1; + Data_[word] &= ~(mask << shift1); + Data_[word] |= (value & mask) << shift1; + if (shift2 < width) { Data_[word + 1] &= ~(mask >> shift2); Data_[word + 1] |= (value & mask) >> shift2; - } - } - - void Set(ui64 pos, TWord value, ui8 width) { - Set(pos, value, width, TTraits::ElemMask(width)); - } - - void Append(TWord value, ui8 width, TWord mask) { + } + } + + void Set(ui64 pos, TWord value, ui8 width) { + Set(pos, value, width, TTraits::ElemMask(width)); + } + + void Append(TWord value, ui8 width, TWord mask) { if (!width) return; - if (Data_.size() * TTraits::NumBits < Size_ + width) { - Data_.push_back(0); - } - Size_ += width; - Set(Size_ - width, value, width, mask); - } - - void Append(TWord value, ui8 width) { - Append(value, width, TTraits::ElemMask(width)); - } - - size_t Count() const { - size_t count = 0; - for (size_t i = 0; i < Data_.size(); ++i) { + if (Data_.size() * TTraits::NumBits < Size_ + width) { + Data_.push_back(0); + } + Size_ += width; + Set(Size_ - width, value, width, mask); + } + + void Append(TWord value, ui8 width) { + Append(value, width, TTraits::ElemMask(width)); + } + + size_t Count() const { + size_t count = 0; + for (size_t i = 0; i < Data_.size(); ++i) { count += (size_t)PopCount(Data_[i]); - } - return count; - } - - ui64 Size() const { - return Size_; - } - - size_t Words() const { - return Data_.size(); - } - - const TWord* Data() const { + } + return count; + } + + ui64 Size() const { + return Size_; + } + + size_t Words() const { + return Data_.size(); + } + + const TWord* Data() const { return Data_.data(); - } - + } + void Save(IOutputStream* out) const { - ::Save(out, Size_); - ::Save(out, Data_); - } - + ::Save(out, Size_); + ::Save(out, Data_); + } + void Load(IInputStream* inp) { - ::Load(inp, Size_); - ::Load(inp, Data_); - } - - ui64 Space() const { - return CHAR_BIT * (sizeof(Size_) + + ::Load(inp, Size_); + ::Load(inp, Data_); + } + + ui64 Space() const { + return CHAR_BIT * (sizeof(Size_) + Data_.size() * sizeof(TWord)); - } - + } + void Print(IOutputStream& out, size_t truncate = 128) { - for (size_t i = 0; i < Data_.size() && i < truncate; ++i) { - for (int j = TTraits::NumBits - 1; j >= 0; --j) { - size_t pos = TTraits::NumBits * i + j; - out << (pos < Size_ && Test(pos) ? '1' : '0'); - } - out << " "; - } - out << Endl; - } -}; + for (size_t i = 0; i < Data_.size() && i < truncate; ++i) { + for (int j = TTraits::NumBits - 1; j >= 0; --j) { + size_t pos = TTraits::NumBits * i + j; + out << (pos < Size_ && Test(pos) ? '1' : '0'); + } + out << " "; + } + out << Endl; + } +}; diff --git a/library/cpp/containers/bitseq/bitvector_ut.cpp b/library/cpp/containers/bitseq/bitvector_ut.cpp index e0598dbceb..6137adab1e 100644 --- a/library/cpp/containers/bitseq/bitvector_ut.cpp +++ b/library/cpp/containers/bitseq/bitvector_ut.cpp @@ -1,71 +1,71 @@ -#include "bitvector.h" +#include "bitvector.h" #include "readonly_bitvector.h" - + #include <library/cpp/testing/unittest/registar.h> - + #include <util/memory/blob.h> #include <util/stream/buffer.h> Y_UNIT_TEST_SUITE(TBitVectorTest) { Y_UNIT_TEST(TestEmpty) { - TBitVector<ui64> v64; - UNIT_ASSERT_EQUAL(v64.Size(), 0); - UNIT_ASSERT_EQUAL(v64.Words(), 0); - - TBitVector<ui32> v32(0); - UNIT_ASSERT_EQUAL(v32.Size(), 0); - UNIT_ASSERT_EQUAL(v32.Words(), 0); - } - + TBitVector<ui64> v64; + UNIT_ASSERT_EQUAL(v64.Size(), 0); + UNIT_ASSERT_EQUAL(v64.Words(), 0); + + TBitVector<ui32> v32(0); + UNIT_ASSERT_EQUAL(v32.Size(), 0); + UNIT_ASSERT_EQUAL(v32.Words(), 0); + } + Y_UNIT_TEST(TestOneWord) { - TBitVector<ui32> v; - v.Append(1, 1); - v.Append(0, 1); - v.Append(1, 3); - v.Append(10, 4); - v.Append(100500, 20); - - UNIT_ASSERT_EQUAL(v.Get(0, 1), 1); - UNIT_ASSERT(v.Test(0)); - UNIT_ASSERT_EQUAL(v.Get(1, 1), 0); - UNIT_ASSERT_EQUAL(v.Get(2, 3), 1); - UNIT_ASSERT_EQUAL(v.Get(5, 4), 10); - UNIT_ASSERT_EQUAL(v.Get(9, 20), 100500); - - v.Reset(0); - v.Set(9, 1234, 15); - UNIT_ASSERT_EQUAL(v.Get(0, 1), 0); - UNIT_ASSERT(!v.Test(0)); - UNIT_ASSERT_EQUAL(v.Get(9, 15), 1234); - - UNIT_ASSERT_EQUAL(v.Size(), 29); - UNIT_ASSERT_EQUAL(v.Words(), 1); - } - + TBitVector<ui32> v; + v.Append(1, 1); + v.Append(0, 1); + v.Append(1, 3); + v.Append(10, 4); + v.Append(100500, 20); + + UNIT_ASSERT_EQUAL(v.Get(0, 1), 1); + UNIT_ASSERT(v.Test(0)); + UNIT_ASSERT_EQUAL(v.Get(1, 1), 0); + UNIT_ASSERT_EQUAL(v.Get(2, 3), 1); + UNIT_ASSERT_EQUAL(v.Get(5, 4), 10); + UNIT_ASSERT_EQUAL(v.Get(9, 20), 100500); + + v.Reset(0); + v.Set(9, 1234, 15); + UNIT_ASSERT_EQUAL(v.Get(0, 1), 0); + UNIT_ASSERT(!v.Test(0)); + UNIT_ASSERT_EQUAL(v.Get(9, 15), 1234); + + UNIT_ASSERT_EQUAL(v.Size(), 29); + UNIT_ASSERT_EQUAL(v.Words(), 1); + } + Y_UNIT_TEST(TestManyWords) { - static const int BITS = 10; - TBitVector<ui64> v; - - for (int i = 0, end = (1 << BITS); i < end; ++i) - v.Append(i, BITS); - - UNIT_ASSERT_EQUAL(v.Size(), BITS * (1 << BITS)); - UNIT_ASSERT_EQUAL(v.Words(), (v.Size() + 63) / 64); - for (int i = 0, end = (1 << BITS); i < end; ++i) - UNIT_ASSERT_EQUAL(v.Get(i * BITS, BITS), (ui64)i); - } - + static const int BITS = 10; + TBitVector<ui64> v; + + for (int i = 0, end = (1 << BITS); i < end; ++i) + v.Append(i, BITS); + + UNIT_ASSERT_EQUAL(v.Size(), BITS * (1 << BITS)); + UNIT_ASSERT_EQUAL(v.Words(), (v.Size() + 63) / 64); + for (int i = 0, end = (1 << BITS); i < end; ++i) + UNIT_ASSERT_EQUAL(v.Get(i * BITS, BITS), (ui64)i); + } + Y_UNIT_TEST(TestMaxWordSize) { - TBitVector<ui32> v; - for (int i = 0; i < 100; ++i) - v.Append(i, 32); - - for (int i = 0; i < 100; ++i) - UNIT_ASSERT_EQUAL(v.Get(i * 32, 32), (ui32)i); - - v.Set(10 * 32, 100500, 32); - UNIT_ASSERT_EQUAL(v.Get(10 * 32, 32), 100500); - } + TBitVector<ui32> v; + for (int i = 0; i < 100; ++i) + v.Append(i, 32); + + for (int i = 0; i < 100; ++i) + UNIT_ASSERT_EQUAL(v.Get(i * 32, 32), (ui32)i); + + v.Set(10 * 32, 100500, 32); + UNIT_ASSERT_EQUAL(v.Get(10 * 32, 32), 100500); + } Y_UNIT_TEST(TestReadonlyVector) { TBitVector<ui64> v(100); @@ -83,4 +83,4 @@ Y_UNIT_TEST_SUITE(TBitVectorTest) { UNIT_ASSERT_VALUES_EQUAL(rv.Test(i), i % 3 == 0); } } -} +} diff --git a/library/cpp/containers/bitseq/traits.h b/library/cpp/containers/bitseq/traits.h index 9136d6e972..2330b1b4f2 100644 --- a/library/cpp/containers/bitseq/traits.h +++ b/library/cpp/containers/bitseq/traits.h @@ -1,30 +1,30 @@ -#pragma once - +#pragma once + #include <util/generic/bitops.h> -#include <util/generic/typetraits.h> +#include <util/generic/typetraits.h> #include <util/system/yassert.h> - -template <typename TWord> -struct TBitSeqTraits { + +template <typename TWord> +struct TBitSeqTraits { static constexpr ui8 NumBits = CHAR_BIT * sizeof(TWord); static constexpr TWord ModMask = static_cast<TWord>(NumBits - 1); static constexpr TWord DivShift = MostSignificantBitCT(NumBits); - - static inline TWord ElemMask(ui8 count) { - // NOTE: Shifting by the type's length is UB, so we need this workaround. + + static inline TWord ElemMask(ui8 count) { + // NOTE: Shifting by the type's length is UB, so we need this workaround. if (Y_LIKELY(count)) - return TWord(-1) >> (NumBits - count); - return 0; - } - - static inline TWord BitMask(ui8 pos) { - return TWord(1) << pos; - } - - static size_t NumOfWords(size_t bits) { - return (bits + NumBits - 1) >> DivShift; - } - + return TWord(-1) >> (NumBits - count); + return 0; + } + + static inline TWord BitMask(ui8 pos) { + return TWord(1) << pos; + } + + static size_t NumOfWords(size_t bits) { + return (bits + NumBits - 1) >> DivShift; + } + static bool Test(const TWord* data, ui64 pos, ui64 size) { Y_ASSERT(pos < size); return data[pos >> DivShift] & BitMask(pos & ModMask); @@ -45,5 +45,5 @@ struct TBitSeqTraits { } static_assert(std::is_unsigned<TWord>::value, "Expected std::is_unsigned<T>::value."); - static_assert((NumBits & (NumBits - 1)) == 0, "NumBits should be a power of 2."); -}; + static_assert((NumBits & (NumBits - 1)) == 0, "NumBits should be a power of 2."); +}; diff --git a/library/cpp/containers/bitseq/ut/ya.make b/library/cpp/containers/bitseq/ut/ya.make index 9485ead99e..7155e82c06 100644 --- a/library/cpp/containers/bitseq/ut/ya.make +++ b/library/cpp/containers/bitseq/ut/ya.make @@ -1,10 +1,10 @@ UNITTEST_FOR(library/cpp/containers/bitseq) - + OWNER(g:util) - -SRCS( - bititerator_ut.cpp - bitvector_ut.cpp -) - -END() + +SRCS( + bititerator_ut.cpp + bitvector_ut.cpp +) + +END() diff --git a/library/cpp/containers/bitseq/ya.make b/library/cpp/containers/bitseq/ya.make index 74594abfc0..7090956c55 100644 --- a/library/cpp/containers/bitseq/ya.make +++ b/library/cpp/containers/bitseq/ya.make @@ -1,7 +1,7 @@ -LIBRARY() - +LIBRARY() + OWNER(g:util) - + PEERDIR( util/draft library/cpp/pop_count @@ -12,4 +12,4 @@ SRCS( readonly_bitvector.cpp ) -END() +END() diff --git a/library/cpp/packedtypes/longs.h b/library/cpp/packedtypes/longs.h index a91d5df178..084098d705 100644 --- a/library/cpp/packedtypes/longs.h +++ b/library/cpp/packedtypes/longs.h @@ -91,7 +91,7 @@ struct mem_traits { #define MY_14(x) ((ui16)(x) < PACK1LIM ? 1 : 2) #define MY_28(x) ((ui32)(x) < PACK2LIM ? MY_14(x) : ((ui32)(x) < PACK3LIM ? 3 : 4)) - + #define MY_32(x) ((ui32)(x) < PACK4LIM ? MY_28(x) : 5) #define MY_64(x) ((ui64)(x) < PACK4LIM ? MY_28(x) : ((ui64)(x) < PACK6LIM ? ((ui64)(x) < PACK5LIM ? 5 : 6) : ((ui64)(x) < PACK7LIM ? 7 : ((ui64)(x) < PACK8LIM ? 8 : 9)))) @@ -141,7 +141,7 @@ struct mem_traits { (ret) = 5; \ } \ MACRO_END - + #define PACK_64(x, buf, how, ret) \ MACRO_BEGIN \ if ((ui64)(x) < PACK4LIM) { \ @@ -227,13 +227,13 @@ struct mem_traits { (ret) = 5; \ } \ MACRO_END - + #define UNPACK_32(x, buf, how, ret) \ MACRO_BEGIN \ ui8 firstByte = how::get_8(buf); \ DO_UNPACK_32(firstByte, x, buf, how, ret); \ MACRO_END - + #define DO_UNPACK_64(firstByte, x, buf, how, ret) \ MACRO_BEGIN \ if (firstByte < 0xF0) { \ @@ -292,13 +292,13 @@ inline int len_long(const i64& longVal) { inline int in_long(i32& longVal, const char* ptrBuf) { int ret; - UNPACK_32(longVal, ptrBuf, mem_traits, ret); + UNPACK_32(longVal, ptrBuf, mem_traits, ret); return ret; } inline int out_long(const i32& longVal, char* ptrBuf) { int ret; - PACK_32(longVal, ptrBuf, mem_traits, ret); + PACK_32(longVal, ptrBuf, mem_traits, ret); return ret; } @@ -311,7 +311,7 @@ inline const C* Unpack32(T& x, const C* src) { int pkLen = 0; const char* c = reinterpret_cast<const char*>(src); Y_UNUSED(pkLen); - UNPACK_32(x, c, mem_traits, pkLen); + UNPACK_32(x, c, mem_traits, pkLen); Y_ASSERT(pkLen); return reinterpret_cast<const C*>(c); } @@ -331,7 +331,7 @@ inline C* Pack32(const T& x, C* dest) { int pkLen = 0; Y_UNUSED(pkLen); char* c = reinterpret_cast<char*>(dest); - PACK_32(x, c, mem_traits, pkLen); + PACK_32(x, c, mem_traits, pkLen); Y_ASSERT(pkLen); return reinterpret_cast<C*>(c); } diff --git a/library/cpp/packedtypes/longs_ut.cpp b/library/cpp/packedtypes/longs_ut.cpp index ae5fc54c25..8b06c934d2 100644 --- a/library/cpp/packedtypes/longs_ut.cpp +++ b/library/cpp/packedtypes/longs_ut.cpp @@ -9,60 +9,60 @@ Y_UNIT_TEST_SUITE(TLongsTest) { Y_UNIT_TEST(TestLongs) { - i16 x16 = 40; - i64 x64 = 40; - i64 y64; + i16 x16 = 40; + i64 x64 = 40; + i64 y64; TString s; - s += Sprintf("x16=0x%x\n", (int)x16); + s += Sprintf("x16=0x%x\n", (int)x16); s += Sprintf("LO_8(x16)=0x%x HI_8(x16)=0x%x\n\n", (int)Lo8(x16), (int)Hi8(x16)); - - char buf[100]; - memset(buf, 0, 100); + + char buf[100]; + memset(buf, 0, 100); char* p = buf; - int l = out_long(x64, buf); - s += Sprintf("x64=0x%" PRIi64 "\n", x64); + int l = out_long(x64, buf); + s += Sprintf("x64=0x%" PRIi64 "\n", x64); s += Sprintf("LO_32(x64)=0x%" PRIu32 " HI_32(x64)=0x%" PRIu32 "\n", (ui32)Lo32(x64), (ui32)Hi32(x64)); - s += Sprintf("buf=%s, l=%d: ", buf, l); - for (int i = 0; i < l; i++) { - s += Sprintf("0x%02x ", buf[i]); - } - s += Sprintf("\n"); + s += Sprintf("buf=%s, l=%d: ", buf, l); + for (int i = 0; i < l; i++) { + s += Sprintf("0x%02x ", buf[i]); + } + s += Sprintf("\n"); - p = buf; - in_long(y64, p); - s += Sprintf("x=0x%" PRIi64 " y=0x%" PRIi64 "\n", x64, y64); - if (x64 != y64) { - s += Sprintf("Error: y64 != x64\n"); - } else { - s += Sprintf("OK\n"); - } + p = buf; + in_long(y64, p); + s += Sprintf("x=0x%" PRIi64 " y=0x%" PRIi64 "\n", x64, y64); + if (x64 != y64) { + s += Sprintf("Error: y64 != x64\n"); + } else { + s += Sprintf("OK\n"); + } UNIT_ASSERT_EQUAL(Crc<ui64>(s.data(), s.size()), 7251624297500315779ULL); // WTF? - } - - template <typename TSignedInt> - void TestOneValue(TSignedInt value) { - char buffer[sizeof(TSignedInt) + 1]; - auto bytes = out_long(value, buffer); - TSignedInt readValue = 0; - auto readBytes = in_long(readValue, buffer); - UNIT_ASSERT_EQUAL(bytes, readBytes); - UNIT_ASSERT_EQUAL(bytes, len_long(value)); - UNIT_ASSERT_EQUAL(value, readValue); - } - - template <typename TSignedInt> - void TestCornerCasesImpl(int maxPow) { - for (int i = 0; i <= maxPow; ++i) { + } + + template <typename TSignedInt> + void TestOneValue(TSignedInt value) { + char buffer[sizeof(TSignedInt) + 1]; + auto bytes = out_long(value, buffer); + TSignedInt readValue = 0; + auto readBytes = in_long(readValue, buffer); + UNIT_ASSERT_EQUAL(bytes, readBytes); + UNIT_ASSERT_EQUAL(bytes, len_long(value)); + UNIT_ASSERT_EQUAL(value, readValue); + } + + template <typename TSignedInt> + void TestCornerCasesImpl(int maxPow) { + for (int i = 0; i <= maxPow; ++i) { TestOneValue<TSignedInt>((TSignedInt)(1ull << i)); TestOneValue<TSignedInt>((TSignedInt)((1ull << i) - 1)); TestOneValue<TSignedInt>((TSignedInt)((1ull << i) + 1)); } - } + } Y_UNIT_TEST(TestCornerCases) { - TestCornerCasesImpl<i32>(31); - TestCornerCasesImpl<i64>(63); - } -} + TestCornerCasesImpl<i32>(31); + TestCornerCasesImpl<i64>(63); + } +} diff --git a/library/cpp/packedtypes/ut/ya.make b/library/cpp/packedtypes/ut/ya.make index 357b03bdd4..a203115e71 100644 --- a/library/cpp/packedtypes/ut/ya.make +++ b/library/cpp/packedtypes/ut/ya.make @@ -13,7 +13,7 @@ SRCS( longs_ut.cpp packed_ut.cpp packedfloat_ut.cpp - zigzag_ut.cpp + zigzag_ut.cpp ) END() diff --git a/library/cpp/packedtypes/ya.make b/library/cpp/packedtypes/ya.make index 5f72331c7d..4c2c950619 100644 --- a/library/cpp/packedtypes/ya.make +++ b/library/cpp/packedtypes/ya.make @@ -15,7 +15,7 @@ SRCS( packed.h packedfloat.cpp packedfloat.h - zigzag.h + zigzag.h ) END() diff --git a/library/cpp/packedtypes/zigzag.h b/library/cpp/packedtypes/zigzag.h index 2adc715902..548403f838 100644 --- a/library/cpp/packedtypes/zigzag.h +++ b/library/cpp/packedtypes/zigzag.h @@ -1,21 +1,21 @@ -#pragma once - -#include <util/generic/typetraits.h> - +#pragma once + +#include <util/generic/typetraits.h> + #include <limits.h> -//! Convert signed values to unsigned. Convenient for further varint encoding. -//! See https://developers.google.com/protocol-buffers/docs/encoding#types for details. - -template <typename TSignedInt> +//! Convert signed values to unsigned. Convenient for further varint encoding. +//! See https://developers.google.com/protocol-buffers/docs/encoding#types for details. + +template <typename TSignedInt> inline auto ZigZagEncode(TSignedInt n) -> std::make_unsigned_t<TSignedInt> { static_assert(std::is_signed<TSignedInt>::value && std::is_integral<TSignedInt>::value, "Expected signed integral type."); auto un = static_cast<std::make_unsigned_t<TSignedInt>>(n); return (un << 1) ^ (n >> (CHAR_BIT * sizeof(TSignedInt) - 1)); -} - -template <typename TUnsignedInt> +} + +template <typename TUnsignedInt> inline auto ZigZagDecode(TUnsignedInt n) -> std::make_signed_t<TUnsignedInt> { static_assert(std::is_unsigned<TUnsignedInt>::value, "Expected unsigned integral type."); return (n >> 1) ^ -static_cast<std::make_signed_t<TUnsignedInt>>(n & 1); -} +} diff --git a/library/cpp/packedtypes/zigzag_ut.cpp b/library/cpp/packedtypes/zigzag_ut.cpp index cd3284d55e..13b78b7481 100644 --- a/library/cpp/packedtypes/zigzag_ut.cpp +++ b/library/cpp/packedtypes/zigzag_ut.cpp @@ -1,38 +1,38 @@ -#include "zigzag.h" - +#include "zigzag.h" + #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TZigZagTest) { - template <typename T> - void TestEncodeDecode(T value) { - auto encoded = ZigZagEncode(value); - UNIT_ASSERT(encoded >= 0); - auto decoded = ZigZagDecode(encoded); - UNIT_ASSERT(decoded == value); - static_assert(sizeof(value) == sizeof(decoded), "sizeof mismatch"); - static_assert(sizeof(value) == sizeof(encoded), "sizeof mismatch"); - } - - template <typename TSignedInt> - void TestImpl() { - static const int bits = CHAR_BIT * sizeof(TSignedInt); - for (int p = 0; p + 1 < bits; ++p) { - TSignedInt value = 1; - value <<= p; - - TestEncodeDecode(value); - TestEncodeDecode(-value); - TestEncodeDecode(value - 1); - TestEncodeDecode(-value + 1); - } - - TestEncodeDecode((TSignedInt)123); - TestEncodeDecode((TSignedInt)-123); - } - + template <typename T> + void TestEncodeDecode(T value) { + auto encoded = ZigZagEncode(value); + UNIT_ASSERT(encoded >= 0); + auto decoded = ZigZagDecode(encoded); + UNIT_ASSERT(decoded == value); + static_assert(sizeof(value) == sizeof(decoded), "sizeof mismatch"); + static_assert(sizeof(value) == sizeof(encoded), "sizeof mismatch"); + } + + template <typename TSignedInt> + void TestImpl() { + static const int bits = CHAR_BIT * sizeof(TSignedInt); + for (int p = 0; p + 1 < bits; ++p) { + TSignedInt value = 1; + value <<= p; + + TestEncodeDecode(value); + TestEncodeDecode(-value); + TestEncodeDecode(value - 1); + TestEncodeDecode(-value + 1); + } + + TestEncodeDecode((TSignedInt)123); + TestEncodeDecode((TSignedInt)-123); + } + Y_UNIT_TEST(TestSigned) { - TestImpl<i16>(); - TestImpl<i32>(); - TestImpl<i64>(); - } -} + TestImpl<i16>(); + TestImpl<i32>(); + TestImpl<i64>(); + } +} |