00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <flgrCoreMalloc.h>
00024 #include <flgrCoreDispatch.h>
00025 #include <flgrCoreVector.h>
00026 #include "flgrDataToolsPStack.h"
00027
00028
00039
00040
00078
00079 FLGR_PStack *flgr_pstack_create(FLGR_Type type, FLGR_VectorCompare fcompare) {
00080 FLGR_PStack *pstk = (FLGR_PStack *) flgr_malloc(sizeof(FLGR_PStack));
00081
00082
00083
00084 if(pstk==NULL) {
00085 POST_ERROR("Could not allocate data\n");
00086 return NULL;
00087 }
00088
00089 pstk->nbPriority = 0;
00090 pstk->first = NULL;
00091 pstk->last = NULL;
00092 pstk->type = type;
00093 pstk->compareStackFunction = fcompare;
00094
00095 return pstk;
00096
00097 }
00098
00099
00101
00105
00106 void flgr_pstack_destroy(FLGR_PStack *pstack) {
00107 FLGR_McStack *stack = pstack->first;
00108 FLGR_McStack *stknext;
00109 int row, col;
00110
00111
00112
00113 if(pstack==NULL) {
00114 POST_ERROR("Null objects!\n");
00115 return;
00116 }
00117
00118 while(stack != NULL) {
00119
00120 while(stack->size!=0) {
00121 flgr_pstack_pop_stack(stack,&row,&col);
00122 }
00123
00124 stknext = stack->next;
00125 flgr_vector_destroy(stack->priorityLabel);
00126 flgr_free(stack);
00127 stack = stknext;
00128 }
00129 }
00130
00131
00132
00133
00134
00135
00136
00138
00140 #define PSTACK_PUSH(dtype) \
00141 FLGR_McStack *stack; \
00142 FLGR_Ret ret; \
00143 \
00144 \
00145 \
00146 stack = flgr_pstack_get_stack_##dtype(pstack, priorityLabel); \
00147 \
00148 if(stack == NULL) { \
00149 stack = flgr_pstack_create_stack_##dtype(pstack, priorityLabel); \
00150 if(stack == NULL) { \
00151 POST_ERROR("Could not create the stack for priority!\n"); \
00152 return FLGR_RET_ALLOCATION_ERROR; \
00153 } \
00154 } \
00155 ret = flgr_pstack_push_stack(stack, row, col); \
00156 \
00157 return ret
00158
00159 FLGR_Ret flgr_pstack_push_fgBIT(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00160 flgr_no_define_type_function(FLGR_BIT);
00161 return FLGR_RET_NOT_IMPLEMENTED;
00162 }
00163 FLGR_Ret flgr_pstack_push_fgUINT8(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00164 PSTACK_PUSH(fgUINT8);
00165 }
00166 FLGR_Ret flgr_pstack_push_fgUINT16(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00167 PSTACK_PUSH(fgUINT16);
00168 }
00169 FLGR_Ret flgr_pstack_push_fgUINT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00170 PSTACK_PUSH(fgUINT32);
00171 }
00172 FLGR_Ret flgr_pstack_push_fgINT8(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00173 PSTACK_PUSH(fgINT8);
00174 }
00175 FLGR_Ret flgr_pstack_push_fgINT16(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00176 PSTACK_PUSH(fgINT16);
00177 }
00178 FLGR_Ret flgr_pstack_push_fgINT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00179 PSTACK_PUSH(fgINT32);
00180 }
00181 FLGR_Ret flgr_pstack_push_fgFLOAT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00182 PSTACK_PUSH(fgFLOAT32);
00183 }
00184 FLGR_Ret flgr_pstack_push_fgFLOAT64(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00185 PSTACK_PUSH(fgFLOAT64);
00186 }
00187
00189
00197
00198 FLGR_Ret flgr_pstack_push(FLGR_PStack *pstack, FLGR_Vector *priorityLabel, int row, int col) {
00199
00200
00201 if(pstack==NULL) {
00202 POST_ERROR("Null objects!\n");
00203 return FLGR_RET_NULL_OBJECT;
00204 }
00205
00206 FLGR_DISPATCH_FUNCTION(FLGR_RET_TYPE_UNKNOWN,pstack->type,flgr_pstack_push, pstack, priorityLabel, row, col);
00207
00208 }
00209
00210
00211
00212
00214
00220
00221 FLGR_Ret flgr_pstack_pop_stack(FLGR_McStack *stack, int *row, int *col) {
00222 FLGR_McStackLink *tmp = stack->top;
00223
00224
00225
00226 if(stack==NULL) {
00227 POST_ERROR("Null objects!\n");
00228 return FLGR_RET_NULL_OBJECT;
00229 }
00230
00231 if(tmp==NULL) {
00232 *row = 0;
00233 *col = 0;
00234 return FLGR_RET_OK;
00235 }
00236
00237 *row = tmp->row;
00238 *col = tmp->col;
00239
00240 stack->top = tmp->lower;
00241 stack->size--;
00242
00243 flgr_free(tmp);
00244
00245 return FLGR_RET_OK;
00246 }
00247
00249
00255
00256 FLGR_Ret flgr_pstack_push_stack(FLGR_McStack *stack, int row, int col) {
00257 FLGR_McStackLink *tmp = (FLGR_McStackLink*) flgr_malloc(sizeof(FLGR_McStackLink));
00258
00259
00260
00261 if(stack==NULL) {
00262 POST_ERROR("Null objects!\n");
00263 return FLGR_RET_NULL_OBJECT;
00264 }
00265
00266 if(tmp==NULL) return FLGR_RET_ALLOCATION_ERROR;
00267
00268 tmp->lower = stack->top;
00269 stack->top = tmp;
00270 stack->size++;
00271
00272 tmp->row = row;
00273 tmp->col = col;
00274
00275 return FLGR_RET_OK;
00276 }
00277
00278
00279
00281
00283 #define FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(dtype) \
00284 \
00285 \
00286 flgr_vector_copy_##dtype##_##dtype(stack->priorityLabel, \
00287 priorityLabel)
00288
00289 void flgr_pstack_set_stack_priority_fgBIT(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00290 flgr_no_define_type_function(FLGR_BIT);
00291 }
00292 void flgr_pstack_set_stack_priority_fgUINT8(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00293 FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(fgUINT8);
00294 }
00295 void flgr_pstack_set_stack_priority_fgUINT16(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00296 FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(fgUINT16);
00297 }
00298 void flgr_pstack_set_stack_priority_fgUINT32(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00299 FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(fgUINT32);
00300 }
00301 void flgr_pstack_set_stack_priority_fgINT8(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00302 FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(fgINT8);
00303 }
00304 void flgr_pstack_set_stack_priority_fgINT16(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00305 FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(fgINT16);
00306 }
00307 void flgr_pstack_set_stack_priority_fgINT32(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00308 FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(fgINT32);
00309 }
00310 void flgr_pstack_set_stack_priority_fgFLOAT32(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00311 FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(fgFLOAT32);
00312 }
00313 void flgr_pstack_set_stack_priority_fgFLOAT64(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00314 FLGR_MACRO_PSTACK_SET_STACK_PRIORITY(fgFLOAT64);
00315 }
00316
00318
00323
00324 FLGR_Ret flgr_pstack_set_stack_priority(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00325
00326
00327 if((stack==NULL) || (priorityLabel==NULL)) {
00328 POST_ERROR("Null objects!\n");
00329 return FLGR_RET_NULL_OBJECT;
00330 }
00331
00332 FLGR_DISPATCH_PROCEDURE(stack->type,flgr_pstack_set_stack_priority,stack,priorityLabel);
00333 }
00334
00335
00336
00337
00338
00340
00345
00346 int flgr_pstack_check_stack_priority(FLGR_McStack *stack, FLGR_Vector *priorityLabel) {
00347
00348
00349 return flgr_vector_equal(stack->priorityLabel, priorityLabel);
00350 }
00351
00352
00353
00355
00357 #define PSTACK_CREATE_STACK(dtype,dtypename) \
00358 FLGR_McStack *new = (FLGR_McStack *) flgr_malloc(sizeof(FLGR_McStack)); \
00359 FLGR_McStack *tmp; \
00360 FLGR_VectorCompare fcompare = pstack->compareStackFunction; \
00361 \
00362 \
00363 \
00364 if(new==NULL) return NULL; \
00365 \
00366 new->next = NULL; \
00367 new->previous = NULL; \
00368 new->top = NULL; \
00369 new->compareStackFunction = fcompare; \
00370 new->priorityLabel = flgr_vector_create(priorityLabel->spp, \
00371 priorityLabel->type); \
00372 new->size = 0; \
00373 new->type = dtypename; \
00374 \
00375 flgr_pstack_set_stack_priority_##dtype(new,priorityLabel); \
00376 \
00377 pstack->nbPriority++; \
00378 \
00379 tmp = pstack->first; \
00380 \
00381 if(tmp == NULL) { \
00382 pstack->first = new; \
00383 pstack->last = new; \
00384 \
00385 }else { \
00386 \
00387 if( (*fcompare)(tmp->priorityLabel,priorityLabel)==FLGR_TRUE ) { \
00388 pstack->first = new; \
00389 new->next = tmp; \
00390 tmp->previous = new; \
00391 }else { \
00392 tmp = tmp->next; \
00393 while(tmp != NULL) { \
00394 if( (*fcompare)(tmp->priorityLabel,priorityLabel)==FLGR_TRUE) { \
00395 new->next = tmp; \
00396 new->previous = tmp->previous; \
00397 tmp->previous->next = new; \
00398 tmp->previous = new; \
00399 return new; \
00400 } \
00401 tmp = tmp->next; \
00402 } \
00403 \
00404 if(tmp==NULL) { \
00405 pstack->last->next = new; \
00406 new->previous = pstack->last; \
00407 pstack->last = new; \
00408 } \
00409 \
00410 } \
00411 \
00412 } \
00413 \
00414 return new
00415
00416 FLGR_McStack *flgr_pstack_create_stack_fgBIT(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00417 flgr_no_define_type_function(FLGR_BIT);
00418 return NULL;
00419 }
00420 FLGR_McStack *flgr_pstack_create_stack_fgUINT8(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00421 PSTACK_CREATE_STACK(fgUINT8,FLGR_UINT8);
00422 }
00423
00424 FLGR_McStack *flgr_pstack_create_stack_fgUINT16(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00425 PSTACK_CREATE_STACK(fgUINT16,FLGR_UINT16);
00426 }
00427
00428 FLGR_McStack *flgr_pstack_create_stack_fgUINT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00429 PSTACK_CREATE_STACK(fgUINT32,FLGR_UINT32);
00430 }
00431
00432 FLGR_McStack *flgr_pstack_create_stack_fgINT8(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00433 PSTACK_CREATE_STACK(fgINT8,FLGR_INT8);
00434 }
00435
00436 FLGR_McStack *flgr_pstack_create_stack_fgINT16(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00437 PSTACK_CREATE_STACK(fgINT16,FLGR_INT16);
00438 }
00439
00440 FLGR_McStack *flgr_pstack_create_stack_fgINT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00441 PSTACK_CREATE_STACK(fgINT32,FLGR_INT32);
00442 }
00443
00444 FLGR_McStack *flgr_pstack_create_stack_fgFLOAT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00445 PSTACK_CREATE_STACK(fgINT32,FLGR_FLOAT32);
00446 }
00447
00448 FLGR_McStack *flgr_pstack_create_stack_fgFLOAT64(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00449 PSTACK_CREATE_STACK(fgINT32,FLGR_FLOAT64);
00450 }
00451
00453
00458
00459 FLGR_McStack *flgr_pstack_create_stack(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00460
00461
00462 if(pstack==NULL) {
00463 POST_ERROR("Null objects!\n");
00464 return NULL;
00465 }
00466
00467 if(pstack->type==FLGR_UINT8) {
00468 return flgr_pstack_create_stack_fgUINT8( pstack, priorityLabel );
00469
00470 }else if(pstack->type==FLGR_UINT16) {
00471 return flgr_pstack_create_stack_fgUINT16( pstack, priorityLabel );
00472
00473 }else if(pstack->type==FLGR_UINT32) {
00474 return flgr_pstack_create_stack_fgUINT32( pstack, priorityLabel );
00475
00476 }else if(pstack->type==FLGR_INT8) {
00477 return flgr_pstack_create_stack_fgINT8( pstack, priorityLabel );
00478
00479 }else if(pstack->type==FLGR_INT16) {
00480 return flgr_pstack_create_stack_fgINT16( pstack, priorityLabel );
00481
00482 }else if(pstack->type==FLGR_INT32) {
00483 return flgr_pstack_create_stack_fgINT32( pstack, priorityLabel );
00484
00485 }else if(pstack->type==FLGR_FLOAT32) {
00486 return flgr_pstack_create_stack_fgFLOAT32( pstack, priorityLabel );
00487
00488 }else if(pstack->type==FLGR_FLOAT64) {
00489 return flgr_pstack_create_stack_fgFLOAT64( pstack, priorityLabel );
00490
00491 }else if(pstack->type==FLGR_BIT) {
00492 return flgr_pstack_create_stack_fgBIT( pstack, priorityLabel );
00493
00494 }else {
00495 POST_ERROR("type unknown!\n");
00496 return NULL;
00497 }
00498
00499 return NULL;
00500
00501 }
00502
00503
00505
00509
00510 FLGR_McStack *flgr_pstack_get_first_no_empty_stack(FLGR_PStack *pstack) {
00511 FLGR_McStack *tmp;
00512
00513
00514
00515 if(pstack==NULL) {
00516 POST_ERROR("Null objects!\n");
00517 return NULL;
00518 }
00519
00520 tmp = pstack->first;
00521
00522 while(tmp != NULL) {
00523 if( tmp->size != 0 ) return tmp;
00524 tmp = tmp->next;
00525 }
00526
00527 return NULL;
00528 }
00529
00531
00535
00536 FLGR_McStack *flgr_pstack_get_last_no_empty_stack(FLGR_PStack *pstack) {
00537 FLGR_McStack *tmp;
00538
00539
00540
00541 if(pstack==NULL) {
00542 POST_ERROR("Null objects!\n");
00543 return NULL;
00544 }
00545
00546 tmp = pstack->last;
00547
00548 while(tmp != NULL) {
00549 if( tmp->size != 0 ) return tmp;
00550 tmp = tmp->previous;
00551 }
00552 return NULL;
00553 }
00554
00556
00558 #define PSTACK_GET_STACK(dtype) \
00559 FLGR_McStack *tmp; \
00560 \
00561 \
00562 \
00563 tmp = pstack->first; \
00564 \
00565 while(tmp != NULL) { \
00566 if( flgr_pstack_check_stack_priority(tmp,priorityLabel)==FLGR_TRUE) \
00567 return tmp; \
00568 tmp = tmp->next; \
00569 } \
00570 \
00571 return NULL
00572
00573
00574
00575 FLGR_McStack *flgr_pstack_get_stack_fgBIT(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00576 flgr_no_define_type_function(FLGR_BIT);
00577 return NULL;
00578 }
00579 FLGR_McStack *flgr_pstack_get_stack_fgUINT8(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00580 PSTACK_GET_STACK(fgUINT8);
00581 }
00582 FLGR_McStack *flgr_pstack_get_stack_fgUINT16(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00583 PSTACK_GET_STACK(fgUINT16);
00584 }
00585 FLGR_McStack *flgr_pstack_get_stack_fgUINT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00586 PSTACK_GET_STACK(fgUINT32);
00587 }
00588 FLGR_McStack *flgr_pstack_get_stack_fgINT8(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00589 PSTACK_GET_STACK(fgINT8);
00590 }
00591 FLGR_McStack *flgr_pstack_get_stack_fgINT16(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00592 PSTACK_GET_STACK(fgINT16);
00593 }
00594 FLGR_McStack *flgr_pstack_get_stack_fgINT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00595 PSTACK_GET_STACK(fgINT32);
00596 }
00597 FLGR_McStack *flgr_pstack_get_stack_fgFLOAT32(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00598 PSTACK_GET_STACK(fgFLOAT32);
00599 }
00600 FLGR_McStack *flgr_pstack_get_stack_fgFLOAT64(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00601 PSTACK_GET_STACK(fgFLOAT64);
00602 }
00603
00605
00610
00611 FLGR_McStack *flgr_pstack_get_stack(FLGR_PStack *pstack, FLGR_Vector *priorityLabel) {
00612
00613
00614 if((pstack==NULL) || (priorityLabel==NULL)) {
00615 POST_ERROR("Null objects\n");
00616 return NULL;
00617 }
00618
00619 if(pstack->type==FLGR_UINT8) {
00620 return flgr_pstack_get_stack_fgUINT8( pstack, priorityLabel );
00621
00622 }else if(pstack->type==FLGR_UINT16) {
00623 return flgr_pstack_get_stack_fgUINT16( pstack, priorityLabel );
00624
00625 }else if(pstack->type==FLGR_UINT32) {
00626 return flgr_pstack_get_stack_fgUINT32( pstack, priorityLabel );
00627
00628 }else if(pstack->type==FLGR_INT8) {
00629 return flgr_pstack_get_stack_fgINT8( pstack, priorityLabel );
00630
00631 }else if(pstack->type==FLGR_INT16) {
00632 return flgr_pstack_get_stack_fgINT16( pstack, priorityLabel );
00633
00634 }else if(pstack->type==FLGR_INT32) {
00635 return flgr_pstack_get_stack_fgINT32( pstack, priorityLabel );
00636
00637 }else if(pstack->type==FLGR_FLOAT32) {
00638 return flgr_pstack_get_stack_fgFLOAT32( pstack, priorityLabel );
00639
00640 }else if(pstack->type==FLGR_FLOAT64) {
00641 return flgr_pstack_get_stack_fgFLOAT64( pstack, priorityLabel );
00642
00643 }else if(pstack->type==FLGR_BIT) {
00644 return flgr_pstack_get_stack_fgBIT( pstack, priorityLabel );
00645
00646 }else {
00647 POST_ERROR("type unknown!\n");
00648 return NULL;
00649 }
00650
00651 return NULL;
00652 }
00653
00654
00655 FLGR_Vector *flgr_pstack_get_label_stack(FLGR_McStack *stack) {
00656
00657
00658 return stack->priorityLabel;
00659 }
00660
00661
00662
00663 int flgr_pstack_is_last_stack(FLGR_McStack *stack) {
00664
00665
00666 return (stack->next==NULL);
00667 }
00668
00669
00670