-
Notifications
You must be signed in to change notification settings - Fork 0
/
termset.cpp
215 lines (173 loc) · 4.17 KB
/
termset.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//////// AB CLASSGEN Thu Mar 06 10:50:40 1997 ////////
// TermSet Implementation
//////////////////////////////////////////////////////
#include "SwitchMinDLL_Global.h"
#define DLLBLD_SWSYSTEM_TERMSET
#include "TermSet.HPP"
#include <assert.h>
namespace swmin {
typedef std::set<Term, std::less<Term> > set_type;
// default constructor
TermSet::TermSet()
{
_commonCtorJob();
//...
}
// called from all constructors
void TermSet::_commonCtorJob()
{
//...
}
// destructor
TermSet::~TermSet()
{
//...
}
TermSet::TermSet(const TermSet& y)
: set_type((set_type&) y)
{
}
TermSet& TermSet::operator=(const TermSet& y)
{
invariant();
if (this==&y) return *this;
set_type::operator=(y);
return *this;
}
void TermSet::invariant()
{
//...
}
bool TermSet::is_subset(const TermSet& y) const
{
TermSet::iterator i, e, j;
for(i=y.begin(), e=y.end(); i!=e; i++) {
if ( (j=find(*i))==end() ) return false; // term in y isn't here
if ( !(*j).is_subset(*i) ) return false; // y term isn't a subset of this term
}
return true;
}
bool TermSet::operator==(const TermSet& y) const
{
if (size()!=y.size()) return false;
return std::equal(begin(), end(), y.begin());
}
bool TermSet::operator<(const TermSet& y) const
{
return std::lexicographical_compare(begin(), end(), y.begin(), y.end());
}
void TermSet::ins(const Term& y)
{
iterator i;
if (y.size()==0) return;
if ( (i=find(y)) == end()) {
set_type::insert(y);
return;
}
(*i).funcs.or(y.funcs); // add funcs in y to existing term
}
unsigned int TermSet::cost(unsigned long inputs, TSwfCost crit) const
{
TermSet::iterator i,e;
unsigned long c=0, idx;
typedef std::vector<unsigned long> TULongVector;
TULongVector FnTally; // holds # of terms used in each function
unsigned long cFn=(*begin()).funcs.getBits();
FnTally.resize(cFn, 0);
// calculate cost of terms
for(i=begin(), e=end(); i!=e; i++) {
idx=(*i).funcs.index();
if (idx>0) c += (*i).cost(inputs, crit, false); // first use of term
/*if ( idx > 1 )
c += ((*i).cost(inputs, crit, true)) * ( idx - 1 );*/
for(unsigned long fn=0; fn<cFn; fn++)
if ((*i).funcs[fn]) FnTally[fn]=FnTally[fn]+1;
}
// calculate cost of combining terms
for(unsigned long fn=0; fn<cFn; fn++) {
switch (crit) {
case Cost_Inputs:
if (FnTally[fn]>1) c += FnTally[fn];
break;
case Cost_Term:
if (FnTally[fn]>1) c++; // one gate needed for this function
break;
case Cost_Literals:
break;
}
}
return c;
}
void TermSet::remove_subsuming() // removes subsuming terms
{
iterator i1, i2, e;
bool reset=false;
e=end();
for(i1=begin(); i1!=e; i1++) {
// check if this term subsumes any other term
if (reset) {
i1=begin(); e=end();
reset=false;
}
i2=i1;
for(i2++; i2!=e; i2++) {
if ((*i2).is_partialcover(*i1)) { // found one
TBitField mask(*((*i1).GetFns()));
mask.and(*((*i2).GetFns())); // mask out intersection of functions
mask.not();
if ((*i1).GetFns()->and(mask).truth()==0) { // delete if necessary
erase(i1);
reset=true;
i1=begin(); e=end();
break;
} // if
} // if
} // for
} // for
}
IMPLEMENT_STREAMABLE(TermSet);
void TermSet::Streamer::Write(opstream& out) const
{
out.writeWord32(GetObject()->size());
iterator i = GetObject()->begin(), e = GetObject()->end();
while (i!=e) {
out << *i;
i++;
}
}
void *TermSet::Streamer::Read(ipstream& in, uint32 /*ver*/) const
{
GetObject()->erase(GetObject()->begin(), GetObject()->end());
size_type count;
Term x;
count=in.readWord32();
while (count--) {
in >> x;
GetObject()->insert(x);
}
return GetObject();
}
ostream& operator<<(ostream& os, const TermSet& t)
{
TermSet::iterator i=t.begin(), e=t.end();
while (i!=e) {
os << *i++;
if (i!=e) os << ',';
}
return os;
}
istream& operator>>(istream& is, TermSet& t)
{
t.erase(t.begin(), t.end());
long flags=is.flags();
is.setf(ios::skipws);
Term x;
do {
if (is.peek()==',') is.ignore();
is >> x;
t.insert(x);
} while (is.peek()==',');
is.flags(flags);
return is;
}
}