]> www.ginac.de Git - ginac.git/blob - ginac/archive.cpp
- changed old-style power() to new-style pow()
[ginac.git] / ginac / archive.cpp
1 /** @file archive.cpp
2  *
3  *  Archiving of GiNaC expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "archive.h"
27 #include "registrar.h"
28 #include "ex.h"
29 #include "config.h"
30 #include "utils.h"
31
32 #ifndef NO_GINAC_NAMESPACE
33 namespace GiNaC {
34 #endif // ndef NO_GINAC_NAMESPACE
35
36
37 /** Archive an expression.
38  *  @param ex  the expression to be archived
39  *  @param name  name under which the expression is stored */
40 void archive::archive_ex(const ex &e, const char *name)
41 {
42         // Create root node (which recursively archives the whole expression tree)
43         // and add it to the archive
44         archive_node_id id = add_node(archive_node(*this, e));
45
46         // Add root node ID to list of archived expressions
47         archived_ex ae = archived_ex(atomize(name), id);
48         exprs.push_back(ae);
49 }
50
51
52 /** Add archive_node to archive if the corresponding expression is
53  *  not already archived.
54  *  @return ID of archived node */
55 archive_node_id archive::add_node(const archive_node &n)
56 {
57         // Search for node in nodes vector
58         vector<archive_node>::const_iterator i = nodes.begin(), iend = nodes.end();
59         archive_node_id id = 0;
60         while (i != iend) {
61                 if (i->has_same_ex_as(n))
62                         return id;
63                 i++; id++;
64         }
65
66         // Not found, add archive_node to nodes vector
67         nodes.push_back(n);
68         return id;
69 }
70
71
72 /** Retrieve archive_node by ID. */
73 archive_node &archive::get_node(archive_node_id id)
74 {
75         if (id >= nodes.size())
76                 throw (std::range_error("archive::get_node(): archive node ID out of range"));
77
78         return nodes[id];
79 }
80
81
82 /** Retrieve expression from archive by name.
83  *  @param sym_lst  list of pre-defined symbols */
84 ex archive::unarchive_ex(const lst &sym_lst, const char *name) const
85 {
86         // Find root node
87         string name_string = name;
88         archive_atom id = atomize(name_string);
89         vector<archived_ex>::const_iterator i = exprs.begin(), iend = exprs.end();
90         while (i != iend) {
91                 if (i->name == id)
92                         goto found;
93                 i++;
94         }
95         throw (std::logic_error("expression with name '" + name_string + "' not found in archive"));
96
97 found:
98         // Recursively unarchive all nodes, starting at the root node
99         return nodes[i->root].unarchive(sym_lst);
100 }
101
102 /** Retrieve expression from archive by index.
103  *  @param sym_lst  list of pre-defined symbols */
104 ex archive::unarchive_ex(const lst &sym_lst, unsigned int index) const
105 {
106         if (index >= exprs.size())
107                 throw (std::range_error("index of archived expression out of range"));
108
109         // Recursively unarchive all nodes, starting at the root node
110         return nodes[exprs[index].root].unarchive(sym_lst);
111 }
112
113 /** Retrieve expression and its name from archive by index.
114  *  @param sym_lst  list of pre-defined symbols */
115 ex archive::unarchive_ex(const lst &sym_lst, string &name, unsigned int index) const
116 {
117         if (index >= exprs.size())
118                 throw (std::range_error("index of archived expression out of range"));
119
120         // Return expression name
121         name = unatomize(exprs[index].name);
122
123         // Recursively unarchive all nodes, starting at the root node
124         return nodes[exprs[index].root].unarchive(sym_lst);
125 }
126
127
128 /** Return number of archived expressions. */
129 unsigned int archive::num_expressions(void) const
130 {
131         return exprs.size();
132 }
133
134
135 /*
136  *  Archive file format
137  *
138  *   - 4 bytes signature 'GARC'
139  *   - unsigned version number
140  *   - unsigned number of atoms
141  *      - atom strings (each zero-terminated)
142  *   - unsigned number of expressions
143  *      - unsigned name atom
144  *      - unsigned root node ID
145  *   - unsigned number of nodes
146  *      - unsigned number of properties
147  *        - unsigned type (PTYPE_*)
148  *        - unsigned name atom
149  *        - unsigned property value
150  *
151  *  Unsigned quantities are stored in a compressed format:
152  *   - numbers in the range 0x00..0x7f are stored verbatim (1 byte)
153  *   - numbers larger than 0x7f are stored in 7-bit packets (1 byte per
154  *     packet), starting with the LSBs; all bytes except the last one have
155  *     their upper bit set
156  *
157  *  Examples:
158  *   0x00           =   0x00
159  *    ..                 ..
160  *   0x7f           =   0x7f
161  *   0x80 0x01      =   0x80
162  *    ..   ..            ..
163  *   0xff 0x01      =   0xff
164  *   0x80 0x02      =  0x100
165  *    ..   ..            ..
166  *   0xff 0x02      =  0x17f
167  *   0x80 0x03      =  0x180
168  *    ..   ..            ..
169  *   0xff 0x7f      = 0x3fff
170  *   0x80 0x80 0x01 = 0x4000
171  *    ..   ..   ..       ..
172  */
173
174 /** Write unsigned integer quantity to stream. */
175 static void write_unsigned(ostream &os, unsigned int val)
176 {
177         while (val > 0x80) {
178                 os.put((val & 0x7f) | 0x80);
179                 val >>= 7;
180         }
181         os.put(val);
182 }
183
184 /** Read unsigned integer quantity from stream. */
185 static unsigned int read_unsigned(istream &is)
186 {
187         unsigned char b;
188         unsigned int ret = 0;
189         unsigned int shift = 0;
190         do {
191                 is.get(b);
192                 ret |= (b & 0x7f) << shift;
193                 shift += 7;
194         } while (b & 0x80);
195         return ret;
196 }
197
198 /** Write archive_node to binary data stream. */
199 ostream &operator<<(ostream &os, const archive_node &n)
200 {
201         // Write properties
202         unsigned int num_props = n.props.size();
203         write_unsigned(os, num_props);
204         for (unsigned int i=0; i<num_props; i++) {
205                 write_unsigned(os, n.props[i].type);
206                 write_unsigned(os, n.props[i].name);
207                 write_unsigned(os, n.props[i].value);
208         }
209 }
210
211 /** Write archive to binary data stream. */
212 ostream &operator<<(ostream &os, const archive &ar)
213 {
214         // Write header
215         os.put('G');    // Signature
216         os.put('A');
217         os.put('R');
218         os.put('C');
219         write_unsigned(os, ARCHIVE_VERSION);
220
221         // Write atoms
222         unsigned int num_atoms = ar.atoms.size();
223         write_unsigned(os, num_atoms);
224         for (unsigned int i=0; i<num_atoms; i++)
225                 os << ar.atoms[i] << ends;
226
227         // Write expressions
228         unsigned int num_exprs = ar.exprs.size();
229         write_unsigned(os, num_exprs);
230         for (unsigned int i=0; i<num_exprs; i++) {
231                 write_unsigned(os, ar.exprs[i].name);
232                 write_unsigned(os, ar.exprs[i].root);
233         }
234
235         // Write nodes
236         unsigned int num_nodes = ar.nodes.size();
237         write_unsigned(os, num_nodes);
238         for (unsigned int i=0; i<num_nodes; i++)
239                 os << ar.nodes[i];
240 }
241
242 /** Read archive_node from binary data stream. */
243 istream &operator>>(istream &is, archive_node &n)
244 {
245         // Read properties
246         unsigned int num_props = read_unsigned(is);
247         n.props.resize(num_props);
248         for (unsigned int i=0; i<num_props; i++) {
249                 n.props[i].type = (archive_node::property_type)read_unsigned(is);
250                 n.props[i].name = read_unsigned(is);
251                 n.props[i].value = read_unsigned(is);
252         }
253 }
254
255 /** Read archive from binary data stream. */
256 istream &operator>>(istream &is, archive &ar)
257 {
258         // Read header
259         char c1, c2, c3, c4;
260         is.get(c1); is.get(c2); is.get(c3); is.get(c4);
261         if (c1 != 'G' || c2 != 'A' || c3 != 'R' || c4 != 'C')
262                 throw (std::runtime_error("not a GiNaC archive (signature not found)"));
263         unsigned int version = read_unsigned(is);
264         if (version > ARCHIVE_VERSION || version < ARCHIVE_VERSION - ARCHIVE_AGE)
265                 throw (std::runtime_error("archive version " + ToString(version) + " cannot be read by this GiNaC library (which supports versions " + ToString(ARCHIVE_VERSION-ARCHIVE_AGE) + " thru " + ToString(ARCHIVE_VERSION)));
266
267         // Read atoms
268         unsigned int num_atoms = read_unsigned(is);
269         ar.atoms.resize(num_atoms);
270         for (unsigned int i=0; i<num_atoms; i++)
271                 getline(is, ar.atoms[i], '\0');
272
273         // Read expressions
274         unsigned int num_exprs = read_unsigned(is);
275         ar.exprs.resize(num_exprs);
276         for (unsigned int i=0; i<num_exprs; i++) {
277                 archive_atom name = read_unsigned(is);
278                 archive_node_id root = read_unsigned(is);
279                 ar.exprs[i] = archive::archived_ex(name, root);
280         }
281
282         // Read nodes
283         unsigned int num_nodes = read_unsigned(is);
284         ar.nodes.resize(num_nodes, ar);
285         for (unsigned int i=0; i<num_nodes; i++)
286                 is >> ar.nodes[i];
287 }
288
289
290 /** Atomize a string (i.e. convert it into an ID number that uniquely
291  *  represents the string). */
292 archive_atom archive::atomize(const string &s) const
293 {
294         // Search for string in atoms vector
295         vector<string>::const_iterator i = atoms.begin(), iend = atoms.end();
296         archive_atom id = 0;
297         while (i != iend) {
298                 if (*i == s)
299                         return id;
300                 i++; id++;
301         }
302
303         // Not found, add to atoms vector
304         atoms.push_back(s);
305         return id;
306 }
307
308 /** Unatomize a string (i.e. convert the ID number back to the string). */
309 const string &archive::unatomize(archive_atom id) const
310 {
311         if (id >= atoms.size())
312                 throw (std::range_error("archive::unatomizee(): atom ID out of range"));
313
314         return atoms[id];
315 }
316
317
318 /** Copy constructor of archive_node. */
319 archive_node::archive_node(const archive_node &other)
320         : a(other.a), props(other.props), has_expression(other.has_expression), e(other.e)
321 {
322 }
323
324
325 /** Assignment operator of archive_node. */
326 const archive_node &archive_node::operator=(const archive_node &other)
327 {
328         if (this != &other) {
329                 a = other.a;
330                 props = other.props;
331                 has_expression = other.has_expression;
332                 e = other.e;
333         }
334         return *this;
335 }
336
337
338 /** Recursively construct archive node from expression. */
339 archive_node::archive_node(archive &ar, const ex &expr)
340         : a(ar), has_expression(true), e(expr)
341 {
342         expr.bp->archive(*this);
343 }
344
345
346 /** Check if the archive_node stores the same expression as another
347  *  archive_node.
348  *  @return "true" if expressions are the same */
349 bool archive_node::has_same_ex_as(const archive_node &other) const
350 {
351         if (!has_expression || !other.has_expression)
352                 return false;
353         return e.bp == other.e.bp;
354 }
355
356
357 /** Add property of type "bool" to node. */
358 void archive_node::add_bool(const string &name, bool value)
359 {
360         props.push_back(property(a.atomize(name), PTYPE_BOOL, value));
361 }
362
363 /** Add property of type "unsigned int" to node. */
364 void archive_node::add_unsigned(const string &name, unsigned int value)
365 {
366         props.push_back(property(a.atomize(name), PTYPE_UNSIGNED, value));
367 }
368
369 /** Add property of type "string" to node. */
370 void archive_node::add_string(const string &name, const string &value)
371 {
372         props.push_back(property(a.atomize(name), PTYPE_STRING, a.atomize(value)));
373 }
374
375 /** Add property of type "ex" to node. */
376 void archive_node::add_ex(const string &name, const ex &value)
377 {
378         // Recursively create an archive_node and add its ID to the properties of this node
379         archive_node_id id = a.add_node(archive_node(a, value));
380         props.push_back(property(a.atomize(name), PTYPE_NODE, id));
381 }
382
383
384 /** Retrieve property of type "bool" from node.
385  *  @return "true" if property was found, "false" otherwise */
386 bool archive_node::find_bool(const string &name, bool &ret) const
387 {
388         archive_atom name_atom = a.atomize(name);
389         vector<property>::const_iterator i = props.begin(), iend = props.end();
390         while (i != iend) {
391                 if (i->type == PTYPE_BOOL && i->name == name_atom) {
392                         ret = i->value;
393                         return true;
394                 }
395                 i++;
396         }
397         return false;
398 }
399
400 /** Retrieve property of type "unsigned" from node.
401  *  @return "true" if property was found, "false" otherwise */
402 bool archive_node::find_unsigned(const string &name, unsigned int &ret) const
403 {
404         archive_atom name_atom = a.atomize(name);
405         vector<property>::const_iterator i = props.begin(), iend = props.end();
406         while (i != iend) {
407                 if (i->type == PTYPE_UNSIGNED && i->name == name_atom) {
408                         ret = i->value;
409                         return true;
410                 }
411                 i++;
412         }
413         return false;
414 }
415
416 /** Retrieve property of type "string" from node.
417  *  @return "true" if property was found, "false" otherwise */
418 bool archive_node::find_string(const string &name, string &ret) const
419 {
420         archive_atom name_atom = a.atomize(name);
421         vector<property>::const_iterator i = props.begin(), iend = props.end();
422         while (i != iend) {
423                 if (i->type == PTYPE_STRING && i->name == name_atom) {
424                         ret = a.unatomize(i->value);
425                         return true;
426                 }
427                 i++;
428         }
429         return false;
430 }
431
432 /** Retrieve property of type "ex" from node.
433  *  @return "true" if property was found, "false" otherwise */
434 bool archive_node::find_ex(const string &name, ex &ret, const lst &sym_lst, unsigned int index) const
435 {
436         archive_atom name_atom = a.atomize(name);
437         vector<property>::const_iterator i = props.begin(), iend = props.end();
438         unsigned int found_index = 0;
439         while (i != iend) {
440                 if (i->type == PTYPE_NODE && i->name == name_atom) {
441                         if (found_index == index)
442                                 goto found;
443                         found_index++;
444                 }
445                 i++;
446         }
447         return false;
448
449 found:
450         ret = a.get_node(i->value).unarchive(sym_lst);
451         return true;
452 }
453
454
455 /** Convert archive node to GiNaC expression. */
456 ex archive_node::unarchive(const lst &sym_lst) const
457 {
458         // Already unarchived? Then return cached unarchived expression.
459         if (has_expression)
460                 return e;
461
462         // Find instantiation function for class specified in node
463         string class_name;
464         if (!find_string("class", class_name))
465                 throw (std::runtime_error("archive node contains no class name"));
466         unarch_func f = find_unarch_func(class_name);
467
468         // Call instantiation function
469         e = f(*this, sym_lst);
470         has_expression = true;
471         return e;
472 }
473
474
475 /** Assignment operator of property. */
476 const archive_node::property &archive_node::property::operator=(const property &other)
477 {
478         if (this != &other) {
479                 type = other.type;
480                 name = other.name;
481                 value = other.value;
482         }
483         return *this;
484 }
485
486
487 /** Clear all archived expressions. */
488 void archive::clear(void)
489 {
490         atoms.clear();
491         exprs.clear();
492         nodes.clear();
493 }
494
495
496 /** Delete cached unarchived expressions in all archive_nodes (mainly for debugging). */
497 void archive::forget(void)
498 {
499         vector<archive_node>::iterator i = nodes.begin(), iend = nodes.end();
500         while (i != iend) {
501                 i->forget();
502                 i++;
503         }
504 }
505
506 /** Delete cached unarchived expressions from node (for debugging). */
507 void archive_node::forget(void)
508 {
509         has_expression = false;
510         e = 0;
511 }
512
513
514 /** Dump archive to stream (for debugging). */
515 void archive::dump(ostream &os) const
516 {
517         // Dump atoms
518         os << "Atoms:\n";
519         {
520                 vector<string>::const_iterator i = atoms.begin(), iend = atoms.end();
521                 archive_atom id = 0;
522                 while (i != iend) {
523                         os << " " << id << " " << *i << endl;
524                         i++; id++;
525                 }
526         }
527         os << endl;
528
529         // Dump expressions
530         os << "Expressions:\n";
531         {
532                 vector<archived_ex>::const_iterator i = exprs.begin(), iend = exprs.end();
533                 unsigned int index = 0;
534                 while (i != iend) {
535                         os << " " << index << " \"" << unatomize(i->name) << "\" root node " << i->root << endl;
536                         i++; index++;
537                 }
538         }
539         os << endl;
540
541         // Dump nodes
542         os << "Nodes:\n";
543         {
544                 vector<archive_node>::const_iterator i = nodes.begin(), iend = nodes.end();
545                 archive_node_id id = 0;
546                 while (i != iend) {
547                         os << " " << id << " ";
548                         i->dump(os);
549                         i++; id++;
550                 }
551         }
552 }
553
554 /** Dump archive_node to stream (for debugging). */
555 void archive_node::dump(ostream &os) const
556 {
557         // Dump cached unarchived expression
558         if (has_expression)
559                 os << "(basic * " << e.bp << " = " << e << ")\n";
560         else
561                 os << "(no expression)\n";
562
563         // Dump properties
564         vector<property>::const_iterator i = props.begin(), iend = props.end();
565         while (i != iend) {
566                 os << "  ";
567                 switch (i->type) {
568                         case PTYPE_BOOL: os << "bool"; break;
569                         case PTYPE_UNSIGNED: os << "unsigned"; break;
570                         case PTYPE_STRING: os << "string"; break;
571                         case PTYPE_NODE: os << "node"; break;
572                         default: os << "<unknown>"; break;
573                 }
574                 os << " \"" << a.unatomize(i->name) << "\" " << i->value << endl;
575                 i++;
576         }
577 }
578
579
580 #ifndef NO_GINAC_NAMESPACE
581 } // namespace GiNaC
582 #endif // ndef NO_GINAC_NAMESPACE