ltlast: simplify with std::make_pair() and c++11's std::tuple

* src/ltlast/atomic_prop.cc, src/ltlast/atomic_prop.hh,
src/ltlast/automatop.cc, src/ltlast/automatop.hh, src/ltlast/binop.cc,
src/ltlast/binop.hh, src/ltlast/bunop.cc, src/ltlast/bunop.hh,
src/ltlast/multop.cc, src/ltlast/multop.hh, src/ltlast/unop.cc,
src/ltlast/unop.hh: Use std::tuple to replace nested std::pair,
simplify calls to std::map::erase, use auto and std::make_pair with
insert, and simplify the dump() method using a range for.
This commit is contained in:
Alexandre Duret-Lutz 2014-01-06 20:04:53 +01:00
parent c64503fb33
commit b37dc0bc90
12 changed files with 89 additions and 119 deletions

View file

@ -23,6 +23,7 @@
#include "config.h" #include "config.h"
#include "atomic_prop.hh" #include "atomic_prop.hh"
#include "visitor.hh" #include "visitor.hh"
#include <cstddef>
#include <cassert> #include <cassert>
#include <ostream> #include <ostream>
@ -67,10 +68,9 @@ namespace spot
atomic_prop::~atomic_prop() atomic_prop::~atomic_prop()
{ {
// Get this instance out of the instance map. // Get this instance out of the instance map.
pair p(name(), &env()); size_t c = instances.erase(key(name(), &env()));
map::iterator i = instances.find(p); assert(c == 1);
assert (i != instances.end()); (void) c; // For the NDEBUG case.
instances.erase(i);
} }
std::string std::string
@ -90,10 +90,8 @@ namespace spot
const atomic_prop* const atomic_prop*
atomic_prop::instance(const std::string& name, environment& env) atomic_prop::instance(const std::string& name, environment& env)
{ {
pair p(name, &env);
const atomic_prop* ap; const atomic_prop* ap;
std::pair<map::iterator, bool> ires = auto ires = instances.insert(std::make_pair(key(name, &env), nullptr));
instances.insert(map::value_type(p, 0));
if (!ires.second) if (!ires.second)
ap = ires.first->second; ap = ires.first->second;
else else
@ -111,13 +109,10 @@ namespace spot
std::ostream& std::ostream&
atomic_prop::dump_instances(std::ostream& os) atomic_prop::dump_instances(std::ostream& os)
{ {
for (const auto& i: instances)
for (map::iterator i = instances.begin(); i != instances.end(); ++i) os << i.second << " = " << i.second->ref_count_()
{ << " * atomic_prop(" << i.first.first << ", "
os << i->second << " = " << i->second->ref_count_() << i.first.second->name() << ")" << std::endl;
<< " * atomic_prop(" << i->first.first << ", "
<< i->first.second->name() << ")" << std::endl;
}
return os; return os;
} }

View file

@ -72,8 +72,8 @@ namespace spot
atomic_prop(const std::string& name, environment& env); atomic_prop(const std::string& name, environment& env);
virtual ~atomic_prop(); virtual ~atomic_prop();
typedef std::pair<std::string, environment*> pair; typedef std::pair<std::string, environment*> key;
typedef std::map<pair, const atomic_prop*> map; typedef std::map<key, const atomic_prop*> map;
static map instances; static map instances;
private: private:

View file

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include <iostream> #include <iostream>
#include <cstddef>
#include "automatop.hh" #include "automatop.hh"
#include "nfa.hh" #include "nfa.hh"
#include "visitor.hh" #include "visitor.hh"
@ -60,10 +61,9 @@ namespace spot
automatop::~automatop() automatop::~automatop()
{ {
// Get this instance out of the instance map. // Get this instance out of the instance map.
triplet p(std::make_pair(get_nfa(), negated_), children_); size_t c = instances.erase(key(get_nfa(), negated_, children_));
map::iterator i = instances.find(p); assert(c == 1);
assert (i != instances.end()); (void) c; // For the NDEBUG case.
instances.erase(i);
// Dereference children. // Dereference children.
unsigned s = size(); unsigned s = size();
@ -97,11 +97,10 @@ namespace spot
const automatop* const automatop*
automatop::instance(const nfa::ptr nfa, vec* v, bool negated) automatop::instance(const nfa::ptr nfa, vec* v, bool negated)
{ {
assert(nfa != 0); assert(nfa);
triplet p(std::make_pair(nfa, negated), v);
const automatop* res; const automatop* res;
std::pair<map::iterator, bool> ires = auto ires = instances.insert(std::make_pair(key(nfa, negated, v),
instances.insert(map::value_type(p, 0)); nullptr));
if (!ires.second) if (!ires.second)
{ {
// The instance already exists. // The instance already exists.
@ -127,13 +126,11 @@ namespace spot
std::ostream& std::ostream&
automatop::dump_instances(std::ostream& os) automatop::dump_instances(std::ostream& os)
{ {
for (map::iterator i = instances.begin(); i != instances.end(); ++i) for (const auto& i: instances)
{ os << i.second << " = "
os << i->second << " = " << i.second->ref_count_() << " * "
<< i->second->ref_count_() << " * " << i.second->dump()
<< i->second->dump() << std::endl;
<< std::endl;
}
return os; return os;
} }
} }

View file

@ -24,6 +24,7 @@
# include "refformula.hh" # include "refformula.hh"
# include <vector> # include <vector>
# include <tuple>
# include <iosfwd> # include <iosfwd>
# include <map> # include <map>
# include "nfa.hh" # include "nfa.hh"
@ -89,21 +90,21 @@ namespace spot
protected: protected:
typedef std::pair<std::pair<nfa::ptr, bool>, vec*> triplet; typedef std::tuple<nfa::ptr, bool, vec*> key;
/// Comparison functor used internally by ltl::automatop. /// Comparison functor used internally by ltl::automatop.
struct tripletcmp struct tripletcmp
{ {
bool bool
operator()(const triplet& p1, const triplet& p2) const operator()(const key& p1, const key& p2) const
{ {
if (p1.first.first != p2.first.first) if (std::get<0>(p1) != std::get<0>(p2))
return p1.first.first < p2.first.first; return std::get<0>(p1) < std::get<0>(p2);
if (p1.first.second != p2.first.second) if (std::get<1>(p1) != std::get<1>(p2))
return p1.first.second < p2.first.second; return std::get<1>(p1) < std::get<1>(p2);
return *p1.second < *p2.second; return *std::get<2>(p1) < *std::get<2>(p2);
} }
}; };
typedef std::map<triplet, const automatop*, tripletcmp> map; typedef std::map<key, const automatop*> map;
static map instances; static map instances;
automatop(const nfa::ptr, vec* v, bool negated); automatop(const nfa::ptr, vec* v, bool negated);

View file

@ -22,6 +22,7 @@
#include "config.h" #include "config.h"
#include <cassert> #include <cassert>
#include <cstddef>
#include <utility> #include <utility>
#include "binop.hh" #include "binop.hh"
#include "unop.hh" #include "unop.hh"
@ -250,11 +251,9 @@ namespace spot
binop::~binop() binop::~binop()
{ {
// Get this instance out of the instance map. // Get this instance out of the instance map.
pairf pf(first(), second()); size_t c = instances.erase(key(op(), first(), second()));
pair p(op(), pf); assert(c == 1);
map::iterator i = instances.find(p); (void) c; // For the NDEBUG case.
assert (i != instances.end());
instances.erase(i);
// Dereference children. // Dereference children.
first()->destroy(); first()->destroy();
@ -506,12 +505,9 @@ namespace spot
break; break;
} }
pairf pf(first, second);
pair p(op, pf);
const binop* res; const binop* res;
std::pair<map::iterator, bool> ires = auto ires = instances.insert(std::make_pair(key(op, first, second),
instances.insert(map::value_type(p, 0)); nullptr));
if (!ires.second) if (!ires.second)
{ {
// This instance already exists. // This instance already exists.
@ -536,15 +532,12 @@ namespace spot
std::ostream& std::ostream&
binop::dump_instances(std::ostream& os) binop::dump_instances(std::ostream& os)
{ {
for (map::iterator i = instances.begin(); i != instances.end(); ++i) for (const auto& i: instances)
{ os << i.second << " = "
os << i->second << " = " << i.second->ref_count_() << " * "
<< i->second->ref_count_() << " * " << i.second->dump()
<< i->second->dump() << std::endl;
<< std::endl;
}
return os; return os;
} }

View file

@ -31,6 +31,7 @@
#include "refformula.hh" #include "refformula.hh"
#include <map> #include <map>
#include <iosfwd> #include <iosfwd>
#include <tuple>
namespace spot namespace spot
{ {
@ -142,9 +143,8 @@ namespace spot
static std::ostream& dump_instances(std::ostream& os); static std::ostream& dump_instances(std::ostream& os);
protected: protected:
typedef std::pair<const formula*, const formula*> pairf; typedef std::tuple<type, const formula*, const formula*> key;
typedef std::pair<type, pairf> pair; typedef std::map<key, const binop*> map;
typedef std::map<pair, const binop*> map;
static map instances; static map instances;
binop(type op, const formula* first, const formula* second); binop(type op, const formula* first, const formula* second);

View file

@ -66,15 +66,14 @@ namespace spot
bunop::~bunop() bunop::~bunop()
{ {
// one_star_ should never get delete. Otherwise, that means // one_star_ should never get deleted. Otherwise, that means it
// is has been destroyed too much, or not cloned enough. // has been destroyed too much, or not cloned enough.
assert(this != one_star_); assert(this != one_star_);
// Get this instance out of the instance map. // Get this instance out of the instance map.
pair p(pairo(op(), child()), pairu(min_, max_)); size_t c = instances.erase(key(op(), child(), min_, max_));
map::iterator i = instances.find(p); assert(c == 1);
assert (i != instances.end()); (void) c; // For the NDEBUG case.
instances.erase(i);
// Dereference child. // Dereference child.
child()->destroy(); child()->destroy();
@ -239,11 +238,9 @@ namespace spot
} }
} }
pair p(pairo(op, child), pairu(min, max));
const formula* res; const formula* res;
std::pair<map::iterator, bool> ires = auto ires = instances.insert(std::make_pair(key(op, child, min, max),
instances.insert(map::value_type(p, 0)); nullptr));
if (!ires.second) if (!ires.second)
{ {
// This instance already exists. // This instance already exists.
@ -303,13 +300,11 @@ namespace spot
std::ostream& std::ostream&
bunop::dump_instances(std::ostream& os) bunop::dump_instances(std::ostream& os)
{ {
for (map::iterator i = instances.begin(); i != instances.end(); ++i) for (const auto& i: instances)
{ os << i.second << " = "
os << i->second << " = " << i.second->ref_count_() << " * "
<< i->second->ref_count_() << " * " << i.second->dump()
<< i->second->dump() << std::endl;
<< std::endl;
}
return os; return os;
} }

View file

@ -25,6 +25,7 @@
#include "refformula.hh" #include "refformula.hh"
#include <map> #include <map>
#include <iosfwd> #include <iosfwd>
#include <tuple>
#include "constant.hh" #include "constant.hh"
namespace spot namespace spot
@ -140,10 +141,8 @@ namespace spot
} }
protected: protected:
typedef std::pair<unsigned, unsigned> pairu; typedef std::tuple<type, const formula*, unsigned, unsigned> key;
typedef std::pair<type, const formula*> pairo; typedef std::map<key, const bunop*> map;
typedef std::pair<pairo, pairu> pair;
typedef std::map<pair, const bunop*> map;
static map instances; static map instances;
bunop(type op, const formula* child, unsigned min, unsigned max); bunop(type op, const formula* child, unsigned min, unsigned max);

View file

@ -21,6 +21,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.h" #include "config.h"
#include <cstddef>
#include <cassert> #include <cassert>
#include <utility> #include <utility>
#include <algorithm> #include <algorithm>
@ -94,10 +95,9 @@ namespace spot
multop::~multop() multop::~multop()
{ {
// Get this instance out of the instance map. // Get this instance out of the instance map.
pair p(op(), children_); size_t c = instances.erase(key(op(), children_));
map::iterator i = instances.find(p); assert(c == 1);
assert (i != instances.end()); (void) c; // For the NDEBUG case.
instances.erase(i);
// Dereference children. // Dereference children.
unsigned s = size(); unsigned s = size();
@ -593,19 +593,16 @@ namespace spot
// to ensure that [*0] is not accepted. // to ensure that [*0] is not accepted.
v->insert(v->begin(), constant::true_instance()); v->insert(v->begin(), constant::true_instance());
} }
// The hash key.
pair p(op, v);
const multop* res; const multop* res;
// Insert the key p with the dummy value 0 just // Insert the key with the dummy nullptr just
// to check if p already exists. // to check if p already exists.
std::pair<map::iterator, bool> ires = auto ires = instances.insert(std::make_pair(key(op, v), nullptr));
instances.insert(map::value_type(p, 0));
if (!ires.second) if (!ires.second)
{ {
// The instance did already exists. Free v. // The instance did already exists. Free v.
for (vec::iterator vi = v->begin(); vi != v->end(); ++vi) for (auto f: *v)
(*vi)->destroy(); f->destroy();
delete v; delete v;
res = ires.first->second; res = ires.first->second;
} }
@ -636,13 +633,11 @@ namespace spot
std::ostream& std::ostream&
multop::dump_instances(std::ostream& os) multop::dump_instances(std::ostream& os)
{ {
for (map::iterator i = instances.begin(); i != instances.end(); ++i) for (const auto& i: instances)
{ os << i.second << " = "
os << i->second << " = " << i.second->ref_count_() << " * "
<< i->second->ref_count_() << " * " << i.second->dump()
<< i->second->dump() << std::endl;
<< std::endl;
}
return os; return os;
} }

View file

@ -181,19 +181,19 @@ namespace spot
static std::ostream& dump_instances(std::ostream& os); static std::ostream& dump_instances(std::ostream& os);
protected: protected:
typedef std::pair<type, vec*> pair; typedef std::pair<type, vec*> key;
/// Comparison functor used internally by ltl::multop. /// Comparison functor used internally by ltl::multop.
struct paircmp struct paircmp
{ {
bool bool
operator()(const pair& p1, const pair& p2) const operator()(const key& p1, const key& p2) const
{ {
if (p1.first != p2.first) if (p1.first != p2.first)
return p1.first < p2.first; return p1.first < p2.first;
return *p1.second < *p2.second; return *p1.second < *p2.second;
} }
}; };
typedef std::map<pair, const multop*, paircmp> map; typedef std::map<key, const multop*, paircmp> map;
static map instances; static map instances;
multop(type op, vec* v); multop(type op, vec* v);

View file

@ -24,6 +24,7 @@
#include "unop.hh" #include "unop.hh"
#include "visitor.hh" #include "visitor.hh"
#include <cassert> #include <cassert>
#include <cstddef>
#include <iostream> #include <iostream>
#include "constant.hh" #include "constant.hh"
#include "atomic_prop.hh" #include "atomic_prop.hh"
@ -146,10 +147,9 @@ namespace spot
unop::~unop() unop::~unop()
{ {
// Get this instance out of the instance map. // Get this instance out of the instance map.
pair p(op(), child()); size_t c = instances.erase(key(op(), child()));
map::iterator i = instances.find(p); assert(c == 1);
assert (i != instances.end()); (void) c; // For the NDEBUG case.
instances.erase(i);
// Dereference child. // Dereference child.
child()->destroy(); child()->destroy();
@ -302,10 +302,7 @@ namespace spot
} }
const unop* res; const unop* res;
pair p(op, child); auto ires = instances.insert(std::make_pair(key(op, child), nullptr));
std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
if (!ires.second) if (!ires.second)
{ {
// This instance already exists. // This instance already exists.
@ -329,13 +326,11 @@ namespace spot
std::ostream& std::ostream&
unop::dump_instances(std::ostream& os) unop::dump_instances(std::ostream& os)
{ {
for (map::iterator i = instances.begin(); i != instances.end(); ++i) for (const auto& i: instances)
{ os << i.second << " = "
os << i->second << " = " << i.second->ref_count_() << " * "
<< i->second->ref_count_() << " * " << i.second->dump()
<< i->second->dump() << std::endl;
<< std::endl;
}
return os; return os;
} }

View file

@ -115,8 +115,8 @@ namespace spot
static std::ostream& dump_instances(std::ostream& os); static std::ostream& dump_instances(std::ostream& os);
protected: protected:
typedef std::pair<type, const formula*> pair; typedef std::pair<type, const formula*> key;
typedef std::map<pair, const unop*> map; typedef std::map<key, const unop*> map;
static map instances; static map instances;
unop(type op, const formula* child); unop(type op, const formula* child);