ltlast: use the return of insert() to avoid a double lookup

* src/ltlast/atomic_prop.cc, src/ltlast/automatop.cc,
src/ltlast/binop.cc, src/ltlast/bunop.cc,
src/ltlast/multop.cc, src/ltlast/unop.cc: Do not look for a key
and then insert the (key,value) on failure.  Simply insert
(key,0), and replace 0 by value on failure.  This replaces two map
lookups by one.
This commit is contained in:
Alexandre Duret-Lutz 2012-12-19 17:37:13 +01:00
parent 2776de87da
commit 13c41ee773
6 changed files with 41 additions and 30 deletions

View file

@ -102,13 +102,13 @@ namespace spot
atomic_prop::instance(const std::string& name, environment& env) atomic_prop::instance(const std::string& name, environment& env)
{ {
pair p(name, &env); pair p(name, &env);
// FIXME: Use lower_bound, or a hash_map.
map::iterator i = instances.find(p);
const atomic_prop* ap; const atomic_prop* ap;
if (i != instances.end()) std::pair<map::iterator, bool> ires =
ap = i->second; instances.insert(map::value_type(p, 0));
if (!ires.second)
ap = ires.first->second;
else else
ap = instances[p] = new atomic_prop(name, env); ap = ires.first->second = new atomic_prop(name, env);
ap->clone(); ap->clone();
return ap; return ap;
} }

View file

@ -95,19 +95,20 @@ namespace spot
{ {
assert(nfa != 0); assert(nfa != 0);
triplet p(std::make_pair(nfa, negated), v); triplet p(std::make_pair(nfa, negated), v);
map::iterator i = instances.find(p);
const automatop* res; const automatop* res;
if (i != instances.end()) std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
if (!ires.second)
{ {
// The instance already exists. // The instance already exists.
for (vec::iterator vi = v->begin(); vi != v->end(); ++vi) for (vec::iterator vi = v->begin(); vi != v->end(); ++vi)
(*vi)->destroy(); (*vi)->destroy();
delete v; delete v;
res = i->second; res = ires.first->second;
} }
else else
{ {
res = instances[p] = new automatop(nfa, v, negated); res = ires.first->second = new automatop(nfa, v, negated);
} }
res->clone(); res->clone();
return res; return res;

View file

@ -525,19 +525,20 @@ namespace spot
pairf pf(first, second); pairf pf(first, second);
pair p(op, pf); pair p(op, pf);
// FIXME: Use lower_bound or hash_map.
map::iterator i = instances.find(p);
const binop* res; const binop* res;
if (i != instances.end()) std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
if (!ires.second)
{ {
// This instance already exists. // This instance already exists.
first->destroy(); first->destroy();
second->destroy(); second->destroy();
res = i->second; res = ires.first->second;
} }
else else
{ {
res = instances[p] = new binop(op, first, second); res = ires.first->second = new binop(op, first, second);
} }
res->clone(); res->clone();
return res; return res;

View file

@ -262,14 +262,20 @@ namespace spot
} }
pair p(pairo(op, child), pairu(min, max)); pair p(pairo(op, child), pairu(min, max));
map::iterator i = instances.find(p);
if (i != instances.end()) const formula* res;
std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
if (!ires.second)
{ {
// This instance already exists. // This instance already exists.
child->destroy(); child->destroy();
return i->second->clone(); res = ires.first->second;
}
else
{
res = ires.first->second = new bunop(op, child, min, max);
} }
const bunop* res = instances[p] = new bunop(op, child, min, max);
res->clone(); res->clone();
return res; return res;
} }

View file

@ -588,21 +588,22 @@ namespace spot
pair p(op, v); pair p(op, v);
const multop* res; const multop* res;
// FIXME: Use lower_bound or hash_map. // Insert the key p with the dummy value 0 just
map::iterator i = instances.find(p); // to check if p already exists.
if (i != instances.end()) std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
if (!ires.second)
{ {
// The instance already exists. // The instance did already exists. Free v.
for (vec::iterator vi = v->begin(); vi != v->end(); ++vi) for (vec::iterator vi = v->begin(); vi != v->end(); ++vi)
(*vi)->destroy(); (*vi)->destroy();
delete v; delete v;
res = i->second; res = ires.first->second;
} }
else else
{ {
// This is the first instance of this formula. // The instance did not already exist.
// Record the instance in the map, res = ires.first->second = new multop(op, v);
res = instances[p] = new multop(op, v);
} }
res->clone(); res->clone();
return res; return res;

View file

@ -314,16 +314,18 @@ namespace spot
const unop* res; const unop* res;
pair p(op, child); pair p(op, child);
map::iterator i = instances.find(p);
if (i != instances.end()) std::pair<map::iterator, bool> ires =
instances.insert(map::value_type(p, 0));
if (!ires.second)
{ {
// This instance already exists. // This instance already exists.
child->destroy(); child->destroy();
res = i->second; res = ires.first->second;
} }
else else
{ {
res = instances[p] = new unop(op, child); res = ires.first->second = new unop(op, child);
} }
res->clone(); res->clone();
return res; return res;