3 * Archiving of GiNaC expressions. */
6 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
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.
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.
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
27 #include "registrar.h"
36 void archive::archive_ex(const ex &e, const char *name)
38 // Create root node (which recursively archives the whole expression tree)
39 // and add it to the archive
40 archive_node_id id = add_node(archive_node(*this, e));
42 // Add root node ID to list of archived expressions
43 archived_ex ae = archived_ex(atomize(name), id);
48 /** Add archive_node to archive if the corresponding expression is
49 * not already archived.
50 * @return ID of archived node */
51 archive_node_id archive::add_node(const archive_node &n)
53 // Look if expression is known to be in some node already.
55 mapit i = exprtable.find(n.get_ex());
56 if (i != exprtable.end())
59 exprtable[n.get_ex()] = nodes.size() - 1;
60 return nodes.size() - 1;
63 // Not found, add archive_node to nodes vector
65 return nodes.size()-1;
69 /** Retrieve archive_node by ID. */
70 archive_node &archive::get_node(archive_node_id id)
72 if (id >= nodes.size())
73 throw (std::range_error("archive::get_node(): archive node ID out of range"));
79 ex archive::unarchive_ex(const lst &sym_lst, const char *name) const
82 std::string name_string = name;
83 archive_atom id = atomize(name_string);
84 std::vector<archived_ex>::const_iterator i = exprs.begin(), iend = exprs.end();
90 throw (std::runtime_error("expression with name '" + name_string + "' not found in archive"));
93 // Recursively unarchive all nodes, starting at the root node
94 lst sym_lst_copy = sym_lst;
95 return nodes[i->root].unarchive(sym_lst_copy);
98 ex archive::unarchive_ex(const lst &sym_lst, unsigned index) const
100 if (index >= exprs.size())
101 throw (std::range_error("index of archived expression out of range"));
103 // Recursively unarchive all nodes, starting at the root node
104 lst sym_lst_copy = sym_lst;
105 return nodes[exprs[index].root].unarchive(sym_lst_copy);
108 ex archive::unarchive_ex(const lst &sym_lst, std::string &name, unsigned index) const
110 if (index >= exprs.size())
111 throw (std::range_error("index of archived expression out of range"));
113 // Return expression name
114 name = unatomize(exprs[index].name);
116 // Recursively unarchive all nodes, starting at the root node
117 lst sym_lst_copy = sym_lst;
118 return nodes[exprs[index].root].unarchive(sym_lst_copy);
121 unsigned archive::num_expressions() const
126 const archive_node &archive::get_top_node(unsigned index) const
128 if (index >= exprs.size())
129 throw (std::range_error("index of archived expression out of range"));
131 return nodes[exprs[index].root];
136 * Archive file format
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 containing type (PTYPE_*) in its lower 3 bits and
148 * name atom in the upper bits
149 * - unsigned property value
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
170 * 0x80 0x80 0x01 = 0x4000
174 /** Write unsigned integer quantity to stream. */
175 static void write_unsigned(std::ostream &os, unsigned val)
177 while (val >= 0x80) {
178 os.put((val & 0x7f) | 0x80);
184 /** Read unsigned integer quantity from stream. */
185 static unsigned read_unsigned(std::istream &is)
194 ret |= (b & 0x7f) << shift;
200 /** Write archive_node to binary data stream. */
201 std::ostream &operator<<(std::ostream &os, const archive_node &n)
204 unsigned num_props = n.props.size();
205 write_unsigned(os, num_props);
206 for (unsigned i=0; i<num_props; i++) {
207 write_unsigned(os, n.props[i].type | (n.props[i].name << 3));
208 write_unsigned(os, n.props[i].value);
213 /** Write archive to binary data stream. */
214 std::ostream &operator<<(std::ostream &os, const archive &ar)
217 os.put('G'); // Signature
221 write_unsigned(os, ARCHIVE_VERSION);
224 unsigned num_atoms = ar.atoms.size();
225 write_unsigned(os, num_atoms);
226 for (unsigned i=0; i<num_atoms; i++)
227 os << ar.atoms[i] << std::ends;
230 unsigned num_exprs = ar.exprs.size();
231 write_unsigned(os, num_exprs);
232 for (unsigned i=0; i<num_exprs; i++) {
233 write_unsigned(os, ar.exprs[i].name);
234 write_unsigned(os, ar.exprs[i].root);
238 unsigned num_nodes = ar.nodes.size();
239 write_unsigned(os, num_nodes);
240 for (unsigned i=0; i<num_nodes; i++)
245 /** Read archive_node from binary data stream. */
246 std::istream &operator>>(std::istream &is, archive_node &n)
249 unsigned num_props = read_unsigned(is);
250 n.props.resize(num_props);
251 for (unsigned i=0; i<num_props; i++) {
252 unsigned name_type = read_unsigned(is);
253 n.props[i].type = (archive_node::property_type)(name_type & 7);
254 n.props[i].name = name_type >> 3;
255 n.props[i].value = read_unsigned(is);
260 /** Read archive from binary data stream. */
261 std::istream &operator>>(std::istream &is, archive &ar)
265 is.get(c1); is.get(c2); is.get(c3); is.get(c4);
266 if (c1 != 'G' || c2 != 'A' || c3 != 'R' || c4 != 'C')
267 throw (std::runtime_error("not a GiNaC archive (signature not found)"));
268 unsigned version = read_unsigned(is);
269 if (version > ARCHIVE_VERSION || version < ARCHIVE_VERSION - ARCHIVE_AGE)
270 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)));
273 unsigned num_atoms = read_unsigned(is);
274 ar.atoms.resize(num_atoms);
275 for (unsigned i=0; i<num_atoms; i++)
276 getline(is, ar.atoms[i], '\0');
279 unsigned num_exprs = read_unsigned(is);
280 ar.exprs.resize(num_exprs);
281 for (unsigned i=0; i<num_exprs; i++) {
282 archive_atom name = read_unsigned(is);
283 archive_node_id root = read_unsigned(is);
284 ar.exprs[i] = archive::archived_ex(name, root);
288 unsigned num_nodes = read_unsigned(is);
289 ar.nodes.resize(num_nodes, ar);
290 for (unsigned i=0; i<num_nodes; i++)
296 /** Atomize a string (i.e. convert it into an ID number that uniquely
297 * represents the string). */
298 archive_atom archive::atomize(const std::string &s) const
300 // Search for string in atoms vector
301 std::vector<std::string>::const_iterator i = atoms.begin(), iend = atoms.end();
309 // Not found, add to atoms vector
314 /** Unatomize a string (i.e. convert the ID number back to the string). */
315 const std::string &archive::unatomize(archive_atom id) const
317 if (id >= atoms.size())
318 throw (std::range_error("archive::unatomizee(): atom ID out of range"));
324 /** Assignment operator of archive_node. */
325 const archive_node &archive_node::operator=(const archive_node &other)
327 if (this != &other) {
328 // archive &a member doesn't get copied
330 has_expression = other.has_expression;
337 /** Recursively construct archive node from expression. */
338 archive_node::archive_node(archive &ar, const ex &expr)
339 : a(ar), has_expression(true), e(expr)
341 expr.bp->archive(*this);
345 /** Check if the archive_node stores the same expression as another
347 * @return "true" if expressions are the same */
348 bool archive_node::has_same_ex_as(const archive_node &other) const
350 if (!has_expression || !other.has_expression)
352 return e.bp == other.e.bp;
356 void archive_node::add_bool(const std::string &name, bool value)
358 props.push_back(property(a.atomize(name), PTYPE_BOOL, value));
361 void archive_node::add_unsigned(const std::string &name, unsigned value)
363 props.push_back(property(a.atomize(name), PTYPE_UNSIGNED, value));
366 void archive_node::add_string(const std::string &name, const std::string &value)
368 props.push_back(property(a.atomize(name), PTYPE_STRING, a.atomize(value)));
371 void archive_node::add_ex(const std::string &name, const ex &value)
373 // Recursively create an archive_node and add its ID to the properties of this node
374 archive_node_id id = a.add_node(archive_node(a, value));
375 props.push_back(property(a.atomize(name), PTYPE_NODE, id));
379 bool archive_node::find_bool(const std::string &name, bool &ret, unsigned index) const
381 archive_atom name_atom = a.atomize(name);
382 std::vector<property>::const_iterator i = props.begin(), iend = props.end();
383 unsigned found_index = 0;
385 if (i->type == PTYPE_BOOL && i->name == name_atom) {
386 if (found_index == index) {
397 bool archive_node::find_unsigned(const std::string &name, unsigned &ret, unsigned index) const
399 archive_atom name_atom = a.atomize(name);
400 std::vector<property>::const_iterator i = props.begin(), iend = props.end();
401 unsigned found_index = 0;
403 if (i->type == PTYPE_UNSIGNED && i->name == name_atom) {
404 if (found_index == index) {
415 bool archive_node::find_string(const std::string &name, std::string &ret, unsigned index) const
417 archive_atom name_atom = a.atomize(name);
418 std::vector<property>::const_iterator i = props.begin(), iend = props.end();
419 unsigned found_index = 0;
421 if (i->type == PTYPE_STRING && i->name == name_atom) {
422 if (found_index == index) {
423 ret = a.unatomize(i->value);
433 bool archive_node::find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index) const
435 archive_atom name_atom = a.atomize(name);
436 std::vector<property>::const_iterator i = props.begin(), iend = props.end();
437 unsigned found_index = 0;
439 if (i->type == PTYPE_NODE && i->name == name_atom) {
440 if (found_index == index) {
441 ret = a.get_node(i->value).unarchive(sym_lst);
451 const archive_node &archive_node::find_ex_node(const std::string &name, unsigned index) const
453 archive_atom name_atom = a.atomize(name);
454 std::vector<property>::const_iterator i = props.begin(), iend = props.end();
455 unsigned found_index = 0;
457 if (i->type == PTYPE_NODE && i->name == name_atom) {
458 if (found_index == index)
459 return a.get_node(i->value);
464 throw (std::runtime_error("property with name '" + name + "' not found in archive node"));
468 void archive_node::get_properties(propinfovector &v) const
471 std::vector<property>::const_iterator i = props.begin(), iend = props.end();
473 property_type type = i->type;
474 std::string name = a.unatomize(i->name);
476 propinfovector::iterator a = v.begin(), aend = v.end();
479 if (a->type == type && a->name == name) {
487 v.push_back(property_info(type, name));
493 /** Convert archive node to GiNaC expression. */
494 ex archive_node::unarchive(lst &sym_lst) const
496 // Already unarchived? Then return cached unarchived expression.
500 // Find instantiation function for class specified in node
501 std::string class_name;
502 if (!find_string("class", class_name))
503 throw (std::runtime_error("archive node contains no class name"));
504 unarch_func f = find_unarch_func(class_name);
506 // Call instantiation function
507 e = f(*this, sym_lst);
508 has_expression = true;
513 void archive::clear()
522 /** Delete cached unarchived expressions in all archive_nodes (mainly for debugging). */
523 void archive::forget()
525 for_each(nodes.begin(), nodes.end(), std::mem_fun_ref(&archive_node::forget));
528 /** Delete cached unarchived expressions from node (for debugging). */
529 void archive_node::forget()
531 has_expression = false;
536 /** Print archive to stream in ugly raw format (for debugging). */
537 void archive::printraw(std::ostream &os) const
542 std::vector<std::string>::const_iterator i = atoms.begin(), iend = atoms.end();
545 os << " " << id << " " << *i << std::endl;
552 os << "Expressions:\n";
554 std::vector<archived_ex>::const_iterator i = exprs.begin(), iend = exprs.end();
557 os << " " << index << " \"" << unatomize(i->name) << "\" root node " << i->root << std::endl;
566 std::vector<archive_node>::const_iterator i = nodes.begin(), iend = nodes.end();
567 archive_node_id id = 0;
569 os << " " << id << " ";
576 /** Output archive_node to stream in ugly raw format (for debugging). */
577 void archive_node::printraw(std::ostream &os) const
579 // Dump cached unarchived expression
581 os << "(basic * " << e.bp << " = " << e << ")\n";
586 std::vector<property>::const_iterator i = props.begin(), iend = props.end();
590 case PTYPE_BOOL: os << "bool"; break;
591 case PTYPE_UNSIGNED: os << "unsigned"; break;
592 case PTYPE_STRING: os << "string"; break;
593 case PTYPE_NODE: os << "node"; break;
594 default: os << "<unknown>"; break;
596 os << " \"" << a.unatomize(i->name) << "\" " << i->value << std::endl;
601 /** Create a dummy archive. The intention is to fill archive_node's default
602 * ctor, which is currently a Cint-requirement. */
603 archive* archive_node::dummy_ar_creator()
605 static archive* some_ar = new archive;