/*
 * RCCL Version 1.0           Author :  Vincent Hayward
 *                                      School of Electrical Engineering
 *                                      Purdue University
 *      Dir     : src
 *      File    : optim.c
 *      Remarks : Strongly connected with makeposition but can be
 *                used indpendently.
 *      Usage   : part of the library
 */

#include "../h/rccl.h"
#include "../h/manip.h"

#define ISCONS(t)       ((t)->trsf->fn == const)
#define ISHOLD(t)       ((t)->trsf->fn == hold)
#define ISVARB(t)       ((t)->trsf->fn == varb)
#define ISFUND(t)       (!(ISCONS(t) || ISHOLD(t) || ISVARB(t)))

/*
 * collapses any possible pair of transforms in an equation
 */



optimize(p) /*::*/
register PST_PTR p;
{
	register TERM_PTR t, t1, t2;
	TRSF_PTR first;
	char *gensym();

	if (prints_out) {
		fprintf(fpi, "optim, initial equation :\tT6 = ");
		for (t = p->t6ptr->prev; t != p->t6ptr; t = t->prev) {
			fprintf(fpi,
				t->rhs ? " %s%s" : " -%s%s", t->trsf->name,
	ISCONS(t) ? "" : ISHOLD(t) ? "(h)" : ISVARB(t) ? "(v)" : "(s)");
		}
		fprintf(fpi, "\n");
	}

	for (t = p->t6ptr->prev; t != p->t6ptr; t = t->prev) {
		if (ISCONS(t) &&
		    ISCONS(t->prev) &&
		    t->prev != p->t6ptr &&
		    t->prev != p->tlptr) { /* found a pair */
			if ((first = newtrans(gensym(), const)) == NULL) {
				giveup("mem. alloc error", YES);
			}
			t1 = t->next;
			for (t2 = t; t2->prev != p->t6ptr &&
				     t2->prev != p->tlptr &&
				     ISCONS(t2->prev);
						t2 = t2->prev) {
			}
			t2 = t2->prev;
			solved_n(first, t1, t2);
			t->oldt = t->altr = t->trsf = first;
			t->rhs = YES;
			while (t->prev != t2) {
				t->prev = t->prev->prev;
				t->prev->next = t;
			}
			p->pos = p->tlptr->next;
		}
	}
	if (prints_out) {
		fprintf(fpi, "optim, final equation   :\tT6 = ");
		for (t = p->t6ptr->prev; t != p->t6ptr; t = t->prev) {
			fprintf(fpi,
				t->rhs ? " %s%s" : " -%s%s", t->trsf->name,
	ISCONS(t) ? "" : ISHOLD(t) ? "(k)" : ISVARB(t) ? "(v)" : "(s)");
		}
		fprintf(fpi, "\n");
	}
}


/*
 * generates temp names
 */

char *gensym()   /*::*/
{
	char *strsave(), *sprintf(), *strcpy(), *strcat();
	static int n = 0;
	static char root[] = "_TEMP", str[10], name[15];

	n++;
 (void) sprintf(str, "%d", n);
	return(strsave(strcat(strcpy(name,root), str)));
}



char *strsave(s) /*::*/
char *s;
{
	char *p, *malloc(), *strcpy();

	if ((p = malloc((unsigned)strlen(s) + 1)) == NULL) {
		giveup("mem. alloc error", YES);
	}
 (void) strcpy(p, s);
	return(p);
}

