mCRL2
Loading...
Searching...
No Matches
bitstream.h
Go to the documentation of this file.
1// Author(s): Maurice Laveaux
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9
10#ifndef MCRL2_UTILITIES_BITSTREAM_H
11#define MCRL2_UTILITIES_BITSTREAM_H
12
13#include <bitset>
14#include <cstdint>
15#include <vector>
16
17namespace mcrl2
18{
19namespace utilities
20{
21
22/// \returns The number of bits needed to represent a value of type T in most significant bit encoding.
23template<typename T>
24constexpr std::size_t integer_encoding_size()
25{
26 return ((sizeof(T) + 1) * 8) / 7;
27}
28
29/// \brief A bitstream provides per bit writing of data to any stream (including stdout).
30/// \details Internally uses bitpacking and buffering for compact and efficient IO.
32{
33public:
34 /// \brief Provides the stream on which the write function operate.
35 obitstream(std::ostream& stream);
37
38 /// \brief Write the num_of_bits least significant bits in descending order from value.
39 /// @param value Variable that contains the bits.
40 /// @param num_of_bits Number of bits to write to the output stream.
41 void write_bits(std::size_t value, unsigned int num_of_bits);
42
43 /// \brief Write the given string to the output stream.
44 /// \details Encoded in bits using <length, string>
45 void write_string(const std::string& string);
46
47 /// \brief Write the given value to the output stream.
48 /// \details Uses most significant bit encoding.
49 void write_integer(std::size_t value);
50
51private:
52 /// \brief Flush the remaining bits in the buffer to the output stream.
53 /// \details Note that this aligns it to the next byte, e.g. when bits_in_buffer is 6 then two zero bits are added redundantly.
54 void flush();
55
56 /// \brief Writes size bytes from the given buffer.
57 void write(const std::uint8_t* buffer, std::size_t size);
58
59 std::ostream& stream;
60
61 /// \brief Buffer that is filled starting from bit 127 when writing
62 std::bitset<128> write_buffer = 0;
63
64 unsigned int bits_in_buffer = 0; ///< how many bits in are used in the buffer.
65
66 std::uint8_t integer_buffer[integer_encoding_size<std::size_t>()]; ///< Reserved space to store an n byte integer.
67};
68
69/// \brief The counterpart of obitstream, guarantees that the same data is read as has been written when calling the read operators
70/// in the same sequence as the corresponding write operators.
72{
73public:
74 /// \brief Provides the stream on which the read function operate.
75 ibitstream(std::istream& stream);
76
77 /// \brief Reads an num_of_bits bits from the input stream and stores them in the least significant part (in descending order) of the return value.
78 /// \param num_of_bits Number of bits to read from the input stream.
79 std::size_t read_bits(unsigned int num_of_bits);
80
81 /// \returns A pointer to the read string.
82 /// \details Remains valid until the next call to read_string.
83 const char* read_string();
84
85 /// \returns A natural number that was read from the binary stream encoded in most significant bit encoding.
86 std::size_t read_integer();
87
88private:
89 /// \brief Read size bytes into the provided buffer.
90 void read(std::size_t size, std::uint8_t* buffer);
91
92 std::istream& stream;
93
94 /// \brief Buffer that is filled starting from bit 127 when reading.
95 std::bitset<128> read_buffer = 0;
96
97 unsigned int bits_in_buffer = 0; ///< how many bits in the buffer are used.
98
99 std::vector<char> m_text_buffer; ///< A temporary buffer to store char array strings.
100};
101
102} // namespace utilities
103} // namespace mcrl2
104
105#endif // MCRL2_UTILITIES_BITSTREAM_H
bool is_a_binary_aterm(std::istream &is)
An integer term stores a single std::size_t value. It carries no arguments.
Definition aterm_int.h:26
The interface for a class that reads aterm from a stream. The default constructed term aterm() indica...
Definition aterm_io.h:59
The interface for a class that writes aterm to a stream. Every written term is retrieved by the corre...
Definition aterm_io.h:48
aterm()
Default constructor.
Definition aterm.h:48
aterm & operator=(const aterm &other) noexcept=default
aterm & operator=(aterm &&other) noexcept=default
std::shared_ptr< mcrl2::utilities::ibitstream > m_stream
unsigned int m_function_symbol_index_width
caches the result of function_symbol_index_width().
atermpp::deque< aterm > m_terms
An index of read terms.
binary_aterm_istream(std::istream &is)
Provide the input stream from which terms are read.
unsigned int m_term_index_width
caches the result of term_index_width().
void get(aterm &t) override
Reads an aterm from this stream.
std::deque< function_symbol > m_function_symbols
An index of read function symbols.
binary_aterm_istream(std::shared_ptr< mcrl2::utilities::ibitstream > stream)
unsigned int m_term_index_width
caches the result of term_index_width().
std::size_t write_function_symbol(const function_symbol &symbol)
Write a function symbol to the output stream.
binary_aterm_ostream(std::ostream &os)
Provide the output stream to which the terms are written.
binary_aterm_ostream(std::shared_ptr< mcrl2::utilities::obitstream > stream)
std::shared_ptr< mcrl2::utilities::obitstream > m_stream
unsigned int m_function_symbol_index_width
caches the result of function_symbol_index_width().
atermpp::indexed_set< aterm > m_terms
An index of already written terms.
void put(const aterm &term) override
Writes an aterm in a compact binary format where subterms are shared. The term that is written itself...
mcrl2::utilities::indexed_set< function_symbol > m_function_symbols
An index of already written function symbols.
bool operator==(const function_symbol &f) const
Equality test.
std::size_t arity() const
Return the arity (number of arguments) of the function symbol (function_symbol).
Standard exception class for reporting runtime errors.
Definition exception.h:27
The counterpart of obitstream, guarantees that the same data is read as has been written when calling...
Definition bitstream.h:72
unsigned int bits_in_buffer
how many bits in the buffer are used.
Definition bitstream.h:97
std::size_t read_bits(unsigned int num_of_bits)
Reads an num_of_bits bits from the input stream and stores them in the least significant part (in des...
void read(std::size_t size, std::uint8_t *buffer)
Read size bytes into the provided buffer.
std::vector< char > m_text_buffer
A temporary buffer to store char array strings.
Definition bitstream.h:99
std::bitset< 128 > read_buffer
Buffer that is filled starting from bit 127 when reading.
Definition bitstream.h:95
ibitstream(std::istream &stream)
Provides the stream on which the read function operate.
A bitstream provides per bit writing of data to any stream (including stdout).
Definition bitstream.h:32
void flush()
Flush the remaining bits in the buffer to the output stream.
std::bitset< 128 > write_buffer
Buffer that is filled starting from bit 127 when writing.
Definition bitstream.h:62
unsigned int bits_in_buffer
how many bits in are used in the buffer.
Definition bitstream.h:64
std::uint8_t integer_buffer[integer_encoding_size< std::size_t >()]
Reserved space to store an n byte integer.
Definition bitstream.h:66
void write_bits(std::size_t value, unsigned int num_of_bits)
Write the num_of_bits least significant bits in descending order from value.
void write(const std::uint8_t *buffer, std::size_t size)
Writes size bytes from the given buffer.
void write_string(const std::string &string)
Write the given string to the output stream.
void write_integer(std::size_t value)
Write the given value to the output stream.
obitstream(std::ostream &stream)
Provides the stream on which the write function operate.
function_symbol g_as_int
These function symbols are used to indicate integer, list and empty list terms.
The main namespace for the aterm++ library.
Definition algorithm.h:21
static constexpr unsigned int packet_bits
The number of bits needed to store an element of packet_type.
void write_term_to_binary_stream(const aterm &t, std::ostream &os)
Writes term t to a stream in binary aterm format.
static constexpr std::uint16_t BAF_MAGIC
The magic value for a binary aterm format stream.
void read_term_from_binary_stream(std::istream &is, aterm &t)
Reads a term from a stream in binary aterm format.
packet_type
Each packet has a header consisting of a type.
void make_aterm_int(aterm_int &target, std::size_t value)
Constructs an integer term from a value.
Definition aterm_int.h:69
static constexpr std::uint16_t BAF_VERSION
The BAF_VERSION constant is the version number of the ATerms written in BAF format....
constexpr std::size_t integer_encoding_size()
Definition bitstream.h:24
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Keep track of whether the term can be written to the stream.
void mark(std::stack< std::reference_wrapper< detail::_aterm > > &todo) const
detail::reference_aterm< aterm > term
write_todo(const aterm &term)
std::size_t operator()(const atermpp::detail::reference_aterm< T > &t) const