3 * Archiving of GiNaC expressions. */
6 * GiNaC Copyright (C) 1999-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "registrar.h"
38 void archive::archive_ex(const ex &e, const char *name)
40 // Create root node (which recursively archives the whole expression tree)
41 // and add it to the archive
42 archive_node_id id = add_node(archive_node(*this, e));
44 // Add root node ID to list of archived expressions
45 archived_ex ae = archived_ex(atomize(name), id);
50 /** Add archive_node to archive if the corresponding expression is
51 * not already archived.
52 * @return ID of archived node */
53 archive_node_id archive::add_node(const archive_node &n)
55 // Look if expression is known to be in some node already.
57 mapit i = exprtable.find(n.get_ex());
58 if (i != exprtable.end())
61 exprtable[n.get_ex()] = nodes.size() - 1;
62 return nodes.size() - 1;
65 // Not found, add archive_node to nodes vector
67 return nodes.size()-1;
71 /** Retrieve archive_node by ID. */
72 archive_node &archive::get_node(archive_node_id id)
74 if (id >= nodes.size())
75 throw (std::range_error("archive::get_node(): archive node ID out of range"));
81 ex archive::unarchive_ex(const lst &sym_lst, const char *name) const
84 std::string name_string = name;
85 archive_atom id = atomize(name_string);
86 std::vector<archived_ex>::const_iterator i = exprs.begin(), iend = exprs.end();
92 throw (std::runtime_error("expression with name '" + name_string + "' not found in archive"));
95 // Recursively unarchive all nodes, starting at the root node
96 lst sym_lst_copy = sym_lst;
97 return nodes[i->root].unarchive(sym_lst_copy);
100 ex archive::unarchive_ex(const lst &sym_lst, unsigned index) const
102 if (index >= exprs.size())
103 throw (std::range_error("index of archived expression out of range"));
105 // Recursively unarchive all nodes, starting at the root node
106 lst sym_lst_copy = sym_lst;
107 return nodes[exprs[index].root].unarchive(sym_lst_copy);
110 ex archive::unarchive_ex(const lst &sym_lst, std::string &name, unsigned index) const
112 if (index >= exprs.size())
113 throw (std::range_error("index of archived expression out of range"));
115 // Return expression name
116 name = unatomize(exprs[index].name);
118 // Recursively unarchive all nodes, starting at the root node
119 lst sym_lst_copy = sym_lst;
120 return nodes[exprs[index].root].unarchive(sym_lst_copy);
123 unsigned archive::num_expressions() const
128 const archive_node &archive::get_top_node(unsigned index) const
130 if (index >= exprs.size())
131 throw (std::range_error("index of archived expression out of range"));
133 return nodes[exprs[index].root];
138 * Archive file format
140 * - 4 bytes signature 'GARC'
141 * - unsigned version number
142 * - unsigned number of atoms
143 * - atom strings (each zero-terminated)
144 * - unsigned number of expressions
145 * - unsigned name atom
146 * - unsigned root node ID
147 * - unsigned number of nodes
148 * - unsigned number of properties
149 * - unsigned containing type (PTYPE_*) in its lower 3 bits and
150 * name atom in the upper bits
151 * - unsigned property value
153 * Unsigned quantities are stored in a compressed format:
154 * - numbers in the range 0x00..0x7f are stored verbatim (1 byte)
155 * - numbers larger than 0x7f are stored in 7-bit packets (1 byte per
156 * packet), starting with the LSBs; all bytes except the last one have
157 * their upper bit set
172 * 0x80 0x80 0x01 = 0x4000
176 /** Write unsigned integer quantity to stream. */
177 static void write_unsigned(std::ostream &os, unsigned val)
179 while (val >= 0x80) {
180 os.put((val & 0x7f) | 0x80);
186 /** Read unsigned integer quantity from stream. */
187 static unsigned read_unsigned(std::istream &is)
196 ret |= (b & 0x7f) << shift;
202 /** Write archive_node to binary data stream. */
203 std::ostream &operator<<(std::ostream &os, const archive_node &n)
206 unsigned num_props = n.props.size();
207 write_unsigned(os, num_props);
208 for (unsigned i=0; i<num_props; i++) {
209 write_unsigned(os, n.props[i].type | (n.props[i].name << 3));
210 write_unsigned(os, n.props[i].value);
215 /** Write archive to binary data stream. */
216 std::ostream &operator<<(std::ostream &os, const archive &ar)
219 os.put('G'); // Signature
223 write_unsigned(os, ARCHIVE_VERSION);
226 unsigned num_atoms = ar.atoms.size();
227 write_unsigned(os, num_atoms);
228 for (unsigned i=0; i<num_atoms; i++)
229 os << ar.atoms[i] << std::ends;
232 unsigned num_exprs = ar.exprs.size();
233 write_unsigned(os, num_exprs);
234 for (unsigned i=0; i<num_exprs; i++) {
235 write_unsigned(os, ar.exprs[i].name);
236 write_unsigned(os, ar.exprs[i].root);
240 unsigned num_nodes = ar.nodes.size();
241 write_unsigned(os, num_nodes);
242 for (unsigned i=0; i<num_nodes; i++)
247 /** Read archive_node from binary data stream. */
248 std::istream &operator>>(std::istream &is, archive_node &n)
251 unsigned num_props = read_unsigned(is);
252 n.props.resize(num_props);
253 for (unsigned i=0; i<num_props; i++) {
254 unsigned name_type = read_unsigned(is);
255 n.props[i].type = (archive_node::property_type)(name_type & 7);
256 n.props[i].name = name_type >> 3;
257 n.props[i].value = read_unsigned(is);
262 /** Read archive from binary data stream. */
263 std::istream &operator>>(std::istream &is, archive &ar)
267 is.get(c1); is.get(c2); is.get(c3); is.get(c4);
268 if (c1 != 'G' || c2 != 'A' || c3 != 'R' || c4 != 'C')
269 throw (std::runtime_error("not a GiNaC archive (signature not found)"));
270 unsigned version = read_unsigned(is);
271 if (version > ARCHIVE_VERSION || version < ARCHIVE_VERSION - ARCHIVE_AGE)
272 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)));
275 unsigned num_atoms = read_unsigned(is);
276 ar.atoms.resize(num_atoms);
277 for (unsigned i=0; i<num_atoms; i++) {
278 getline(is, ar.atoms[i], '\0');
279 ar.inverse_atoms[ar.atoms[i]] = i;
283 unsigned num_exprs = read_unsigned(is);
284 ar.exprs.resize(num_exprs);
285 for (unsigned i=0; i<num_exprs; i++) {
286 archive_atom name = read_unsigned(is);
287 archive_node_id root = read_unsigned(is);
288 ar.exprs[i] = archive::archived_ex(name, root);
292 unsigned num_nodes = read_unsigned(is);
293 ar.nodes.resize(num_nodes, ar);
294 for (unsigned i=0; i<num_nodes; i++)
300 /** Atomize a string (i.e. convert it into an ID number that uniquely
301 * represents the string). */
302 archive_atom archive::atomize(const std::string &s) const
304 // Search for string in inverse_atoms map.
305 inv_at_cit i = inverse_atoms.find(s);
306 if (i!=inverse_atoms.end())
309 // Not found, add to atoms vector
310 archive_atom id = atoms.size();
312 inverse_atoms[s] = id;
316 /** Unatomize a string (i.e. convert the ID number back to the string). */
317 const std::string &archive::unatomize(archive_atom id) const
319 if (id >= atoms.size())
320 throw (std::range_error("archive::unatomizee(): atom ID out of range"));
326 /** Assignment operator of archive_node. */
327 const archive_node &archive_node::operator=(const archive_node &other)
329 if (this != &other) {
330 // archive &a member doesn't get copied
332 has_expression = other.has_expression;
339 /** Recursively construct archive node from expression. */
340 archive_node::archive_node(archive &ar, const ex &expr)
341 : a(ar), has_expression(true), e(expr)
343 expr.bp->archive(*this);
347 /** Check if the archive_node stores the same expression as another
349 * @return "true" if expressions are the same */
350 bool archive_node::has_same_ex_as(const archive_node &other) const
352 if (!has_expression || !other.has_expression)
354 return e.bp == other.e.bp;
357 archive_node::archive_node_cit
358 archive_node::find_first(const std::string &name) const
360 archive_atom name_atom = a.atomize(name);
361 for (archive_node_cit i=props.begin(); i!=props.end(); ++i)
362 if (i->name == name_atom)
367 archive_node::archive_node_cit
368 archive_node::find_last(const std::string &name) const
370 archive_atom name_atom = a.atomize(name);
371 for (archive_node_cit i=props.end(); i!=props.begin();) {
373 if (i->name == name_atom)
379 void archive_node::add_bool(const std::string &name, bool value)
381 props.push_back(property(a.atomize(name), PTYPE_BOOL, value));
384 void archive_node::add_unsigned(const std::string &name, unsigned value)
386 props.push_back(property(a.atomize(name), PTYPE_UNSIGNED, value));
389 void archive_node::add_string(const std::string &name, const std::string &value)
391 props.push_back(property(a.atomize(name), PTYPE_STRING, a.atomize(value)));
394 void archive_node::add_ex(const std::string &name, const ex &value)
396 // Recursively create an archive_node and add its ID to the properties of this node
397 archive_node_id id = a.add_node(archive_node(a, value));
398 props.push_back(property(a.atomize(name), PTYPE_NODE, id));
402 bool archive_node::find_bool(const std::string &name, bool &ret, unsigned index) const
404 archive_atom name_atom = a.atomize(name);
405 archive_node_cit i = props.begin(), iend = props.end();
406 unsigned found_index = 0;
408 if (i->type == PTYPE_BOOL && i->name == name_atom) {
409 if (found_index == index) {
420 bool archive_node::find_unsigned(const std::string &name, unsigned &ret, unsigned index) const
422 archive_atom name_atom = a.atomize(name);
423 archive_node_cit i = props.begin(), iend = props.end();
424 unsigned found_index = 0;
426 if (i->type == PTYPE_UNSIGNED && i->name == name_atom) {
427 if (found_index == index) {
438 bool archive_node::find_string(const std::string &name, std::string &ret, unsigned index) const
440 archive_atom name_atom = a.atomize(name);
441 archive_node_cit i = props.begin(), iend = props.end();
442 unsigned found_index = 0;
444 if (i->type == PTYPE_STRING && i->name == name_atom) {
445 if (found_index == index) {
446 ret = a.unatomize(i->value);
456 void archive_node::find_ex_by_loc(archive_node_cit loc, ex &ret, lst &sym_lst)
459 ret = a.get_node(loc->value).unarchive(sym_lst);
462 bool archive_node::find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index) const
464 archive_atom name_atom = a.atomize(name);
465 archive_node_cit i = props.begin(), iend = props.end();
466 unsigned found_index = 0;
468 if (i->type == PTYPE_NODE && i->name == name_atom) {
469 if (found_index == index) {
470 ret = a.get_node(i->value).unarchive(sym_lst);
480 const archive_node &archive_node::find_ex_node(const std::string &name, unsigned index) const
482 archive_atom name_atom = a.atomize(name);
483 archive_node_cit i = props.begin(), iend = props.end();
484 unsigned found_index = 0;
486 if (i->type == PTYPE_NODE && i->name == name_atom) {
487 if (found_index == index)
488 return a.get_node(i->value);
493 throw (std::runtime_error("property with name '" + name + "' not found in archive node"));
497 void archive_node::get_properties(propinfovector &v) const
500 archive_node_cit i = props.begin(), iend = props.end();
502 property_type type = i->type;
503 std::string name = a.unatomize(i->name);
505 propinfovector::iterator a = v.begin(), aend = v.end();
508 if (a->type == type && a->name == name) {
516 v.push_back(property_info(type, name));
521 static synthesize_func find_factory_fcn(const std::string& name)
523 static unarchive_table_t the_table;
524 synthesize_func ret = the_table.find(name);
528 /** Convert archive node to GiNaC expression. */
529 ex archive_node::unarchive(lst &sym_lst) const
531 // Already unarchived? Then return cached unarchived expression.
535 // Find instantiation function for class specified in node
536 std::string class_name;
537 if (!find_string("class", class_name))
538 throw (std::runtime_error("archive node contains no class name"));
540 // Call instantiation function
541 synthesize_func factory_fcn = find_factory_fcn(class_name);
542 ptr<basic> obj(factory_fcn());
543 obj->setflag(status_flags::dynallocated);
544 obj->read_archive(*this, sym_lst);
546 has_expression = true;
550 int unarchive_table_t::usecount = 0;
551 unarchive_map_t* unarchive_table_t::unarch_map = 0;
553 unarchive_table_t::unarchive_table_t()
556 unarch_map = new unarchive_map_t();
560 synthesize_func unarchive_table_t::find(const std::string& classname) const
562 unarchive_map_t::const_iterator i = unarch_map->find(classname);
563 if (i != unarch_map->end())
565 throw std::runtime_error(std::string("no unarchiving function for \"")
566 + classname + "\" class");
569 void unarchive_table_t::insert(const std::string& classname, synthesize_func f)
571 if (unarch_map->find(classname) != unarch_map->end())
572 throw std::runtime_error(std::string("Class \"" + classname
573 + "\" is already registered"));
574 unarch_map->operator[](classname) = f;
577 unarchive_table_t::~unarchive_table_t()
584 void archive::clear()
587 inverse_atoms.clear();
594 /** Delete cached unarchived expressions in all archive_nodes (mainly for debugging). */
595 void archive::forget()
597 for_each(nodes.begin(), nodes.end(), std::mem_fun_ref(&archive_node::forget));
600 /** Delete cached unarchived expressions from node (for debugging). */
601 void archive_node::forget()
603 has_expression = false;
608 /** Print archive to stream in ugly raw format (for debugging). */
609 void archive::printraw(std::ostream &os) const
614 std::vector<std::string>::const_iterator i = atoms.begin(), iend = atoms.end();
617 os << " " << id << " " << *i << std::endl;
624 os << "Expressions:\n";
626 std::vector<archived_ex>::const_iterator i = exprs.begin(), iend = exprs.end();
629 os << " " << index << " \"" << unatomize(i->name) << "\" root node " << i->root << std::endl;
638 std::vector<archive_node>::const_iterator i = nodes.begin(), iend = nodes.end();
639 archive_node_id id = 0;
641 os << " " << id << " ";
648 /** Output archive_node to stream in ugly raw format (for debugging). */
649 void archive_node::printraw(std::ostream &os) const
651 // Dump cached unarchived expression
653 os << "(basic * " << e.bp << " = " << e << ")\n";
658 archive_node_cit i = props.begin(), iend = props.end();
662 case PTYPE_BOOL: os << "bool"; break;
663 case PTYPE_UNSIGNED: os << "unsigned"; break;
664 case PTYPE_STRING: os << "string"; break;
665 case PTYPE_NODE: os << "node"; break;
666 default: os << "<unknown>"; break;
668 os << " \"" << a.unatomize(i->name) << "\" " << i->value << std::endl;
673 /** Create a dummy archive. The intention is to fill archive_node's default
674 * ctor, which is currently a Cint-requirement. */
675 archive* archive_node::dummy_ar_creator()
677 static archive* some_ar = new archive;