-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints.cpp
176 lines (151 loc) · 5.49 KB
/
constraints.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
#include <cstring>
#include <algorithm>
#include "constraints.h"
using namespace std;
Constraints::Constraints(char *name){
m_file.open(name);
if (!m_file.is_open()){
cerr << "cannot open constraint file " << name << endl;
exit(1);
}
//read the constraints, one per line
//if first field is C then it is an col/attribute constraint
//if first field is R then it is a row constraint
//items on each line are considered to be conjunctions
// different lines are considered as disjunctions
//each line is assumed to be sorted in increasing order
const int linesz = 16834;
const int wordsz = 1024;
char line[linesz];
char word[wordsz];
char constraintType[1];
while(!m_file.eof()){
m_file.getline(line, linesz);
int line_len = m_file.gcount(); //how many chars read
if (line_len <= 0) break;
parseline pl(line, line_len);
pl.next(constraintType);
vector<int> *cons = new vector<int>;
while (pl.next(word)){
int w = atoi(word);
cons->push_back(w);
}
if (strcmp(constraintType,"C") == 0){
m_colConstraints.push_back(cons);
}
else if (strcmp(constraintType, "R") == 0){
m_rowConstraints.push_back(cons);
//cout << "ADD " << m_rowConstraints.size() << endl;
}
else{
cout << constraintType << " option NOT IMPLEMENTED" << endl;
exit(-1);
}
cout << "READ " << constraintType << " " << *cons << endl;
}
}
Constraints::~Constraints()
{
vector<vector<int> *>::iterator i;
for (i=m_rowConstraints.begin(); i != m_rowConstraints.end(); ++i){
delete *i;
}
for (i=m_colConstraints.begin(); i != m_colConstraints.end(); ++i){
delete *i;
}
m_file.close();
}
bool Constraints::checkRowConstraints(idlist& list, int iter)
{
//if there are no row constraints, then by default they are satisfied
if (m_rowConstraints.size() == 0){
return true;
}
bool res = false;
//cout << "CAME HEREa " << m_rowConstraints.size() << " == " <<
// iter << " " << diff_type << " -- " << list << endl;
//we rely on the fact that for tidsets, we can simply check if the
//constraints is included in the idlist given by list
//for diffsets, we use the observation that if all the items were
//checked to be present in the previous step, then after extension,
//we have only to monitor if an item from the constraint is now
//part of the diffset (i.e., if the constraint - list is not equal
//to constraint), in which case the constraint is not satisfied
if (diff_type == nodiff || (diff_type == diff && iter <= 2) ||
(diff_type == diff2 && iter <= 1)){
const bool reverse = false;
vector<vector<int> *>::iterator i;
for (i = m_rowConstraints.begin();
i != m_rowConstraints.end(); ++i){
//cout << "CAME HERE " << *(*i) << endl;
res = res || checkSubset(*(*i),list,reverse);
}
}
else{
const bool reverse = true;
vector<vector<int> *>::iterator i;
for (i = m_rowConstraints.begin();
i != m_rowConstraints.end(); ++i){
res = res || checkSubset(*(*i),list,reverse);
}
}
return res;
}
//we rely on the fact that for tidsets, we can simply check if the
//constraints is included in the idlist given by list
//for diffsets, we use the observation that if all the items were
//checked to be present in the previous step, then after extension,
//we have only to monitor if an item from the constraint is now
//part of the diffset (i.e., if the constraint - list is not equal
//to constraint), in which case the constraint is not satisfied
bool Constraints::checkSubset(vector<int> &pat, idlist &list, bool reverse)
{
if (reverse){
vector<int> tmp;
insert_iterator< vector<int> > tadd(tmp, tmp.begin());
set_difference(pat.begin(),pat.end(),
list.begin(), list.end(),tadd);
if (tmp.size() == pat.size()) return true;
else return false;
}
else{
//cout << "CHECK " << pat << " -- " << list << endl;
return includes(list.begin(), list.end(), pat.begin(), pat.end());
}
}
//add eq->prefix to iset
//if addnodes flag is true, add each element in the class to eq
// if lval != INVALID then add lval to the iset
bool Constraints::checkColConstraints(Eqclass *eq, bool addnodes, int lval)
{
//if there are no col constraints, then by default they are satisfied
if (m_colConstraints.size() == 0){
return true;
}
//construct the set of items that is the union of prefix and all
//other elements in eq (if addnodes), and add lval (if not
//invalid)
//have to convert each item to its original value
//sort iset as well
vector<int> iset;
for (int j=0; j < eq->prefix().size(); ++j){
iset.push_back(Dbase_Ctrl_Blk::FreqIdx[(eq->prefix())[j]]);
}
if (addnodes){
list<Eqnode *>::iterator ni = eq->nlist().begin();
for (; ni != eq->nlist().end(); ++ni){
iset.push_back(Dbase_Ctrl_Blk::FreqIdx[(*ni)->val]);
}
}
if (lval != INVALID) iset.push_back(Dbase_Ctrl_Blk::FreqIdx[lval]);
sort(iset.begin(),iset.end());
//now see if each constraint is subset of iset
const bool reverse = false;
bool res = false;
vector<vector<int> *>::iterator i;
for (i = m_colConstraints.begin();
i != m_colConstraints.end(); ++i){
res = res || checkSubset(*(*i),iset,reverse);
}
return res;
}