1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Clark Barrett, 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 |
|
* Implementation of type set class. |
14 |
|
*/ |
15 |
|
#include "theory/type_set.h" |
16 |
|
|
17 |
|
using namespace std; |
18 |
|
using namespace cvc5::kind; |
19 |
|
|
20 |
|
namespace cvc5 { |
21 |
|
namespace theory { |
22 |
|
|
23 |
110760 |
TypeSet::~TypeSet() |
24 |
|
{ |
25 |
55380 |
iterator it; |
26 |
140523 |
for (it = d_typeSet.begin(); it != d_typeSet.end(); ++it) |
27 |
|
{ |
28 |
85143 |
delete (*it).second; |
29 |
|
} |
30 |
55380 |
TypeToTypeEnumMap::iterator it2; |
31 |
59842 |
for (it2 = d_teMap.begin(); it2 != d_teMap.end(); ++it2) |
32 |
|
{ |
33 |
4462 |
delete (*it2).second; |
34 |
|
} |
35 |
55380 |
} |
36 |
|
|
37 |
945 |
void TypeSet::setTypeEnumeratorProperties(TypeEnumeratorProperties* tep) |
38 |
|
{ |
39 |
945 |
d_tep = tep; |
40 |
945 |
} |
41 |
|
|
42 |
850289 |
void TypeSet::add(TypeNode t, TNode n) |
43 |
|
{ |
44 |
850289 |
iterator it = d_typeSet.find(t); |
45 |
|
std::set<Node>* s; |
46 |
850289 |
if (it == d_typeSet.end()) |
47 |
|
{ |
48 |
82432 |
s = new std::set<Node>; |
49 |
82432 |
d_typeSet[t] = s; |
50 |
|
} |
51 |
|
else |
52 |
|
{ |
53 |
767857 |
s = (*it).second; |
54 |
|
} |
55 |
850289 |
s->insert(n); |
56 |
850289 |
} |
57 |
|
|
58 |
384190 |
std::set<Node>* TypeSet::getSet(TypeNode t) const |
59 |
|
{ |
60 |
384190 |
const_iterator it = d_typeSet.find(t); |
61 |
384190 |
if (it == d_typeSet.end()) |
62 |
|
{ |
63 |
175137 |
return NULL; |
64 |
|
} |
65 |
209053 |
return (*it).second; |
66 |
|
} |
67 |
|
|
68 |
14805 |
Node TypeSet::nextTypeEnum(TypeNode t, bool useBaseType) |
69 |
|
{ |
70 |
|
TypeEnumerator* te; |
71 |
14805 |
TypeToTypeEnumMap::iterator it = d_teMap.find(t); |
72 |
14805 |
if (it == d_teMap.end()) |
73 |
|
{ |
74 |
4462 |
te = new TypeEnumerator(t, d_tep); |
75 |
4462 |
d_teMap[t] = te; |
76 |
|
} |
77 |
|
else |
78 |
|
{ |
79 |
10343 |
te = (*it).second; |
80 |
|
} |
81 |
14805 |
if (te->isFinished()) |
82 |
|
{ |
83 |
|
return Node(); |
84 |
|
} |
85 |
|
|
86 |
14805 |
if (useBaseType) |
87 |
|
{ |
88 |
14318 |
t = t.getBaseType(); |
89 |
|
} |
90 |
14805 |
iterator itSet = d_typeSet.find(t); |
91 |
|
std::set<Node>* s; |
92 |
14805 |
if (itSet == d_typeSet.end()) |
93 |
|
{ |
94 |
2711 |
s = new std::set<Node>; |
95 |
2711 |
d_typeSet[t] = s; |
96 |
|
} |
97 |
|
else |
98 |
|
{ |
99 |
12094 |
s = (*itSet).second; |
100 |
|
} |
101 |
29610 |
Node n = **te; |
102 |
26789 |
while (s->find(n) != s->end()) |
103 |
|
{ |
104 |
5992 |
++(*te); |
105 |
5992 |
if (te->isFinished()) |
106 |
|
{ |
107 |
|
return Node(); |
108 |
|
} |
109 |
5992 |
n = **te; |
110 |
|
} |
111 |
14805 |
s->insert(n); |
112 |
|
// add all subterms of n to this set as well |
113 |
|
// this is necessary for parametric types whose values are constructed from |
114 |
|
// other types to ensure that we do not enumerate subterms of other |
115 |
|
// previously enumerated values |
116 |
29610 |
std::unordered_set<TNode> visited; |
117 |
14805 |
addSubTerms(n, visited); |
118 |
14805 |
++(*te); |
119 |
14805 |
return n; |
120 |
|
} |
121 |
|
|
122 |
46004 |
void TypeSet::addSubTerms(TNode n, |
123 |
|
std::unordered_set<TNode>& visited, |
124 |
|
bool topLevel) |
125 |
|
{ |
126 |
46004 |
if (visited.find(n) == visited.end()) |
127 |
|
{ |
128 |
36567 |
visited.insert(n); |
129 |
36567 |
if (!topLevel) |
130 |
|
{ |
131 |
21762 |
add(n.getType(), n); |
132 |
|
} |
133 |
67766 |
for (unsigned i = 0; i < n.getNumChildren(); i++) |
134 |
|
{ |
135 |
31199 |
addSubTerms(n[i], visited, false); |
136 |
|
} |
137 |
|
} |
138 |
46004 |
} |
139 |
|
|
140 |
|
} // namespace theory |
141 |
29505 |
} // namespace cvc5 |