working version: displays only bests results so far.
This commit is contained in:
109
stack.c
109
stack.c
@@ -4,7 +4,10 @@
|
||||
#include "lceb.h"
|
||||
|
||||
static STACK *stacks=NULL;
|
||||
static int nstacks=NULL;
|
||||
static STACK *allstacks=NULL;
|
||||
static int nstacks=0;
|
||||
static int totalstacks=0;
|
||||
static int laststack=0;
|
||||
|
||||
void print_stack(stack, details)
|
||||
STACK *stack;
|
||||
@@ -27,13 +30,17 @@ void print_stack(stack, details)
|
||||
}
|
||||
}
|
||||
|
||||
print_stacks()
|
||||
void print_stacks()
|
||||
{
|
||||
STACK *stack=stacks;
|
||||
printf("nstacks=%d\n", nstacks);
|
||||
while (stack) {
|
||||
print_stack(stack, 1);
|
||||
stack=stack->next;
|
||||
int i;
|
||||
//STACK *stack=stacks;
|
||||
printf("nstacks=%d laststack=%d totalstacks=%d\n", nstacks, laststack, totalstacks);
|
||||
//while (stack) {
|
||||
for (i=0; i<laststack; ++i) {
|
||||
printf("stack %d=", i);
|
||||
//stack=stack->next;
|
||||
print_stack(nth_stack(i), 1);
|
||||
//stack=stack->next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +50,7 @@ int keep_stack(stack)
|
||||
stack->next=stacks;
|
||||
stacks=stack;
|
||||
nstacks++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
STACK *new_stack(size, name, keep)
|
||||
@@ -54,27 +62,39 @@ STACK *new_stack(size, name, keep)
|
||||
int *pelt;
|
||||
int i;
|
||||
|
||||
stack=malloc(sizeof (STACK));
|
||||
stack->stack=NULL;
|
||||
stack->size=0;
|
||||
if (!allstacks) {
|
||||
totalstacks=ALLOCSIZE;
|
||||
printf("new_stack %d: allocating %d stacks\n", laststack, totalstacks);
|
||||
allstacks=malloc(totalstacks*sizeof (STACK));
|
||||
}
|
||||
if (laststack==totalstacks) {
|
||||
totalstacks+=ALLOCSIZE;
|
||||
printf("new_stack %d: resizing stacks array to %d\n", laststack, totalstacks);
|
||||
allstacks=realloc(stacks, totalstacks*sizeof (STACK));
|
||||
}
|
||||
|
||||
stack=allstacks+laststack;
|
||||
printf("new_stack %d/%d: address=%p\n", laststack, totalstacks, stack);
|
||||
laststack++;
|
||||
//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; i<size; ++i) {
|
||||
//(pelt+i)->nop=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;
|
||||
//if (size) {
|
||||
//pelt=malloc(size*sizeof(int));
|
||||
stack->size=MAXINPUT;
|
||||
//stack->stack=pelt;
|
||||
if (keep)
|
||||
keep_stack(stack);
|
||||
for (i=0; i<MAXINPUT; ++i) {
|
||||
//(pelt+i)->nop=0;
|
||||
//(pelt+i)->curop=0;
|
||||
//(pelt+i)->op[0]=End;
|
||||
stack->stack[i]=-1;
|
||||
//(pelt+i)->next=i+1;
|
||||
//(pelt+i+1)->prev=i;
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
@@ -174,20 +194,35 @@ int gen_stacks(stack)
|
||||
char name[80];
|
||||
int last=stack->last;
|
||||
int n=1;
|
||||
STACK *new=stack;
|
||||
STACK *new;
|
||||
int exists=1;
|
||||
|
||||
printf("sorting stack...\n");
|
||||
//printf("before sort: ");
|
||||
//print_stack(stack, 0);
|
||||
mergesort_stack(stack->stack, 0, last-1);
|
||||
//printf("after sort: ");
|
||||
//print_stack(stack, 0);
|
||||
// push initial stack
|
||||
//printf("++++++++++++++++ Adding main stack... ");
|
||||
new=dup_stack(stack, "Main stack");
|
||||
//keep_stack(new);
|
||||
//print_stacks();
|
||||
//print_stack(stack, 1);
|
||||
while (exists) {
|
||||
sprintf(name, "Stack copy %d", n);
|
||||
new=dup_stack(new, name);
|
||||
exists=permute_stack(new->stack, new->last);
|
||||
//new=dup_stack(new, name);
|
||||
exists=permute_stack(stack->stack, stack->last);
|
||||
//printf("Permute : ");
|
||||
//print_stack(stack, 0);
|
||||
if (exists) {
|
||||
//print_stack(new, 1);
|
||||
keep_stack(new);
|
||||
printf("++++++++++++++++ Adding stack... ");
|
||||
new=dup_stack(stack, name);
|
||||
// print_stack(new, 0);
|
||||
//keep_stack(new);
|
||||
}
|
||||
// printf("---------------------- Stack... ");
|
||||
// print_stacks();
|
||||
n++;
|
||||
}
|
||||
}
|
||||
@@ -224,15 +259,15 @@ void mergesort_stack(array, left, right)
|
||||
STACK *nth_stack(n)
|
||||
int n;
|
||||
{
|
||||
int i;
|
||||
STACK *stack=stacks;
|
||||
for (i=0; i<n && stack; ++i) {
|
||||
stack=stack->next;
|
||||
}
|
||||
return stack;
|
||||
//int i;
|
||||
//STACK *stack=stacks;
|
||||
//for (i=0; i<n && stack; ++i) {
|
||||
// stack=stack->next;
|
||||
//}
|
||||
return allstacks+n;
|
||||
}
|
||||
|
||||
int n_stacks()
|
||||
{
|
||||
return nstacks;
|
||||
return laststack;
|
||||
}
|
||||
|
Reference in New Issue
Block a user