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