1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Aina Niemetz, Clark Barrett, Morgan Deters |
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 |
|
* An enumerator for arrays. |
14 |
|
*/ |
15 |
|
#include "theory/arrays/type_enumerator.h" |
16 |
|
|
17 |
|
#include "expr/array_store_all.h" |
18 |
|
#include "expr/kind.h" |
19 |
|
#include "expr/type_node.h" |
20 |
|
#include "theory/rewriter.h" |
21 |
|
#include "theory/type_enumerator.h" |
22 |
|
|
23 |
|
namespace cvc5 { |
24 |
|
namespace theory { |
25 |
|
namespace arrays { |
26 |
|
|
27 |
107 |
ArrayEnumerator::ArrayEnumerator(TypeNode type, TypeEnumeratorProperties* tep) |
28 |
|
: TypeEnumeratorBase<ArrayEnumerator>(type), |
29 |
|
d_tep(tep), |
30 |
214 |
d_index(type.getArrayIndexType(), tep), |
31 |
|
d_constituentType(type.getArrayConstituentType()), |
32 |
107 |
d_nm(NodeManager::currentNM()), |
33 |
|
d_indexVec(), |
34 |
|
d_constituentVec(), |
35 |
|
d_finished(false), |
36 |
428 |
d_arrayConst() |
37 |
|
{ |
38 |
107 |
d_indexVec.push_back(*d_index); |
39 |
107 |
d_constituentVec.push_back(new TypeEnumerator(d_constituentType, d_tep)); |
40 |
107 |
d_arrayConst = |
41 |
214 |
d_nm->mkConst(ArrayStoreAll(type, (*(*d_constituentVec.back())))); |
42 |
107 |
Trace("array-type-enum") << "Array const : " << d_arrayConst << std::endl; |
43 |
107 |
} |
44 |
|
|
45 |
2042 |
ArrayEnumerator::ArrayEnumerator(const ArrayEnumerator& ae) |
46 |
|
: TypeEnumeratorBase<ArrayEnumerator>( |
47 |
4084 |
ae.d_nm->mkArrayType(ae.d_index.getType(), ae.d_constituentType)), |
48 |
2042 |
d_tep(ae.d_tep), |
49 |
|
d_index(ae.d_index), |
50 |
|
d_constituentType(ae.d_constituentType), |
51 |
2042 |
d_nm(ae.d_nm), |
52 |
|
d_indexVec(ae.d_indexVec), |
53 |
|
d_constituentVec(), // copied below |
54 |
2042 |
d_finished(ae.d_finished), |
55 |
12252 |
d_arrayConst(ae.d_arrayConst) |
56 |
|
{ |
57 |
2042 |
for (std::vector<TypeEnumerator*>::const_iterator |
58 |
2042 |
i = ae.d_constituentVec.begin(), |
59 |
2042 |
i_end = ae.d_constituentVec.end(); |
60 |
4084 |
i != i_end; |
61 |
|
++i) |
62 |
|
{ |
63 |
2042 |
d_constituentVec.push_back(new TypeEnumerator(**i)); |
64 |
|
} |
65 |
2042 |
} |
66 |
|
|
67 |
6447 |
ArrayEnumerator::~ArrayEnumerator() |
68 |
|
{ |
69 |
6481 |
while (!d_constituentVec.empty()) |
70 |
|
{ |
71 |
2166 |
delete d_constituentVec.back(); |
72 |
2166 |
d_constituentVec.pop_back(); |
73 |
|
} |
74 |
4298 |
} |
75 |
|
|
76 |
11116 |
Node ArrayEnumerator::operator*() |
77 |
|
{ |
78 |
11116 |
if (d_finished) |
79 |
|
{ |
80 |
|
throw NoMoreValuesException(getType()); |
81 |
|
} |
82 |
11116 |
Node n = d_arrayConst; |
83 |
22458 |
for (size_t i = 0, size = d_indexVec.size(); i < size; ++i) |
84 |
|
{ |
85 |
34026 |
n = d_nm->mkNode(kind::STORE, |
86 |
|
n, |
87 |
11342 |
d_indexVec[d_indexVec.size() - 1 - i], |
88 |
22684 |
*(*(d_constituentVec[i]))); |
89 |
|
} |
90 |
11116 |
Trace("array-type-enum") << "operator * prerewrite: " << n << std::endl; |
91 |
11116 |
n = Rewriter::rewrite(n); |
92 |
11116 |
Trace("array-type-enum") << "operator * returning: " << n << std::endl; |
93 |
11116 |
return n; |
94 |
|
} |
95 |
|
|
96 |
2165 |
ArrayEnumerator& ArrayEnumerator::operator++() |
97 |
|
{ |
98 |
4330 |
Trace("array-type-enum") << "operator++ called, **this = " << **this |
99 |
2165 |
<< std::endl; |
100 |
|
|
101 |
2165 |
if (d_finished) |
102 |
|
{ |
103 |
|
Trace("array-type-enum") << "operator++ finished!" << std::endl; |
104 |
|
return *this; |
105 |
|
} |
106 |
2227 |
while (!d_constituentVec.empty()) |
107 |
|
{ |
108 |
2175 |
++(*d_constituentVec.back()); |
109 |
2175 |
if (d_constituentVec.back()->isFinished()) |
110 |
|
{ |
111 |
31 |
delete d_constituentVec.back(); |
112 |
31 |
d_constituentVec.pop_back(); |
113 |
|
} |
114 |
|
else |
115 |
|
{ |
116 |
2144 |
break; |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
2165 |
if (d_constituentVec.empty()) |
121 |
|
{ |
122 |
21 |
++d_index; |
123 |
21 |
if (d_index.isFinished()) |
124 |
|
{ |
125 |
1 |
Trace("array-type-enum") << "operator++ finished!" << std::endl; |
126 |
1 |
d_finished = true; |
127 |
1 |
return *this; |
128 |
|
} |
129 |
20 |
d_indexVec.push_back(*d_index); |
130 |
20 |
d_constituentVec.push_back(new TypeEnumerator(d_constituentType, d_tep)); |
131 |
20 |
++(*d_constituentVec.back()); |
132 |
20 |
if (d_constituentVec.back()->isFinished()) |
133 |
|
{ |
134 |
|
Trace("array-type-enum") << "operator++ finished!" << std::endl; |
135 |
|
d_finished = true; |
136 |
|
return *this; |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
2220 |
while (d_constituentVec.size() < d_indexVec.size()) |
141 |
|
{ |
142 |
28 |
d_constituentVec.push_back(new TypeEnumerator(d_constituentType, d_tep)); |
143 |
|
} |
144 |
|
|
145 |
4328 |
Trace("array-type-enum") << "operator++ returning, **this = " << **this |
146 |
2164 |
<< std::endl; |
147 |
2164 |
return *this; |
148 |
|
} |
149 |
|
|
150 |
8952 |
bool ArrayEnumerator::isFinished() |
151 |
|
{ |
152 |
17904 |
Trace("array-type-enum") << "isFinished returning: " << d_finished |
153 |
8952 |
<< std::endl; |
154 |
8952 |
return d_finished; |
155 |
|
} |
156 |
|
|
157 |
|
} // namespace arrays |
158 |
|
} // namespace theory |
159 |
31137 |
} // namespace cvc5 |