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-29 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
122
ExampleEvalCache::ExampleEvalCache(TermDbSygus* tds,
27
122
                                   Node e)
28
122
    : d_tds(tds), d_stn(e.getType())
29
{
30
122
  d_indexSearchVals = !d_tds->isVariableAgnosticEnumerator(e);
31
122
}
32
33
122
ExampleEvalCache::~ExampleEvalCache() {}
34
35
564
void ExampleEvalCache::addExample(const std::vector<Node>& ex)
36
{
37
564
  d_examples.push_back(ex);
38
564
}
39
40
14446
Node ExampleEvalCache::addSearchVal(TypeNode tn, Node bv)
41
{
42
14446
  if (!d_indexSearchVals)
43
  {
44
    // not indexing search values
45
    return Node::null();
46
  }
47
28892
  std::vector<Node> vals;
48
14446
  evaluateVec(bv, vals, true);
49
14446
  Trace("sygus-pbe-debug") << "Add to trie..." << std::endl;
50
28892
  Node ret = d_trie[tn].addOrGetTerm(bv, vals);
51
14446
  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
14446
  if (ret != bv)
57
  {
58
8494
    clearEvaluationCache(bv);
59
  }
60
14446
  Assert(ret.getType().isComparableTo(bv.getType()));
61
14446
  return ret;
62
}
63
64
17633
void ExampleEvalCache::evaluateVec(Node bv,
65
                                   std::vector<Node>& exOut,
66
                                   bool doCache)
67
{
68
  // is it in the cache?
69
17633
  std::map<Node, std::vector<Node>>::iterator it = d_exOutCache.find(bv);
70
17633
  if (it != d_exOutCache.end())
71
  {
72
1830
    exOut.insert(exOut.end(), it->second.begin(), it->second.end());
73
1830
    return;
74
  }
75
  // get the evaluation
76
15803
  evaluateVecInternal(bv, exOut);
77
  // store in cache if necessary
78
15803
  if (doCache)
79
  {
80
14370
    std::vector<Node>& eocv = d_exOutCache[bv];
81
14370
    eocv.insert(eocv.end(), exOut.begin(), exOut.end());
82
  }
83
}
84
85
15803
void ExampleEvalCache::evaluateVecInternal(Node bv,
86
                                           std::vector<Node>& exOut) const
87
{
88
  // use ExampleMinEval
89
15803
  SygusTypeInfo& ti = d_tds->getTypeInfo(d_stn);
90
15803
  const std::vector<Node>& varlist = ti.getVarList();
91
31606
  EmeEvalTds emetds(d_tds, d_stn);
92
31606
  ExampleMinEval eme(bv, varlist, &emetds);
93
154136
  for (size_t j = 0, esize = d_examples.size(); j < esize; j++)
94
  {
95
276666
    Node res = eme.evaluate(d_examples[j]);
96
138333
    exOut.push_back(res);
97
  }
98
15803
}
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
3388
void ExampleEvalCache::clearEvaluationAll() { d_exOutCache.clear(); }
113
114
}  // namespace quantifiers
115
}  // namespace theory
116
22746
}  // namespace cvc5