-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlattice.cpp
314 lines (264 loc) · 6.92 KB
/
lattice.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <iostream>
#include "lattice.h"
#include "calcdb.h"
LatticeNode::LatticeNode(){
_parent = new list<LatticeNode *>;
_child = new list<LatticeNode *>;
_mingen = new list<vector<int> >;
_sup=0;
_flg = 0;
}
LatticeNode::~LatticeNode()
{
if (_parent) delete _parent;
if (_child) delete _child;
if (_mingen) delete _mingen;
}
LatticeNode::LatticeNode(LatticeNode *fit)
{
(*(vector<int> *)this) = *((vector<int> *)fit);
//this(*(vector<int> *)fit);
_sup = fit->_sup;
//cout << "COPY " << fit->seqcnt << " " << fit->seqsz << " " <<
// seqcnt << " " << seqsz << endl << flush;
}
LatticeNode::LatticeNode(vector<int> &vec, int sup)
{
_parent = new list<LatticeNode *>;
_child = new list<LatticeNode *>;
_mingen = new list<vector<int> >;
_sup = sup;
(*(vector<int> *)this) = vec;
}
void LatticeNode::add(int it)
{
push_back(it);
}
void LatticeNode::compact()
{
//_seqsz = reAlloc(_seqcnt, sizeof(int), _seq);
}
void LatticeNode::sort(bool childpar_sort)
{
std::sort(begin(), end());
if (childpar_sort){
_child->sort(cmpless);
_parent->sort(cmpless);
}
}
// ostream & operator << (ostream & out, vector<int> &vint)
// {
// for (int i=0; i < vint.size(); ++i)
// out << vint[i] << " ";
// return out;
// }
void LatticeNode::print(int fend)
{
cout << *this;
//cout << "( " << _sup << " )";
if (fend) cout << endl;
}
ostream& operator << (ostream& outputStream, LatticeNode& freq)
{
//outputStream << "FREQ : ";
// outputStream << static_cast<vector<int>&> (freq);
for (int i=0; i < freq.size(); ++i)
outputStream << freq[i] << " ";
//outputStream << Dbase_Ctrl_Blk::FreqIdx[freq[i]] << " ";
outputStream << "( " << freq._sup << " )";
return outputStream;
}
int LatticeNode::compare(LatticeNode *fit){
LatticeNode *tmp = this;
return LatticeNode::cmpfreqit(&tmp, &fit);
}
//this is a subset of par; return the difference par-this
void LatticeNode::subdiff(LatticeNode *par, LatticeNode *diff){
diff->reset();
int i, j;
for (i=0, j=0; i < size() && j < par->size() ;){
if ((*this)[i] > (*par)[j]){
diff->add((*par)[j]);
j++;
}
else if ((*this)[i] < (*par)[j]){
i++;
}
else{
i++;
j++;
}
}
for (; j < par->size(); j++)
diff->add((*par)[j]);
}
subset_res LatticeNode::subset(LatticeNode *set)
{
int i,j;
int di=0, dj=0;
// if (sortitemsets){
for (i=0, j=0; i < size() && j < set->size() ;){
if ((*this)[i] > (*set)[j]){
j++;
dj++;
}
else if ((*this)[i] < (*set)[j]){
i++;
di++;
}
else{
i++;
j++;
}
}
if (i < size()) di++;
if (j < set->size()) dj++;
if (di == 0 && dj == 0) return EQUAL;
else if (di == 0 && dj > 0) return SUBSET;
else if (di > 0 && dj == 0) return SUPSET;
else return NEQUAL;
// }
// else{
// //freq itemsest are not sorted
// int fcnt = 0, nfcnt = 0;
// for (i=0; i < size(); ++i){
// bool found=false;
// for (j=0; j < set->size(); ++j){
// if ((*this)[i] == (*set)[j]){
// found = true;
// }
// }
// if (found) fcnt++;
// else nfcnt++;
// }
// if (fcnt == size() && fcnt == set->size()) return EQUAL;
// else if (fcnt == size() && fcnt < set->size()) return SUBSET;
// else if (fcnt < size() && fcnt == set->size()) return SUPSET;
// else return NEQUAL;
// }
}
int LatticeNode::cmpit(const void *f1, const void *f2)
{
int i1 = *(int *)f1;
int i2 = *(int *)f2;
if (i1 > i2) return 1;
else if (i1 < i2) return -1;
else return 0;
}
int LatticeNode::cmpfreqit(const void *f1, const void *f2)
{
int i, res=0;
vector<int> *fit1 = *(vector<int> **)f1;
vector<int> *fit2 = *(vector<int> **)f2;
//compare size
if (fit1->size() > fit2->size()) res = 1;
else if (fit1->size() < fit2->size()) res = -1;
if (res == 0){
//compare items
for (i=0; i < fit1->size(); i++){
if ((*fit1)[i] < (*fit2)[i]) res = -1;
else if ((*fit1)[i] > (*fit2)[i]) res = 1;
if (res != 0) break;
}
}
return res;
}
void LatticeNode::print_node(ostream &out, bool print_mingen){
list<LatticeNode *>::iterator i;
list<vector<int> >::iterator j;
out << "NODE : " << *this << endl;
out << "PARENTS : ";
//parent()->sort(LatticeNode::cmpless);
for (i = parent()->begin(); i!= parent()->end(); i++){
out << *(*i) << " , ";
}
out << endl;
out << "CHILDREN : ";
//child()->sort(LatticeNode::cmpless);
for (i = child()->begin(); i!= child()->end(); i++){
out << *(*i) << " , ";
}
out << endl;
if (print_mingen){
out << "MINGENS : ";
for (j = mingen()->begin(); j!= mingen()->end(); ++j){
out << (*j) << " , ";
}
out << endl;
}
}
void LatticeNode::print_lattice(LatticeNode *node, long marker)
{
list<LatticeNode *>::iterator i;
if (node->flag() == marker) return;
node->flag() = marker;
node->print_node(cout);
for (i = node->child()->begin(); i!= node->child()->end(); i++){
print_lattice(*i, marker);
}
}
LatticeNode * LatticeNode::add_child(vector<int> &vec, int sup)
{
LatticeNode *ln = new LatticeNode(vec, sup);
_child->push_back(ln);
ln->_parent->push_back(this);
std::sort(ln->begin(), ln->end(), less<int>());
return ln;
}
LatticeNode * LatticeNode::add_child(LatticeNode *ln)
{
_child->push_back(ln);
ln->_parent->push_back(this);
return ln;
}
LatticeNode * LatticeNode::add_child(vector<int> &vec, int litem, int sup)
{
LatticeNode *ln = new LatticeNode(vec, sup);
ln->push_back(litem);
_child->push_back(ln);
ln->_parent->push_back(this);
std::sort(ln->begin(), ln->end(), less<int>());
return ln;
}
void LatticeNode::remove_child(LatticeNode *ln)
{
list<LatticeNode *>::iterator i;
for (i = _child->begin(); i!= _child->end(); ++i){
if (*i == ln){
i = _child->erase(i);
break;
}
}
//cout << "AFTERC :";
//print_node();
}
void LatticeNode::remove_parent(LatticeNode *ln)
{
list<LatticeNode *>::iterator i;
for (i = _parent->begin(); i!= _parent->end(); ++i){
if (*i == ln){
i = _parent->erase(i);
break;
}
}
//cout << "AFTERP :";
//print_node();
}
void LatticeNode::mark_parents(long marker)
{
if (_flg == marker) return;
list<LatticeNode *>::iterator i;
for (i = _parent->begin(); i!= _parent->end(); ++i){
(*i)->mark_parents(marker);
(*i)->flag() = marker;
}
}
void LatticeNode::mark_children(long marker)
{
if (_flg == marker) return;
list<LatticeNode *>::iterator i;
for (i = _child->begin(); i!= _child->end(); ++i){
(*i)->mark_children(marker);
(*i)->flag() = marker;
}
}