static void proc_hash(Term t) { if(is_list(t)) { for(;t;t=ListTail(t)) proc_hash(ListFirst(t)); return; } if(is_compound(t)) { int i; for(i=1;i<=CompoundArity(t);i++) { Term t1=CompoundArgN(t,i); if(is_compound(t1)&&CompoundName(t1)==OPR_HASH) { char cbuf[1024]; int n=0; List l,al; t1=ConsumeCompoundArg(t,i); al=OperToList(t1,OPR_HASH); for(l=al;l;l=ListTail(l)) n+=sWriteTerm(cbuf+n,ListFirst(l)); FreeAtomic(al); SetCompoundArg(t,i,NewAtom(cbuf,0)); } else proc_hash(t1); } } return; }
static int only_prm(Term a1) { List l1,l2; for(l1=CompoundArg1(a1);l1;l1=ListTail(l1)) { if(IntegerValue(CompoundArg2(ListFirst(l1)))<0) return 0; for(l2=CompoundArgN(ListFirst(l1),3);l2;l2=ListTail(l2)) { if(CompoundName(ListFirst(l2))==OPR_PARAMETER) continue; if(CompoundName(ListFirst(l2))==OPR_WILD) { List l3; for(l3=CompoundArg2(ListFirst(l2));l3;l3=ListTail(l3)) if(!only_prm(ListFirst(l3))) return 0; continue; } return 0; } } return 1; }
static void RecDisplayList(listADT list) { if (list != NULL) { printf("%s", ListHead(list)); if (ListTail(list) != NULL) printf(" "); RecDisplayList(ListTail(list)); } }
static Term l2expr(List l, int n) { List l1; Term t, res=0; Atom pw; if(CalcOutput) pw=OPR_POW; else pw=OPR_POW; if(l==0) return NewInteger(n); if(n>1 || n<-1) res=NewInteger(n); else { t=ListFirst(l); if(CompoundArg2(t)==NewInteger(1)) res=CompoundArg1(t); else if(CompoundArg2(t)==NewInteger(-1)) res=MakeCompound2(OPR_DIV,NewInteger(1),CompoundArg1(t)); else if(IntegerValue(CompoundArg2(t))>1) res=MakeCompound2(pw,CompoundArg1(t),CompoundArg2(t)); else res=MakeCompound2(OPR_DIV,NewInteger(1), MakeCompound2(pw,CompoundArg1(t), NewInteger(-IntegerValue(CompoundArg2(t))))); if(n<0) res=MakeCompound1(OPR_MINUS,res); l=ListTail(l); } for(l1=l;l1;l1=ListTail(l1)) { t=ListFirst(l1); if(CompoundArg2(t)==NewInteger(1)) res=MakeCompound2(OPR_MLT,res,CompoundArg1(t)); else if(CompoundArg2(t)==NewInteger(-1)) res=MakeCompound2(OPR_DIV,res,CompoundArg1(t)); else if(IntegerValue(CompoundArg2(t))>1) res=MakeCompound2(OPR_MLT,res, MakeCompound2(pw,CompoundArg1(t),CompoundArg2(t))); else res=MakeCompound2(OPR_DIV,res, MakeCompound2(pw,CompoundArg1(t), NewInteger(-IntegerValue(CompoundArg2(t))))); } return res; }
static void alg1_set_cos0(Term a1) { List l; Term c0; int scno=0; for(l=CompoundArg1(a1);l;l=ListTail(l)) { List l2; for(l2=CompoundArgN(ListFirst(l),3);l2;l2=ListTail(l2)) { Term t=ListFirst(l2); if(CompoundName(t)==OPR_PARAMETER && (CompoundArg2(t)==A_SIN || CompoundArg2(t)==A_COS)) scno++; if(CompoundName(t)==OPR_WILD) { List l3; for(l3=CompoundArg2(t);l3;l3=ListTail(l3)) alg1_set_cos0(ListFirst(l3)); } } } if(scno==0) return; c0=MakeCompound3(OPR_PARAMETER,0,A_COS,NewInteger(0)); for(l=CompoundArg1(a1);l;l=ListTail(l)) { List l2; scno=0; for(l2=CompoundArgN(ListFirst(l),3);l2;l2=ListTail(l2)) { Term t=ListFirst(l2); if(CompoundName(t)==OPR_PARAMETER && (CompoundArg2(t)==A_SIN || CompoundArg2(t)==A_COS)) scno++; } if(scno) continue; l2=ConsumeCompoundArg(ListFirst(l),3); l2=AppendLast(l2,CopyTerm(c0)); SetCompoundArg(ListFirst(l),3,l2); } FreeAtomic(c0); /*WriteTerm(a1);puts("\n");*/ }
static Term finda2(List pl, int del) { int pli, ii; List l; pli=ListLength(pl); mmmpos=0; for(ii=0;ii<LagrHashSize;ii++) { for(l=lagr_hash[ii];l;l=ListTail(l)) { List cpl=CompoundArg1(ListFirst(l)); List vpl=0; List ll1,ll2; int pos=1; if(cpl==0 || ListLength(cpl)!=pli) continue; for(ll1=cpl;ll1;ll1=ListTail(ll1),pos++) { Atom a,prp; a=CompoundArg1(ListFirst(ll1)); prp=GetAtomProperty(a,PROP_TYPE); if(is_compound(prp)&&CompoundName(prp)==OPR_FIELD && CompoundArg2(prp)==NewInteger(4)) a=CompoundArg1(prp); if(a==mmm) mmmpos=pos; vpl=AppendLast(vpl,a); } vpl=SortedList(vpl, acmp); for(ll1=pl,ll2=vpl;ll1;ll1=ListTail(ll1),ll2=ListTail(ll2)) if(ListFirst(ll1)!=ListFirst(ll2)) break; if(is_empty_list(ll1)) break; } if(l) break; } if(l && del) lagr_hash[ii]=CutFromList(lagr_hash[ii],l); return l; }
void alg2_recommon_n(Term a2) { List m2l,l,l1,nl; int cnum,n,d; m2l=CompoundArgN(a2,5); nl=NewList(); l=m2l; if(is_empty_list(l)) return; while(!is_empty_list(l)) { nl=AppendLast(nl,CompoundArg1(ListFirst(l))); l=ListTail(l); } cnum=gcf_list(nl); if(IntegerValue(ListFirst(nl))<0) cnum*=-1; if(cnum==1) { RemoveList(nl); return; } l1=m2l; l=nl; while(!is_empty_list(l)) { SetCompoundArg(ListFirst(l1),1, NewInteger(IntegerValue(ListFirst(l))/cnum)); l=ListTail(l); l1=ListTail(l1); } RemoveList(nl); n=IntegerValue(CompoundArg1(CompoundArg2(a2))); d=IntegerValue(CompoundArg2(CompoundArg2(a2))); n*=cnum; cnum=gcf(n,d); n/=cnum; d/=cnum; SetCompoundArg(CompoundArg2(a2),1,NewInteger(n)); SetCompoundArg(CompoundArg2(a2),2,NewInteger(d)); return ; }
static void rename_ind(Term t, Label from, Label to) { if(is_list(t)) { List l; l=t; while(!is_empty_list(l)) { Term u; u=ListFirst(l); if(u==from) ChangeList(l,to); else rename_ind(u,from,to); l=ListTail(l); } return; } if(is_compound(t)) { int i,ac; ac=CompoundArity(t); for(i=1;i<=ac;i++) { Term u; u=CompoundArgN(t,i); if(u==from) SetCompoundArg(t,i,to); else rename_ind(u,from,to); } } return; }
ListNode *CreateList() { ListNode *list = HEAP(ListNode); ListSize(list) = 0; ListHead(list) = ListTail(list) = NULL; return(list); }
static void ForwardHTMLCB () { /* * Callback to step back to the previous HTML document. */ char *htext; char *file; int error, verbose; verbose = GetVerboseLevel(); if( verbose > VERBOSE_0 ) printf ("ForwardHTMLCB\n"); /* * Read in the previous file. Read in last file if at end of list. */ file = ListNext ( historyList ); printf("Next file = %s\n", file ); if ( file == NULL ) file = ListTail ( historyList ); printf("Next file = %s\n", file ); htext = ReadHyperTextFile ( file, &error ); Free ( file ); HTMLSetText ( help.htlmWid, htext, NULL, NULL, 0, 0, NULL ); XtFree ( htext ); }
void alg2_decommon_n(Term a2) { List l1; int cnum,cden; Term t; l1=CompoundArgN(a2,5); if(is_empty_list(l1)) { SetCompoundArg(a2,2,0); return; } t=ConsumeCompoundArg(a2,2); cnum=IntegerValue(CompoundArg1(t)); cden=IntegerValue(CompoundArg2(t)); FreeAtomic(t); while(!is_empty_list(l1)) { Term t; int c1,c2,c3; c1=cnum*IntegerValue(CompoundArg1(ListFirst(l1))); c2=cden; c3=gcf(c1,c2); c1/=c3; c2/=c3; t=MakeCompound2(OPR_DIV,NewInteger(c1),NewInteger(c2)); SetCompoundArg(ListFirst(l1),1,t); l1=ListTail(l1); } }
bool TestListInsertBefore(List *list) { if (!ListEmpty(list)) { sprintf(error, "This test requires list to be empty"); return false; } int i; int k = 5; for (i = length-k; i < length; i++) ListAppend(list, numbers[i]); // Already tested for (i = k - 1; i >= 0; i--) ListPrepend(list, numbers[i]); // Already tested ListItor itor = ListTail(list); for (i = 0; i < k-1; i++) itor = ListItorPrev(itor); for (i = k; i < length - k; i++) if (!ListInsertBefore(itor, numbers[i])) { sprintf(error, "ListInsertBefore failed"); return false; } if (!VerifyListConsistency(list)) { sprintf(error, "List is not consistent with numbers after ListInsertBefore"); return false; } return true; }
/* * Arma 3 listas: * ncp = Non Created Precedencies * dcp = Drain Connected Precedencies * ndcp = Non Drain Connected Precedencies * Las mismas las arma con cada una de las actividades que preceden a tal actividad. */ void SetPrecedencesLists(graphADT g, lisInfADT list, listInfADT * ncp, listInfADT * dcp, listInfADT * ndcp, char ** precedencies) { int i = 0; activityADT act; actInfo * info; listActADT actList; while(precedencies[i]) { info = CopyActInfo(getActInfo(list, precedencies[i])); if((act = GetActivity(g, precedencies[i])) == NULL) insertInf(ncp, info); else { actList = GetStageStart(GetActivityOrig(g, precedencies[i])); while(!ListActIsEmpty(actList)) {Act act = ListHeader(actList); if(IsDrain(GetActivityDest(g, ListActHeadID(list)))) insertInf(dcp, info); else insertInf(ndcp, info); actList = ListTail(actList); } } i++; } }
bool VerifyListConsistency(List *list) { int i; ListItor itor = ListHead(list); for (i = 0; i < length; i++) { if (ListValue(itor) != numbers[i]) { sprintf(error, "Encounter wrong data [i:%d/%d] when walking from the head to the tail", i, length-1); return false; } itor = ListItorNext(itor); } if (!ListItorNull(itor)) { sprintf(error, "List has more elements at the end then numbers"); return false; } itor = ListTail(list); for (i = length-1; i >= 0; i--) { if (ListValue(itor) != numbers[i]) { sprintf(error, "Encounter wrong data [i:%d/%d] when walking from the tail to the head", i, length-1); return false; } itor = ListItorPrev(itor); } if (!ListItorNull(itor)) { sprintf(error, "List has more elements at the begining then numbers"); return false; } return true; }
static void lablist(Term t) { if(is_list(t)) { List l; for(l=t;l;l=ListTail(l)) { Term m=ListFirst(l); if(is_label(m) && !ListMember(labl,m)) labl=AppendFirst(labl,m); else lablist(m); } } if(is_compound(t)) { int i; for(i=1;i<=CompoundArity(t);i++) { Term m=CompoundArgN(t,i); if(is_label(m) && !ListMember(labl,m)) labl=AppendFirst(labl,m); else lablist(m); } } }
static void chlabs(Term t) { if(is_list(t)) { List l; for(l=t;l;l=ListTail(l)) { Term m=ListFirst(l); if(is_label(m)) ChangeList(l,ListNth(labm,ListMember(labl,m))); else chlabs(m); } } if(is_compound(t)) { int i; for(i=1;i<=CompoundArity(t);i++) { Term m=CompoundArgN(t,i); if(is_label(m)) SetCompoundArg(t,i,ListNth(labm,ListMember(labl,m))); else chlabs(m); } } }
int Delete( listADT list, listElementT element) { nodoCDT *ant, *rec; if( list ==NULL) return 0; rec = ant = list->nodos; // Me muevo x la lista comparando nodo a nodo while( rec != NULL && ( rec->dato < element) ) { ant = rec; rec = ListTail(rec); } // Si rec es null o la funcion de comparacion me dice que el siguiente // es mayor, el elemento no se encontro if (rec == NULL || (rec->dato != element) ) return 0; if ( ant == rec ) list->nodos = ant->tail; else ant->tail = rec->tail; if (rec == list->actual) list->actual = (rec->tail); free(rec); return 1; }
void alg2_decommon_s(Term a2) { Term cfl; List l; cfl=ConsumeCompoundArg(a2,3); if(is_empty_list(cfl)) return; for(l=CompoundArgN(a2,5);l;l=ListTail(l)) { List pfl; List l1,l2; pfl=ConsumeCompoundArg(ListFirst(l),2); for(l1=cfl;l1;l1=ListTail(l1)) { Atom p; p=CompoundArg1(ListFirst(l1)); for(l2=pfl;l2;l2=ListTail(l2)) { if(CompoundArg1(ListFirst(l2))==p) { int pw; pw=IntegerValue(CompoundArg2(ListFirst(l2))) +IntegerValue(CompoundArg2(ListFirst(l1))); SetCompoundArg(ListFirst(l2),2,NewInteger(pw)); break; } } if(is_empty_list(l2)) pfl=AppendFirst(pfl,CopyTerm(ListFirst(l1))); } rr: for(l1=pfl;l1;l1=ListTail(l1)) if(CompoundArg2(ListFirst(l1))==NewInteger(0)) { pfl=CutFromList(pfl,l1); goto rr; } pfl=SortedList(pfl,prtcmp); SetCompoundArg(ListFirst(l),2,pfl); } FreeAtomic(cfl); }
void alg1_fix_wild(Term a1) { List l1; l1=CompoundArg1(a1); while(!is_empty_list(l1)) { List l2; l2=CompoundArgN(ListFirst(l1),3); while(!is_empty_list(l2)) { if(CompoundName(ListFirst(l2))==OPR_WILD) alg1_fix_w1(ListFirst(l2)); l2=ListTail(l2); } l1=ListTail(l1); } }
static int ListLength(listADT list) { if (list == NULL) { return (0); } else { return (ListLength(ListTail(list)) + 1); } }
/* static int mlt_list1(List l) { int ret=1; while(!is_empty_list(l)) { if( is_empty_list(ListTail(l)) || !ListMember(ListTail(l),ListFirst(l))) ret*=IntegerValue(ListFirst(l)); l=ListTail(l); } return ret; } */ static int gcf_list(List l) { int ret; if(is_empty_list(l)) return 1; ret=IntegerValue(ListFirst(l)); if(ret<0) ret=-ret; l=ListTail(l); while(!is_empty_list(l)) { if(ret==1) return 1; ret=gcf(ret,IntegerValue(ListFirst(l))); l=ListTail(l); } return ret; }
static listADT ListConcat(listADT list1, listADT list2) { if (list1 == NULL) { return (list2); } else { return (ListCons(ListHead(list1), ListConcat(ListTail(list1), list2))); } }
static void *th_w(void *d) { List l1,fret=0, frete=0, ret=0,rete=0; int ind=*(int *)d; pthread_setspecific(TermsKey,&ind); begin: ret=NewList(); pthread_mutex_lock(&mtww); if(th_src==0) { *(List *)d=fret; pthread_mutex_unlock(&mtww); return 0; } ret=ListFirst(th_src); th_src=ListTail(th_src); pthread_mutex_unlock(&mtww); ret=AppendFirst(0,ret); l1=th_ww; while(!is_empty_list(l1)) { Term rrr; List l2; rrr=ret; ret=NewList(); rete=ret; l2=rrr; while(!is_empty_list(l2)) { List nn=alg1_s_w_m_1(ListFirst(l2),ListFirst(l1)); if(nn) { if(ret==0) {ret=nn;rete=ret;} else ListConcat(rete,nn); while(ListTail(rete)) rete=ListTail(rete); } /*ret=ConcatList(ret,nn);*/ l2=ListTail(l2); } /*printf(" %d ",ListLength(ret));fflush(stdout);*/ FreeAtomic2(rrr,ind); l1=ListTail(l1); } if(ret==0) goto begin; if(fret==0) {fret=ret; frete=fret;} else ConcatList(frete,ret); while(ListTail(frete)) frete=ListTail(frete); goto begin; }
static listElementT NthElement(listADT list, int n) { if (list == NULL) { Error("NthElement: No such element"); } else if (n == 0) { return (ListHead(list)); } else { return (NthElement(ListTail(list), n - 1)); } }
static void mult_no(List e1, int no) { while(!is_empty_list(e1)) { int v; v=IntegerValue(CompoundArg1(ListFirst(e1))); v*=no; SetCompoundArg(ListFirst(e1),1,NewInteger(v)); e1=ListTail(e1); } }
void renewlab(Term t) { List l; labl=0; labm=0; lablist(t); for(l=labl;l;l=ListTail(l)) labm=AppendFirst(labm,NewLabel()); chlabs(t); if(labl) {FreeAtomic(labl);FreeAtomic(labm);} }
static int mlt_list1(List l) { int ret=1; for(;l;l=ListTail(l)) { int i=IntegerValue(ListFirst(l)); i/=gcf(i,ret); ret*=i; } return ret; }
void alg1_sum_wild(Term a1) { List ww,l1,rl; List free_w; free_w=NewList(); rl=NewList(); l1=CompoundArg2(a1); while(!is_empty_list(l1)) { Term t1; t1=ListFirst(l1); if(CompoundName(t1)==OPR_WILD) free_w=AppendFirst(free_w,CompoundArg2(t1)); l1=ListTail(l1); } l1=CompoundArg1(a1); while(!is_empty_list(l1)) { Term t1; t1=ListFirst(l1); ww=alg1_w_ind(t1,free_w); /*WriteTerm(free_w);WriteTerm(ww);fflush(stdout);*/ if(is_empty_list(ww)) { /*WriteTerm(t1); puts(": pure");*/ rl=AppendLast(rl,t1); } else rl=ConcatList(rl,alg1_s_w_m(t1,ww)); l1=ListTail(l1); } RemoveList(free_w); l1=ConsumeCompoundArg(a1,1); RemoveList(l1); SetCompoundArg(a1,1,rl); }
static int inst_in(Term a, List ind) { int i=0; while(!is_empty_list(ind)) { Term il; il=CompoundArg1(ListFirst(ind)); if(equal_repres(il,a)) i++; ind=ListTail(ind); } return i; }
static Term multiply_l(Term t1,Term t2) { List l1,l2,ll; ll=NewList(); l1=t1; while(!is_empty_list(l1)) { l2=t2; while(!is_empty_list(l2)) { Term q1,q2; q1=CopyTerm(ListFirst(l1)); q2=CopyTerm(ListFirst(l2)); ll=AppendLast(ll,multiply(q1,q2)); l2=ListTail(l2); } l1=ListTail(l1); } FreeAtomic(t1); FreeAtomic(t2); return ll; }