// Copyright (C) 2009, 2010 Laboratoire de Recherche et Développement // de l'Epita (LRDE). // Copyright (C) 2003, 2005 Laboratoire d'Informatique de Paris // 6 (LIP6), département Systèmes Répartis Coopératifs (SRC), // Université Pierre et Marie Curie. // // This file is part of Spot, a model checking library. // // Spot is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // Spot is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public // License for more details. // // You should have received a copy of the GNU General Public License // along with Spot; see the file COPYING. If not, write to the Free // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. #include #include #include "binop.hh" #include "visitor.hh" #include namespace spot { namespace ltl { binop::binop(type op, formula* first, formula* second) : op_(op), first_(first), second_(second) { } binop::~binop() { // Get this instance out of the instance map. pairf pf(first(), second()); pair p(op(), pf); map::iterator i = instances.find(p); assert (i != instances.end()); instances.erase(i); // Dereference children. first()->destroy(); second()->destroy(); } std::string binop::dump() const { return (std::string("binop(") + op_name() + ", " + first()->dump() + ", " + second()->dump() + ")"); } void binop::accept(visitor& v) { v.visit(this); } void binop::accept(const_visitor& v) const { v.visit(this); } const formula* binop::first() const { return first_; } formula* binop::first() { return first_; } const formula* binop::second() const { return second_; } formula* binop::second() { return second_; } binop::type binop::op() const { return op_; } const char* binop::op_name() const { switch (op_) { case Xor: return "Xor"; case Implies: return "Implies"; case Equiv: return "Equiv"; case U: return "U"; case R: return "R"; case W: return "W"; case M: return "M"; } // Unreachable code. assert(0); return 0; } binop::map binop::instances; binop* binop::instance(type op, formula* first, formula* second) { // Sort the operands of commutative operators, so that for // example the formula instance for 'a xor b' is the same as // that for 'b xor a'. switch (op) { case Xor: case Equiv: if (second < first) std::swap(first, second); break; case Implies: case U: case R: case W: case M: // Non commutative operators. break; } pairf pf(first, second); pair p(op, pf); map::iterator i = instances.find(p); if (i != instances.end()) { // This instance already exists. first->destroy(); second->destroy(); return static_cast(i->second->clone()); } binop* ap = new binop(op, first, second); instances[p] = ap; return static_cast(ap->clone()); } unsigned binop::instance_count() { return instances.size(); } std::ostream& binop::dump_instances(std::ostream& os) { for (map::iterator i = instances.begin(); i != instances.end(); ++i) { os << i->second << " = " << i->second->ref_count_() << " * " << i->second->dump() << std::endl; } return os; } } }