Commit b37dc0bc authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz
Browse files

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.
parent c64503fb
......@@ -23,6 +23,7 @@
#include "config.h"
#include "atomic_prop.hh"
#include "visitor.hh"
#include <cstddef>
#include <cassert>
#include <ostream>
......@@ -67,10 +68,9 @@ namespace spot
atomic_prop::~atomic_prop()
{
// Get this instance out of the instance map.
pair p(name(), &env());
map::iterator i = instances.find(p);
assert (i != instances.end());
instances.erase(i);
size_t c = instances.erase(key(name(), &env()));
assert(c == 1);
(void) c; // For the NDEBUG case.
}
std::string
......@@ -90,10 +90,8 @@ namespace spot
const atomic_prop*
atomic_prop::instance(const std::string& name, environment& env)
{
pair p(name, &env);
const atomic_prop* ap;
std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
auto ires = instances.insert(std::make_pair(key(name, &env), nullptr));
if (!ires.second)
ap = ires.first->second;
else
......@@ -111,13 +109,10 @@ namespace spot
std::ostream&
atomic_prop::dump_instances(std::ostream& os)
{
for (map::iterator i = instances.begin(); i != instances.end(); ++i)
{
os << i->second << " = " << i->second->ref_count_()
<< " * atomic_prop(" << i->first.first << ", "
<< i->first.second->name() << ")" << std::endl;
}
for (const auto& i: instances)
os << i.second << " = " << i.second->ref_count_()
<< " * atomic_prop(" << i.first.first << ", "
<< i.first.second->name() << ")" << std::endl;
return os;
}
......
......@@ -72,8 +72,8 @@ namespace spot
atomic_prop(const std::string& name, environment& env);
virtual ~atomic_prop();
typedef std::pair<std::string, environment*> pair;
typedef std::map<pair, const atomic_prop*> map;
typedef std::pair<std::string, environment*> key;
typedef std::map<key, const atomic_prop*> map;
static map instances;
private:
......
......@@ -19,6 +19,7 @@
#include "config.h"
#include <iostream>
#include <cstddef>
#include "automatop.hh"
#include "nfa.hh"
#include "visitor.hh"
......@@ -60,10 +61,9 @@ namespace spot
automatop::~automatop()
{
// Get this instance out of the instance map.
triplet p(std::make_pair(get_nfa(), negated_), children_);
map::iterator i = instances.find(p);
assert (i != instances.end());
instances.erase(i);
size_t c = instances.erase(key(get_nfa(), negated_, children_));
assert(c == 1);
(void) c; // For the NDEBUG case.
// Dereference children.
unsigned s = size();
......@@ -97,11 +97,10 @@ namespace spot
const automatop*
automatop::instance(const nfa::ptr nfa, vec* v, bool negated)
{
assert(nfa != 0);
triplet p(std::make_pair(nfa, negated), v);
assert(nfa);
const automatop* res;
std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
auto ires = instances.insert(std::make_pair(key(nfa, negated, v),
nullptr));
if (!ires.second)
{
// The instance already exists.
......@@ -127,13 +126,11 @@ namespace spot
std::ostream&
automatop::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;
}
for (const auto& i: instances)
os << i.second << " = "
<< i.second->ref_count_() << " * "
<< i.second->dump()
<< std::endl;
return os;
}
}
......
......@@ -24,6 +24,7 @@
# include "refformula.hh"
# include <vector>
# include <tuple>
# include <iosfwd>
# include <map>
# include "nfa.hh"
......@@ -89,21 +90,21 @@ namespace spot
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.
struct tripletcmp
{
bool
operator()(const triplet& p1, const triplet& p2) const
operator()(const key& p1, const key& p2) const
{
if (p1.first.first != p2.first.first)
return p1.first.first < p2.first.first;
if (p1.first.second != p2.first.second)
return p1.first.second < p2.first.second;
return *p1.second < *p2.second;
if (std::get<0>(p1) != std::get<0>(p2))
return std::get<0>(p1) < std::get<0>(p2);
if (std::get<1>(p1) != std::get<1>(p2))
return std::get<1>(p1) < std::get<1>(p2);
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;
automatop(const nfa::ptr, vec* v, bool negated);
......
......@@ -22,6 +22,7 @@
#include "config.h"
#include <cassert>
#include <cstddef>
#include <utility>
#include "binop.hh"
#include "unop.hh"
......@@ -250,11 +251,9 @@ namespace spot
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);
size_t c = instances.erase(key(op(), first(), second()));
assert(c == 1);
(void) c; // For the NDEBUG case.
// Dereference children.
first()->destroy();
......@@ -506,12 +505,9 @@ namespace spot
break;
}
pairf pf(first, second);
pair p(op, pf);
const binop* res;
std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
auto ires = instances.insert(std::make_pair(key(op, first, second),
nullptr));
if (!ires.second)
{
// This instance already exists.
......@@ -536,15 +532,12 @@ namespace spot
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;
}
for (const auto& i: instances)
os << i.second << " = "
<< i.second->ref_count_() << " * "
<< i.second->dump()
<< std::endl;
return os;
}
......
......@@ -31,6 +31,7 @@
#include "refformula.hh"
#include <map>
#include <iosfwd>
#include <tuple>
namespace spot
{
......@@ -142,9 +143,8 @@ namespace spot
static std::ostream& dump_instances(std::ostream& os);
protected:
typedef std::pair<const formula*, const formula*> pairf;
typedef std::pair<type, pairf> pair;
typedef std::map<pair, const binop*> map;
typedef std::tuple<type, const formula*, const formula*> key;
typedef std::map<key, const binop*> map;
static map instances;
binop(type op, const formula* first, const formula* second);
......
......@@ -66,15 +66,14 @@ namespace spot
bunop::~bunop()
{
// one_star_ should never get delete. Otherwise, that means
// is has been destroyed too much, or not cloned enough.
// one_star_ should never get deleted. Otherwise, that means it
// has been destroyed too much, or not cloned enough.
assert(this != one_star_);
// Get this instance out of the instance map.
pair p(pairo(op(), child()), pairu(min_, max_));
map::iterator i = instances.find(p);
assert (i != instances.end());
instances.erase(i);
size_t c = instances.erase(key(op(), child(), min_, max_));
assert(c == 1);
(void) c; // For the NDEBUG case.
// Dereference child.
child()->destroy();
......@@ -239,11 +238,9 @@ namespace spot
}
}
pair p(pairo(op, child), pairu(min, max));
const formula* res;
std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
auto ires = instances.insert(std::make_pair(key(op, child, min, max),
nullptr));
if (!ires.second)
{
// This instance already exists.
......@@ -303,13 +300,11 @@ namespace spot
std::ostream&
bunop::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;
}
for (const auto& i: instances)
os << i.second << " = "
<< i.second->ref_count_() << " * "
<< i.second->dump()
<< std::endl;
return os;
}
......
......@@ -25,6 +25,7 @@
#include "refformula.hh"
#include <map>
#include <iosfwd>
#include <tuple>
#include "constant.hh"
namespace spot
......@@ -140,10 +141,8 @@ namespace spot
}
protected:
typedef std::pair<unsigned, unsigned> pairu;
typedef std::pair<type, const formula*> pairo;
typedef std::pair<pairo, pairu> pair;
typedef std::map<pair, const bunop*> map;
typedef std::tuple<type, const formula*, unsigned, unsigned> key;
typedef std::map<key, const bunop*> map;
static map instances;
bunop(type op, const formula* child, unsigned min, unsigned max);
......
......@@ -21,6 +21,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "config.h"
#include <cstddef>
#include <cassert>
#include <utility>
#include <algorithm>
......@@ -94,10 +95,9 @@ namespace spot
multop::~multop()
{
// Get this instance out of the instance map.
pair p(op(), children_);
map::iterator i = instances.find(p);
assert (i != instances.end());
instances.erase(i);
size_t c = instances.erase(key(op(), children_));
assert(c == 1);
(void) c; // For the NDEBUG case.
// Dereference children.
unsigned s = size();
......@@ -593,19 +593,16 @@ namespace spot
// to ensure that [*0] is not accepted.
v->insert(v->begin(), constant::true_instance());
}
// The hash key.
pair p(op, v);
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.
std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
auto ires = instances.insert(std::make_pair(key(op, v), nullptr));
if (!ires.second)
{
// The instance did already exists. Free v.
for (vec::iterator vi = v->begin(); vi != v->end(); ++vi)
(*vi)->destroy();
for (auto f: *v)
f->destroy();
delete v;
res = ires.first->second;
}
......@@ -636,13 +633,11 @@ namespace spot
std::ostream&
multop::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;
}
for (const auto& i: instances)
os << i.second << " = "
<< i.second->ref_count_() << " * "
<< i.second->dump()
<< std::endl;
return os;
}
......
......@@ -181,19 +181,19 @@ namespace spot
static std::ostream& dump_instances(std::ostream& os);
protected:
typedef std::pair<type, vec*> pair;
typedef std::pair<type, vec*> key;
/// Comparison functor used internally by ltl::multop.
struct paircmp
{
bool
operator()(const pair& p1, const pair& p2) const
operator()(const key& p1, const key& p2) const
{
if (p1.first != p2.first)
return p1.first < p2.first;
return *p1.second < *p2.second;
}
};
typedef std::map<pair, const multop*, paircmp> map;
typedef std::map<key, const multop*, paircmp> map;
static map instances;
multop(type op, vec* v);
......
......@@ -24,6 +24,7 @@
#include "unop.hh"
#include "visitor.hh"
#include <cassert>
#include <cstddef>
#include <iostream>
#include "constant.hh"
#include "atomic_prop.hh"
......@@ -146,10 +147,9 @@ namespace spot
unop::~unop()
{
// Get this instance out of the instance map.
pair p(op(), child());
map::iterator i = instances.find(p);
assert (i != instances.end());
instances.erase(i);
size_t c = instances.erase(key(op(), child()));
assert(c == 1);
(void) c; // For the NDEBUG case.
// Dereference child.
child()->destroy();
......@@ -302,10 +302,7 @@ namespace spot
}
const unop* res;
pair p(op, child);
std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
auto ires = instances.insert(std::make_pair(key(op, child), nullptr));
if (!ires.second)
{
// This instance already exists.
......@@ -329,13 +326,11 @@ namespace spot
std::ostream&
unop::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;
}
for (const auto& i: instances)
os << i.second << " = "
<< i.second->ref_count_() << " * "
<< i.second->dump()
<< std::endl;
return os;
}
......
......@@ -115,8 +115,8 @@ namespace spot
static std::ostream& dump_instances(std::ostream& os);
protected:
typedef std::pair<type, const formula*> pair;
typedef std::map<pair, const unop*> map;
typedef std::pair<type, const formula*> key;
typedef std::map<key, const unop*> map;
static map instances;
unop(type op, const formula* child);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment