ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PackedBits.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File PackedBits.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
35/*--------------------------------------------------------------------------*/
36/*----------------------------- DEFINITIONS --------------------------------*/
37/*--------------------------------------------------------------------------*/
38
39#ifndef __ADS_PackedBits
40 #define __ADS_PackedBits
41/* self-identification: #endif constant */
42
43/*--------------------------------------------------------------------------*/
44/*------------------------------ INCLUDES ----------------------------------*/
45/*--------------------------------------------------------------------------*/
46
47#include "ADS/Contract.h"
48
49#include <algorithm>
50#include <cstddef>
51#include <cstdint>
52#include <span>
53#include <string>
54#include <vector>
55
56/*--------------------------------------------------------------------------*/
57/*--------------------------- NAMESPACE ------------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60namespace ADS
61{
62
63/*--------------------------------------------------------------------------*/
64/*------------------------- CLASS PackedBitWriter --------------------------*/
65/*--------------------------------------------------------------------------*/
66
70{
71
72/*--------------------------------------------------------------------------*/
73 public:
75 void put( bool b );
76
79 void put( std::uint64_t v , int k );
80
84 void put( const std::string & s );
85
87 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
88
90 [[nodiscard]] const std::vector< std::uint8_t > & bytes( void ) const {
91 return( f_bytes );
92 }
93
94/*--------------------------------------------------------------------------*/
95 private:
96 std::vector< std::uint8_t > f_bytes;
97 std::size_t f_size = 0;
98
99 }; // end( class PackedBitWriter )
100
101/*--------------------------------------------------------------------------*/
102/*------------------------- CLASS PackedBitReader --------------------------*/
103/*--------------------------------------------------------------------------*/
104
108{
109
110/*--------------------------------------------------------------------------*/
111 public:
113 explicit PackedBitReader( std::span< const std::uint8_t > b )
114 : f_bytes( b ) { }
115
117 bool get( void );
118
121 std::uint64_t get( int k );
122
124 [[nodiscard]] std::size_t position( void ) const { return( f_pos ); }
125
127 [[nodiscard]] std::size_t size( void ) const {
128 return( 8 * f_bytes.size() );
129 }
130
131/*--------------------------------------------------------------------------*/
132 private:
133 std::span< const std::uint8_t > f_bytes;
134 std::size_t f_pos = 0;
135
136 }; // end( class PackedBitReader )
137
138/*--------------------------------------------------------------------------*/
139
140} // end( namespace ADS )
141
142/*--------------------------------------------------------------------------*/
143/*-------------------------- IMPLEMENTATION --------------------------------*/
144/*--------------------------------------------------------------------------*/
145
146#include "ADS/compression/impl/PackedBits.tpp"
147
148/*--------------------------------------------------------------------------*/
149
150#endif /* PackedBits.h included */
151
152/*--------------------------------------------------------------------------*/
153/*------------------------- End File PackedBits.h --------------------------*/
154/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
PackedBitReader(std::span< const std::uint8_t > b)
the reader of the bytes , which must outlive it
Definition PackedBits.h:113
std::uint64_t get(int k)
the value of the next bits, the first the most significant
std::size_t position(void) const
the number of bits read, past the end too
Definition PackedBits.h:124
bool get(void)
the next bit, 0 past the end
std::size_t size(void) const
the number of bits of the bytes
Definition PackedBits.h:127
a growing sequence of bits, packed in bytes, the first bit in the most significant bit of the first b...
Definition PackedBits.h:70
void put(bool b)
appends the bit
std::size_t size(void) const
the number of bits written
Definition PackedBits.h:87
const std::vector< std::uint8_t > & bytes(void) const
the bytes, the last one padded with zeros
Definition PackedBits.h:90
void put(std::uint64_t v, int k)
appends the low bits of , the most significant first
void put(const std::string &s)
appends the bits of , a string of '0' and '1'
Definition ADT.h:96