GiNaC 1.8.10
archive.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2026 Johannes Gutenberg University Mainz, Germany
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef GINAC_ARCHIVE_H
23#define GINAC_ARCHIVE_H
24
25#include "ex.h"
26
27#include <iosfwd>
28#include <map>
29#include <string>
30#include <vector>
31
32namespace GiNaC {
33
34class archive;
35
36
38typedef unsigned archive_node_id;
39
41typedef unsigned archive_atom;
42
43
48{
49 friend std::ostream &operator<<(std::ostream &os, const archive_node &ar);
50 friend std::istream &operator>>(std::istream &is, archive_node &ar);
51
52public:
60
66 property_info(property_type t, const std::string &n, unsigned c = 1) : type(t), name(n), count(c) {}
67
69 std::string name;
70 unsigned count;
71 };
72 typedef std::vector<property_info> propinfovector;
73
75 struct property {
77 property(archive_atom n, property_type t, unsigned v) : type(t), name(n), value(v) {}
78
81 unsigned value;
82 };
83 typedef std::vector<property>::const_iterator archive_node_cit;
87
88 archive_node(archive &ar) : a(ar), has_expression(false) {}
89 archive_node(archive &ar, const ex &expr);
90
91 const archive_node &operator=(const archive_node &other);
92
94 void add_bool(const std::string &name, bool value);
95
97 void add_unsigned(const std::string &name, unsigned value);
98
100 void add_string(const std::string &name, const std::string &value);
101
103 void add_ex(const std::string &name, const ex &value);
104
107 bool find_bool(const std::string &name, bool &ret, unsigned index = 0) const;
108
111 bool find_unsigned(const std::string &name, unsigned &ret, unsigned index = 0) const;
112
115 bool find_string(const std::string &name, std::string &ret, unsigned index = 0) const;
116
119 archive_node_cit find_first(const std::string &name) const;
120 archive_node_cit find_last(const std::string &name) const;
121
125 archive_node_cit_range find_property_range(const std::string &name1, const std::string &name2) const;
126
129 bool find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index = 0) const;
130
134 void find_ex_by_loc(archive_node_cit loc, ex &ret, lst &sym_lst) const;
135
138 const archive_node &find_ex_node(const std::string &name, unsigned index = 0) const;
139
141 void get_properties(propinfovector &v) const;
142
143 ex unarchive(lst &sym_lst) const;
144 bool has_same_ex_as(const archive_node &other) const;
145 bool has_ex() const {return has_expression;}
146 ex get_ex() const {return e;}
147
148 void forget();
149 void printraw(std::ostream &os) const;
150
151private:
154
156 std::vector<property> props;
157
159 mutable bool has_expression;
160
162 mutable ex e;
163};
164
165typedef basic* (*synthesize_func)();
166typedef std::map<std::string, synthesize_func> unarchive_map_t;
167
169{
170 static int usecount;
172public:
175 synthesize_func find(const std::string& classname) const;
176 void insert(const std::string& classname, synthesize_func f);
177};
179
218#define GINAC_DECLARE_UNARCHIVER(classname) \
219class classname ## _unarchiver \
220{ \
221 static int usecount; \
222public: \
223 static GiNaC::basic* create(); \
224 classname ## _unarchiver(); \
225 ~ classname ## _unarchiver(); \
226}; \
227static classname ## _unarchiver classname ## _unarchiver_instance
228
229#define GINAC_BIND_UNARCHIVER(classname) \
230classname ## _unarchiver::classname ## _unarchiver() \
231{ \
232 static GiNaC::unarchive_table_t table; \
233 if (usecount++ == 0) { \
234 table.insert(std::string(#classname), \
235 &(classname ## _unarchiver::create)); \
236 } \
237} \
238GiNaC::basic* classname ## _unarchiver::create() \
239{ \
240 return new classname(); \
241} \
242classname ## _unarchiver::~ classname ## _unarchiver() { } \
243int classname ## _unarchiver::usecount = 0
244
245
254{
255 friend std::ostream &operator<<(std::ostream &os, const archive &ar);
256 friend std::istream &operator>>(std::istream &is, archive &ar);
257
258public:
261
263 archive(const ex &e) {archive_ex(e, "ex");}
264
266 archive(const ex &e, const char *n) {archive_ex(e, n);}
267
271 void archive_ex(const ex &e, const char *name);
272
276 ex unarchive_ex(const lst &sym_lst, const char *name) const;
277
282 ex unarchive_ex(const lst &sym_lst, unsigned index = 0) const;
283
289 ex unarchive_ex(const lst &sym_lst, std::string &name, unsigned index = 0) const;
290
292 unsigned num_expressions() const;
293
295 const archive_node &get_top_node(unsigned index = 0) const;
296
298 void clear();
299
302
303 void forget();
304 void printraw(std::ostream &os) const;
305
306private:
308 std::vector<archive_node> nodes;
309
318
320 std::vector<archived_ex> exprs;
321
322public:
323 archive_atom atomize(const std::string &s) const;
324 const std::string &unatomize(archive_atom id) const;
325
326private:
328 mutable std::vector<std::string> atoms;
332 typedef std::map<std::string, archive_atom>::const_iterator inv_at_cit;
333 mutable std::map<std::string, archive_atom> inverse_atoms;
334
336 mutable std::map<ex, archive_node_id, ex_is_less> exprtable;
337};
338
339
340std::ostream &operator<<(std::ostream &os, const archive &ar);
341std::istream &operator>>(std::istream &is, archive &ar);
342
343} // namespace GiNaC
344
345#endif // ndef GINAC_ARCHIVE_H
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:48
void printraw(std::ostream &os) const
Output archive_node to stream in ugly raw format (for debugging).
Definition archive.cpp:662
std::vector< property > props
Vector of stored properties.
Definition archive.h:156
property_type
Property data types.
Definition archive.h:54
void get_properties(propinfovector &v) const
Return vector of properties stored in node.
Definition archive.cpp:510
bool has_expression
Flag indicating whether a cached unarchived representation of this node exists.
Definition archive.h:159
ex get_ex() const
Definition archive.h:146
bool has_ex() const
Definition archive.h:145
bool find_unsigned(const std::string &name, unsigned &ret, unsigned index=0) const
Retrieve property of type "unsigned" from node.
Definition archive.cpp:434
void add_unsigned(const std::string &name, unsigned value)
Add property of type "unsigned int" to node.
Definition archive.cpp:398
bool find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index=0) const
Retrieve property of type "ex" from node.
Definition archive.cpp:475
bool has_same_ex_as(const archive_node &other) const
Check if the archive_node stores the same expression as another archive_node.
Definition archive.cpp:347
void find_ex_by_loc(archive_node_cit loc, ex &ret, lst &sym_lst) const
Retrieve property of type "ex" from the node if it is known that this node in fact contains such a pr...
Definition archive.cpp:470
bool find_bool(const std::string &name, bool &ret, unsigned index=0) const
Retrieve property of type "bool" from node.
Definition archive.cpp:416
void forget()
Delete cached unarchived expressions from node (for debugging).
Definition archive.cpp:614
std::vector< property >::const_iterator archive_node_cit
Definition archive.h:83
archive_node(archive &ar)
Definition archive.h:88
const archive_node & find_ex_node(const std::string &name, unsigned index=0) const
Retrieve property of type "ex" from node, returning the node of the sub-expression.
Definition archive.cpp:493
friend std::istream & operator>>(std::istream &is, archive_node &ar)
Read archive_node from binary data stream.
Definition archive.cpp:243
std::vector< property_info > propinfovector
Definition archive.h:72
bool find_string(const std::string &name, std::string &ret, unsigned index=0) const
Retrieve property of type "string" from node.
Definition archive.cpp:452
void add_ex(const std::string &name, const ex &value)
Add property of type "ex" to node.
Definition archive.cpp:408
void add_bool(const std::string &name, bool value)
Add property of type "bool" to node.
Definition archive.cpp:393
archive_node_cit_range find_property_range(const std::string &name1, const std::string &name2) const
Find a range of locations in the vector of properties.
Definition archive.cpp:377
friend std::ostream & operator<<(std::ostream &os, const archive_node &ar)
Write archive_node to binary data stream.
Definition archive.cpp:198
ex e
The cached unarchived representation of this node (if any).
Definition archive.h:162
archive_node_cit find_last(const std::string &name) const
Definition archive.cpp:365
const archive_node & operator=(const archive_node &other)
Assignment operator of archive_node.
Definition archive.cpp:324
archive_node_cit find_first(const std::string &name) const
Find the location in the vector of properties of the first/last property with a given name.
Definition archive.cpp:355
archive & a
Reference to the archive to which this node belongs.
Definition archive.h:153
ex unarchive(lst &sym_lst) const
Convert archive node to GiNaC expression.
Definition archive.cpp:542
void add_string(const std::string &name, const std::string &value)
Add property of type "string" to node.
Definition archive.cpp:403
This class holds archived versions of GiNaC expressions (class ex).
Definition archive.h:254
friend std::ostream & operator<<(std::ostream &os, const archive &ar)
Write archive to binary data stream.
Definition archive.cpp:211
archive_node_id add_node(const archive_node &n)
Add archive_node to archive if the corresponding expression is not already archived.
Definition archive.cpp:48
archive_atom atomize(const std::string &s) const
Atomize a string (i.e.
Definition archive.cpp:299
archive(const ex &e, const char *n)
Construct archive from expression using the specified name.
Definition archive.h:266
void forget()
Delete cached unarchived expressions in all archive_nodes (mainly for debugging).
Definition archive.cpp:608
ex unarchive_ex(const lst &sym_lst, const char *name) const
Retrieve expression from archive by name.
Definition archive.cpp:76
archive(const ex &e)
Construct archive from expression using the default name "ex".
Definition archive.h:263
void clear()
Clear all archived expressions.
Definition archive.cpp:597
void archive_ex(const ex &e, const char *name)
Archive an expression.
Definition archive.cpp:33
unsigned num_expressions() const
Return number of archived expressions.
Definition archive.cpp:118
const std::string & unatomize(archive_atom id) const
Unatomize a string (i.e.
Definition archive.cpp:314
std::map< std::string, archive_atom >::const_iterator inv_at_cit
The map of from strings to indices of the atoms vectors allows for faster archiving.
Definition archive.h:332
std::map< ex, archive_node_id, ex_is_less > exprtable
Map of stored expressions to nodes for faster archiving.
Definition archive.h:336
const archive_node & get_top_node(unsigned index=0) const
Return reference to top node of an expression specified by index.
Definition archive.cpp:123
friend std::istream & operator>>(std::istream &is, archive &ar)
Read archive from binary data stream.
Definition archive.cpp:258
std::vector< archive_node > nodes
Vector of archived nodes.
Definition archive.h:308
archive_node & get_node(archive_node_id id)
Retrieve archive_node by ID.
Definition archive.cpp:67
void printraw(std::ostream &os) const
Print archive to stream in ugly raw format (for debugging).
Definition archive.cpp:622
std::vector< archived_ex > exprs
Vector of archived expression descriptors.
Definition archive.h:320
std::vector< std::string > atoms
Vector of atomized strings (using a vector allows faster unarchiving).
Definition archive.h:328
std::map< std::string, archive_atom > inverse_atoms
Definition archive.h:333
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:104
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:72
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:72
void insert(const std::string &classname, synthesize_func f)
Definition archive.cpp:582
synthesize_func find(const std::string &classname) const
Definition archive.cpp:573
static unarchive_map_t * unarch_map
Definition archive.h:171
Interface to GiNaC's light-weight expression handles.
static const bool value
Definition factor.cpp:198
size_t n
Definition factor.cpp:1431
size_t c
Definition factor.cpp:756
Definition add.cpp:35
unsigned archive_atom
Numerical ID value to refer to a string.
Definition archive.h:41
std::ostream & operator<<(std::ostream &os, const archive_node &n)
Write archive_node to binary data stream.
Definition archive.cpp:198
std::map< std::string, synthesize_func > unarchive_map_t
Definition archive.h:166
basic *(* synthesize_func)()
Definition archive.h:165
unsigned archive_node_id
Numerical ID value to refer to an archive_node.
Definition archive.h:38
static unarchive_table_t unarch_table_instance
Definition archive.h:178
std::istream & operator>>(std::istream &is, archive_node &n)
Read archive_node from binary data stream.
Definition archive.cpp:243
Archived expression descriptor.
Definition archive.h:311
archive_node_id root
ID of root node.
Definition archive.h:316
archived_ex(archive_atom n, archive_node_id node)
Definition archive.h:313
archive_atom name
Name of expression.
Definition archive.h:315
Information about a stored property.
Definition archive.h:64
property_info(property_type t, const std::string &n, unsigned c=1)
Definition archive.h:66
unsigned count
Number of occurrences.
Definition archive.h:70
std::string name
Name of property.
Definition archive.h:69
property_type type
Data type of property.
Definition archive.h:68
Archived property (data type, name and associated data)
Definition archive.h:75
property_type type
Data type of property.
Definition archive.h:79
property(archive_atom n, property_type t, unsigned v)
Definition archive.h:77
unsigned value
Stored value.
Definition archive.h:81
archive_atom name
Name of property.
Definition archive.h:80

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.