| 1 | #cxx . . $ `<file>/io/bitreader.h`/ifndef IOUTILITIES_BITREADER_H |
| 2 | #define IOUTILITIES_BITREADER_Hcxx . . $ `io/bitreader.h:2:9`! |
| 3 | |
| 4 | #include "../global.h"cxx . . $ `<file>/global.h`/ |
| 5 | |
| 6 | #include <cstdint> |
| 7 | #include <ios> |
| 8 | #include <iostream> |
| 9 | #include <type_traits> |
| 10 | |
| 11 | namespace CppUtilitiescxx . . $ CppUtilities/ { |
| 12 | |
| 13 | class CPP_UTILITIES_EXPORTcxx . . $ `global.h:11:9`! BitReadercxx . . $ CppUtilities/BitReader# { |
| 14 | public: |
| 15 | BitReadercxx . . $ CppUtilities/BitReader#BitReader(7cafb6e0818eff3b).(const char *buffer, stdcxx . . $ std/::size_tcxx . . $ std/size_t# bufferSize); |
| 16 | BitReadercxx . . $ CppUtilities/BitReader#BitReader(2fa786934fe80e24).(const char *buffer, const char *end); |
| 17 | |
| 18 | template <typename intType> intType readBitscxx . . $ CppUtilities/BitReader#readBits(c36cca2cac3cfd3b).(stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# bitCount); |
| 19 | stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# readBitcxx . . $ CppUtilities/BitReader#readBit(7c4be072098b358f).(); |
| 20 | template <typename intType> intType readUnsignedExpGolombCodedBitscxx . . $ CppUtilities/BitReader#readUnsignedExpGolombCodedBits(5f357b03ed472e5e).(); |
| 21 | template <typename intType> intType readSignedExpGolombCodedBitscxx . . $ CppUtilities/BitReader#readSignedExpGolombCodedBits(5f357b03ed472e5e).(); |
| 22 | template <typename intType> intType showBitscxx . . $ CppUtilities/BitReader#showBits(c36cca2cac3cfd3b).(stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# bitCount); |
| 23 | void skipBitscxx . . $ CppUtilities/BitReader#skipBits(2c44cd906ae4e829).(stdcxx . . $ std/::size_tcxx . . $ std/size_t# bitCount); |
| 24 | void aligncxx . . $ CppUtilities/BitReader#align(49f6e7a06ebc5aa8).(); |
| 25 | stdcxx . . $ std/::size_tcxx . . $ std/size_t# bitsAvailablecxx . . $ CppUtilities/BitReader#bitsAvailable(f708dce9a8856c70).(); |
| 26 | void resetcxx . . $ CppUtilities/BitReader#reset(7cafb6e0818eff3b).(const char *buffer, stdcxx . . $ std/::size_tcxx . . $ std/size_t# bufferSize); |
| 27 | void resetcxx . . $ CppUtilities/BitReader#reset(2fa786934fe80e24).(const char *buffer, const char *end); |
| 28 | |
| 29 | private: |
| 30 | const stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# *m_buffercxx . . $ CppUtilities/BitReader#m_buffer.; |
| 31 | const stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# *m_endcxx . . $ CppUtilities/BitReader#m_end.; |
| 32 | stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail.; |
| 33 | }; |
| 34 | |
| 35 | /*! |
| 36 | * \brief Constructs a new BitReader. |
| 37 | * \remarks |
| 38 | * - Does not take ownership over the specified \a buffer. |
| 39 | * - bufferSize must be equal or greater than 1. |
| 40 | */ |
| 41 | inline BitReadercxx . . $ CppUtilities/BitReader#::BitReadercxx . . $ CppUtilities/BitReader#BitReader(7cafb6e0818eff3b).(const char *buffer, stdcxx . . $ std/::size_tcxx . . $ std/size_t# bufferSize) |
| 42 | : BitReadercxx . . $ CppUtilities/BitReader#(buffer, buffer + bufferSize) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | /*! |
| 47 | * \brief Constructs a new BitReader. |
| 48 | * \remarks |
| 49 | * - Does not take ownership over the specified \a buffer. |
| 50 | * - \a end must be greater than \a buffer. |
| 51 | */ |
| 52 | inline BitReadercxx . . $ CppUtilities/BitReader#::BitReadercxx . . $ CppUtilities/BitReader#BitReader(2fa786934fe80e24).(const char *buffer, const char *end) |
| 53 | : m_buffercxx . . $ CppUtilities/BitReader#m_buffer.(reinterpret_cast<const stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# *>(buffer)) |
| 54 | , m_endcxx . . $ CppUtilities/BitReader#m_end.(reinterpret_cast<const stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# *>(end)) |
| 55 | , m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail.(8) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | /*! |
| 60 | * \brief Reads the specified number of bits from the buffer advancing the current position by \a bitCount bits. |
| 61 | * \param bitCount Specifies the number of bits read. |
| 62 | * \tparam intType Specifies the type of the returned value. |
| 63 | * \remarks Does not check whether intType is big enough to hold result. |
| 64 | * \throws Throws ios_base::failure if the end of the buffer is exceeded. |
| 65 | * The reader becomes invalid in that case. |
| 66 | */ |
| 67 | template <typename intType> intType BitReadercxx . . $ CppUtilities/BitReader#::readBitscxx . . $ CppUtilities/BitReader#readBits(c36cca2cac3cfd3b).(stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# bitCount) |
| 68 | { |
| 69 | intType val = 0; |
| 70 | for (stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# readAtOnce; bitCount; bitCount -= readAtOnce) { |
| 71 | if (!m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail.) { |
| 72 | if (++m_buffercxx . . $ CppUtilities/BitReader#m_buffer. >= m_endcxx . . $ CppUtilities/BitReader#m_end.) { |
| 73 | throw stdcxx . . $ std/::ios_basecxx . . $ std/ios_base#::failurecxx . . $ std/ios_base#failure#("end of buffer exceeded"); |
| 74 | } |
| 75 | m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail. = 8; |
| 76 | } |
| 77 | readAtOnce = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(bitCount, m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail.); |
| 78 | val = static_cast<intType>( |
| 79 | (val << readAtOnce) | static_cast<intType>(((*m_buffercxx . . $ CppUtilities/BitReader#m_buffer.) >> (m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail. -= readAtOnce)) & (0xFF >> (0x08 - readAtOnce)))); |
| 80 | } |
| 81 | return val; |
| 82 | } |
| 83 | |
| 84 | /*! |
| 85 | * \brief Reads the one bit from the buffer advancing the current position by one bit. |
| 86 | * \throws Throws ios_base::failure if the end of the buffer is exceeded. |
| 87 | * The reader becomes invalid in that case. |
| 88 | */ |
| 89 | inline stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# BitReadercxx . . $ CppUtilities/BitReader#::readBitcxx . . $ CppUtilities/BitReader#readBit(7c4be072098b358f).() |
| 90 | { |
| 91 | return readBitscxx . . $ CppUtilities/BitReader#readBits(c36cca2cac3cfd3b).<stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t#>(1) == 1; |
| 92 | } |
| 93 | |
| 94 | /*! |
| 95 | * \brief Reads "Exp-Golomb coded" bits (unsigned). |
| 96 | * \tparam intType Specifies the type of the returned value. |
| 97 | * \remarks Does not check whether intType is big enough to hold result. |
| 98 | * \throws Throws ios_base::failure if the end of the buffer is exceeded. |
| 99 | * The reader becomes invalid in that case. |
| 100 | * \sa https://en.wikipedia.org/wiki/Exponential-Golomb_coding |
| 101 | */ |
| 102 | template <typename intType> intType BitReadercxx . . $ CppUtilities/BitReader#::readUnsignedExpGolombCodedBitscxx . . $ CppUtilities/BitReader#readUnsignedExpGolombCodedBits(5f357b03ed472e5e).() |
| 103 | { |
| 104 | stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# count = 0; |
| 105 | while (!readBitcxx . . $ CppUtilities/BitReader#readBit(7c4be072098b358f).()) { |
| 106 | ++count; |
| 107 | } |
| 108 | return count ? static_cast<intType>(((1 << count) | readBitscxx . . $ CppUtilities/BitReader#readBits(c36cca2cac3cfd3b).<intType>(count)) - 1) : 0; |
| 109 | } |
| 110 | |
| 111 | /*! |
| 112 | * \brief Reads "Exp-Golomb coded" bits (signed). |
| 113 | * \tparam intType Specifies the type of the returned value which should be signed (obviously). |
| 114 | * \remarks Does not check whether intType is big enough to hold result. |
| 115 | * \throws Throws ios_base::failure if the end of the buffer is exceeded. |
| 116 | * The reader becomes invalid in that case. |
| 117 | * \sa https://en.wikipedia.org/wiki/Exponential-Golomb_coding |
| 118 | */ |
| 119 | template <typename intType> intType BitReadercxx . . $ CppUtilities/BitReader#::readSignedExpGolombCodedBitscxx . . $ CppUtilities/BitReader#readSignedExpGolombCodedBits(5f357b03ed472e5e).() |
| 120 | { |
| 121 | auto value = readUnsignedExpGolombCodedBitscxx . . $ CppUtilities/BitReader#readUnsignedExpGolombCodedBits(5f357b03ed472e5e).<typename stdcxx . . $ std/::make_unsigned<intType>::type>(); |
| 122 | return (value % 2) ? static_cast<intType>((value + 1) / 2) : (-static_cast<intType>(value / 2)); |
| 123 | } |
| 124 | |
| 125 | /*! |
| 126 | * \brief Reads the specified number of bits from the buffer without advancing the current position. |
| 127 | */ |
| 128 | template <typename intType> intType BitReadercxx . . $ CppUtilities/BitReader#::showBitscxx . . $ CppUtilities/BitReader#showBits(c36cca2cac3cfd3b).(stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# bitCount) |
| 129 | { |
| 130 | auto tmp = *this; |
| 131 | return tmp.readBitscxx . . $ CppUtilities/BitReader#readBits(c36cca2cac3cfd3b).<intType>(bitCount); |
| 132 | } |
| 133 | |
| 134 | /*! |
| 135 | * \brief Returns the number of bits which are still available to read. |
| 136 | */ |
| 137 | inline stdcxx . . $ std/::size_tcxx . . $ std/size_t# BitReadercxx . . $ CppUtilities/BitReader#::bitsAvailablecxx . . $ CppUtilities/BitReader#bitsAvailable(f708dce9a8856c70).() |
| 138 | { |
| 139 | return m_buffercxx . . $ CppUtilities/BitReader#m_buffer. != m_endcxx . . $ CppUtilities/BitReader#m_end. ? static_cast<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(((m_endcxx . . $ CppUtilities/BitReader#m_end. - m_buffercxx . . $ CppUtilities/BitReader#m_buffer. - 1) * 8) + m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail.) : static_cast<stdcxx . . $ std/::size_tcxx . . $ std/size_t#>(0); |
| 140 | } |
| 141 | |
| 142 | /*! |
| 143 | * \brief Resets the reader. |
| 144 | * \remarks |
| 145 | * - Does not take ownership over the specified \a buffer. |
| 146 | * - bufferSize must be equal or greater than 1. |
| 147 | */ |
| 148 | inline void BitReadercxx . . $ CppUtilities/BitReader#::resetcxx . . $ CppUtilities/BitReader#reset(7cafb6e0818eff3b).(const char *buffer, stdcxx . . $ std/::size_tcxx . . $ std/size_t# bufferSize) |
| 149 | { |
| 150 | m_buffercxx . . $ CppUtilities/BitReader#m_buffer. = reinterpret_cast<const stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# *>(buffer); |
| 151 | m_endcxx . . $ CppUtilities/BitReader#m_end. = reinterpret_cast<const stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# *>(buffer + bufferSize); |
| 152 | m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail. = 8; |
| 153 | } |
| 154 | |
| 155 | /*! |
| 156 | * \brief Resets the reader. |
| 157 | * \remarks |
| 158 | * - Does not take ownership over the specified \a buffer. |
| 159 | * - \a end must be greater than \a buffer. |
| 160 | */ |
| 161 | inline void BitReadercxx . . $ CppUtilities/BitReader#::resetcxx . . $ CppUtilities/BitReader#reset(2fa786934fe80e24).(const char *buffer, const char *end) |
| 162 | { |
| 163 | m_buffercxx . . $ CppUtilities/BitReader#m_buffer. = reinterpret_cast<const stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# *>(buffer); |
| 164 | m_endcxx . . $ CppUtilities/BitReader#m_end. = reinterpret_cast<const stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t# *>(end); |
| 165 | m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail. = 8; |
| 166 | } |
| 167 | |
| 168 | /*! |
| 169 | * \brief Re-establishes alignment. |
| 170 | */ |
| 171 | inline void BitReadercxx . . $ CppUtilities/BitReader#::aligncxx . . $ CppUtilities/BitReader#align(49f6e7a06ebc5aa8).() |
| 172 | { |
| 173 | skipBitscxx . . $ CppUtilities/BitReader#skipBits(2c44cd906ae4e829).(m_bitsAvailcxx . . $ CppUtilities/BitReader#m_bitsAvail.); |
| 174 | } |
| 175 | |
| 176 | } // namespace CppUtilities |
| 177 | |
| 178 | #endif // IOUTILITIES_BITREADER_H |