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/containers | |
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/containers')
-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 |
7 files changed, 454 insertions, 454 deletions
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() |