-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTable.cpp
1446 lines (1246 loc) · 37.6 KB
/
Table.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Argus Open Source
* Software to apply Statistical Disclosure Control techniques
*
* Copyright 2014 Statistics Netherlands
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the European Union Public Licence
* (EUPL) version 1.1, as published by the European Commission.
*
* You can find the text of the EUPL v1.1 on
* https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
*
* This software is distributed on an "AS IS" basis without
* warranties or conditions of any kind, either express or implied.
*/
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include "General.h"
#include "Table.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTable
// This is an operator comparison. This is used in recoded tables.
// The recoded table is the same as the original table. Only the cell ptr is empty.
void CTable:: operator = (CTable &table2)
{
long i;
CKMType = table2.CKMType;
CKMTopK = table2.CKMTopK;
KeepMinScore = table2.KeepMinScore;
NumberofMaxScoreCell = table2.NumberofMaxScoreCell;
NumberofMaxScoreHolding = table2.NumberofMaxScoreHolding;
Prepared = table2.Prepared;
nDim = table2.nDim;
for (i=0; i<MAXDIM; i++) {
SizeDim[i] = table2.SizeDim[i];
}
for (i=0; i<MAXDIM; i++) {
ExplVarnr[i] = table2.ExplVarnr[i];
}
ResponseVarnr = table2.ResponseVarnr;
ShadowVarnr = table2.ShadowVarnr ;
CostVarnr = table2.CostVarnr ;
PeepVarnr = table2.PeepVarnr;
Lambda = table2.Lambda;
MaxScaledCost = table2.MaxScaledCost;
MinLPL = table2.MinLPL;
SetMissingAsSafe = table2.SetMissingAsSafe;
DominanceRule = table2.DominanceRule;
DominanceNumberCell_1 = table2.DominanceNumberCell_1 ;
DominancePercCell_1 = table2.DominancePercCell_1 ;
DominanceNumberCell_2 = table2.DominanceNumberCell_2 ;
DominancePercCell_2 = table2.DominancePercCell_2;
DominanceNumberHolding_1 = table2.DominanceNumberHolding_1 ;
DominancePercHolding_1 = table2.DominancePercHolding_1 ;
DominanceNumberHolding_2 = table2.DominanceNumberHolding_2 ;
DominancePercHolding_2 = table2.DominancePercHolding_2;
PQRule = table2.PQRule;
PQ_PCell_1 = table2.PQ_PCell_1 ;
PQ_QCell_1 = table2.PQ_QCell_1 ;
PQ_NCell_1 = table2.PQ_NCell_1 ;
PQ_PCell_2 = table2.PQ_PCell_2 ;
PQ_QCell_2 = table2.PQ_QCell_2 ;
PQ_NCell_2 = table2.PQ_NCell_2 ;
PQ_PHolding_1 = table2.PQ_PHolding_1 ;
PQ_QHolding_1 = table2.PQ_QHolding_1 ;
PQ_NHolding_1 = table2.PQ_NHolding_1 ;
PQ_PHolding_2 = table2.PQ_PHolding_2 ;
PQ_QHolding_2 = table2.PQ_QHolding_2 ;
PQ_NHolding_2 = table2.PQ_NHolding_2 ;
PeepPercCell_1 = table2.PeepPercCell_1;
PeepPercCell_2 = table2.PeepPercCell_2;
PeepPercHolding_1 = table2.PeepPercHolding_1;
PeepPercHolding_2 = table2.PeepPercHolding_2;
PeepSafetyRangePercCell = table2.PeepSafetyRangePercCell;
PeepSafetyRangePercHolding = table2.PeepSafetyRangePercHolding;
PeepMinFreqCell = table2.PeepMinFreqCell;
PeepMinFreqHold = table2.PeepMinFreqHold;
SafeMinRec = table2.SafeMinRec ;
SafeMinHoldings = table2.SafeMinHoldings;
CellFreqSafetyPerc = table2.CellFreqSafetyPerc;
HoldingFreqSafetyPerc = table2.HoldingFreqSafetyPerc;
ManualSafetyPerc = table2.ManualSafetyPerc;
NSEmptySafetyRange = table2.NSEmptySafetyRange;
SingletonSafetyRangePerc = table2.SingletonSafetyRangePerc;
ZeroSafetyRange = table2.ZeroSafetyRange;
CellPtr.resize(1);
nCell = 0 ;
IsFrequencyTable = table2.IsFrequencyTable;
ApplyHolding = table2.ApplyHolding;
ApplyWeight = table2.ApplyWeight;
ApplyWeightOnSafetyRule = table2.ApplyWeightOnSafetyRule;
ApplyPeeper = table2.ApplyPeeper;
ApplyZeroRule = table2.ApplyZeroRule;
EmptyCellsAsNSEmpty = table2.EmptyCellsAsNSEmpty;
HasRecode = table2.HasRecode ;
}
// Table constructor. All values are initialized.
CTable::CTable()
{
Prepared = false;
IsFrequencyTable = false;
ApplyHolding = false;
ApplyWeight = false;
ApplyWeightOnSafetyRule = false;
ApplyPeeper = false;
ApplyZeroRule = false;
EmptyCellsAsNSEmpty = false;
HasRecode = 0;
SetMissingAsSafe = false;
memset(ExplVarnr,0,sizeof(int)*MAXDIM); // Added
ResponseVarnr = -1; // Added
ShadowVarnr = -1; // Added
CostVarnr = -1; // Added
CellKeyVarnr = -1; // Added
PeepVarnr = -1;
nDim = 0;
memset(SizeDim,0,sizeof(int)*MAXDIM); // Added
nCell = 0;
NumberofMaxScoreCell = 0;
NumberofMaxScoreHolding = 0;
CKMTopK = 1;
CKMType = "N";
KeepMinScore = false;
Lambda = 1;
MaxScaledCost = 20000;
MinLPL = 1;
DominanceRule = false;
DominanceNumberCell_1 = 0;
DominancePercCell_1 = 0;
DominanceNumberCell_2 = 0;
DominancePercCell_2 = 0;
DominanceNumberHolding_1 = 0;
DominancePercHolding_1 = 0;
DominanceNumberHolding_2 = 0;
DominancePercHolding_2 = 0;
PQRule = false;
PQ_PCell_1 =0;
PQ_QCell_1 = 100; //AHNL initialiseer PQ_Q = 100 ipv 0
PQ_NCell_1 = 0;
PQ_PCell_2 =0;
PQ_QCell_2 = 100; //AHNL initialiseer PQ_Q = 100 ipv 0
PQ_NCell_2 = 0;
PQ_PHolding_1 =0;
PQ_QHolding_1 = 100; //AHNL initialiseer PQ_Q = 100 ipv 0
PQ_NHolding_1 = 0;
PQ_PHolding_2 =0;
PQ_QHolding_2 = 100; //AHNL initialiseer PQ_Q = 100 ipv 0
PQ_NHolding_2 = 0;
PeepPercCell_1 = 0;
PeepPercCell_2 = 0;
PeepPercHolding_1 = 0;
PeepPercHolding_2 = 0;
PeepSafetyRangePercCell = 0;
PeepSafetyRangePercHolding = 0;
PeepMinFreqCell = 0;
PeepMinFreqHold = 0;
SafeMinRec = 0;
SafeMinHoldings = 0;
CellFreqSafetyPerc = 0;
HoldingFreqSafetyPerc = 0;
ManualSafetyPerc = 0;
NSEmptySafetyRange= 0;
SingletonSafetyRangePerc = 0;
ZeroSafetyRange = 0; // Added
CellDistance = 0;
}
CTable::~CTable()
{
CleanUp();
}
// Not used????? PWOF
// explanatory variables are set
/*bool CTable::SetExplVariables(int inDim, long* ExplVar)
{
nDim = inDim;
for (int i = 0; i < nDim; i++) {
ExplVarnr[i] = ExplVar[i] - 1;
}
ShadowVarnr = -1;
CostVarnr = -1;
ResponseVarnr = -1;
return true;
}*/
// other variables are set. In case only frequency tables are created, this has to be changed
bool CTable::SetVariables(int inDim, long *ExplVar, long RespVar, long ShadVar, long CostVar, long CellKeyVar, long PeepVar)
{
// Add HoldingVarnr to this shit
nDim = inDim;
for (int i = 0; i < nDim; i++) {
ExplVarnr[i] = ExplVar[i];
}
if (RespVar >= 0) {
ResponseVarnr = RespVar;
}
/*else {
ResponseVarnr = -1;
}*/
if (ShadVar >= 0) {
ShadowVarnr = ShadVar;
}
/*else {
ShadowVarnr = -1;
}*/
if (CellKeyVar >= 0){
CellKeyVarnr = CellKeyVar;
}
CostVarnr = CostVar;
PeepVarnr = PeepVar;
return true;
}
// cell_1 = LongArray[0], cell_2 = LongArray[1],
//Holding_1 = LongArray[2],Holding_2,= LongArray[3];
bool CTable::SetDominance(long *DominanceNumber, long *DominancePerc)
{
if (!ApplyHolding) {
if (DominanceNumber[0] < 1) return false;
if (DominancePerc[0] < 0 || DominancePerc[0] > 100) return false;
this->DominanceNumberCell_1 = DominanceNumber[0];
this->DominancePercCell_1 = DominancePerc[0];
this->PQ_QCell_1 = 100; //ahnl 30 jan 2002
//if DominanceNumberCell = 0; don't do anything
//other wiseset
if (DominanceNumber[1] > 0) {
if (DominancePerc[1] < 0 || DominancePerc[1] > 100) return false;
this->DominanceNumberCell_2 = DominanceNumber[1];
this->DominancePercCell_2 = DominancePerc[1];
this->PQ_QCell_2 = 100; //ahnl 30 jan 2000
}
}
else {
if ((DominanceNumber[0] <1) && (DominanceNumber[2] <1)) return false;
if (DominanceNumber[0] > 0) {
if (DominancePerc[0] < 0 || DominancePerc[0] > 100) return false;
this->DominanceNumberCell_1 = DominanceNumber[0];
this->DominancePercCell_1 = DominancePerc[0];
this->PQ_QCell_1 = 100; //ahnl 30 jan 2000
}
if (DominanceNumber[1] > 0) {
if (DominancePerc[1] < 0 || DominancePerc[1] > 100) return false;
this->DominanceNumberCell_2 = DominanceNumber[1];
this->DominancePercCell_2 = DominancePerc[1];
this->PQ_QCell_2 = 100; //ahnl 30 jan 2000
}
if (DominanceNumber[2] > 0) {
if (DominancePerc[2] < 0 || DominancePerc[2] > 100) return false;
this->DominanceNumberHolding_1 = DominanceNumber[2];
this->DominancePercHolding_1 = DominancePerc[2];
this->PQ_QHolding_1 = 100; //ahnl 30 jan 2000
}
if (DominanceNumber[3] > 0) {
if (DominancePerc[3] < 0 || DominancePerc[3] > 100) return false;
this->DominanceNumberHolding_2 = DominanceNumber[3];
this->DominancePercHolding_2 = DominancePerc[3];
this->PQ_QHolding_2 = 100; //ahnl 30 jan 2000
}
}
// Holding
DominanceRule = true;
return true;
}
// Set PQ rule
bool CTable::SetPQRule(long *PriorPosteriorP,long *PriorPosteriorQ, long *PriorPosteriorN )
{
if (!ApplyHolding) {
if (PriorPosteriorN[0] <1) return false;
if (PriorPosteriorP[0] > PriorPosteriorQ[0]) return false;
PQ_PCell_1 = PriorPosteriorP[0];
PQ_QCell_1 = PriorPosteriorQ[0];
PQ_NCell_1 = PriorPosteriorN[0];
if (PriorPosteriorN[1] >0) {
if (PriorPosteriorP[1] > PriorPosteriorQ[1]) return false;
PQ_PCell_2 = PriorPosteriorP[1];
PQ_QCell_2 = PriorPosteriorQ[1];
PQ_NCell_2 = PriorPosteriorN[1];
}
}
else
{
if ((PriorPosteriorN[0] <1) && (PriorPosteriorN[2] <1)) return false;
if (PriorPosteriorN[0] >0) {
if (PriorPosteriorP[0] > PriorPosteriorQ[0]) return false;
PQ_PCell_1 = PriorPosteriorP[0];
PQ_QCell_1 = PriorPosteriorQ[0];
PQ_NCell_1 = PriorPosteriorN[0];
}
if (PriorPosteriorN[1] >0) {
if (PriorPosteriorP[1] > PriorPosteriorQ[1]) return false;
PQ_PCell_2 = PriorPosteriorP[1];
PQ_QCell_2 = PriorPosteriorQ[1];
PQ_NCell_2 = PriorPosteriorN[1];
}
if (PriorPosteriorN[2] >0) {
if (PriorPosteriorP[2] > PriorPosteriorQ[2]) return false;
PQ_PHolding_1 = PriorPosteriorP[2];
PQ_QHolding_1 = PriorPosteriorQ[2];
PQ_NHolding_1 = PriorPosteriorN[2];
}
if (PriorPosteriorN[3] >0) {
if (PriorPosteriorP[3] > PriorPosteriorQ[3]) return false;
PQ_PHolding_2 = PriorPosteriorP[3];
PQ_QHolding_2 = PriorPosteriorQ[3];
PQ_NHolding_2 = PriorPosteriorN[3];
}
}
PQRule = true;
return true;
}
// safe minimum records and safe minimum holdings
bool CTable::SetSafeMinRecAndHold(long SafeMinRec, long SafeMinHoldings)
{
if (ApplyHolding) {
if ((SafeMinRec < 1)&&(SafeMinHoldings < 1)) {
return false;
}
this->SafeMinHoldings = SafeMinHoldings;
this->SafeMinRec = SafeMinRec;
}
else {
if (SafeMinRec < 1) {
return false;
}
this->SafeMinRec = SafeMinRec;
}
return true;
}
// memory for table is created. In this method the length of top n is calculated.
// The top n is used by datacell to reserve memory for top n inputs for the table.
// Since there could be two PQ and two Dominance rules. The top n is the maximum n.
// One cell is also created as an empty cell. This is the (Size of table + 1)th cell. This is used
// whenever information for a cell is asked that is not created.
bool CTable::PrepareTable()
{
int nScoreCellDom, nScoreHoldingDom, i;
int nScoreCellPQ, nScoreHoldingPQ;
nCell = GetSizeTable();
// The rest are null pointers.
CellPtr.resize(nCell+1); // last pointer to emptycell;
if (CellPtr.size() == 0) {
return false;
}
// one empty cell with last Cellptr pointing to.
CDataCell *dcempty = new CDataCell(NumberofMaxScoreCell, NumberofMaxScoreHolding, ApplyHolding, ApplyWeight);
//hier ook een nus struct empty maken; Size eentje hoger; Iedereen verwijst naaar de empty cell ipv NULL
CellPtr[nCell] = dcempty;
for (i=0; i<nCell; i++) {
CellPtr[i] = NULL;
}
nScoreCellDom = 0;
nScoreHoldingDom = 0;
nScoreCellPQ = 0;
nScoreHoldingPQ = 0;
// each cell has to maintain max score . it has to be the maximum
// of the dom numbers and PQN +1
if (DominanceRule) {
nScoreCellDom = max(DominanceNumberCell_1, DominanceNumberCell_2);
if (ApplyHolding) {
nScoreHoldingDom = max(DominanceNumberHolding_1, DominanceNumberHolding_2);
}
}
if (PQRule) {
nScoreCellPQ = max(PQ_NCell_1,PQ_NCell_2) + 1;
if ((ApplyHolding) && max(PQ_NHolding_1,PQ_NHolding_2) > 0) {
nScoreHoldingPQ = max(PQ_NHolding_1,PQ_NHolding_2) +1;
}
}
NumberofMaxScoreHolding = max(nScoreHoldingDom,nScoreHoldingPQ);
NumberofMaxScoreCell= max(nScoreCellDom, nScoreCellPQ);
// To be able to deal with CKMType = "T" with TopK
if (CKMType == "T"){
NumberofMaxScoreCell = max(NumberofMaxScoreCell, CKMTopK);
}
else{
if (CKMType != "N"){
NumberofMaxScoreCell = max(NumberofMaxScoreCell, 1L);
}
}
// If CKMType = "D" we need to keep smallest value per cell
KeepMinScore = (CKMType == "D");
Prepared = true;
return true;
}
// cleans up all allocated memory.
bool CTable::CleanUp()
{
int i;
if (nCell == 0) {
return true; // nothing to do
}
//delete [] CellDistance; // CellDistance is no longer set with new[]
for (i=0; i<nCell+1; i++) {
delete CellPtr[i];
CellPtr[i] = NULL;
}
CellPtr.clear();
return true;
}
// got to do this
long CTable::GetMemSizeTable()
{
// int nScoreCellDom = 1;
// int nScoreHoldingDom = 0;
long MemUsage;
MemUsage = GetSizeTable() * sizeof(CDataCell);
/*
case DOMINANCE:
nScoreCell = max(DominanceNumberCell_1, DominanceNumberCell_2);
if (ApplyHolding) {
nScoreHolding = max(DominanceNumberHolding_1, DominanceNumberHolding_2);
}
break;
case PQRULE:
nScoreCell = max(PQ_NCell_1,PQ_NCell_2) + 1;
if ((ApplyHolding) && max(PQ_NHolding_1,PQ_NHolding_2) > 0) {
nScoreHolding = max(PQ_NHolding_1,PQ_NHolding_2) +1;
}
break;
// Add memory for MaxScore and MaxScoreWeight
MemUsage += nScoreCell * 2 * sizeof(double) * GetSizeTable();
*/
return MemUsage;
}
// returns how large the table is.
// this function now does not make sense because not all cells are filled.
long CTable::GetSizeTable()
{
int i;
long SizeTable = 1;
ASSERT(nDim > 0 && nDim <= MAXDIM);
for (i = 0; i < nDim; i++) {
ASSERT(SizeDim[i] > 0);
SizeTable *= SizeDim[i]; // number of codes of variable
}
return SizeTable;
}
// sets per dimension the size of this dimension.
bool CTable::SetDimSize(int dim, int value)
{
ASSERT(nDim != 0 && dim >= 0 && dim < nDim && value > 0);
if (nDim == 0 || dim < 0 || dim >= nDim || value < 1) {
return false;
}
SizeDim[dim] = value;
return true;
}
// returns cell if an array of dimension positions are given.
CDataCell* CTable::GetCell(long *VarValueList)
{
return GetCell(GetCellNrFromIndices(VarValueList));
}
// returns a datacell. if the datacell does not exist the empty cell is returned.
// The empty cell is described in Prepare table.
CDataCell* CTable::GetCell(long CellNr)
{
CDataCell *dc;
if (CellPtr[CellNr] != NULL) {
dc = CellPtr[CellNr];
switch (CostVarnr) {
case CVT_DISTANCE:
dc->SetCost(1); // provisional solution, Anco
break;
case CVT_UNITY: // always one
dc->SetCost(1);
break;
case CVT_FREQ: // frequency
dc->SetCost(dc->GetFreq());
break;
}
return dc;
}
else {
dc = CellPtr[nCell]; // return last empty cell;
return dc;
}
}
// given a cell and an array of dimension positions.
//The cell can be placed on position in the CellPtr array.
bool CTable::SetCell(long *VarValueList, CDataCell &datacell)
{
SetCell(GetCellNrFromIndices(VarValueList), datacell);
return true;
}
// Given a cell an a position. It sets the cell.
bool CTable::SetCell(long CellNr, CDataCell &datacell)
{
ASSERT(CellNr >= 0 && CellNr < nCell);
CellPtr[CellNr] = &datacell;
return true;
}
// Unsafe cell through Dominance rule. This is used to determine that the cell is
// primary unsafe.
bool CTable::UnsafeCellDominance(CDataCell &dc)
{
if (DominanceNumberCell_1 > 0) {
if (dc.GetDominancePercCell(ApplyWeight, ApplyWeightOnSafetyRule,
DominanceNumberCell_1) > DominancePercCell_1) {
return true;
}
}
if (DominanceNumberCell_2 > 0) {
if (dc.GetDominancePercCell(ApplyWeight, ApplyWeightOnSafetyRule,
DominanceNumberCell_2) > DominancePercCell_2) {
return true;
}
}
if (ApplyHolding) {
if (DominanceNumberHolding_1 > 0) {
if (dc.GetDominancePercHolding(ApplyWeight, ApplyWeightOnSafetyRule,
DominanceNumberHolding_1) > DominancePercHolding_1) {
return true;
}
}
if (DominanceNumberHolding_2 > 0) {
if (dc.GetDominancePercHolding(ApplyWeight, ApplyWeightOnSafetyRule,
DominanceNumberHolding_2) > DominancePercHolding_2) {
return true;
}
}
}
return false;
}
// Unsafe cell through PQ rule. This is used to determine that a cell is
// primary unsafe.
bool CTable::UnsafeCellPQRule(CDataCell &dc)
{
if (PQ_PCell_1 > 0) {
if (dc.GetPQCell(PQ_PCell_1, PQ_QCell_1,
PQ_NCell_1, ApplyWeight, ApplyWeightOnSafetyRule) > 0) {
return true;
}
}
if (PQ_PCell_2 > 0) {
if (dc.GetPQCell(PQ_PCell_2, PQ_QCell_2,
PQ_NCell_2, ApplyWeight, ApplyWeightOnSafetyRule) > 0) {
return true;
}
}
if (ApplyHolding) {
if (PQ_PHolding_1 > 0) {
if (dc.GetPQHolding(PQ_PHolding_1, PQ_QHolding_1,
PQ_NHolding_1, ApplyWeight, ApplyWeightOnSafetyRule) > 0) {
return true;
}
}
if (PQ_PHolding_2 > 0) {
if (dc.GetPQHolding(PQ_PHolding_2, PQ_QHolding_2,
PQ_NHolding_2, ApplyWeight, ApplyWeightOnSafetyRule) > 0) {
return true;
}
}
}
return false;
}
// Unsafe cell through peep rule. This is used to determine if the cell is unsafe
// because it fails in the peep rule.
bool CTable::UnsafeCellPeep(CDataCell &dc)
{
if(dc.GetPeepSortCell() == PEEP1) {
if (PeepPercCell_1 >0) {
if (dc.GetPeepCell() > (PeepPercCell_1/100.0)*dc.GetShadow()) {
return true;
}
}
}
if(dc.GetPeepSortCell() == PEEP2) {
if (PeepPercCell_2 >0) {
if (dc.GetPeepCell() > (PeepPercCell_2/100.0)*dc.GetShadow()) {
return true;
}
}
}
if (ApplyHolding) {
if (dc.GetPeepSortHolding() == PEEP1) {
if (PeepPercHolding_1 >0) {
if (dc.GetPeepHolding() > (PeepPercHolding_1/100.0)*dc.GetShadow()) {
return true;
}
}
}
if (dc.GetPeepSortHolding() == PEEP2) {
if (PeepPercHolding_2 >0) {
if (dc.GetPeepHolding() > (PeepPercHolding_2/100.0)*dc.GetShadow()) {
return true;
}
}
}
}
// AHNL 6.1.2003: testen op PEEP1 en PEEP2 en niet op <> NOPEEP want soms staat de
// statsu nog op EMPTY!!!
if (dc.GetPeepSortCell() == PEEP1 || dc.GetPeepSortCell() == PEEP2) {
// freq
if (dc.GetFreq() < PeepMinFreqCell) {
return true;
}
}
if (ApplyHolding) {
if (dc.GetPeepSortHolding() == PEEP1 || dc.GetPeepSortHolding() == PEEP2) {
// freq
if (dc.GetFreqHolding() < PeepMinFreqHold) {
return true;
}
}
}
return false;
}
// Unsafe cells through minimum records or minimum holdings.
bool CTable::UnsafeCellMinRec(CDataCell &dc)
{
if (!ApplyWeight) {
if ((dc.GetFreq() > 0) && (dc.GetFreq() < SafeMinRec)) {
return true;
}
if (ApplyHolding) {
if ((dc.GetFreqHolding() > 0) && (dc.GetFreqHolding() < SafeMinHoldings)) {
return true;
}
}
}
// Now Holding and weight cannot happen at the same time. (atleast
// not yet)
else {
if ((dc.GetWeight() < SafeMinRec)) {
return true;
}
}
return false;
}
// This is used to determine, if a cell is safe or unsafe.
// 1) check if the cell is empty.
// 2) Check if the cell fails Dominance or PQ rule
// 3) Check if the Frequency rule fails.
//Know that apply holding and weight cannot occur at the same time.
// 4) Check if Zero rule fails.
// If the cell passes all checks then the cell is safe.
int CTable::ComputeCellSafeCode(CDataCell &dc)
{
if (dc.GetFreq() == 0) { // empty cell
if (EmptyCellsAsNSEmpty) {
return CS_EMPTY_NONSTRUCTURAL;
}
else {
return CS_EMPTY;
}
// NIET WAAR als de status gegeven is en de freq niet.
}
if (DominanceRule) {
if (UnsafeCellDominance(dc) ) { // dominance too big
return CS_UNSAFE_RULE;
}
}
if (PQRule) {
if (UnsafeCellPQRule(dc) ) { // Prior Posteriority incorrect
return CS_UNSAFE_RULE;
}
}
if (ApplyPeeper) {
if (UnsafeCellPeep(dc) ) {
return CS_UNSAFE_PEEP;
}
}
if (UnsafeCellMinRec(dc) ) { // safe minimal?
return CS_UNSAFE_FREQ;
}
if (ApplyZeroRule) {
if ((dc.GetResp() == 0) && (dc.GetFreq() > 0)) {
return CS_UNSAFE_ZERO;
}
else {
return CS_SAFE;
}
}
else
{
return CS_SAFE;
}
}
// nUnsafe: array with max MAXDIM longs.
// For a given explanatory variable this method returns the
//number of unsafe cells per code (index)
bool CTable::GetUnsafeCells(int VarIndex, long *nUnsafe)
{
long DimList[MAXDIM], c;
int d, i, n;
memset(nUnsafe, 0, sizeof(long) * nDim);
// find index variable
for (i = 0; i < nDim; i++) {
if (ExplVarnr[i] == VarIndex) {
break;
}
}
if (i == nDim) {
return false; // Variable not in table, stupid boy
}
long tempstatus;
for (c = 0; c < nCell; c++) {
tempstatus = GetCell(c)->GetStatus();
if ((tempstatus == CS_UNSAFE_FREQ) || (tempstatus == CS_UNSAFE_RULE) ||
(tempstatus == CS_UNSAFE_PEEP) || (tempstatus == CS_UNSAFE_SINGLETON)||
(tempstatus == CS_UNSAFE_ZERO) ){
GetIndicesFromCellNr(c, DimList);
// count number of dimensions of this cell
for (d = n = 0; d < nDim; d++) {
if (DimList[d] != 0) n++;
}
if (DimList[i] != 0) { // Variable VarIndex != total
nUnsafe[n - 1]++;
}
}
}
return true;
}
// Given a variable and code. This method returns the number of cells
bool CTable::GetUnsafeCells(int VarIndex, int VarCodeIndex, long *nUnsafe)
{
long DimList[MAXDIM];
int i;
CDataCell dc;
memset(nUnsafe, 0, sizeof(long) * nDim);
// find index variable
for (i = 0; i < nDim; i++) {
if (ExplVarnr[i] == VarIndex) {
break;
}
}
if (i == nDim) {
return false; // Variable not in table, stupid boy
}
if (VarCodeIndex < 0 || VarCodeIndex >= SizeDim[i]) {
return false;
}
DimList[i] = VarCodeIndex;
CountUnSafe(DimList, i, nUnsafe, 0);
return true;
}
// Counts Unsafe
void CTable::CountUnSafe(long *DimList, int FixedDimNr, long *Unsafe, int niv)
{
int i;
if (niv == FixedDimNr) {
CountUnSafe(DimList, FixedDimNr, Unsafe, niv + 1);
return;
}
if (niv == nDim) {
if ((GetCell(DimList)->GetStatus() == CS_UNSAFE_FREQ) ||
(GetCell(DimList)->GetStatus() == CS_UNSAFE_PEEP) ||
(GetCell(DimList)->GetStatus() == CS_UNSAFE_RULE) ||
(GetCell(DimList)->GetStatus() == CS_UNSAFE_ZERO) ||
(GetCell(DimList)->GetStatus() == CS_UNSAFE_FREQ)){
int d, n;
for (d = n = 0; d < nDim; d++) {
if (DimList[d] != 0) n++;
}
if (n > 0) { // n == 0 in case of general total, no variable is connected
Unsafe[n - 1]++;
}
}
return;
}
for (i = 0; i < SizeDim[niv]; i++) {
DimList[niv] = i;
CountUnSafe(DimList, FixedDimNr, Unsafe, niv + 1);
}
}
// Returns the cell number given indices.
//For example (0,0,0..0) returns 0 and (1,0,0..0) returns 1.
long CTable::GetCellNrFromIndices(long *Indices)
{ int i, c = 0;
for (i = 0; i < nDim; i++) {
ASSERT(Indices[i] >= 0 && Indices[i] < SizeDim[i]);
c *= SizeDim[i];
c += Indices[i];
}
return c;
}
// Given a CellNr returns an array of indices.
bool CTable::GetIndicesFromCellNr(long CellNr, long *Indices)
{
int i, c = CellNr;
for (i = nDim - 1; i >= 0; i--) {
Indices[i] = c % SizeDim[i];
c -= Indices[i];
c /= SizeDim[i];
}
return (c != 0);
}
// cells that are secondary unsafe are reset to safe.
void CTable::UndoSecondarySuppress(long SortSuppress)
{
long i;
CDataCell *dc;
// set realized upper and upper to initial values
for (i = 0; i < nCell; i++) {
dc = GetCell(i);
dc->SetRealizedUpperValue(0);
dc->SetRealizedLowerValue(0);
dc->SetCTAValue(0);
switch (SortSuppress) {
case WITHOUT_SINGLETON:
switch (dc->GetStatus()) {
case CS_SECONDARY_UNSAFE:
if (dc->GetFreq() == 0 ) {
dc->SetStatus (CS_EMPTY_NONSTRUCTURAL);}
else {
dc->SetStatus(CS_SAFE);};
break;
case CS_SECONDARY_UNSAFE_MANUAL:
dc->SetStatus(CS_SAFE_MANUAL);
break;
}
break;
case WITH_SINGLETON:
switch (dc->GetStatus()) {
case CS_UNSAFE_SINGLETON:
dc->SetStatus(CS_SAFE);
break;
case CS_UNSAFE_SINGLETON_MANUAL:
dc->SetStatus(CS_SAFE_MANUAL);
break;
}
break;
case BOTH:
switch (dc->GetStatus()) {
case CS_SECONDARY_UNSAFE:
if (dc->GetFreq() == 0 ) {
dc->SetStatus (CS_EMPTY_NONSTRUCTURAL);}
else {
dc->SetStatus(CS_SAFE);};
break;
case CS_SECONDARY_UNSAFE_MANUAL:
dc->SetStatus(CS_SAFE_MANUAL);
break;
case CS_UNSAFE_SINGLETON:
dc->SetStatus(CS_SAFE);
break;
case CS_UNSAFE_SINGLETON_MANUAL:
dc->SetStatus(CS_SAFE_MANUAL);
break;
}
}
}
}
// Get Status and Cost per Explanatory variable