#include #include #include #include "lceb.h" static STACK *stacks=NULL; static int nstacks=NULL; void print_stack(stack, details) STACK *stack; int details; { int i; if (details) { printf("Stack [%s] dump: %d/%d used\n", stack->name, stack->last, stack->size); //printf("\tValue: %4d\n", stack->eval); for (i=0; ilast; ++i) printf("\t%02d: Val=%4d\n", i, stack->stack[i]); } else { printf("Stack [%s] : ", stack->name); for (i=0; ilast; ++i) { printf("%d ", stack->stack[i]); } printf("\n"); } } print_stacks() { STACK *stack=stacks; printf("nstacks=%d\n", nstacks); while (stack) { print_stack(stack, 1); stack=stack->next; } } int keep_stack(stack) STACK *stack; { stack->next=stacks; stacks=stack; nstacks++; } STACK *new_stack(size, name, keep) int size; /* 0 if empty */ char *name; int keep; { STACK *stack; int *pelt; int i; stack=malloc(sizeof (STACK)); stack->stack=NULL; stack->size=0; stack->last=0; //stack->eval=0; strncpy(stack->name, name? name: "No name", sizeof(stack->name)); if (size) { pelt=malloc(size*sizeof(int)); stack->size=size; stack->stack=pelt; if (keep) keep_stack(stack); for (i=0; inop=0; //(pelt+i)->curop=0; //(pelt+i)->op[0]=End; pelt[i]=-1; //(pelt+i)->next=i+1; //(pelt+i+1)->prev=i; } //(pelt+i-1)->next=-1; } return stack; } int *push_stack(stack, val) STACK *stack; int val; { int pos=stack->last; int size=stack->size; int *pelt=stack->stack+stack->last; if (pos >= size) { fprintf(stderr, "stack overflow: size=%d last=%d\n", size, pos); return NULL; } //pelt->nop=0; //pelt->curop=0; //pelt->op[0]=op; *pelt=val; stack->last++; return pelt; } int *pop_stack(stack) STACK *stack; { int pos=stack->last+1; int size=stack->size; int *pelt=stack->stack+stack->last; if (pos==0) { fprintf(stderr, "stack empty: size=%d last=%d\n", size, pos); return NULL; } stack->last--; return pelt; } STACK *dup_stack(stack, name) STACK *stack; char *name; { STACK *new; int *src=stack->stack, *dst; int size=stack->size, last=stack->last, i; new=new_stack(size, name, 0); new->last=stack->last; dst=new->stack; for (i=0; istack[i]; //dst->nop=src->nop; //dst->curop=src->curop; //dst->val=src->val; //memcpy(dst->op, src->op, sizeof(src->op)); } //stack->last++; return new; } void swap(elts, i, j) int *elts; int i, j; { int tmp=elts[i]; elts[i] = elts[j]; elts[j] = tmp; } int permute_stack(array, n) int *array; int n; { int i, j, start, end; for(i=n-2; i>-1; i--) { if(array[i+1] > array[i]) { for(j=n-1; j>i; j--){ if(array[j] > array[i]){ // swap swap(array, i, j); // reverse for (start=i+1, end=n-1; start < end; ++start, --end) swap(array, start, end); return 1; } } } } return 0; } int gen_stacks(stack) STACK *stack; { char name[80]; int last=stack->last; int n=1; STACK *new=stack; int exists=1; printf("sorting stack...\n"); mergesort_stack(stack->stack, 0, last-1); //print_stack(stack, 1); while (exists) { sprintf(name, "Stack copy %d", n); new=dup_stack(new, name); exists=permute_stack(new->stack, new->last); if (exists) { //print_stack(new, 1); keep_stack(new); } n++; } } void mergesort_stack(array, left, right) int *array; int left, right; { int mid = (left+right)/2; int pos=0, l=left, r=mid+1, i; int tmp[right-left+1]; if(left < right) { mergesort_stack(array, left, mid); mergesort_stack(array, mid+1,right); while (l <= mid && r <= right) { if (array[l] < array[r]) { tmp[pos++] = array[l++]; } else { tmp[pos++] = array[r++]; } } while (l <= mid) tmp[pos++] = array[l++]; while (r <= right) tmp[pos++] = array[r++]; for (i = 0; i < pos; ++i) array[i+left] = tmp[i]; } } STACK *nth_stack(n) int n; { int i; STACK *stack=stacks; for (i=0; inext; } return stack; } int n_stacks() { return nstacks; }