source: tags/svn.1.5.4/NALIGNER/ali_pathmap.cxx

Last change on this file was 6659, checked in by westram, 15 years ago
  • free()
    • removed unneeded casts of argument
    • removed unneeded if-clauses
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : ali_pathmap.cxx                                   //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "ali_pathmap.hxx"
12
13ALI_PATHMAP::ALI_PATHMAP(unsigned long w, unsigned long h)
14{
15    width = w;
16    height = h;
17    height_real = (h / 2) + 1;
18
19    pathmap     = (unsigned char **) CALLOC((unsigned int) (height_real * w), sizeof(unsigned char));
20    up_pointers = (ALI_TARRAY < ali_pathmap_up_pointer > ****) CALLOC((unsigned int) w, sizeof(ALI_TARRAY < ali_pathmap_up_pointer > *));
21    optimized   = (unsigned char **) CALLOC((unsigned int) ((w / 8) + 1), sizeof(unsigned char));
22    if (pathmap == 0 || up_pointers == 0 || optimized == 0)
23        ali_fatal_error("Out of memory");
24}
25
26ALI_PATHMAP::~ALI_PATHMAP()
27{
28    unsigned long   l;
29
30    free(pathmap);
31    if (up_pointers) {
32        for (l = 0; l < width; l++)
33            if ((*up_pointers)[l])
34                free((*up_pointers)[l]);
35        free(up_pointers);
36    }
37    free(optimized);
38}
39
40
41void ALI_PATHMAP::set(unsigned long x, unsigned long y, unsigned char val, ALI_TARRAY < ali_pathmap_up_pointer > *up_pointer) {
42    // Set a value in the pathmap
43    if (x >= width || y >= height)
44        ali_fatal_error("Out of range", "ALI_PATHMAP::set()");
45
46    if (val & ALI_LUP) {
47        if ((*up_pointers)[x] == 0) {
48            (*up_pointers)[x] = (ALI_TARRAY < ali_pathmap_up_pointer > **)
49                CALLOC((unsigned int) height,
50                       sizeof(ALI_TARRAY < ali_pathmap_up_pointer > *));
51            if ((*up_pointers)[x] == 0)
52                ali_fatal_error("Out of memory");
53            (*optimized)[x / 8] &= (unsigned char) ~(0x01 << (7 - (x % 8)));
54        }
55        if ((*optimized)[x / 8] & (0x01 << (7 - (x % 8))))
56            ali_fatal_error("Try to change optimized value", "ALI_PATHMAP::set()");
57
58        (*up_pointers)[x][y] = up_pointer;
59    }
60    if (y & 0x01)
61        val &= 0x0f;
62    else
63        val <<= 4;
64    (*pathmap)[x * height_real + (y >> 1)] |= val;
65}
66
67void ALI_PATHMAP::get(unsigned long x, unsigned long y, unsigned char *val, ALI_TARRAY < ali_pathmap_up_pointer > **up_pointer) {
68    // Get a value from the pathmap
69    unsigned long   l, counter;
70
71    *up_pointer = 0;
72    if (x >= width || y >= height) {
73        ali_fatal_error("Out of range", "ALI_PATHMAP::get()");
74    }
75    if (y & 0x01)
76        *val = (*pathmap)[x * height_real + (y >> 1)] & 0x0f;
77    else
78        *val = (*pathmap)[x * height_real + (y >> 1)] >> 4;
79
80    if (*val & ALI_LUP) {
81        if ((*optimized)[x / 8] & (0x01 << (7 - (x % 8)))) {
82            for (l = 0, counter = 0; l < y / 2; l++) {
83                if ((*pathmap)[x * height_real + l] & ALI_LUP)
84                    counter++;
85                if (((*pathmap)[x * height_real + l] >> 4) & ALI_LUP)
86                    counter++;
87            }
88            if (y & 0x01 && ((*pathmap)[x * height_real + l] >> 4) & ALI_LUP)
89                counter++;
90            *up_pointer = (*up_pointers)[x][counter];
91        } else
92            *up_pointer = (*up_pointers)[x][y];
93    }
94}
95
96
97void ALI_PATHMAP::optimize(unsigned long x) {
98    // optimize the pathmap (the dynamic field with up_pointers)
99    unsigned long   l, counter;
100    ALI_TARRAY < ali_pathmap_up_pointer > *(*buffer)[];
101
102    if ((*up_pointers)[x] == 0 || (*optimized)[x / 8] & (0x01 << (7 - (x % 8))))
103        return;
104
105    for (l = 0, counter = 0; l < height; l++)
106        if ((*up_pointers)[x][l] != 0)
107            counter++;
108
109    if (counter == 0) {
110        free((*up_pointers)[x]);
111        (*up_pointers)[x] = 0;
112        return;
113    }
114    (*optimized)[x / 8] |= (unsigned char) (0x01 << (7 - (x % 8)));
115
116    buffer = (ALI_TARRAY < ali_pathmap_up_pointer > *(*)[])
117        CALLOC((unsigned int) counter + 1,
118               sizeof(ALI_TARRAY < ali_pathmap_up_pointer > *));
119    if (buffer == 0)
120        ali_fatal_error("Out of memory");
121
122    for (l = 0, counter = 0; l < height; l++)
123        if ((*up_pointers)[x][l] != 0)
124            (*buffer)[counter++] = (*up_pointers)[x][l];
125    (*buffer)[counter] = 0;
126
127    free((*up_pointers)[x]);
128    (*up_pointers)[x] = (ALI_TARRAY < ali_pathmap_up_pointer > **) buffer;
129}
130
131
132void ALI_PATHMAP::print()
133{
134    ali_pathmap_up_pointer up;
135    unsigned long   x, y, i;
136    unsigned char   val;
137
138    printf("PATH_MATRIX:\n");
139    for (y = 0; y < height; y++) {
140        for (x = 0; x < width; x++) {
141            if (y & 0x01)
142                val = (*pathmap)[x * height_real + y / 2] & 0x0f;
143            else
144                val = (*pathmap)[x * height_real + y / 2] >> 4;
145            printf("%d ", val);
146        }
147        printf("\n");
148    }
149
150    printf("UP_POINTERS:\n");
151    for (x = 0; x < width; x++) {
152        if ((*up_pointers)[x]) {
153            printf("%3ld : ", x);
154            if ((*optimized)[x / 8] & 0x01 << (7 - (x % 8))) {
155                for (y = 0; (*up_pointers)[x][y] != 0; y++) {
156                    printf("(");
157                    for (i = 0; i < (*up_pointers)[x][y]->size(); i++) {
158                        up = (*up_pointers)[x][y]->get(i);
159                        printf("%ld:%d ", up.start, up.operation);
160                    }
161                    printf(") ");
162                }
163            }
164            else {
165                for (y = 0; y < height; y++) {
166                    printf("(");
167                    if ((*up_pointers)[x][y]) {
168                        for (i = 0; i < (*up_pointers)[x][y]->size(); i++) {
169                            up = (*up_pointers)[x][y]->get(i);
170                            printf("%ld:%d ", up.start, up.operation);
171                        }
172                    }
173                    printf(") ");
174                }
175            }
176            printf("\n");
177        }
178    }
179}
180
181
182
183
184
185
186/* ------------------------------------------------------------
187 *
188 * TEST PART
189 *
190
191#include "ali_tarray.hxx"
192
193ALI_TARRAY<ali_pathmap_up_pointer> *array1, *array2;
194
195void init_pathmap(ALI_PATHMAP *pmap)
196{
197
198   pmap->set(0,0,ALI_LEFT);
199        pmap->set(0,1,ALI_DIAG);
200        pmap->set(0,2,ALI_UP);
201        pmap->set(0,3,ALI_LUP,array1);
202   pmap->set(0,4,ALI_LEFT);
203        pmap->set(0,5,ALI_DIAG);
204        pmap->set(0,6,ALI_UP);
205        pmap->set(0,7,ALI_LUP,array2);
206
207
208        pmap->set(1,0,ALI_LEFT|ALI_DIAG);
209        pmap->set(1,1,ALI_LEFT|ALI_UP);
210        pmap->set(1,2,ALI_LEFT|ALI_LUP,array2);
211        pmap->set(1,3,ALI_LEFT|ALI_DIAG);
212        pmap->set(1,4,ALI_LEFT|ALI_UP);
213        pmap->set(1,5,ALI_LEFT|ALI_LUP,array1);
214
215        pmap->set(30,23,ALI_LEFT);
216        pmap->set(30,24,ALI_DIAG);
217        pmap->set(30,25,ALI_UP);
218        pmap->set(30,26,ALI_LUP,array1);
219   pmap->set(30,27,ALI_LEFT);
220        pmap->set(30,28,ALI_DIAG);
221        pmap->set(30,29,ALI_UP);
222        pmap->set(30,30,ALI_LUP,array2);
223
224        pmap->set(29,25,ALI_LEFT|ALI_DIAG);
225        pmap->set(29,26,ALI_LEFT|ALI_UP);
226        pmap->set(29,27,ALI_LEFT|ALI_LUP,array2);
227        pmap->set(29,28,ALI_LEFT|ALI_DIAG);
228        pmap->set(29,29,ALI_LEFT|ALI_UP);
229        pmap->set(29,30,ALI_LEFT|ALI_LUP,array1);
230}
231
232void print_array(ALI_TARRAY<ali_pathmap_up_pointer> *array)
233{
234   unsigned long l;
235        ali_pathmap_up_pointer up;
236
237   if (array == 0)
238                return;
239
240   printf("<");
241        for (l = 0; l < array->size(); l++) {
242                up = array->get(l);
243                printf("%d:%d ",up.start,up.operation);
244        }
245        printf(">");
246}
247
248void check_pathmap(ALI_PATHMAP *pmap)
249{
250   unsigned long l;
251
252   unsigned char val;
253        ALI_TARRAY<ali_pathmap_up_pointer> *array_of_pointer;
254
255   printf("******************\n");
256        for (l = 0; l < 8; l++) {
257                pmap->get(0,l,&val,&array_of_pointer);
258                printf("(0,%d)  %d ",l,val);
259                print_array(array_of_pointer);
260                printf("\n");
261        }
262        for (l = 0; l < 6; l++) {
263                pmap->get(1,l,&val,&array_of_pointer);
264                printf("(1,%d)  %d ",l,val);
265                print_array(array_of_pointer);
266                printf("\n");
267        }
268        for (l = 23; l < 31; l++) {
269                pmap->get(29,l,&val,&array_of_pointer);
270                printf("(29,%d)  %d ",l,val);
271                print_array(array_of_pointer);
272                printf("\n");
273        }
274        for (l = 25; l < 31; l++) {
275                pmap->get(30,l,&val,&array_of_pointer);
276                printf("(30,%d)  %d ",l,val);
277                print_array(array_of_pointer);
278                printf("\n");
279        }
280}
281
282
283main()
284{
285   ali_pathmap_up_pointer up;
286   ALI_PATHMAP *pmap;
287
288        array1 = new ALI_TARRAY<ali_pathmap_up_pointer>(3);
289        up.start = 1; up.operation = ALI_LEFT;
290        array1->set(0,up);
291        up.start = 3; up.operation = ALI_DIAG;
292        array1->set(1,up);
293        up.start = 5; up.operation = ALI_LEFT;
294        array1->set(2,up);
295        array2 = new ALI_TARRAY<ali_pathmap_up_pointer>(4);
296        up.start = 2; up.operation = ALI_DIAG;
297        array2->set(0,up);
298        up.start = 4; up.operation = ALI_LEFT;
299        array2->set(1,up);
300        up.start = 8; up.operation = ALI_DIAG;
301        array2->set(2,up);
302        up.start = 16; up.operation = ALI_LEFT;
303        array2->set(3,up);
304
305        pmap = new ALI_PATHMAP(31,31);
306
307        init_pathmap(pmap);
308   pmap->print();
309        check_pathmap(pmap);
310        pmap->optimize(0);
311        pmap->optimize(1);
312        pmap->optimize(30);
313        pmap->optimize(29);
314        pmap->print();
315        check_pathmap(pmap);
316}
317
318------------------------------------------------------------ */
Note: See TracBrowser for help on using the repository browser.