]> www.ginac.de Git - ginac.git/blob - ginac/archive.h
Fix of a bug in sqrfree_parfrac() related to Yun factorisation.
[ginac.git] / ginac / archive.h
1 /** @file archive.h
2  *
3  *  Archiving of GiNaC expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2020 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, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef GINAC_ARCHIVE_H
24 #define GINAC_ARCHIVE_H
25
26 #include "ex.h"
27
28 #include <iosfwd>
29 #include <map>
30 #include <string>
31 #include <vector>
32
33 namespace GiNaC {
34
35 class archive;
36
37
38 /** Numerical ID value to refer to an archive_node. */
39 typedef unsigned archive_node_id;
40
41 /** Numerical ID value to refer to a string. */
42 typedef unsigned archive_atom;
43
44
45 /** This class stores all properties needed to record/retrieve the state
46  *  of one object of class basic (or a derived class). Each property is
47  *  addressed by its name and data type. */
48 class archive_node
49 {
50         friend std::ostream &operator<<(std::ostream &os, const archive_node &ar);
51         friend std::istream &operator>>(std::istream &is, archive_node &ar);
52
53 public:
54         /** Property data types */
55         enum property_type {
56                 PTYPE_BOOL,
57                 PTYPE_UNSIGNED,
58                 PTYPE_STRING,
59                 PTYPE_NODE
60         };
61
62         /** Information about a stored property. A vector of these structures
63          *  is returned by get_properties().
64          *  @see get_properties */
65         struct property_info {
66                 property_info() {}
67                 property_info(property_type t, const std::string &n, unsigned c = 1) : type(t), name(n), count(c) {}
68
69                 property_type type; /**< Data type of property. */
70                 std::string name;   /**< Name of property. */
71                 unsigned count;     /**< Number of occurrences. */
72         };
73         typedef std::vector<property_info> propinfovector;
74
75         /** Archived property (data type, name and associated data) */
76         struct property {
77                 property() {}
78                 property(archive_atom n, property_type t, unsigned v) : type(t), name(n), value(v) {}
79
80                 property_type type; /**< Data type of property. */
81                 archive_atom name;  /**< Name of property. */
82                 unsigned value;     /**< Stored value. */
83         };
84         typedef std::vector<property>::const_iterator archive_node_cit;
85         struct archive_node_cit_range {
86                 archive_node_cit begin, end;
87         };
88
89         archive_node(archive &ar) : a(ar), has_expression(false) {}
90         archive_node(archive &ar, const ex &expr);
91
92         const archive_node &operator=(const archive_node &other);
93
94         /** Add property of type "bool" to node. */
95         void add_bool(const std::string &name, bool value);
96
97         /** Add property of type "unsigned int" to node. */
98         void add_unsigned(const std::string &name, unsigned value);
99
100         /** Add property of type "string" to node. */
101         void add_string(const std::string &name, const std::string &value);
102
103         /** Add property of type "ex" to node. */
104         void add_ex(const std::string &name, const ex &value);
105
106         /** Retrieve property of type "bool" from node.
107          *  @return "true" if property was found, "false" otherwise */
108         bool find_bool(const std::string &name, bool &ret, unsigned index = 0) const;
109
110         /** Retrieve property of type "unsigned" from node.
111          *  @return "true" if property was found, "false" otherwise */
112         bool find_unsigned(const std::string &name, unsigned &ret, unsigned index = 0) const;
113
114         /** Retrieve property of type "string" from node.
115          *  @return "true" if property was found, "false" otherwise */
116         bool find_string(const std::string &name, std::string &ret, unsigned index = 0) const;
117
118         /** Find the location in the vector of properties of the first/last
119          *  property with a given name. */
120         archive_node_cit find_first(const std::string &name) const;
121         archive_node_cit find_last(const std::string &name) const;
122
123         /** Find a range of locations in the vector of properties. The result
124          *  begins at the first property with name1 and ends one past the last
125          *  property with name2. */
126         archive_node_cit_range find_property_range(const std::string &name1, const std::string &name2) const;
127
128         /** Retrieve property of type "ex" from node.
129          *  @return "true" if property was found, "false" otherwise */
130         bool find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index = 0) const;
131
132         /** Retrieve property of type "ex" from the node if it is known
133          *  that this node in fact contains such a property at the given
134          *  location. This is much more efficient than the preceding function. */
135         void find_ex_by_loc(archive_node_cit loc, ex &ret, lst &sym_lst) const;
136
137         /** Retrieve property of type "ex" from node, returning the node of
138          *  the sub-expression. */
139         const archive_node &find_ex_node(const std::string &name, unsigned index = 0) const;
140
141         /** Return vector of properties stored in node. */
142         void get_properties(propinfovector &v) const;
143
144         ex unarchive(lst &sym_lst) const;
145         bool has_same_ex_as(const archive_node &other) const;
146         bool has_ex() const {return has_expression;}
147         ex get_ex() const {return e;}
148
149         void forget();
150         void printraw(std::ostream &os) const;
151
152 private:
153         /** Reference to the archive to which this node belongs. */
154         archive &a;
155
156         /** Vector of stored properties. */
157         std::vector<property> props;
158
159         /** Flag indicating whether a cached unarchived representation of this node exists. */
160         mutable bool has_expression;
161
162         /** The cached unarchived representation of this node (if any). */
163         mutable ex e;
164 };
165
166 typedef basic* (*synthesize_func)();
167 typedef std::map<std::string, synthesize_func> unarchive_map_t;
168
169 class unarchive_table_t
170 {
171         static int usecount;
172         static unarchive_map_t* unarch_map;
173 public:
174         unarchive_table_t();
175         ~unarchive_table_t();
176         synthesize_func find(const std::string& classname) const;
177         void insert(const std::string& classname, synthesize_func f);
178 };
179 static unarchive_table_t unarch_table_instance;
180
181 /** Helper macros to register a class with (un)archiving (a.k.a.
182  * (de)serialization).
183  *
184  * Usage: put 
185  *
186  * GINAC_DECLARE_UNARCHIVER(myclass);
187  *
188  * into the header file (in the global or namespace scope), and
189  *
190  * GINAC_BIND_UNARCHIVER(myclass);
191  *
192  * into the source file.
193  *
194  * Effect: the `myclass' (being a class derived directly or indirectly
195  * from GiNaC::basic) can be archived and unarchived.
196  *
197  * Note: you need to use GINAC_{DECLARE,BIND}_UNARCHIVER incantations
198  * in order to make your class (un)archivable _even if your class does
199  * not overload `read_archive' method_. Sorry for inconvenience.
200  *
201  * How it works: 
202  *
203  * The `basic' class has a `read_archive' virtual method which reads an
204  * expression from archive. Derived classes can overload that method.
205  * There's a small problem, though. On unarchiving all we have is a set
206  * of named byte streams. In C++ the class name (as written in the source
207  * code) has nothing to do with its actual type. Thus, we need establish
208  * a correspondence ourselves. To do so we maintain a `class_name' =>
209  * `function_pointer' table (see the unarchive_table_t class above).
210  * Every function in this table is supposed to create a new object of
211  * the `class_name' type. The `archive_node' class uses that table to
212  * construct an object of correct type. Next it invokes read_archive
213  * virtual method of newly created object, which does the actual job.
214  *
215  * Note: this approach is very simple-minded (it does not handle classes
216  * with same names from different namespaces, multiple inheritance, etc),
217  * but it happens to work surprisingly well.
218  */
219 #define GINAC_DECLARE_UNARCHIVER(classname)                     \
220 class classname ## _unarchiver                                  \
221 {                                                               \
222         static int usecount;                                    \
223 public:                                                         \
224         static GiNaC::basic* create();                          \
225         classname ## _unarchiver();                             \
226         ~ classname ## _unarchiver();                           \
227 };                                                              \
228 static classname ## _unarchiver classname ## _unarchiver_instance
229
230 #define GINAC_BIND_UNARCHIVER(classname)                        \
231 classname ## _unarchiver::classname ## _unarchiver()            \
232 {                                                               \
233         static GiNaC::unarchive_table_t table;                  \
234         if (usecount++ == 0) {                                  \
235                 table.insert(std::string(#classname),           \
236                         &(classname ## _unarchiver::create));   \
237         }                                                       \
238 }                                                               \
239 GiNaC::basic* classname ## _unarchiver::create()                \
240 {                                                               \
241         return new classname();                                 \
242 }                                                               \
243 classname ## _unarchiver::~ classname ## _unarchiver() { }      \
244 int classname ## _unarchiver::usecount = 0
245
246
247 /** This class holds archived versions of GiNaC expressions (class ex).
248  *  An archive can be constructed from an expression and then written to
249  *  a stream; or it can be read from a stream and then unarchived, yielding
250  *  back the expression. Archives can hold multiple expressions which can
251  *  be referred to by name or index number. The main component of the
252  *  archive class is a vector of archive_nodes which each store one object
253  *  of class basic (or a derived class). */
254 class archive
255 {
256         friend std::ostream &operator<<(std::ostream &os, const archive &ar);
257         friend std::istream &operator>>(std::istream &is, archive &ar);
258
259 public:
260         archive() {}
261         ~archive() {}
262
263         /** Construct archive from expression using the default name "ex". */
264         archive(const ex &e) {archive_ex(e, "ex");}
265
266         /** Construct archive from expression using the specified name. */
267         archive(const ex &e, const char *n) {archive_ex(e, n);}
268
269         /** Archive an expression.
270          *  @param e the expression to be archived
271          *  @param name name under which the expression is stored */
272         void archive_ex(const ex &e, const char *name);
273
274         /** Retrieve expression from archive by name.
275          *  @param sym_lst list of pre-defined symbols
276          *  @param name name of expression */
277         ex unarchive_ex(const lst &sym_lst, const char *name) const;
278
279         /** Retrieve expression from archive by index.
280          *  @param sym_lst list of pre-defined symbols
281          *  @param index index of expression
282          *  @see count_expressions */
283         ex unarchive_ex(const lst &sym_lst, unsigned index = 0) const;
284
285         /** Retrieve expression and its name from archive by index.
286          *  @param sym_lst list of pre-defined symbols
287          *  @param name receives the name of the expression
288          *  @param index index of expression
289          *  @see count_expressions */
290         ex unarchive_ex(const lst &sym_lst, std::string &name, unsigned index = 0) const;
291
292         /** Return number of archived expressions. */
293         unsigned num_expressions() const;
294
295         /** Return reference to top node of an expression specified by index. */
296         const archive_node &get_top_node(unsigned index = 0) const;
297
298         /** Clear all archived expressions. */
299         void clear();
300
301         archive_node_id add_node(const archive_node &n);
302         archive_node &get_node(archive_node_id id);
303
304         void forget();
305         void printraw(std::ostream &os) const;
306
307 private:
308         /** Vector of archived nodes. */
309         std::vector<archive_node> nodes;
310
311         /** Archived expression descriptor. */
312         struct archived_ex {
313                 archived_ex() {}
314                 archived_ex(archive_atom n, archive_node_id node) : name(n), root(node) {}
315
316                 archive_atom name;     /**< Name of expression. */
317                 archive_node_id root;  /**< ID of root node. */
318         };
319
320         /** Vector of archived expression descriptors. */
321         std::vector<archived_ex> exprs;
322
323 public:
324         archive_atom atomize(const std::string &s) const;
325         const std::string &unatomize(archive_atom id) const;
326
327 private:
328         /** Vector of atomized strings (using a vector allows faster unarchiving). */
329         mutable std::vector<std::string> atoms;
330         /** The map of from strings to indices of the atoms vectors allows for
331          *  faster archiving.
332          */
333         typedef std::map<std::string, archive_atom>::const_iterator inv_at_cit;
334         mutable std::map<std::string, archive_atom> inverse_atoms;
335
336         /** Map of stored expressions to nodes for faster archiving */
337         mutable std::map<ex, archive_node_id, ex_is_less> exprtable;
338 };
339
340
341 std::ostream &operator<<(std::ostream &os, const archive &ar);
342 std::istream &operator>>(std::istream &is, archive &ar);
343
344 } // namespace GiNaC
345
346 #endif // ndef GINAC_ARCHIVE_H