mCRL2
Loading...
Searching...
No Matches
aterm_io_binary.h
Go to the documentation of this file.
1// Author(s): Jan Friso Groote
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_ATERMPP_ATERM_IO_BINARY_H
11#define MCRL2_ATERMPP_ATERM_IO_BINARY_H
12
13#include "mcrl2/atermpp/aterm_io.h"
14
15#include "mcrl2/utilities/bitstream.h"
16#include "mcrl2/atermpp/standard_containers/deque.h"
17#include "mcrl2/atermpp/standard_containers/indexed_set.h"
18
19namespace atermpp
20{
21
22/// \brief Writes terms in a streamable binary aterm format to an output stream.
23/// \details The streamable aterm format:
24///
25/// Aterms (and function symbols) are written as packets (with an identifier in the header) and their
26/// indices are derived from the number of aterms, resp. symbols, that occur before them in this stream. For each term
27/// we first ensure that its arguments and symbol are written to the stream (avoiding duplicates). Then its
28/// symbol index followed by a number of indices (depending on the arity) for its argments are written as integers.
29/// Packet headers also contain a special value to indicate that the read term should be visible as output as opposed to
30/// being only a subterm.
31/// The start of the stream is a zero followed by a header and a version and a term with function symbol index zero
32/// indicates the end of the stream.
33///
34class binary_aterm_ostream final : public aterm_ostream
35{
36public:
37 /// \brief Provide the output stream to which the terms are written.
38 binary_aterm_ostream(std::ostream& os);
39 binary_aterm_ostream(std::shared_ptr<mcrl2::utilities::obitstream> stream);
40
41 ~binary_aterm_ostream() override;
42
43 /// \brief Writes an aterm in a compact binary format where subterms are shared. The term that is
44 /// written itself is not shared whenever it occurs as the argument of another term.
45 void put(const aterm &term) override;
46
47private:
48 /// \brief Write a function symbol to the output stream.
49 std::size_t write_function_symbol(const function_symbol& symbol);
50
51 /// \returns The number of bits needed to index terms.
52 unsigned int term_index_width();
53
54 /// \returns The number of bits needed to index function symbols.
55 unsigned int function_symbol_index_width();
56
57 std::shared_ptr<mcrl2::utilities::obitstream> m_stream;
58
59 unsigned int m_term_index_width; ///< caches the result of term_index_width().
60 unsigned int m_function_symbol_index_width; ///< caches the result of function_symbol_index_width().
61
62 atermpp::indexed_set<aterm> m_terms; ///< An index of already written terms.
63 mcrl2::utilities::indexed_set<function_symbol> m_function_symbols; ///< An index of already written function symbols.
64};
65
66/// \brief Reads terms from a stream in the steamable binary aterm format.
67class binary_aterm_istream final : public aterm_istream
68{
69public:
70 /// \brief Provide the input stream from which terms are read.
71 binary_aterm_istream(std::istream& is);
72 binary_aterm_istream(std::shared_ptr<mcrl2::utilities::ibitstream> stream);
73
74 void get(aterm& t) override;
75
76private:
77 /// \returns The number of bits needed to index terms.
78 unsigned int term_index_width();
79
80 /// \returns The number of bits needed to index function symbols.
81 unsigned int function_symbol_index_width();
82
83 std::shared_ptr<mcrl2::utilities::ibitstream> m_stream;
84
85 unsigned int m_term_index_width; ///< caches the result of term_index_width().
86 unsigned int m_function_symbol_index_width; ///< caches the result of function_symbol_index_width().
87
88 atermpp::deque<aterm> m_terms; ///< An index of read terms.
89 std::deque<function_symbol> m_function_symbols; ///< An index of read function symbols.
90};
91
92} // namespace atermpp
93
94bool is_a_binary_aterm(std::istream& is);
95
96#endif // MCRL2_ATERMPP_ATERM_IO_BINARY_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
A bitstream provides per bit writing of data to any stream (including stdout).
Definition bitstream.h:32
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....
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