3 * Archiving of GiNaC expressions. */
6 * GiNaC Copyright (C) 1999-2015 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"
39 void archive::archive_ex(const ex &e, const char *name)
41 // Create root node (which recursively archives the whole expression tree)
42 // and add it to the archive
43 archive_node_id id = add_node(archive_node(*this, e));
45 // Add root node ID to list of archived expressions
46 archived_ex ae = archived_ex(atomize(name), id);
51 /** Add archive_node to archive if the corresponding expression is
52 * not already archived.
53 * @return ID of archived node */
54 archive_node_id archive::add_node(const archive_node &n)
56 // Look if expression is known to be in some node already.
58 mapit i = exprtable.find(n.get_ex());
59 if (i != exprtable.end())
62 exprtable[n.get_ex()] = nodes.size() - 1;
63 return nodes.size() - 1;
66 // Not found, add archive_node to nodes vector
68 return nodes.size()-1;
72 /** Retrieve archive_node by ID. */
73 archive_node &archive::get_node(archive_node_id id)
75 if (id >= nodes.size())
76 throw (std::range_error("archive::get_node(): archive node ID out of range"));
82 ex archive::unarchive_ex(const lst &sym_lst, const char *name) const
85 std::string name_string = name;
86 archive_atom id = atomize(name_string);
87 std::vector<archived_ex>::const_iterator i = exprs.begin(), iend = exprs.end();
93 throw (std::runtime_error("expression with name '" + name_string + "' not found in archive"));
96 // Recursively unarchive all nodes, starting at the root node
97 lst sym_lst_copy = sym_lst;
98 return nodes[i->root].unarchive(sym_lst_copy);
101 ex archive::unarchive_ex(const lst &sym_lst, unsigned index) const
103 if (index >= exprs.size())
104 throw (std::range_error("index of archived expression out of range"));
106 // Recursively unarchive all nodes, starting at the root node
107 lst sym_lst_copy = sym_lst;
108 return nodes[exprs[index].root].unarchive(sym_lst_copy);
111 ex archive::unarchive_ex(const lst &sym_lst, std::string &name, unsigned index) const
113 if (index >= exprs.size())
114 throw (std::range_error("index of archived expression out of range"));
116 // Return expression name
117 name = unatomize(exprs[index].name);
119 // Recursively unarchive all nodes, starting at the root node
120 lst sym_lst_copy = sym_lst;
121 return nodes[exprs[index].root].unarchive(sym_lst_copy);
124 unsigned archive::num_expressions() const
129 const archive_node &archive::get_top_node(unsigned index) const
131 if (index >= exprs.size())
132 throw (std::range_error("index of archived expression out of range"));
134 return nodes[exprs[index].root];
139 * Archive file format
141 * - 4 bytes signature 'GARC'
142 * - unsigned version number
143 * - unsigned number of atoms
144 * - atom strings (each zero-terminated)
145 * - unsigned number of expressions
146 * - unsigned name atom
147 * - unsigned root node ID
148 * - unsigned number of nodes
149 * - unsigned number of properties
150 * - unsigned containing type (PTYPE_*) in its lower 3 bits and
151 * name atom in the upper bits
152 * - unsigned property value
154 * Unsigned quantities are stored in a compressed format:
155 * - numbers in the range 0x00..0x7f are stored verbatim (1 byte)
156 * - numbers larger than 0x7f are stored in 7-bit packets (1 byte per
157 * packet), starting with the LSBs; all bytes except the last one have
158 * their upper bit set
173 * 0x80 0x80 0x01 = 0x4000
177 /** Write unsigned integer quantity to stream. */
178 static void write_unsigned(std::ostream &os, unsigned val)
180 while (val >= 0x80) {
181 os.put((val & 0x7f) | 0x80);
187 /** Read unsigned integer quantity from stream. */
188 static unsigned read_unsigned(std::istream &is)
197 ret |= (b & 0x7f) << shift;
203 /** Write archive_node to binary data stream. */
204 std::ostream &operator<<(std::ostream &os, const archive_node &n)
207 unsigned num_props = n.props.size();
208 write_unsigned(os, num_props);
209 for (unsigned i=0; i<num_props; i++) {
210 write_unsigned(os, n.props[i].type | (n.props[i].name << 3));
211 write_unsigned(os, n.props[i].value);
216 /** Write archive to binary data stream. */
217 std::ostream &operator<<(std::ostream &os, const archive &ar)
220 os.put('G'); // Signature
224 write_unsigned(os, GINACLIB_ARCHIVE_VERSION);
227 unsigned num_atoms = ar.atoms.size();
228 write_unsigned(os, num_atoms);
229 for (unsigned i=0; i<num_atoms; i++)
230 os << ar.atoms[i] << std::ends;
233 unsigned num_exprs = ar.exprs.size();
234 write_unsigned(os, num_exprs);
235 for (unsigned i=0; i<num_exprs; i++) {
236 write_unsigned(os, ar.exprs[i].name);
237 write_unsigned(os, ar.exprs[i].root);
241 unsigned num_nodes = ar.nodes.size();
242 write_unsigned(os, num_nodes);
243 for (unsigned i=0; i<num_nodes; i++)
248 /** Read archive_node from binary data stream. */
249 std::istream &operator>>(std::istream &is, archive_node &n)
252 unsigned num_props = read_unsigned(is);
253 n.props.resize(num_props);
254 for (unsigned i=0; i<num_props; i++) {
255 unsigned name_type = read_unsigned(is);
256 n.props[i].type = (archive_node::property_type)(name_type & 7);
257 n.props[i].name = name_type >> 3;
258 n.props[i].value = read_unsigned(is);
263 /** Read archive from binary data stream. */
264 std::istream &operator>>(std::istream &is, archive &ar)
268 is.get(c1); is.get(c2); is.get(c3); is.get(c4);
269 if (c1 != 'G' || c2 != 'A' || c3 != 'R' || c4 != 'C')
270 throw (std::runtime_error("not a GiNaC archive (signature not found)"));
271 static const unsigned max_version = GINACLIB_ARCHIVE_VERSION;
272 static const unsigned min_version = GINACLIB_ARCHIVE_VERSION - GINACLIB_ARCHIVE_AGE;
273 unsigned version = read_unsigned(is);
274 if ((version > max_version) || (version < min_version))
275 throw (std::runtime_error("archive version " + ToString(version) + " cannot be read by this GiNaC library (which supports versions " + ToString(min_version) + " thru " + ToString(max_version)));
278 unsigned num_atoms = read_unsigned(is);
279 ar.atoms.resize(num_atoms);
280 for (unsigned i=0; i<num_atoms; i++) {
281 getline(is, ar.atoms[i], '\0');
282 ar.inverse_atoms[ar.atoms[i]] = i;
286 unsigned num_exprs = read_unsigned(is);
287 ar.exprs.resize(num_exprs);
288 for (unsigned i=0; i<num_exprs; i++) {
289 archive_atom name = read_unsigned(is);
290 archive_node_id root = read_unsigned(is);
291 ar.exprs[i] = archive::archived_ex(name, root);
295 unsigned num_nodes = read_unsigned(is);
296 ar.nodes.resize(num_nodes, ar);
297 for (unsigned i=0; i<num_nodes; i++)
303 /** Atomize a string (i.e. convert it into an ID number that uniquely
304 * represents the string). */
305 archive_atom archive::atomize(const std::string &s) const
307 // Search for string in inverse_atoms map.
308 inv_at_cit i = inverse_atoms.find(s);
309 if (i!=inverse_atoms.end())
312 // Not found, add to atoms vector
313 archive_atom id = atoms.size();
315 inverse_atoms[s] = id;
319 /** Unatomize a string (i.e. convert the ID number back to the string). */
320 const std::string &archive::unatomize(archive_atom id) const
322 if (id >= atoms.size())
323 throw (std::range_error("archive::unatomize(): atom ID out of range"));
329 /** Assignment operator of archive_node. */
330 const archive_node &archive_node::operator=(const archive_node &other)
332 if (this != &other) {
333 // archive &a member doesn't get copied
335 has_expression = other.has_expression;
342 /** Recursively construct archive node from expression. */
343 archive_node::archive_node(archive &ar, const ex &expr)
344 : a(ar), has_expression(true), e(expr)
346 expr.bp->archive(*this);
350 /** Check if the archive_node stores the same expression as another
352 * @return "true" if expressions are the same */
353 bool archive_node::has_same_ex_as(const archive_node &other) const
355 if (!has_expression || !other.has_expression)
357 return e.bp == other.e.bp;
360 archive_node::archive_node_cit
361 archive_node::find_first(const std::string &name) const
363 archive_atom name_atom = a.atomize(name);
364 for (archive_node_cit i=props.begin(); i!=props.end(); ++i)
365 if (i->name == name_atom)
370 archive_node::archive_node_cit
371 archive_node::find_last(const std::string &name) const
373 archive_atom name_atom = a.atomize(name);
374 for (archive_node_cit i=props.end(); i!=props.begin();) {
376 if (i->name == name_atom)
382 void archive_node::add_bool(const std::string &name, bool value)
384 props.push_back(property(a.atomize(name), PTYPE_BOOL, value));
387 void archive_node::add_unsigned(const std::string &name, unsigned value)
389 props.push_back(property(a.atomize(name), PTYPE_UNSIGNED, value));
392 void archive_node::add_string(const std::string &name, const std::string &value)
394 props.push_back(property(a.atomize(name), PTYPE_STRING, a.atomize(value)));
397 void archive_node::add_ex(const std::string &name, const ex &value)
399 // Recursively create an archive_node and add its ID to the properties of this node
400 archive_node_id id = a.add_node(archive_node(a, value));
401 props.push_back(property(a.atomize(name), PTYPE_NODE, id));
405 bool archive_node::find_bool(const std::string &name, bool &ret, unsigned index) const
407 archive_atom name_atom = a.atomize(name);
408 archive_node_cit i = props.begin(), iend = props.end();
409 unsigned found_index = 0;
411 if (i->type == PTYPE_BOOL && i->name == name_atom) {
412 if (found_index == index) {
423 bool archive_node::find_unsigned(const std::string &name, unsigned &ret, unsigned index) const
425 archive_atom name_atom = a.atomize(name);
426 archive_node_cit i = props.begin(), iend = props.end();
427 unsigned found_index = 0;
429 if (i->type == PTYPE_UNSIGNED && i->name == name_atom) {
430 if (found_index == index) {
441 bool archive_node::find_string(const std::string &name, std::string &ret, unsigned index) const
443 archive_atom name_atom = a.atomize(name);
444 archive_node_cit i = props.begin(), iend = props.end();
445 unsigned found_index = 0;
447 if (i->type == PTYPE_STRING && i->name == name_atom) {
448 if (found_index == index) {
449 ret = a.unatomize(i->value);
459 void archive_node::find_ex_by_loc(archive_node_cit loc, ex &ret, lst &sym_lst)
462 ret = a.get_node(loc->value).unarchive(sym_lst);
465 bool archive_node::find_ex(const std::string &name, ex &ret, lst &sym_lst, unsigned index) const
467 archive_atom name_atom = a.atomize(name);
468 archive_node_cit i = props.begin(), iend = props.end();
469 unsigned found_index = 0;
471 if (i->type == PTYPE_NODE && i->name == name_atom) {
472 if (found_index == index) {
473 ret = a.get_node(i->value).unarchive(sym_lst);
483 const archive_node &archive_node::find_ex_node(const std::string &name, unsigned index) const
485 archive_atom name_atom = a.atomize(name);
486 archive_node_cit i = props.begin(), iend = props.end();
487 unsigned found_index = 0;
489 if (i->type == PTYPE_NODE && i->name == name_atom) {
490 if (found_index == index)
491 return a.get_node(i->value);
496 throw (std::runtime_error("property with name '" + name + "' not found in archive node"));
500 void archive_node::get_properties(propinfovector &v) const
503 archive_node_cit i = props.begin(), iend = props.end();
505 property_type type = i->type;
506 std::string name = a.unatomize(i->name);
508 propinfovector::iterator a = v.begin(), aend = v.end();
511 if (a->type == type && a->name == name) {
519 v.push_back(property_info(type, name));
524 static synthesize_func find_factory_fcn(const std::string& name)
526 static unarchive_table_t the_table;
527 synthesize_func ret = the_table.find(name);
531 /** Convert archive node to GiNaC expression. */
532 ex archive_node::unarchive(lst &sym_lst) const
534 // Already unarchived? Then return cached unarchived expression.
538 // Find instantiation function for class specified in node
539 std::string class_name;
540 if (!find_string("class", class_name))
541 throw (std::runtime_error("archive node contains no class name"));
543 // Call instantiation function
544 synthesize_func factory_fcn = find_factory_fcn(class_name);
545 ptr<basic> obj(factory_fcn());
546 obj->setflag(status_flags::dynallocated);
547 obj->read_archive(*this, sym_lst);
549 has_expression = true;
553 int unarchive_table_t::usecount = 0;
554 unarchive_map_t* unarchive_table_t::unarch_map = 0;
556 unarchive_table_t::unarchive_table_t()
559 unarch_map = new unarchive_map_t();
563 synthesize_func unarchive_table_t::find(const std::string& classname) const
565 unarchive_map_t::const_iterator i = unarch_map->find(classname);
566 if (i != unarch_map->end())
568 throw std::runtime_error(std::string("no unarchiving function for \"")
569 + classname + "\" class");
572 void unarchive_table_t::insert(const std::string& classname, synthesize_func f)
574 if (unarch_map->find(classname) != unarch_map->end())
575 throw std::runtime_error(std::string("Class \"" + classname
576 + "\" is already registered"));
577 unarch_map->operator[](classname) = f;
580 unarchive_table_t::~unarchive_table_t()
587 void archive::clear()
590 inverse_atoms.clear();
597 /** Delete cached unarchived expressions in all archive_nodes (mainly for debugging). */
598 void archive::forget()
600 for_each(nodes.begin(), nodes.end(), std::mem_fun_ref(&archive_node::forget));
603 /** Delete cached unarchived expressions from node (for debugging). */
604 void archive_node::forget()
606 has_expression = false;
611 /** Print archive to stream in ugly raw format (for debugging). */
612 void archive::printraw(std::ostream &os) const
617 std::vector<std::string>::const_iterator i = atoms.begin(), iend = atoms.end();
620 os << " " << id << " " << *i << std::endl;
627 os << "Expressions:\n";
629 std::vector<archived_ex>::const_iterator i = exprs.begin(), iend = exprs.end();
632 os << " " << index << " \"" << unatomize(i->name) << "\" root node " << i->root << std::endl;
641 std::vector<archive_node>::const_iterator i = nodes.begin(), iend = nodes.end();
642 archive_node_id id = 0;
644 os << " " << id << " ";
651 /** Output archive_node to stream in ugly raw format (for debugging). */
652 void archive_node::printraw(std::ostream &os) const
654 // Dump cached unarchived expression
656 os << "(basic * " << e.bp << " = " << e << ")\n";
661 archive_node_cit i = props.begin(), iend = props.end();
665 case PTYPE_BOOL: os << "bool"; break;
666 case PTYPE_UNSIGNED: os << "unsigned"; break;
667 case PTYPE_STRING: os << "string"; break;
668 case PTYPE_NODE: os << "node"; break;
669 default: os << "<unknown>"; break;
671 os << " \"" << a.unatomize(i->name) << "\" " << i->value << std::endl;