-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVariable.cpp
1153 lines (987 loc) · 23.1 KB
/
Variable.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 <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include "General.h"
#include "Variable.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CVariable
// Variable constructor
CVariable::CVariable()
{
bPos= -1;
nPos=0; //Added
nDec=0; //Added
IsPeeper = false;
IsCategorical = false; //Added
IsNumeric = false; //Added
IsWeight = false; //Added
IsHierarchical = false; //Added
IsHolding = false; //Added
IsRecordKey = false; //Added
nMissing = 0;
Missing1 = "";
Missing2 = "";
TotalCode = "";
MinValue = 0;
MaxValue = 0;
nCode = 0;
nDigitSplit = 0;
memset(DigitSplit, 0, sizeof(int) * MAXDIGITGROUP); //Added
hCode = 0;
TableIndex = -1; //Added
ValueToggle = -1; //Added
Value = 0; //Added
HasRecode = false;
HasCodeList = false;
Recode.nCode = 0; //Addedd
Recode.Missing1 = ""; //Addedd
Recode.Missing2 = ""; //Addedd
Recode.nMissing = 0; //Addedd
Recode.DestCode = 0;
Recode.CodeWidth = 0; //Addedd
Recode.nBogus = 0;
Recode.hCode = 0;
PeepCode1 = "";
PeepCode2 = "";
PositionSet = false;
NumSubCodes = 0; // Added
m_SubCodes = 0;
nBogus = 0;
hfCodeWidth = 0; //Added
}
// memory allocated has to be destroyed
CVariable::~CVariable()
{
if (HasRecode) {
UndoRecode();
}
if (hCode != 0) {
delete [] hCode;
}
if (m_SubCodes != 0) {
delete [] m_SubCodes;
}
}
/*
BEGIN_MESSAGE_MAP(CVariable, CWnd)
//{{AFX_MSG_MAP(CVariable)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
*/
// Set position for variables.
bool CVariable::SetPosition(long lbPos, long lnPos, long lnDec)
{
bPos = lbPos - 1;
nPos = lnPos;
nDec = lnDec;
PositionSet = true;
return true;
}
//Set number of decimal places
bool CVariable::SetDecPosition(long lnDec)
{
if (lnDec < 0) return false;
nDec = lnDec;
return true;
}
// Set what type the variable is.
// Categorical, Numerical etc
bool CVariable::SetType(bool bIsCategorical, bool bIsNumeric, bool bIsWeight, bool bIsHierarchical,
bool bIsHolding, bool bIsPeeper, bool bIsRecordKey)
{
IsCategorical = bIsCategorical;
IsNumeric = bIsNumeric;
IsWeight = bIsWeight;
IsHierarchical = bIsHierarchical;
IsHolding = bIsHolding;
IsPeeper = bIsPeeper;
IsRecordKey = bIsRecordKey;
return true;
}
// set the missing strings for variable. note a variable need not always
// have missing strings
bool CVariable::SetMissing(LPCTSTR sMissing1, LPCTSTR sMissing2, long NumMissing)
{
if (NumMissing > 0) {
Missing1 = sMissing1;
Missing2 = sMissing2;
if (Missing1.empty()) {
Missing1 = Missing2;
}
if (Missing1.empty()) {
nMissing = 0;
}
else if (Missing2.empty()) {
nMissing = 1;
}
else {
nMissing = 2;
}
}
else {
nMissing = 0;
}
return true;
}
bool CVariable::SetTotalCode(LPCTSTR sTotalCode)
{
TotalCode = sTotalCode;
return true;
}
// code is added to the code list.
// if code already in code list it is not added
bool CVariable::AddCode(const char *newcode, bool tail)
{
int i, n;
bool IsMissing;
n = sCode.size();
// add at the end of array if not found
if (tail) {
// not already in list?
for (i = 0; i < n; i++) {
if (sCode[i] == newcode) break; // found!
}
if (i == n) { // not found
sCode.push_back(newcode);
}
return true;
}
// keep list sorted
if (n == 0) {
sCode.push_back(newcode);
}
else {
int res = BinSearchStringArray(sCode, newcode, 0, IsMissing);
if (res < 0) { // not found
for (i = 0; i < n; i++) {
if (sCode[i] > newcode) break;
}
if (i < n) {
vector<string>::iterator p = sCode.begin() + i;
sCode.insert(p, newcode);
}
else {
sCode.push_back(newcode);
}
}
}
return true;
}
// compute the hierarchical codes where digit split is involved
bool CVariable::ComputeHierarchicalCodes()
{
int i, j, n;
string s, t;
if (nDigitSplit < 2) return false;
for (i = 0; i < (int) sCode.size(); i++) {
n = 0;
t = sCode[i];
if (((int) t.length()) != nPos) continue; // no datafile code
for (j = 0; j < nDigitSplit - 1; j++) {
n += DigitSplit[j];
s = t.substr(0, n);
AddCode(s.c_str(), false);
}
}
return true;
}
// Returns the number of codes
int CVariable::GetnCode()
{
if (HasRecode) {
return Recode.nCode;
}
return nCode;
}
// returns the number of Bogus codes
int CVariable::GetnBogus()
{
if (HasRecode) {
return Recode.nBogus;
}
return nBogus;
}
// returns number of active codes
int CVariable::GetnCodeActive()
{
int i, n, k;
if (hCode == 0) { // not hierarchical
return GetnCode(); // so all active
}
// hierarchical
k = GetnCode();
for (i = n = 0; i < k; i++) {
if (GethCode()[i].Active) n++;
}
return n;
}
// get number of inactive codes. This is used in recodes to see if
// you can apply recodes
int CVariable::GetnCodeInActive()
{
int i, n, k;
if (hCode == 0) { // not hierarchical
return 0; // so no inactives
}
// hierarchical
k = GetnCode();
for (i = n = 0; i < k; i++) {
if (!GethCode()[i].Active) n++;
}
return n;
}
// Given a position i returns the code for that position. Can only
// be done for a categorical variable
std::string CVariable::GetCode(int i)
{
//string s; // not used?
if (HasRecode) {
return Recode.sCode[i];
}
return sCode[i];
}
int CVariable::GetLevel(int i)
{
int l;
if (IsHierarchical) {
l = hLevel[i];
return l;
}
return 0;
}
// returns number of missing
int CVariable::GetnMissing()
{
if (HasRecode) {
return Recode.nMissing;
}
return nMissing;
}
// returns codelist
vector<string> * CVariable::GetCodeList()
{
if (HasRecode) {
return &(Recode.sCode);
}
return &(sCode);
}
// returns hierarchical code
CCode* CVariable::GethCode()
{
if (HasRecode) {
return Recode.hCode;
}
return hCode;
}
// only for non hierarchical variables
bool CVariable::IsCodeBasic(int i)
{
ASSERT(IsHierarchical == false);
return ((int)(*GetCodeList())[i].length() == GetCodeWidth());
}
// returns code width
int CVariable::GetCodeWidth()
{
if (HasRecode) {
return Recode.CodeWidth;
}
if (IsHierarchical) {
if (nDigitSplit != 0) {
return nPos;
}
else {
return hfCodeWidth;
}
}
else {
return nPos;
}
}
// always with width of 8 characters
// not needed: code is original code, not the one used to feed GHMIter
//void CVariable::GetGHMITERCode(int i, char *code)
void CVariable::GetGHMITERCode(int i, std::string &code)
{
//string s;
ASSERT(i >= 0 && i < (int) GetCodeList()->size() );
//s = GetCode(i);
code = GetCode(i);
//ASSERT(s.length() < 9);
//ASSERT(s.length() < 64);
//sprintf(code, "%8s", s.c_str());
//sprintf(code, "%s", s.c_str());
}
// set the hierarchy. Very important and used by explore file and
// completed codelist
// In this functions the hierarchies, levels and number of children are calculated.
bool CVariable::SetHierarch()
{
int i, j, n, lev[MAXDIGITGROUP + 1];
if (hCode != 0) {
delete [] hCode;
}
hCode = new CCode[nCode];
if (hCode == 0) {
return false; // not enough memory
}
// assume DigitSplit = {2,1,2}, then
// lev[] = {0, 2, 3, 5}
if (nDigitSplit > 0) {
lev[0] = 0;
for (i = n = 0; i < nDigitSplit; i++) {
n += DigitSplit[i];
lev[i + 1] = n;
}
}
// set Level, IsParent
for (i = 0; i < nCode; i++) {
if (nDigitSplit > 0) {
n = sCode[i].length();
for (j = 0; j <= nDigitSplit; j++) {
if (n == lev[j]) break;
}
}
else {
j = hLevel[i];
}
hCode[i].Level = j;
if (i > 0) {
if (hCode[i].Level > hCode[i - 1].Level) {
hCode[i - 1].IsParent = true;
}
else {
hCode[i - 1].IsParent = false;
}
}
}
hCode[nCode - 1].IsParent = false;
// set nChildren and IsBogus and nBogus
nBogus = 0;
for (i = 0; i < nCode - nMissing; i++) {
n = 0;
int level = hCode[i].Level;
for (j = i + 1; j < nCode - nMissing && hCode[j].Level > level; j++) {
if (hCode[j].Level == level + 1) n++; // that's a child
}
hCode[i].nChildren = n;
if (n == 1) {
hCode[i + 1].IsBogus = true;
nBogus++;
}
}
for (; i < nCode; i++) { // Missing
hCode[i].nChildren = 0;
hCode[i].Level = 1;
}
return true;
}
// set all descendants on active / not active
void CVariable::SetActive(long CodeIndex, bool active)
{
int c = CodeIndex, level;
ASSERT(c >= 0 && c < nCode - nMissing);
ASSERT(hCode != 0);
ASSERT(hCode[c].IsParent);
level = hCode[c].Level;
// set all descendants on active / not active
for (c = CodeIndex + 1; c < nCode - nMissing; c++) {
if (hCode[c].Level <= level) break;
hCode[c].Active = active;
}
}
// from a file the code list is created. This is used for hierarchical codelists
// without digit split
int CVariable::SetCodeList(LPCTSTR FileName, LPCTSTR LevelString)
{
FILE *fd;
int i, LenLevelString;
string s;
if (LenLevelString = strlen(LevelString), LenLevelString < 1) return HC_LEVELSTRINGEMPTY;
hLevel.clear();
hLevel.reserve(20);
hLevel.push_back(0); // for total
sCode.clear();
sCode.reserve(20);
sCode.push_back(""); // code for total
hLevelBasic.clear();
hLevelBasic.reserve(20);
hLevelBasic.push_back(false); // for total
fd = fopen(FileName, "r");
if (fd == 0) return HC_FILENOTFOUND;
// ftemp = fopen(tempfilename, "w");
while (!feof(fd) ) {
char str[200];
str[0] = 0;
fgets(str, 200, fd);
if (str[0] == 0) break;
s = str;
// trim right
size_t found = s.find_last_not_of(" \n\r\t");
if (found != string::npos)
s.erase(found + 1);
else
s.resize(0); // str is all whitespace
/// add a few checks.
if (s.empty()) continue;
strcpy(str, s.c_str());
// count number of LevelStrings
i = 0;
while (strncmp(LevelString, &str[i * LenLevelString], LenLevelString) == 0) i++;
hLevel.push_back(i + 1);
// fprintf(ftemp,"%s","hLevel ");
// fprintf(ftemp,"%i\n",hLevel.GetAt(num));
s = &str[i * LenLevelString];
if (s == Missing1 || (nMissing == 2 && s == Missing2) ) {
return HC_CODEISMISSING;
}
sCode.push_back(s);
// fprintf(ftemp,"%s","sCode ");
// fprintf(ftemp,"%s\n",sCode.GetAt(num));
// num++;
}
fclose(fd);
// num = 1;
// set nCode
nCode = sCode.size();
hfCodeWidth = sCode[0].length();
// check and compute hLevelBasic
for (i = 1; i < nCode; i++) {
// compute max width code
if (((int)sCode[i].length()) > hfCodeWidth) {
hfCodeWidth = sCode[i].length();
}
// level oke?
if (hLevel[i] > hLevel[i - 1] && hLevel[i] - hLevel[i - 1] != 1) return HC_LEVELINCORRECT;
if (sCode[i].empty()) return HC_CODEEMPTY;
if (i == nCode - 1) {
hLevelBasic.push_back(true); // last one always basic
}
else {
if (hLevel[i + 1] <= hLevel[i]) {
hLevelBasic.push_back(true);
}
else {
hLevelBasic.push_back(false);
}
}
// fprintf(ftemp,"%s","hLevelBasic ");
// if (hLevelBasic.GetAt(num)) {fprintf(ftemp,"%s\n","TRUE");}
// else {fprintf(ftemp,"%s\n","FALSE");}
// num ++;
}
// fclose(ftemp);
long ret;
// ret = OrganizeCodelist();
// Maar even niet; slaat tenslotte nergens op; AHNL 9 juni 2005
ret = 1;
if (!(ret == 1)) {
return HC_CODETOOLONG;
}
return true;
}
// Finds a code in a hierarchical codelist
int CVariable::FindAllHierarchicalCode(LPCTSTR code)
{
int n = sCode.size();
for (int i = 0; i < n; i++) {
if (sCode[i] == code) {
return i; // found
}
}
return -1; // not found
}
// Finds only the basic codes in a hierarchical codelist
int CVariable::FindHierarchicalCode(LPCTSTR code)
{
int n = sCode.size();
for (int i = 0; i < n; i++) {
if (hLevelBasic[i] && sCode[i] == code) {
return i; // found
}
}
return -1; // not found
}
//
bool CVariable::SetHierarchicalDigits(long nDigitPairs, long *nDigits)
{
int i, npos = 0;
if (nDigitPairs > MAXDIGITGROUP) return false;
// save array, count digits
for (i = 0; i < nDigitPairs; i++) {
if (nDigits[i] < 1) return false;
DigitSplit[i] = nDigits[i];
npos += nDigits[i];
}
// sum digits should be equal to number of positions in data file
if (npos != nPos) return false;
nDigitSplit = nDigitPairs;
return true;
}
bool CVariable::WriteCodelist(LPCTSTR FileName, LPCTSTR LevelString, LPCTSTR Name, bool bogus)
{
vector<string> *CodeList = GetCodeList();
CCode *phCode = GethCode();
int i, n = CodeList->size();
FILE *fd;
if (IsHierarchical && phCode == 0) return false; // not yet set complete
fd = fopen(FileName, "w");
if (fd == 0) return false;
// write name, only if specified
if (Name != 0 && Name[0] != 0) {
fprintf(fd, "%s\n", (LPCTSTR) Name);
}
if (IsHierarchical && bogus) {
// Als de eerste een bogus is, dan gaat dit mis.
WriteBogusCodelist(fd, LevelString, 1, 1, 1, n, CodeList);
}
else {
for (i = 1; i < n; i++) { // ignore total (always index 0)
if (IsHierarchical) {
PrintLevelStrings(fd, phCode[i].Level - 1, LevelString);
}
PrintLevelCode(fd, (*CodeList)[i].c_str(), LevelString);
}
}
fclose(fd);
return true;
}
void CVariable::WriteBogusCodelist(FILE *fd, LPCTSTR LevelString, int index, int level, int boguslevel, int ncode, vector<string> *CodeList)
{
int i, a;
CCode *phCode = GethCode();
// if ( index==1 && phCode[i].nChildren = 1 ) {
// index = index + 1} // Als testje toegevoegd voor het geval dat de hoogste code bogus is.
for (i = index; i < ncode; i++) {
if (phCode[i].Level < level) break;
if (phCode[i].Level > level) continue;
if (phCode[i].nChildren != 1) { // no bogus
PrintLevelStrings(fd, boguslevel - 1, LevelString);
PrintLevelCode(fd, (*CodeList)[i].c_str(), LevelString);
}
if (phCode[i].IsParent) {
if (phCode[i].nChildren == 1) a = 0;
else a = 1;
WriteBogusCodelist(fd, LevelString, i + 1, level + 1, boguslevel + a, ncode, CodeList);
}
}
}
void CVariable::PrintLevelStrings(FILE *fd, int nLevel, LPCTSTR LevelString)
{
for (int i = 0; i < nLevel; i++) { // print LevelStrings
fprintf(fd, "%s", LevelString);
}
}
void CVariable::PrintLevelCode(FILE *fd, LPCTSTR code, LPCTSTR LevelString)
{
fprintf(fd,"%s%s\n", LevelString, code);
}
bool CVariable::SetHierarchicalRecode()
{
int i, j;
vector<unsigned char> RLevel; // levels recoded hierarchical variable
// free previous recode
if (HasRecode) {
if (Recode.hCode != 0) {
delete [] Recode.hCode;
Recode.hCode = 0; //PWOF
}
if (Recode.DestCode != 0) {
free(Recode.DestCode);
Recode.DestCode = 0; // PWOF
}
}
// initialize new recode
Recode.DestCode = (int *) malloc(nCode * sizeof(int) );
if (Recode.DestCode == 0) {
return false;
}
Recode.sCode.clear();
Recode.CodeWidth = 0;
Recode.nMissing = nMissing;
Recode.Missing1 = Missing1;
Recode.Missing2 = Missing2;
RLevel.reserve(20);
// set new codelist, remember RLevel
for (i = j = 0; i < nCode; i++) {
if (hCode[i].Active) {
Recode.sCode.push_back(sCode[i]);
// compute max width code
if (((int) sCode[i].length()) > Recode.CodeWidth) {
Recode.CodeWidth = sCode[i].length();
}
Recode.DestCode[i] = j++;
RLevel.push_back(hCode[i].Level);
}
else {
Recode.DestCode[i] = -1;
}
}
Recode.nCode = Recode.sCode.size();
// initialize hCode
Recode.hCode = new CCode[Recode.nCode];
if (Recode.hCode == 0) {
return false;
}
// compute hCode
for (i = 0; i < Recode.nCode; i++) {
Recode.hCode[i].Active = true;
Recode.hCode[i].Level = RLevel[i];
}
Recode.nBogus = 0;
for (i = 0; i < Recode.nCode - Recode.nMissing; i++) {
int n = 0;
for (j = i + 1; j < Recode.nCode; j++) {
int LevelDiff = Recode.hCode[i].Level - Recode.hCode[j].Level;
if (LevelDiff >= 0) break;
if (LevelDiff == -1) n++;
}
Recode.hCode[i].IsParent = (n > 0);
Recode.hCode[i].nChildren = n;
if (n == 1) {
Recode.hCode[i + 1].IsBogus = true;
Recode.nBogus++;
}
}
for (; i < Recode.nCode; i++) { // Missing
Recode.hCode[i].nChildren = 0;
Recode.hCode[i].Level = 1;
}
HasRecode = true;
return true;
}
void CVariable::UndoRecode()
{
if (HasRecode) {
if (Recode.DestCode != 0) {
free(Recode.DestCode);
Recode.DestCode = 0;
}
if (IsHierarchical) {
if (Recode.hCode != 0) {
delete [] Recode.hCode;
Recode.hCode = 0;
}
SetActive(0, true); // at toplevel, so every code is set at Active
}
HasRecode = false;
}
}
long CVariable::OrganizeCodelist()
{
long i;
long n = sCode.size();
long lowestlevel = 0;
for (i = 0; i < n; i++) {
if (hLevel[i] > lowestlevel) {
lowestlevel = hLevel[i];
}
}
for (i = 0; i < n; i++) {
if (hLevel[i] == lowestlevel) {
string s = sCode[i];
if (((int)s.length()) > nPos) {
string temps = s;
s.substr(0, nPos);
temps.substr(nPos);
RemoveStringInPlace(temps, ' ');
if (!temps.empty()) {
return HC_CODETOOLONG;
}
}
if (((int)s.length()) < nPos) {
// add spaces.
s.insert((size_t)0, (size_t)(nPos - s.length()), ' ');
}
sCode[i] = s;
}
}
return 1;
}
bool CVariable::SetPeepCodes(const string &Peep1, const string &Peep2)
{
if (IsPeeper) {
if ((Peep1.empty()) && (Peep2.empty())) {
return false;
}
if (Peep1.empty()) {
PeepCode1 = Peep2;
}
else {
PeepCode1 = Peep1;
PeepCode2 = Peep2;
}
}
return true;
}
// returns the depth of Hierarchical codelist. This is used
// to calculate marginals.
long CVariable::GetDepthOfHerarchicalBoom(bool Recoded)
{
if (!IsHierarchical) {
return 0;
}
else
{
int maxdepth = 0;
// figure out what to do with recodes
if (HasRecode && Recoded) {
for (int i = 0; i < Recode.nCode; i++) {
maxdepth = max(maxdepth, Recode.hCode[i].Level);
}
}
else
{
for (int i = 0; i < nCode; i++) {
maxdepth = max(maxdepth, hCode[i].Level);
}
}
return maxdepth;
}
}
// To find the number of sub codes. Every parent has a sub code list
long CVariable::NumberOfSubCodeList()
{
long i;
long num;
if (!IsHierarchical) {
num = 1;
return num;
}
else {
num = 0;
for (i=0; i<nCode; i++) {
if (hCode[i].IsParent) { // for every parent there is a code list
num ++;
}
}
return num;
}
}
// allocate memory for subcodelist
bool CVariable::PrepareSubCodeList()
{
long numsubcodes;
if (m_SubCodes != 0) {
delete [] m_SubCodes;
}
numsubcodes = NumberOfSubCodeList();
m_SubCodes = new CSubCodeList[numsubcodes];
if (m_SubCodes == 0) {
return false;
}
NumSubCodes = numsubcodes;
return true;
}
// for a variable make a collection of subcodelist
bool CVariable::FillSubCodeList()
{
long SeqNum,ind;
if (m_SubCodes == 0) {
return false; // memory not allocated
}
if (!IsHierarchical) {
if (!CreateSubCodeForNonHierarchicalCode()) {
return false;
}
}
else {
SeqNum = 0;
for (ind = 0; ind<nCode; ind++) {
if (hCode[ind].IsParent) {
CreateSubCodeForHierarchicalCode(ind,SeqNum);
SeqNum++;
}
}
}
return true;
}
// for non-hierarchical codes is the subcodelist all the codes.
bool CVariable::CreateSubCodeForNonHierarchicalCode()
{
CSubCodeList *subcodelist;
long *indices;
long i;
vector<string> subcodes;
if (IsHierarchical) {
return false;
}
else {
subcodes.resize(sCode.size()-1);
indices = new long [sCode.size()-1];
subcodelist = &(m_SubCodes[0]);
for (i=1; i<nCode; i++) {
subcodes[i-1] = sCode[i];
indices[i-1] = i;
}
subcodelist->SetParentCode(sCode[0]);
subcodelist->SetParentIndex(0);
subcodelist->SetSequenceNumber(0);
subcodelist->FillSubCodes(subcodes, indices);
delete [] indices;
return true;
}
}