GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/nl/icp/contraction_origins.cpp Lines: 34 43 79.1 %
Date: 2021-09-15 Branches: 45 134 33.6 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Gereon Kremer
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
 * Implements a way to track the origins of ICP-style contractions.
14
 */
15
16
#include "theory/arith/nl/icp/contraction_origins.h"
17
18
namespace cvc5 {
19
namespace theory {
20
namespace arith {
21
namespace nl {
22
namespace icp {
23
24
120
void ContractionOriginManager::getOrigins(ContractionOrigin const* const origin,
25
                                          std::set<Node>& res) const
26
{
27
120
  if (!origin->candidate.isNull())
28
  {
29
120
    res.insert(origin->candidate);
30
  }
31
194
  for (const auto& co : origin->origins)
32
  {
33
74
    getOrigins(co, res);
34
  }
35
120
}
36
37
const std::map<Node, ContractionOriginManager::ContractionOrigin*>&
38
ContractionOriginManager::currentOrigins() const
39
{
40
  return d_currentOrigins;
41
}
42
43
158
void ContractionOriginManager::add(const Node& targetVariable,
44
                                   const Node& candidate,
45
                                   const std::vector<Node>& originVariables,
46
                                   bool addTarget)
47
{
48
158
  Trace("nl-icp") << "Adding contraction for " << targetVariable << std::endl;
49
316
  std::vector<ContractionOrigin*> origins;
50
158
  if (addTarget)
51
  {
52
154
    auto it = d_currentOrigins.find(targetVariable);
53
154
    if (it != d_currentOrigins.end())
54
    {
55
62
      origins.emplace_back(it->second);
56
    }
57
  }
58
164
  for (const auto& v : originVariables)
59
  {
60
6
    auto it = d_currentOrigins.find(v);
61
6
    if (it != d_currentOrigins.end())
62
    {
63
6
      origins.emplace_back(it->second);
64
    }
65
  }
66
158
  d_allocations.emplace_back(
67
316
      new ContractionOrigin{candidate, std::move(origins)});
68
158
  d_currentOrigins[targetVariable] = d_allocations.back().get();
69
158
}
70
71
24
std::vector<Node> ContractionOriginManager::getOrigins(
72
    const Node& variable) const
73
{
74
24
  Trace("nl-icp") << "Obtaining origins for " << variable << std::endl;
75
48
  std::set<Node> origins;
76
24
  Assert(d_currentOrigins.find(variable) != d_currentOrigins.end())
77
      << "Using variable as origin that is unknown yet.";
78
24
  getOrigins(d_currentOrigins.at(variable), origins);
79
24
  Assert(!origins.empty()) << "There should be at least one origin";
80
48
  return std::vector<Node>(origins.begin(), origins.end());
81
}
82
83
22
bool ContractionOriginManager::isInOrigins(const Node& variable,
84
                                           const Node& c) const
85
{
86
44
  std::set<Node> origins;
87
22
  Assert(d_currentOrigins.find(variable) != d_currentOrigins.end())
88
      << "Using variable as origin that is unknown yet.";
89
22
  getOrigins(d_currentOrigins.at(variable), origins);
90
44
  return origins.find(c) != origins.end();
91
}
92
93
void print(std::ostream& os,
94
           const std::string& indent,
95
           const ContractionOriginManager::ContractionOrigin* co)
96
{
97
  os << indent << co->candidate << std::endl;
98
  for (const auto& o : co->origins)
99
  {
100
    print(os, indent + "\t", o);
101
  }
102
}
103
104
inline std::ostream& operator<<(std::ostream& os,
105
                                const ContractionOriginManager& com)
106
{
107
  os << "ContractionOrigins:" << std::endl;
108
  const auto& origins = com.currentOrigins();
109
  for (const auto& vars : origins)
110
  {
111
    os << vars.first << ":" << std::endl;
112
    print(os, "\t", vars.second);
113
  }
114
  return os;
115
}
116
117
}  // namespace icp
118
}  // namespace nl
119
}  // namespace arith
120
}  // namespace theory
121
29577
}  // namespace cvc5