1 : |
agomez |
1 |
#include "intvector.h" |
2 : |
|
|
#include "agebandmatrixratioptrvector.h" |
3 : |
|
|
#include "commentstream.h" |
4 : |
|
|
#include "gadget.h" |
5 : |
|
|
|
6 : |
|
|
AgeBandMatrixRatioPtrVector::AgeBandMatrixRatioPtrVector(int sz, int minage,
|
7 : |
|
|
const IntVector& minl, const IntVector& lsize) {
|
8 : |
|
|
|
9 : |
|
|
int i;
|
10 : |
|
|
size = (sz > 0 ? sz : 0);
|
11 : |
|
|
if (size == 0) {
|
12 : |
|
|
v = 0;
|
13 : |
|
|
} else {
|
14 : |
|
|
v = new AgeBandMatrixRatio*[size];
|
15 : |
|
|
for (i = 0; i < size; i++)
|
16 : |
|
|
v[i] = new AgeBandMatrixRatio(minage, minl, lsize);
|
17 : |
|
|
}
|
18 : |
|
|
}
|
19 : |
|
|
|
20 : |
|
|
AgeBandMatrixRatioPtrVector::~AgeBandMatrixRatioPtrVector() {
|
21 : |
|
|
int i;
|
22 : |
|
|
for (i = 0; i < tagID.Size(); i++)
|
23 : |
|
|
delete[] tagID[i];
|
24 : |
|
|
|
25 : |
|
|
if (v != 0) {
|
26 : |
|
|
for (i = 0; i < size; i++)
|
27 : |
|
|
delete v[i];
|
28 : |
|
|
delete[] v;
|
29 : |
|
|
v = 0;
|
30 : |
|
|
}
|
31 : |
|
|
}
|
32 : |
|
|
|
33 : |
|
|
void AgeBandMatrixRatioPtrVector::resize(int addsize, AgeBandMatrixRatio* matr) {
|
34 : |
|
|
if (addsize <= 0)
|
35 : |
|
|
return;
|
36 : |
|
|
|
37 : |
|
|
int i;
|
38 : |
|
|
if (v == 0) {
|
39 : |
|
|
size = addsize;
|
40 : |
|
|
v = new AgeBandMatrixRatio*[size];
|
41 : |
|
|
for (i = 0; i < size; i++)
|
42 : |
|
|
v[i] = matr;
|
43 : |
|
|
|
44 : |
|
|
} else {
|
45 : |
|
|
AgeBandMatrixRatio** vnew = new AgeBandMatrixRatio*[addsize + size];
|
46 : |
|
|
for (i = 0; i < size; i++)
|
47 : |
|
|
vnew[i] = v[i];
|
48 : |
|
|
delete[] v;
|
49 : |
|
|
v = vnew;
|
50 : |
|
|
for (i = size; i < addsize + size; i++)
|
51 : |
|
|
v[i] = matr;
|
52 : |
|
|
size += addsize;
|
53 : |
|
|
}
|
54 : |
|
|
}
|
55 : |
|
|
|
56 : |
|
|
void AgeBandMatrixRatioPtrVector::resize(int addsize, int minage,
|
57 : |
|
|
const IntVector& minl, const IntVector& lsize) {
|
58 : |
|
|
|
59 : |
|
|
if (addsize <= 0)
|
60 : |
|
|
return;
|
61 : |
|
|
|
62 : |
|
|
int i;
|
63 : |
|
|
if (v == 0) {
|
64 : |
|
|
size = addsize;
|
65 : |
|
|
v = new AgeBandMatrixRatio*[size];
|
66 : |
|
|
for (i = 0; i < size; i++)
|
67 : |
|
|
v[i] = new AgeBandMatrixRatio(minage, minl, lsize);
|
68 : |
|
|
|
69 : |
|
|
} else {
|
70 : |
|
|
AgeBandMatrixRatio** vnew = new AgeBandMatrixRatio*[size + addsize];
|
71 : |
|
|
for (i = 0; i < size; i++)
|
72 : |
|
|
vnew[i] = v[i];
|
73 : |
|
|
delete[] v;
|
74 : |
|
|
v = vnew;
|
75 : |
|
|
for (i = size; i < size + addsize; i++)
|
76 : |
|
|
v[i] = new AgeBandMatrixRatio(minage, minl, lsize);
|
77 : |
|
|
size += addsize;
|
78 : |
|
|
}
|
79 : |
|
|
}
|
80 : |
|
|
|
81 : |
|
|
// New memory has been allocated for each v[i][age][length][tag].N.
|
82 : |
|
|
// All v[i][age][length][tag].N, v[i][age][length][tag].R added equal -1.0.
|
83 : |
|
|
void AgeBandMatrixRatioPtrVector::addTag(const char* tagname) {
|
84 : |
|
|
|
85 : |
|
|
double* num;
|
86 : |
|
|
int minlength, maxlength, age, length, i;
|
87 : |
|
|
|
88 : |
|
|
this->addTagName(tagname);
|
89 : |
|
|
int minage = v[0]->minAge();
|
90 : |
|
|
int maxage = v[0]->maxAge();
|
91 : |
|
|
for (i = 0; i < size; i++) {
|
92 : |
|
|
for (age = minage; age <= maxage; age++) {
|
93 : |
|
|
minlength = v[i]->minLength(age);
|
94 : |
|
|
maxlength = v[i]->maxLength(age);
|
95 : |
|
|
for (length = minlength; length < maxlength; length++) {
|
96 : |
|
|
num = new double[1];
|
97 : |
|
|
num[0] = 0.0;
|
98 : |
|
|
(*v[i])[age][length].resize(num, 0.0);
|
99 : |
|
|
}
|
100 : |
|
|
}
|
101 : |
|
|
}
|
102 : |
|
|
}
|
103 : |
|
|
|
104 : |
|
|
// No memory has been allocated. v[i][age][length][tag].N points to
|
105 : |
|
|
// the same memory location as initial[i][age][length].N.
|
106 : |
|
|
void AgeBandMatrixRatioPtrVector::addTag(AgeBandMatrixPtrVector* initial,
|
107 : |
|
|
const AgeBandMatrixPtrVector& Alkeys, const char* tagname, double tagloss) {
|
108 : |
|
|
|
109 : |
|
|
int minlength, maxlength, i, age, length;
|
110 : |
|
|
double totalnum;
|
111 : |
|
|
|
112 : |
|
|
this->addTagName(tagname);
|
113 : |
|
|
tagLoss.resize(1, tagloss);
|
114 : |
|
|
int minage = v[0]->minAge();
|
115 : |
|
|
int maxage = v[0]->maxAge();
|
116 : |
|
|
for (i = 0; i < size; i++) {
|
117 : |
|
|
for (age = minage; age <= maxage; age++) {
|
118 : |
|
|
minlength = v[i]->minLength(age);
|
119 : |
|
|
maxlength = v[i]->maxLength(age);
|
120 : |
|
|
for (length = minlength; length < maxlength; length++) {
|
121 : |
|
|
totalnum = Alkeys[i][age][length].N;
|
122 : |
|
|
if (totalnum < verysmall)
|
123 : |
|
|
(*v[i])[age][length].resize((&(*initial)[i][age][length].N), 0.0);
|
124 : |
|
|
else
|
125 : |
|
|
(*v[i])[age][length].resize((&(*initial)[i][age][length].N), (*initial)[i][age][length].N / totalnum);
|
126 : |
|
|
}
|
127 : |
|
|
}
|
128 : |
|
|
}
|
129 : |
|
|
}
|
130 : |
|
|
|
131 : |
|
|
void AgeBandMatrixRatioPtrVector::addTagName(const char* tagname) {
|
132 : |
|
|
char* tempid;
|
133 : |
|
|
tempid = new char[strlen(tagname) + 1];
|
134 : |
|
|
strcpy(tempid, tagname);
|
135 : |
|
|
tagID.resize(tempid);
|
136 : |
|
|
}
|
137 : |
|
|
|
138 : |
|
|
// Returns -1 if do not contain tag with name == tagname.
|
139 : |
|
|
// Else return the index into the location of the tag with name == tagname.
|
140 : |
|
|
int AgeBandMatrixRatioPtrVector::getTagID(const char* tagname) {
|
141 : |
|
|
int i = 0;
|
142 : |
|
|
int found = 0;
|
143 : |
|
|
while (i < tagID.Size() && found == 0) {
|
144 : |
|
|
if (strcasecmp(tagID[i], tagname) == 0)
|
145 : |
|
|
found = 1;
|
146 : |
|
|
i++;
|
147 : |
|
|
}
|
148 : |
|
|
if (i == tagID.Size() && found == 0)
|
149 : |
|
|
return -1;
|
150 : |
|
|
else
|
151 : |
|
|
return i - 1;
|
152 : |
|
|
}
|
153 : |
|
|
|
154 : |
|
|
void AgeBandMatrixRatioPtrVector::deleteTag(const char* tagname) {
|
155 : |
|
|
int minlength, maxlength, i, age, length;
|
156 : |
|
|
int index = getTagID(tagname);
|
157 : |
|
|
int minage = v[0]->minAge();
|
158 : |
|
|
int maxage = v[0]->maxAge();
|
159 : |
|
|
if (index >= 0) {
|
160 : |
|
|
delete[] tagID[index];
|
161 : |
|
|
tagID.Delete(index);
|
162 : |
|
|
tagLoss.Delete(index);
|
163 : |
|
|
for (i = 0; i < size; i++) {
|
164 : |
|
|
for (age = minage; age <= maxage; age++) {
|
165 : |
|
|
minlength = v[i]->minLength(age);
|
166 : |
|
|
maxlength = v[i]->maxLength(age);
|
167 : |
|
|
for (length = minlength; length < maxlength; length++)
|
168 : |
|
|
(*v[i])[age][length].Delete(index);
|
169 : |
|
|
}
|
170 : |
|
|
}
|
171 : |
|
|
}
|
172 : |
|
|
}
|