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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<fstream>
#include<math.h>
#include"Arguments.h"
#include"Model.h"
using namespace std;
struct ADTreenode{
int count;
//i.e. start attr which = *;
int startVaryNodeInd;
//struct ADVarynode * ADVaryNodePs[];
struct ADVarynode ** ADVaryNodePs;
};
typedef struct ADTreenode ADTreeNode;
struct ADVarynode{
//Represent Vary Index
int index;
int mcv;
//struct ADTreenode * ADTreeNodePs[];
struct ADTreenode ** ADTreeNodePs;
};
typedef struct ADVarynode ADVaryNode;
typedef struct {
//the count for each arrangment of contiTableAttrs
//contiTableCounts.size() == the product of the arity of each element in contiTableAttrs
//do not explicitly record the whole contiTable
vector<int> contiTableCounts;
//invariant: in the decreasing order
vector<int> contiTableAttrs;
//condiAttrs.size() == condiVals.size()
//invariant: in the increasing order
vector<int> condiAttrs;
vector<int> condiVals;
//invariant: contiTableAttrs.size() + condiAttrs.size() == the whole original inquiry length
// finally: condiAttrs.size() == 0
} CondiContiTable;
ADTreeNode * MakeADTree(int, vector<int> &, vector< vector<int> > &, vector<int> &);
ADVaryNode * MakeVaryNode(int, vector<int> &, vector< vector<int> > &, vector<int> &);
void PrintADTreeNode(ADTreeNode *, int, const int, vector<int> &);
void PrintADVaryNode(ADVaryNode *, int, const int, vector<int> &);
void FreeADTreeNode(ADTreeNode *, const int, vector<int> &);
void FreeADVaryNode(ADVaryNode *, const int, vector<int> &);
CondiContiTable MakeContab(deque<int>, ADTreeNode *,
vector<int> &, vector<int> &, const vector<int> &);
void MinusContab(CondiContiTable &, CondiContiTable *, int, int);
void ConcatContab(CondiContiTable *, int, int, CondiContiTable &);
//HR:
// a_i is in {0, ..., M-1}
// dm: the data matrix: R * M
// dmIndex: initially stores {0, 1, ..., R - 1}, it is just the light-weight index for dm
// arities: stores the arity of each attr
// could use vector<int> & dmIndex instead of vector<int> dmIndex
ADTreeNode * MakeADTree(int a_i, vector<int> & dmIndex, vector< vector<int> > & dm, vector<int> & arities){
//M = num of attr
int M = dm[0].size();
ADTreeNode * ADTreeNodeP1 = (ADTreeNode *) malloc(sizeof(ADTreeNode));
ADTreeNodeP1->count = dmIndex.size();
ADTreeNodeP1->startVaryNodeInd = a_i;
//base
if(a_i >= M){
ADTreeNodeP1->ADVaryNodePs = NULL;
}
else{
ADTreeNodeP1->ADVaryNodePs = (ADVaryNode **) malloc(sizeof(ADVaryNode*) *
(M - 1 - a_i + 1));
}
for(int a_j = a_i; a_j <= M-1; a_j++){
ADTreeNodeP1->ADVaryNodePs[a_j - a_i] = MakeVaryNode(a_j, dmIndex, dm, arities);
}
return ADTreeNodeP1;
}//end ADTreeNode * MakeADTree()
//HR:
// a_i is in {0, ..., M-1}
// dm: the data matrix: R * M
// dmIndex: initially stores {0, 1, ..., R - 1}
// arities: stores the arity of each attr
// could use vector<int> & dmIndex instead of vector<int> dmIndex
ADVaryNode * MakeVaryNode(int a_i, vector<int> & dmIndex, vector< vector<int> > & dm, vector<int> & arities){
ADVaryNode * ADVaryNodeP1 = (ADVaryNode *) malloc(sizeof(ADVaryNode));
ADVaryNodeP1->index = a_i;
int n_i = arities[a_i];//may be used as global var
ADVaryNodeP1->ADTreeNodePs = (ADTreeNode **) malloc(sizeof(ADTreeNode *) * n_i);
vector<int> dmIndexSub[n_i];
for(unsigned int j = 0; j <= dmIndex.size() - 1; j++){
int v = dm[dmIndex[j]][a_i];
dmIndexSub[v].push_back(dmIndex[j]);
}
ADVaryNodeP1->mcv = 0;
int maxCount = dmIndexSub[0].size();
for(int j = 1; j <= n_i - 1; j++){
if((int) dmIndexSub[j].size() > maxCount){
maxCount = dmIndexSub[j].size();
ADVaryNodeP1->mcv = j;
}
}
for(int j = 0; j <= n_i - 1; j++){
if(dmIndexSub[j].size() == 0 || j == ADVaryNodeP1->mcv){
ADVaryNodeP1->ADTreeNodePs[j] = NULL;
}
else{
ADVaryNodeP1->ADTreeNodePs[j] = MakeADTree(a_i+1, dmIndexSub[j], dm, arities);
}
}
//not necessary in fact
//dmIndex.clear();
//HR: Neither necessary nor correct
//delete dmIndex;
return ADVaryNodeP1;
}//end ADVaryNode * MakeVaryNode()
//HR:
// ADTreeNodeP1: pointers to the ADTreeNode
// level: starting from 0
// M: the num of attr
// arities: stores the arity of each attr
// initial call: PrintADTreeNode(ADTreeNodeP1, 0, M, arities) in Model.h
// pre-order to print the ADtree
void PrintADTreeNode(ADTreeNode * ADTreeNodeP1, int level, const int M, vector<int> & arities){
string strSpace = "";
for(int i = 0; i < level; i++){
strSpace += " ";
}
cout << strSpace << "ADTreeNode" << endl;
cout << strSpace << "Count = " << ADTreeNodeP1->count << endl;
cout << strSpace << "startVaryNodeInd = " << ADTreeNodeP1->startVaryNodeInd << endl;
if(ADTreeNodeP1->ADVaryNodePs == NULL){
return;
}
else{
for(int i = 0; i < M - 1 - ADTreeNodeP1->startVaryNodeInd + 1; i++){
PrintADVaryNode(ADTreeNodeP1->ADVaryNodePs[i], level+1, M, arities);
}
}
} //end void PrintADTreeNode()
//HR:
// ADVaryNodeP1: pointers to the ADVaryNode
// level: starting from 0
// M: the num of attr
// arities: stores the arity of each attr
// pre-order to print the ADtree
void PrintADVaryNode(ADVaryNode * ADVaryNodeP1, int level, const int M, vector<int> & arities){
string strSpace = "";
for(int i = 0; i < level; i++){
strSpace += " ";
}
cout << strSpace << "ADVaryNode" << endl;
cout << strSpace << "index = " << ADVaryNodeP1->index << endl;
cout << strSpace << "mcv = " << ADVaryNodeP1->mcv << endl;
int n_i = arities[ADVaryNodeP1->index]; //may be changed to global var
for(int j = 0; j <= n_i - 1; j++){
if(j == ADVaryNodeP1->mcv){
cout << strSpace << " mcv" << endl;
}
else if(ADVaryNodeP1->ADTreeNodePs[j] == NULL){
cout << strSpace << " NULL" << endl;
}
else{
PrintADTreeNode(ADVaryNodeP1->ADTreeNodePs[j], level+1, M, arities);
}
}
}//end void PrintADVaryNode()
//HR:
// ADTreeNodeP1: pointers to the ADTreeNode
// M: the num of attr
// arities: stores the arity of each attr
// initial call: FreeADTreeNode(ADTreeNodeP1, M, arities) in Model.h
// pre-order to free the ADtree
void FreeADTreeNode(ADTreeNode * ADTreeNodeP1, const int M, vector<int> & arities){
if(ADTreeNodeP1->ADVaryNodePs != NULL){
for(int i = 0; i < M - 1 - ADTreeNodeP1->startVaryNodeInd + 1; i++){
FreeADVaryNode(ADTreeNodeP1->ADVaryNodePs[i], M, arities);
}
free(ADTreeNodeP1->ADVaryNodePs);
}
free(ADTreeNodeP1);
} //end void FreeADTreeNode()
//HR:
// ADVaryNodeP1: pointers to the ADVaryNode
// M: the num of attr
// arities: stores the arity of each attr
// pre-order to free the ADtree
void FreeADVaryNode(ADVaryNode * ADVaryNodeP1, const int M, vector<int> & arities){
int n_i = arities[ADVaryNodeP1->index]; //may be changed to global var
for(int j = 0; j <= n_i - 1; j++){
if(j == ADVaryNodeP1->mcv){
;
}
else if(ADVaryNodeP1->ADTreeNodePs[j] == NULL){
;
}
else{
FreeADTreeNode(ADVaryNodeP1->ADTreeNodePs[j], M, arities);
}
}
free(ADVaryNodeP1->ADTreeNodePs);
free(ADVaryNodeP1);
}//end void FreeADVaryNode()
//Error: if use ofstream of1 instead of ofstream & of1
//In file included from Model.h:19,
// from main.cc:6:
//UpdateHR2.h: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
///usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/ios_base.h:781: error: `std::ios_base::ios_base(const std::ios_base&)' is private
//UpdateHR2.h:220: error: within this context
//UpdateHR2.h: In copy constructor `std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)':
///usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/streambuf:769: error: `std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
//UpdateHR2.h:220: error: within this context
//UpdateHR2.h: In function `void print_CondiContiTable(CondiContiTable&, std::ofstream)':
//UpdateHR2.h:220: error: initializing argument 2 of `void print_vec(std::vector<int, std::allocator<int> >, std::ofstream)'
void print_vec(vector<int> & vec1, ofstream & of1){
for(int i = 0; i < (int) vec1.size(); i++){
of1 << vec1[i] << " ";
}
of1 << endl;
}
void print_deque(deque<int> & v1, ofstream & of1){
for(int i = 0; i < (int) v1.size(); i++){
of1 << v1[i] << " ";
}
of1 << endl;
}
void print_CondiContiTable(CondiContiTable & ccTable1, ofstream & of1){
of1 << "ccTable1.contiTableCounts: " << endl;
print_vec(ccTable1.contiTableCounts, of1);
of1 << "ccTable1.contiTableAttrs: " << endl;
print_vec(ccTable1.contiTableAttrs, of1);
of1 << "ccTable1.condiAttrs: " << endl;
print_vec(ccTable1.condiAttrs, of1);
of1 << "ccTable1.condiVals: " << endl;
print_vec(ccTable1.condiVals, of1);
}
void print_vec(vector<int> & vec1){
for(int i = 0; i < (int) vec1.size(); i++){
cout << vec1[i] << " ";
}
cout << endl;
}
void print_deque(deque<int> & v1){
for(int i = 0; i < (int) v1.size(); i++){
cout << v1[i] << " ";
}
cout << endl;
}
void print_CondiContiTable(CondiContiTable & ccTable1){
cout << "ccTable1.contiTableCounts: " << endl;
print_vec(ccTable1.contiTableCounts);
cout << "ccTable1.contiTableAttrs: " << endl;
print_vec(ccTable1.contiTableAttrs);
cout << "ccTable1.condiAttrs: " << endl;
print_vec(ccTable1.condiAttrs);
cout << "ccTable1.condiVals: " << endl;
print_vec(ccTable1.condiVals);
}
//HR:
// inquiryAttrs: {a_i_1, ..., a_i_n}
// ADNP: The current pointer to ADTreeNode
// givenAttrs, givenVals explicilty record the meaning of ADNP
// {b_i_1, ..., b_i_m} = {value_b_i_1, .. value_b_i_m}
// pre:
// inquiryAttrs is in the increasing order
// givenAttrs is in the increasing order
CondiContiTable MakeContab(deque<int> inquiryAttrs, ADTreeNode * ADNP,
vector<int> & givenAttrs, vector<int> & givenVals, const vector<int> & arities){
// cout << "\nMakeContab()" << endl;
// cout << "inquiryAttrs" << endl;
// print_deque(inquiryAttrs);
// cout << "givenAttrs" << endl;
// print_vec(givenAttrs);
// cout << "givenVals" << endl;
// print_vec(givenVals);
//base 1
if(ADNP == NULL){
CondiContiTable bottomTable;
//base 1.1
//if(inquiryAttrs.size() == 0){
if(inquiryAttrs.empty()){
bottomTable.contiTableCounts.push_back(0);
//bottomTable.contiTableAttrs is empty
bottomTable.condiAttrs = givenAttrs;
bottomTable.condiVals = givenVals;
//cout << "//base 1.1: if(ADNP == NULL) and if(inquiryAttrs.empty())" << endl;
//print_CondiContiTable(bottomTable);
}
//base 1.2
//if(inquiryAttrs.size() > 0)
else{
int rowNum = 1;
for(unsigned int i = 0; i < inquiryAttrs.size(); i++){
//Note: I explicitly write inquiryAttrs[inquiryAttrs.size() - 1 - i ] instead of
// inquiryAttrs[i] because this is really the ordering to create the whole contiTable
int lastAttr = inquiryAttrs[inquiryAttrs.size() - 1 - i ];
rowNum *= arities[lastAttr];
bottomTable.contiTableAttrs.push_back(lastAttr);
}
bottomTable.condiAttrs = givenAttrs;
bottomTable.condiVals = givenVals;
for(int i = 0; i < rowNum; i++){
bottomTable.contiTableCounts.push_back(0);
}
//cout << "//base 1.2: if(ADNP == NULL) and if(!inquiryAttrs.empty())" << endl;
//print_CondiContiTable(bottomTable);
}
return bottomTable;
}// end if(ADNP == NULL)
//base 2
else if (inquiryAttrs.empty()){
CondiContiTable bottomTable;
bottomTable.contiTableCounts.push_back(ADNP->count);
//bottomTable.contiTableAttrs is empty
bottomTable.condiAttrs = givenAttrs;
bottomTable.condiVals = givenVals;
//cout << "//base 2: if(ADNP != NULL) and if(inquiryAttrs.empty())" << endl;
//print_CondiContiTable(bottomTable);
return bottomTable;
}
else{
int a_i_1 = inquiryAttrs.front();
int VNIndex = a_i_1 - ADNP->startVaryNodeInd;
ADVaryNode * VNP = ADNP->ADVaryNodePs[VNIndex];
int mcv = VNP->mcv;
int n_i_1 = arities[a_i_1];
CondiContiTable CTs[n_i_1];
//delete the 1st element a_i_1 of inquiryAtts, will never use the 1st element a_i_1 again
//but does not allow the sub_program to delete more elements, so use deque<int> inquiryAttrs instead of deque<int> & inquiryAttrs
//vector<int> & givenAttrs, vector<int> & givenVals can be used because the sub_program will change but then restore it
inquiryAttrs.pop_front();
for(int k = 0; k < n_i_1; k++){
if(k != mcv){
ADTreeNode * ADNP_k = VNP -> ADTreeNodePs[k];
vector<int> newGivenAttrs = givenAttrs;
newGivenAttrs.push_back(a_i_1);
vector<int> newGivenVals = givenVals;
newGivenVals.push_back(k);
CTs[k] = MakeContab(inquiryAttrs, ADNP_k, newGivenAttrs, newGivenVals, arities);
}
}
CondiContiTable sumCTs = MakeContab(inquiryAttrs, ADNP, givenAttrs, givenVals, arities);
MinusContab(sumCTs, CTs, n_i_1, mcv);
CondiContiTable result;
ConcatContab(CTs, n_i_1, a_i_1, result);
//cout << "//non-base: " << endl;
//print_CondiContiTable(result);
return result;
}
}// end CondiContiTable MakeContab()
void MinusContab(CondiContiTable & sumCTs, CondiContiTable * CTs, int n_i_1, int mcv){
CTs[mcv].contiTableCounts = sumCTs.contiTableCounts;
CTs[mcv].contiTableAttrs = sumCTs.contiTableAttrs;
for(unsigned int i = 0; i < CTs[mcv].contiTableCounts.size(); i++){
for(int k = 0; k < n_i_1; k++){
if(k != mcv){
CTs[mcv].contiTableCounts[i] -= CTs[k].contiTableCounts[i];
}
}
}
//assume arities of each attr >=2
if(mcv != 0){
CTs[mcv].condiAttrs = CTs[0].condiAttrs;
CTs[mcv].condiVals = CTs[0].condiVals;
}
else{
CTs[mcv].condiAttrs = CTs[1].condiAttrs;
CTs[mcv].condiVals = CTs[1].condiVals;
}
CTs[mcv].condiVals.pop_back();
CTs[mcv].condiVals.push_back(mcv);
} //end void MinusContab()
void ConcatContab(CondiContiTable * CTs, int n_i_1, int a_i_1, CondiContiTable & concatedTable){
for(int i = 0; i < n_i_1; i++){
//for each i, CTs[i].contiTableCounts.size() is the same
for(unsigned int j = 0; j < CTs[i].contiTableCounts.size(); j++){
concatedTable.contiTableCounts.push_back(CTs[i].contiTableCounts[j]);
}
}
//check: a_i_1 == CTs[0].condiAttrs.back();
if(a_i_1 != CTs[0].condiAttrs.back()){
cout << "Error: a_i_1 != CTs[0].condiAttrs.back()" << endl;
cout << "a_i_1 = " << a_i_1 << endl;
cout << "CTs[0].condiAttrs.back() = " << CTs[0].condiAttrs.back() << endl;
}
//move a_i_1 from the end of CTs[0].condiAttrs to the end of concatedTable.contiTableAttrs
concatedTable.contiTableAttrs = CTs[0].contiTableAttrs;
concatedTable.contiTableAttrs.push_back(a_i_1);
concatedTable.condiAttrs = CTs[0].condiAttrs;
concatedTable.condiAttrs.pop_back();
concatedTable.condiVals = CTs[0].condiVals;
concatedTable.condiVals.pop_back();
} //end void ContatContab()
|