ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BitStream.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File BitStream.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
46/*--------------------------------------------------------------------------*/
47/*----------------------------- DEFINITIONS --------------------------------*/
48/*--------------------------------------------------------------------------*/
49
50#ifndef __ADS_BitStream
51 #define __ADS_BitStream
52/* self-identification: #endif constant */
53
54/*--------------------------------------------------------------------------*/
55/*------------------------------ INCLUDES ----------------------------------*/
56/*--------------------------------------------------------------------------*/
57
58#include "ADS/Contract.h"
59#include "ADS/Portable.h"
62
63#include <bit>
64#include <cstddef>
65#include <cstdint>
66#include <type_traits>
67#include <utility>
68
69/*--------------------------------------------------------------------------*/
70/*--------------------------- NAMESPACE ------------------------------------*/
71/*--------------------------------------------------------------------------*/
72
73namespace ADS
74{
75
76/*--------------------------------------------------------------------------*/
77/*---------------------------- CLASS BitWriter -----------------------------*/
78/*--------------------------------------------------------------------------*/
79
82{
83
84/*--------------------------------------------------------------------------*/
85 public:
87 BitWriter( void ) = default;
88
90 [[nodiscard]] std::size_t size( void ) const;
91
93 void writeBit( bool b );
94
97 void write( Word v , int len );
98
113 void writeBinary( Word v , int len );
114
128 void writeUnary( std::uint64_t q );
129
131 [[nodiscard]] const BitVector & bits( void ) const;
132
134 [[nodiscard]] BitVector release( void );
135
136/*--------------------------------------------------------------------------*/
137 private:
138 BitVector f_b;
139
140 }; // end( class BitWriter )
141
142/*--------------------------------------------------------------------------*/
143/*---------------------------- CLASS BitReader -----------------------------*/
144/*--------------------------------------------------------------------------*/
145
149{
150
151/*--------------------------------------------------------------------------*/
152 public:
155 explicit BitReader( const BitVector & b , std::size_t p = 0 );
156
158 [[nodiscard]] std::size_t position( void ) const;
159
162 void seek( std::size_t p );
163
165 [[nodiscard]] bool atEnd( void ) const;
166
169 [[nodiscard]] bool readBit( void );
170
175 [[nodiscard]] Word read( int len );
176
181 [[nodiscard]] Word readBinary( int len );
182
203 [[nodiscard]] std::uint64_t readUnary( void );
204
205/*--------------------------------------------------------------------------*/
206 private:
207 const BitVector * f_b;
208 std::size_t f_p;
209
210 }; // end( class BitReader )
211
212/*--------------------------------------------------------------------------*/
213
214} // end( namespace ADS )
215
216/*--------------------------------------------------------------------------*/
217/*-------------------------- IMPLEMENTATION --------------------------------*/
218/*--------------------------------------------------------------------------*/
219
220#include "ADS/succinct/impl/BitStream.tpp"
221
222/*--------------------------------------------------------------------------*/
223
224#endif /* BitStream.h included */
225
226/*--------------------------------------------------------------------------*/
227/*-------------------------- End File BitStream.h --------------------------*/
228/*--------------------------------------------------------------------------*/
The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits ...
The broadword tricks of Knuth (section 7.1.3 of The Art of Computer Programming), beyond the book: op...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
void seek(std::size_t p)
moves to the place
Word readBinary(int len)
the next bits, the first in the most significant place, the field of writeBinary()
bool readBit(void)
the next bit
BitReader(const BitVector &b, std::size_t p=0)
the bits of from the place
Word read(int len)
the next bits, the first in the least significant place
bool atEnd(void) const
whether all the bits have been read
std::uint64_t readUnary(void)
the number of zeros before the next 1, which is read too
std::size_t position(void) const
the place of the next bit
a vector of bits packed in words of 64 bits
Definition BitVector.h:479
void write(Word v, int len)
the bits of , the least significant first
void writeBit(bool b)
appends the bit
BitWriter(void)=default
an empty stream
void writeUnary(std::uint64_t q)
zeros and a 1, the unary code
std::size_t size(void) const
the bits written so far
const BitVector & bits(void) const
the bits written
BitVector release(void)
the bits written, which the writer leaves empty
void writeBinary(Word v, int len)
the bits of , the most significant first
Definition ADT.h:96
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73