#include <vector>
#include <string>
#include <sstream>
+#include <ext/hash_map>
#include "context/cdlist.h"
#include "context/cdset.h"
#include "util/exception.h"
#include "util/options.h"
#include "util/output.h"
+#include "util/hash.h"
#include "theory/builtin/theory_builtin.h"
#include "theory/booleans/theory_bool.h"
#include "theory/uf/theory_uf.h"
/**
* Expand definitions in n.
*/
- static Node expandDefinitions(SmtEngine& smt, TNode n)
+ static Node expandDefinitions(SmtEngine& smt, TNode n,
+ hash_map<TNode, Node, TNodeHashFunction>& cache)
throw(NoSuchFunctionException, AssertionException);
};/* class SmtEnginePrivate */
d_definedFunctions->insert(funcNode, def);
}
-Node SmtEnginePrivate::expandDefinitions(SmtEngine& smt, TNode n)
+Node SmtEnginePrivate::expandDefinitions(SmtEngine& smt, TNode n,
+ hash_map<TNode, Node, TNodeHashFunction>& cache)
throw(NoSuchFunctionException, AssertionException) {
+
+ if(n.getKind() != kind::APPLY && n.getNumChildren() == 0) {
+ // don't bother putting in the cache
+ return n;
+ }
+
+ // maybe it's in the cache
+ hash_map<TNode, Node, TNodeHashFunction>::iterator cacheHit = cache.find(n);
+ if(cacheHit != cache.end()) {
+ return (*cacheHit).second;
+ }
+
+ // otherwise expand it
if(n.getKind() == kind::APPLY) {
TNode func = n.getOperator();
SmtEngine::DefinedFunctionMap::const_iterator i =
n.begin(), n.end());
Debug("expand") << "made : " << instance << endl;
- Node expanded = expandDefinitions(smt, instance);
+ Node expanded = expandDefinitions(smt, instance, cache);
+ cache[n] = expanded;
return expanded;
- } else if(n.getNumChildren() == 0) {
- return n;
} else {
Debug("expand") << "cons : " << n << endl;
NodeBuilder<> nb(n.getKind());
iend = n.end();
i != iend;
++i) {
- Node expanded = expandDefinitions(smt, *i);
+ Node expanded = expandDefinitions(smt, *i, cache);
Debug("expand") << "exchld: " << expanded << endl;
nb << expanded;
}
Node node = nb;
+ cache[n] = node;
return node;
}
}
Node n;
if(!Options::current()->lazyDefinitionExpansion) {
Debug("expand") << "have: " << n << endl;
- n = expandDefinitions(smt, in);
+ hash_map<TNode, Node, TNodeHashFunction> cache;
+ n = expandDefinitions(smt, in, cache);
Debug("expand") << "made: " << n << endl;
} else {
n = in;