| 1 | #include <stdio.h>
|
|---|
| 2 | #include "treeverse.h"
|
|---|
| 3 |
|
|---|
| 4 | static int stack1[15] ;
|
|---|
| 5 | static int stack2[297] ;
|
|---|
| 6 | static int is1 = -1 ;
|
|---|
| 7 | static int is2 = -1 ;
|
|---|
| 8 |
|
|---|
| 9 | #define BOTTOM stack1[3*is1]
|
|---|
| 10 | #define MAXSNP stack1[3*is1+1]
|
|---|
| 11 | #define OFFSET stack1[3*is1+2]
|
|---|
| 12 | #define LENGTH stack2[3*is2]
|
|---|
| 13 | #define CURPOS stack2[3*is2+1]
|
|---|
| 14 | #define CKPCUT stack2[3*is2+2]
|
|---|
| 15 |
|
|---|
| 16 | void trv_init(int length, int nbSnap, int firstStep) {
|
|---|
| 17 | if (length<=0) {
|
|---|
| 18 | printf("Error: Cannot reverse a sequence of length %i\n",length) ;
|
|---|
| 19 | } else if (nbSnap==0 && length>=2) {
|
|---|
| 20 | printf("Error: Cannot reverse a sequence of length %i with no snapshot\n",length) ;
|
|---|
| 21 | } else if (is1>4 || is2+nbSnap+1>98) {
|
|---|
| 22 | printf("Error: Treeverse memory exceeded !\n") ;
|
|---|
| 23 | } else {
|
|---|
| 24 | is1 = is1+1 ;
|
|---|
| 25 | is2 = is2+1 ;
|
|---|
| 26 | BOTTOM = is2 ;
|
|---|
| 27 | MAXSNP = nbSnap ;
|
|---|
| 28 | OFFSET = firstStep ;
|
|---|
| 29 | LENGTH= length ;
|
|---|
| 30 | CURPOS = 0 ;
|
|---|
| 31 | CKPCUT = 0 ;
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | void trv_setcut() {
|
|---|
| 36 | int length = LENGTH ;
|
|---|
| 37 | int nbSnap = MAXSNP-(is2-BOTTOM)+1 ;
|
|---|
| 38 | if (length<=1) {
|
|---|
| 39 | CKPCUT = 0 ;
|
|---|
| 40 | } else if (nbSnap==1) {
|
|---|
| 41 | CKPCUT = length-1 ;
|
|---|
| 42 | } else {
|
|---|
| 43 | int minRecomp = 1 ;
|
|---|
| 44 | int eta = nbSnap+1 ;
|
|---|
| 45 | while (eta<length) {
|
|---|
| 46 | minRecomp++ ;
|
|---|
| 47 | eta = (eta*(nbSnap+minRecomp))/minRecomp ;
|
|---|
| 48 | }
|
|---|
| 49 | CKPCUT = (length*minRecomp)/(minRecomp+nbSnap) ;
|
|---|
| 50 | if (CKPCUT==0)
|
|---|
| 51 | CKPCUT=1 ;
|
|---|
| 52 | else if (CKPCUT>=length)
|
|---|
| 53 | CKPCUT=length-1 ;
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | int trv_next_action(int *action, int *step) {
|
|---|
| 58 | int i ;
|
|---|
| 59 |
|
|---|
| 60 | // // Part "only for debug":
|
|---|
| 61 | // printf("\n") ;
|
|---|
| 62 | // printf("STACK1:\n") ;
|
|---|
| 63 | // for (i=is1 ; i>=0 ; i--)
|
|---|
| 64 | // printf("%i snapshots, stack2 bottom:%i (offset:%i)\n",
|
|---|
| 65 | // stack1[3*i+1],stack1[3*i],stack1[3*i+2]) ;
|
|---|
| 66 | // printf("-------------------\n") ;
|
|---|
| 67 | // printf("STACK2:\n") ;
|
|---|
| 68 | // for (i=is2 ; i>=0 ; i--)
|
|---|
| 69 | // printf("%i: R( ,%i) %i/%i\n",
|
|---|
| 70 | // i,stack2[3*i],stack2[3*i+1],stack2[3*i+2]) ;
|
|---|
| 71 | // // printf("%i: at %i, nextckp %i, end %i\n",
|
|---|
| 72 | // // i,stack2[3*i+1],stack2[3*i+2],stack2[3*i]) ;
|
|---|
| 73 | // printf("-------------------\n") ;
|
|---|
| 74 | // // End of "only for debug" part
|
|---|
| 75 |
|
|---|
| 76 | if (LENGTH<=0 && is2==BOTTOM) {
|
|---|
| 77 | *step = -1 ;
|
|---|
| 78 | *action = -1 ;
|
|---|
| 79 | is1-- ;
|
|---|
| 80 | is2-- ;
|
|---|
| 81 | return 0 ;
|
|---|
| 82 | } else {
|
|---|
| 83 | *step = 1 ;
|
|---|
| 84 | for (i=BOTTOM+1 ; i<=is2 ; i++) *step += stack2[3*i+1] ;
|
|---|
| 85 | if (CURPOS==-1) {
|
|---|
| 86 | *action = (LENGTH==1)?POPSNAP:LOOKSNAP ;
|
|---|
| 87 | CURPOS = 0 ;
|
|---|
| 88 | } else {
|
|---|
| 89 | if (CURPOS==LENGTH-1) {
|
|---|
| 90 | *action = (*step==stack2[3*BOTTOM])?FIRSTTURN:TURN ;
|
|---|
| 91 | if (CURPOS==0 && is2>BOTTOM) {
|
|---|
| 92 | is2-- ;
|
|---|
| 93 | LENGTH = CKPCUT ;
|
|---|
| 94 | } else {
|
|---|
| 95 | LENGTH = CURPOS ;
|
|---|
| 96 | }
|
|---|
| 97 | CURPOS = -1 ;
|
|---|
| 98 | trv_setcut() ;
|
|---|
| 99 | } else if (CURPOS==CKPCUT) {
|
|---|
| 100 | int remainingLength = LENGTH-CURPOS ;
|
|---|
| 101 | *action = PUSHSNAP ;
|
|---|
| 102 | is2++ ;
|
|---|
| 103 | LENGTH = remainingLength ;
|
|---|
| 104 | CURPOS = 0 ;
|
|---|
| 105 | trv_setcut() ;
|
|---|
| 106 | (*step)-- ;
|
|---|
| 107 | } else {
|
|---|
| 108 | *action = ADVANCE ;
|
|---|
| 109 | CURPOS++ ;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 | return 1 ;
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void trv_resize() {
|
|---|
| 117 | int step = 1, i ;
|
|---|
| 118 | for (i=BOTTOM+1 ; i<=is2 ; ++i) step += stack2[3*i+1] ;
|
|---|
| 119 | printf("Binomial iteration exits on step %i before expected %i\n",
|
|---|
| 120 | step-1, stack2[3*BOTTOM]) ;
|
|---|
| 121 | stack2[3*BOTTOM] = step-1 ;
|
|---|
| 122 | LENGTH = CURPOS ;
|
|---|
| 123 | --(CURPOS) ;
|
|---|
| 124 | }
|
|---|