diff options
author | vng <viktor.govako@gmail.com> | 2015-12-04 20:33:03 +0300 |
---|---|---|
committer | Sergey Yershov <yershov@corp.mail.ru> | 2016-03-23 16:02:59 +0300 |
commit | 83690aa414831f2ea6e1035c67b9734bb0a7a060 (patch) | |
tree | a2295b000b90e0bbd0e3bc261f099db0f0d6c71f /coding/bit_streams.hpp | |
parent | bae24455806ddc56fbf0187a3d628409c5c58995 (diff) |
[search] Added FixedBitsDDVector container.
Diffstat (limited to 'coding/bit_streams.hpp')
-rw-r--r-- | coding/bit_streams.hpp | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/coding/bit_streams.hpp b/coding/bit_streams.hpp index 5bc00a9419..702e31145d 100644 --- a/coding/bit_streams.hpp +++ b/coding/bit_streams.hpp @@ -1,10 +1,12 @@ #pragma once +#include "base/assert.hpp" +#include "base/logging.hpp" + +#include "std/algorithm.hpp" #include "std/cstdint.hpp" #include "std/limits.hpp" -#include "base/assert.hpp" -#include "base/logging.hpp" namespace { @@ -39,7 +41,7 @@ public: // Writes n bits starting with the least significant bit. // They are written one byte at a time so endianness is of no concern. // All the other bits except for the first n must be set to zero. - void Write(uint8_t bits, uint32_t n) + void Write(uint8_t bits, uint8_t n) { if (n == 0) return; @@ -68,6 +70,17 @@ public: } } + // Same as Write but accept up to 32 bits to write. + void WriteAtMost32Bits(uint32_t bits, uint8_t n) + { + ASSERT_LESS_OR_EQUAL(n, 32, ()); + + uint8_t constexpr kMinBits = CHAR_BIT; + Write(static_cast<uint8_t>(bits), min(n, kMinBits)); + if (n > kMinBits) + WriteAtMost32Bits(bits >> kMinBits, n - kMinBits); + } + private: // Writes up to CHAR_BIT-1 last bits if they have not been written yet // and pads them with zeros. @@ -96,7 +109,7 @@ public: // The underlying m_src is supposed to be byte-aligned (which is the // case when it reads from the place that was written to using BitWriter). // Read may use one lookahead byte. - uint8_t Read(uint32_t n) + uint8_t Read(uint8_t n) { if (n == 0) return 0; |