GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/sygus/example_eval_cache.cpp Lines: 47 48 97.9 %
Date: 2021-09-30 Branches: 59 148 39.9 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * This class caches the evaluation of nodes on a fixed list of examples.
14
 */
15
#include "theory/quantifiers/sygus/example_eval_cache.h"
16
17
#include "theory/quantifiers/sygus/example_min_eval.h"
18
19
using namespace cvc5;
20
using namespace cvc5::kind;
21
22
namespace cvc5 {
23
namespace theory {
24
namespace quantifiers {
25
26
123
ExampleEvalCache::ExampleEvalCache(TermDbSygus* tds,
27
123
                                   Node e)
28
123
    : d_tds(tds), d_stn(e.getType())
29
{
30
123
  d_indexSearchVals = !d_tds->isVariableAgnosticEnumerator(e);
31
123
}
32
33
123
ExampleEvalCache::~ExampleEvalCache() {}
34
35
565
void ExampleEvalCache::addExample(const std::vector<Node>& ex)
36
{
37
565
  d_examples.push_back(ex);
38
565
}
39
40
14448
Node ExampleEvalCache::addSearchVal(TypeNode tn, Node bv)
41
{
42
14448
  if (!d_indexSearchVals)
43
  {
44
    // not indexing search values
45
    return Node::null();
46
  }
47
28896
  std::vector<Node> vals;
48
14448
  evaluateVec(bv, vals, true);
49
14448
  Trace("sygus-pbe-debug") << "Add to trie..." << std::endl;
50
28896
  Node ret = d_trie[tn].addOrGetTerm(bv, vals);
51
14448
  Trace("sygus-pbe-debug") << "...got " << ret << std::endl;
52
  // Only save the cache data if necessary: if the enumerated term
53
  // is redundant, its cached data will not be used later and thus should
54
  // be discarded. This applies also to the case where the evaluation
55
  // was cached prior to this call.
56
14448
  if (ret != bv)
57
  {
58
8494
    clearEvaluationCache(bv);
59
  }
60
14448
  Assert(ret.getType().isComparableTo(bv.getType()));
61
14448
  return ret;
62
}
63
64
17635
void ExampleEvalCache::evaluateVec(Node bv,
65
                                   std::vector<Node>& exOut,
66
                                   bool doCache)
67
{
68
  // is it in the cache?
69
17635
  std::map<Node, std::vector<Node>>::iterator it = d_exOutCache.find(bv);
70
17635
  if (it != d_exOutCache.end())
71
  {
72
1831
    exOut.insert(exOut.end(), it->second.begin(), it->second.end());
73
1831
    return;
74
  }
75
  // get the evaluation
76
15804
  evaluateVecInternal(bv, exOut);
77
  // store in cache if necessary
78
15804
  if (doCache)
79
  {
80
14371
    std::vector<Node>& eocv = d_exOutCache[bv];
81
14371
    eocv.insert(eocv.end(), exOut.begin(), exOut.end());
82
  }
83
}
84
85
15804
void ExampleEvalCache::evaluateVecInternal(Node bv,
86
                                           std::vector<Node>& exOut) const
87
{
88
  // use ExampleMinEval
89
15804
  SygusTypeInfo& ti = d_tds->getTypeInfo(d_stn);
90
15804
  const std::vector<Node>& varlist = ti.getVarList();
91
31608
  EmeEvalTds emetds(d_tds, d_stn);
92
31608
  ExampleMinEval eme(bv, varlist, &emetds);
93
154138
  for (size_t j = 0, esize = d_examples.size(); j < esize; j++)
94
  {
95
276668
    Node res = eme.evaluate(d_examples[j]);
96
138334
    exOut.push_back(res);
97
  }
98
15804
}
99
100
390
Node ExampleEvalCache::evaluate(Node bn, unsigned i) const
101
{
102
390
  Assert(i < d_examples.size());
103
390
  return d_tds->evaluateBuiltin(d_stn, bn, d_examples[i]);
104
}
105
106
8494
void ExampleEvalCache::clearEvaluationCache(Node bv)
107
{
108
8494
  Assert(d_exOutCache.find(bv) != d_exOutCache.end());
109
8494
  d_exOutCache.erase(bv);
110
8494
}
111
112
3389
void ExampleEvalCache::clearEvaluationAll() { d_exOutCache.clear(); }
113
114
}  // namespace quantifiers
115
}  // namespace theory
116
22755
}  // namespace cvc5