source: git/kernel/tgb.cc @ 6e08ad

spielwiese
Last change on this file since 6e08ad was 6e08ad, checked in by Michael Brickenstein <bricken@…>, 18 years ago
+ improved initialization process git-svn-id: file:///usr/local/Singular/svn/trunk@9464 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 81.1 KB
Line 
1//! \file tgb.cc
2//       multiple rings
3//       shorten_tails und dessen Aufrufe pruefen wlength!!!
4/****************************************
5*  Computer Algebra System SINGULAR     *
6****************************************/
7/* $Id: tgb.cc,v 1.104 2006-10-25 10:14:51 bricken Exp $ */
8/*
9* ABSTRACT: slimgb and F4 implementation
10*/
11//#include <vector>
12//using namespace std;
13#include "mod2.h"
14#include "tgb.h"
15#include "tgb_internal.h"
16#include "tgbgauss.h"
17#include "F4.h"
18#include "digitech.h"
19#include "gring.h"
20
21#include "longrat.h"
22#define SR_HDL(A) ((long)(A))
23static const int bundle_size=100;
24static const int delay_factor=3;
25int QlogSize(number n);
26#define ADD_LATER_SIZE 500
27#if 1
28static omBin lm_bin=NULL;
29
30static void simplify_poly(poly p, ring r) {
31     assume(r==currRing);
32     if (!rField_is_Zp(r))
33     {
34        pCleardenom(p);
35        pContent(p); //is a duplicate call, but belongs here
36     }
37     else
38       pNorm(p);
39}
40//static const BOOLEAN up_to_radical=TRUE;
41
42int slim_nsize(number n, ring r)
43{
44  if (rField_is_Zp(r))
45  {
46    return 1;
47  }
48  if (rField_is_Q(r))
49  {
50    return QlogSize(n);
51  }
52  else
53  {
54    return n_Size(n,r);
55  }
56}
57static BOOLEAN monomial_root(poly m, ring r){
58    BOOLEAN changed=FALSE;
59    int i;
60    for(i=1;i<=rVar(r);i++){
61        int e=p_GetExp(m,i,r);
62        if (e>1){
63            p_SetExp(m,i,1,r);
64            changed=TRUE;
65        }
66    }
67    if (changed) {
68        p_Setm(m,r);
69    }
70    return changed;
71}
72static BOOLEAN polynomial_root(poly h, ring r){
73  poly got=gcd_of_terms(h,r);
74  BOOLEAN changed=FALSE;
75  if((got!=NULL) &&(TEST_V_UPTORADICAL)) {
76    poly copy=p_Copy(got,r);
77    //p_wrp(got,c->r);
78    changed=monomial_root(got,r);
79    if (changed)
80    {
81         poly div_by=pDivide(copy, got);
82         poly iter=h;
83         while(iter){
84            pExpVectorSub(iter,div_by);
85            pIter(iter);
86         }
87         p_Delete(&div_by, r);
88         if (TEST_OPT_PROT)
89             PrintS("U");
90    }
91    p_Delete(&copy,r);
92  }
93  p_Delete(&got,r);
94  return changed;
95}
96static inline poly p_Init_Special(const ring r)
97{
98  return p_Init(r,lm_bin);
99}
100static inline poly pOne_Special(const ring r=currRing)
101{
102  poly rc = p_Init_Special(r);
103  pSetCoeff0(rc,r->cf->nInit(1));
104  return rc;
105}
106// zum Initialiseren: in t_rep_gb plazieren:
107
108#endif
109#define LEN_VAR3
110#define degbound(p) assume(pTotaldegree(p)<10)
111//#define inDebug(p) assume((debug_Ideal==NULL)||(kNF(debug_Ideal,NULL,p,0,0)==0))
112
113//die meisten Varianten stossen sich an coef_buckets
114
115
116
117#ifdef LEN_VAR1
118// erste Variante: Laenge: Anzahl der Monome
119inline int pSLength(poly p, int l) {
120  return l; }
121inline int kSBucketLength(kBucket* bucket, poly lm) {return bucket_guess(bucket);}
122#endif
123
124#ifdef LEN_VAR2
125// 2. Variante: Laenge: Platz fuer die Koeff.
126int pSLength(poly p,int l)
127{
128  int s=0;
129  while (p!=NULL) { s+=nSize(pGetCoeff(p));pIter(p); }
130  return s;
131}
132int kSBucketLength(kBucket* b, poly lm)
133{
134  int s=0;
135  int i;
136  for (i=MAX_BUCKET;i>=0;i--)
137  {
138    s+=pSLength(b->buckets[i],0);
139  }
140  return s;
141}
142#endif
143
144
145
146
147
148
149int QlogSize(number n){
150
151    if (SR_HDL(n) & SR_INT){
152       long i=SR_TO_INT(n);
153       if (i==0) return 0;
154
155       unsigned long v;
156       v=(i>=0)?i:-i;
157       int r = 0;
158
159       while (v >>= 1)
160       {
161        r++;
162       }
163       return r+1;
164    }
165    //assume denominator is 0
166    return mpz_sizeinbase(&n->z,2);
167}
168
169
170#ifdef LEN_VAR3
171inline wlen_type pSLength(poly p,int l)
172{
173  wlen_type c;
174  number coef=pGetCoeff(p);
175  if (rField_is_Q(currRing)){
176    c=QlogSize(coef);
177  }
178  else
179    c=nSize(coef);
180  if (!(TEST_V_COEFSTRAT))
181      return (wlen_type)c*(wlen_type)l /*pLength(p)*/;
182  else {
183    wlen_type res=l;
184    res*=c;
185    res*=c;
186    return res;
187  }
188}
189//! TODO CoefBuckets berücksichtigen
190wlen_type kSBucketLength(kBucket* b, poly lm=NULL)
191{
192  int s=0;
193  wlen_type c;
194  number coef;
195  if(lm==NULL)
196    coef=pGetCoeff(kBucketGetLm(b));
197    //c=nSize(pGetCoeff(kBucketGetLm(b)));
198  else
199    coef=pGetCoeff(lm);
200    //c=nSize(pGetCoeff(lm));
201  if (rField_is_Q(currRing)){
202    c=QlogSize(coef);
203  }
204  else
205    c=nSize(coef);
206
207  int i;
208  for (i=b->buckets_used;i>=0;i--)
209  {
210    assume((b->buckets_length[i]==0)||(b->buckets[i]!=NULL));
211    s+=b->buckets_length[i] /*pLength(b->buckets[i])*/;
212  }
213  #ifdef HAVE_COEF_BUCKETS
214  assume(b->buckets[0]==kBucketGetLm(b));
215  if (b->coef[0]!=NULL){
216
217    if (rField_is_Q(currRing)){
218      int modifier=QlogSize(pGetCoeff(b->coef[0]));
219      c+=modifier;
220  }
221    else{
222      int modifier=nSize(pGetCoeff(b->coef[0]));
223      c*=modifier;}
224    }
225  #endif
226  if (!(TEST_V_COEFSTRAT)){
227  return s*c;
228  } else
229  {
230    wlen_type res=s;
231    res*=c;
232    res*=c;
233    return res;
234  }
235}
236#endif
237#ifdef LEN_VAR5
238inline wlen_type pSLength(poly p,int l)
239{
240  int c;
241  number coef=pGetCoeff(p);
242  if (rField_is_Q(currRing)){
243    c=QlogSize(coef);
244  }
245  else
246    c=nSize(coef);
247  wlen_type erg=l;
248  erg*=c;
249  erg*=c;
250  //PrintS("lenvar 5");
251  assume(erg>=0);
252  return erg; /*pLength(p)*/;
253}
254//! TODO CoefBuckets berücksichtigen
255wlen_type kSBucketLength(kBucket* b, poly lm=NULL)
256{
257  wlen_type s=0;
258  int c;
259  number coef;
260  if(lm==NULL)
261    coef=pGetCoeff(kBucketGetLm(b));
262    //c=nSize(pGetCoeff(kBucketGetLm(b)));
263  else
264    coef=pGetCoeff(lm);
265    //c=nSize(pGetCoeff(lm));
266  if (rField_is_Q(currRing)){
267    c=QlogSize(coef);
268  }
269  else
270    c=nSize(coef);
271
272  int i;
273  for (i=b->buckets_used;i>=0;i--)
274  {
275    assume((b->buckets_length[i]==0)||(b->buckets[i]!=NULL));
276    s+=b->buckets_length[i] /*pLength(b->buckets[i])*/;
277  }
278  #ifdef HAVE_COEF_BUCKETS
279  assume(b->buckets[0]==kBucketGetLm(b));
280  if (b->coef[0]!=NULL){
281
282    if (rField_is_Q(currRing)){
283      int modifier=QlogSize(pGetCoeff(b->coef[0]));
284      c+=modifier;
285  }
286    else{
287      int modifier=nSize(pGetCoeff(b->coef[0]));
288      c*=modifier;}
289    }
290  #endif
291  wlen_type erg=s;
292  erg*=c;
293  erg*=c;
294  return erg;
295}
296#endif
297
298#ifdef LEN_VAR4
299// 4.Variante: Laenge: Platz fuer Leitk * (1+Platz fuer andere Koeff.)
300int pSLength(poly p, int l)
301{
302  int s=1;
303  int c=nSize(pGetCoeff(p));
304  pIter(p);
305  while (p!=NULL) { s+=nSize(pGetCoeff(p));pIter(p); }
306  return s*c;
307}
308int kSBucketLength(kBucket* b)
309{
310  int s=1;
311  int c=nSize(pGetCoeff(kBucketGetLm(b)));
312  int i;
313  for (i=MAX_BUCKET;i>0;i--)
314  {
315    if(b->buckets[i]==NULL) continue;
316    s+=pSLength(b->buckets[i],0);
317  }
318  return s*c;
319}
320#endif
321//BUG/TODO this stuff will fail on internal Schreyer orderings
322static BOOLEAN elength_is_normal_length(poly p, slimgb_alg* c){
323    ring r=c->r;
324    if (p_GetComp(p,r)!=0) return FALSE;
325    if (c->lastDpBlockStart<=pVariables){
326        int i;
327        for(i=1;i<=pVariables;i++){
328            if (p_GetExp(p,i,r)!=0){
329                break;
330            }
331        }
332        if (i>=c->lastDpBlockStart) {
333        //wrp(p);
334        //PrintS("\n");
335        return TRUE;
336        }
337        else return FALSE;
338    }else 
339    return FALSE;
340}
341static BOOLEAN get_last_dp_block_start(ring r){
342    //ring r=c->r;
343    int last_block;
344   
345    if (rRing_has_CompLastBlock(r)){
346        last_block=rBlocks(r) - 3;
347    }
348    else {last_block=rBlocks(r)-2;}
349    assume(last_block>=0);
350    if (r->order[last_block]==ringorder_dp)
351        return r->block0[last_block];
352    return pVariables+1;
353
354}
355
356static wlen_type do_pELength(poly p, slimgb_alg* c, int dlm=-1){
357
358  if(p==NULL) return 0;
359  wlen_type s=0;
360  poly pi=p;
361  if(dlm<0){
362    dlm=pTotaldegree(p,c->r);
363    s=1;
364    pi=p->next;
365  }
366
367  while(pi){
368    int d=pTotaldegree(pi,c->r);
369    if(d>dlm)
370      s+=1+d-dlm;
371    else
372      ++s;
373    pi=pi->next;
374  }
375  return s;
376}
377
378wlen_type pELength(poly p, ring r){
379  if(p==NULL) return 0;
380  wlen_type s=0;
381  poly pi=p;
382  int dlm;
383    dlm=pTotaldegree(p,r);
384    s=1;
385    pi=p->next;
386
387
388  while(pi){
389    int d=pTotaldegree(pi,r);
390    if(d>dlm)
391      s+=1+d-dlm;
392    else
393      ++s;
394    pi=pi->next;
395  }
396  return s;
397}
398
399wlen_type kEBucketLength(kBucket* b, poly lm,slimgb_alg* ca)
400{
401  wlen_type s=0;
402  if(lm==NULL){
403    lm=kBucketGetLm(b);
404  }
405  if(lm==NULL) return 0;
406  if(elength_is_normal_length(lm,ca)) {
407    return bucket_guess(b);
408  }
409  int d=pTotaldegree(lm,ca->r);
410  int i;
411  for (i=b->buckets_used;i>=0;i--)
412  {
413    if(b->buckets[i]==NULL) continue;
414   
415    if ((pTotaldegree(b->buckets[i])<=d) &&(elength_is_normal_length(b->buckets[i],ca))){
416        s+=b->buckets_length[i];
417    } else
418    {
419    s+=do_pELength(b->buckets[i],ca,d);
420    }
421  }
422  return s;
423}
424
425static inline int pELength(poly p, slimgb_alg* c,int l){
426  if (p==NULL) return 0;
427  if ((l>0) &&(elength_is_normal_length(p,c)))
428    return l;
429  return do_pELength(p,c);
430}
431
432
433
434
435static inline wlen_type pQuality(poly p, slimgb_alg* c, int l=-1){
436
437  if(l<0)
438    l=pLength(p);
439  if(c->isDifficultField) {
440    if(c->eliminationProblem){
441      wlen_type cs;
442      number coef=pGetCoeff(p);
443      if (rField_is_Q(currRing)){
444         cs=QlogSize(coef);
445      }
446      else
447        cs=nSize(coef);
448     wlen_type erg=cs;
449     if(TEST_V_COEFSTRAT)
450        erg*=cs;
451     //erg*=cs;//for quadratic
452     erg*=pELength(p,c,l);
453    //FIXME: not quadratic coeff size
454      //return cs*pELength(p,c,l);
455      return erg;
456    }
457    //Print("I am here");
458    wlen_type r=pSLength(p,l);
459    assume(r>=0);
460    return r;
461  }
462  if(c->eliminationProblem) return pELength(p,c,l);
463  return l;
464}
465
466static inline int pTotaldegree_full(poly p){
467  int r=0;
468  while(p){
469    int d=pTotaldegree(p);
470    r=si_max(r,d);
471    pIter(p);
472  }
473  return r;
474}
475
476wlen_type red_object::guess_quality(slimgb_alg* c){
477    //works at the moment only for lenvar 1, because in different
478    //case, you have to look on coefs
479    wlen_type s=0;
480    if (c->isDifficultField){
481      //s=kSBucketLength(bucket,this->p);
482      if(c->eliminationProblem){
483    wlen_type cs;
484    number coef;
485
486    coef=pGetCoeff(kBucketGetLm(bucket));
487    //c=nSize(pGetCoeff(kBucketGetLm(b)));
488
489    //c=nSize(pGetCoeff(lm));
490    if (rField_is_Q(currRing)){
491      cs=QlogSize(coef);
492    }
493    else
494      cs=nSize(coef);
495    #ifdef HAVE_COEF_BUCKETS
496    if (bucket->coef[0]!=NULL){
497      if (rField_is_Q(currRing)){
498        int modifier=QlogSize(pGetCoeff(bucket->coef[0]));
499        cs+=modifier;
500      }
501      else{
502        int modifier=nSize(pGetCoeff(bucket->coef[0]));
503        cs*=modifier;}
504    }
505    #endif
506    //FIXME:not quadratic
507    wlen_type erg=kEBucketLength(this->bucket,this->p,c);
508    //erg*=cs;//for quadratic
509    erg*=cs;
510    if (TEST_V_COEFSTRAT)
511        erg*=cs;
512    //return cs*kEBucketLength(this->bucket,this->p,c);
513    return erg;
514      }
515      s=kSBucketLength(bucket,NULL);
516    }
517    else
518    {
519      if(c->eliminationProblem)
520  //if (false)
521  s=kEBucketLength(this->bucket,this->p,c);
522      else s=bucket_guess(bucket);
523    }
524
525    return s;
526}
527
528
529
530static void finalize_reduction_step(reduction_step* r){
531  delete r;
532}
533static int LObject_better_gen(const void* ap, const void* bp)
534{
535  LObject* a=*(LObject**)ap;
536  LObject* b=*(LObject**)bp;
537  return(pLmCmp(a->p,b->p));
538}
539static int red_object_better_gen(const void* ap, const void* bp)
540{
541
542
543  return(pLmCmp(((red_object*) ap)->p,((red_object*) bp)->p));
544}
545
546
547static int pLmCmp_func_inverted(const void* ap1, const void* ap2){
548    poly p1,p2;
549  p1=*((poly*) ap1);
550  p2=*((poly*)ap2);
551
552  return -pLmCmp(p1,p2);
553}
554
555int tgb_pair_better_gen2(const void* ap,const void* bp){
556  return(-tgb_pair_better_gen(ap,bp));
557}
558int kFindDivisibleByInS_easy(kStrategy strat,const red_object & obj){
559  int i;
560  long not_sev=~obj.sev;
561  poly p=obj.p;
562  for(i=0;i<=strat->sl;i++){
563    if (pLmShortDivisibleBy(strat->S[i],strat->sevS[i],p,not_sev))
564      return i;
565  }
566  return -1;
567}
568int kFindDivisibleByInS_easy(kStrategy strat,poly p, long sev){
569  int i;
570  long not_sev=~sev;
571  for(i=0;i<=strat->sl;i++){
572    if (pLmShortDivisibleBy(strat->S[i],strat->sevS[i],p,not_sev))
573      return i;
574  }
575  return -1;
576}
577static int posInPairs (sorted_pair_node**  p, int pn, sorted_pair_node* qe,slimgb_alg* c,int an=0)
578{
579  if(pn==0) return 0;
580
581  int length=pn-1;
582  int i;
583  //int an = 0;
584  int en= length;
585
586  if (pair_better(qe,p[en],c))
587    return length+1;
588
589  while(1)
590    {
591      //if (an >= en-1)
592      if(en-1<=an)
593      {
594        if (pair_better(p[an],qe,c)) return an;
595        return en;
596      }
597      i=(an+en) / 2;
598        if (pair_better(p[i],qe,c))
599          en=i;
600      else an=i;
601    }
602}
603
604static BOOLEAN  ascending(int* i,int top){
605  if(top<1) return TRUE;
606  if(i[top]<i[top-1]) return FALSE;
607  return ascending(i,top-1);
608}
609
610sorted_pair_node**  spn_merge(sorted_pair_node** p, int pn,sorted_pair_node **q, int qn,slimgb_alg* c){
611  int i;
612  int* a= (int*) omalloc(qn*sizeof(int));
613//   int mc;
614//   PrintS("Debug\n");
615//   for(mc=0;mc<qn;mc++)
616// {
617
618//     wrp(q[mc]->lcm_of_lm);
619//     PrintS("\n");
620// }
621//    PrintS("Debug they are in\n");
622//   for(mc=0;mc<pn;mc++)
623// {
624
625//     wrp(p[mc]->lcm_of_lm);
626//     PrintS("\n");
627// }
628  int lastpos=0;
629  for(i=0;i<qn;i++){
630    lastpos=posInPairs(p,pn,q[i],c, si_max(lastpos-1,0));
631    //   cout<<lastpos<<"\n";
632    a[i]=lastpos;
633
634  }
635  if((pn+qn)>c->max_pairs){
636    p=(sorted_pair_node**) omrealloc(p,2*(pn+qn)*sizeof(sorted_pair_node*));
637    c->max_pairs=2*(pn+qn);
638  }
639  for(i=qn-1;i>=0;i--){
640    size_t size;
641    if(qn-1>i)
642      size=(a[i+1]-a[i])*sizeof(sorted_pair_node*);
643    else
644      size=(pn-a[i])*sizeof(sorted_pair_node*); //as indices begin with 0
645    memmove (p+a[i]+(1+i), p+a[i], size);
646    p[a[i]+i]=q[i];
647  }
648  omfree(a);
649  return p;
650}
651
652
653static BOOLEAN trivial_syzygie(int pos1,int pos2,poly bound,slimgb_alg* c){
654
655
656  poly p1=c->S->m[pos1];
657  poly p2=c->S->m[pos2];
658
659
660  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
661    return FALSE;
662  int i = 1;
663  poly m=NULL;
664  poly gcd1=c->gcd_of_terms[pos1];
665  poly gcd2=c->gcd_of_terms[pos2];
666
667  if((gcd1!=NULL) && (gcd2!=NULL))
668    {
669      gcd1->next=gcd2; //may ordered incorrect
670      m=gcd_of_terms(gcd1,c->r);
671      gcd1->next=NULL;
672
673    }
674
675  if (m==NULL)
676  {
677     loop
678      {
679  if (pGetExp(p1, i)+ pGetExp(p2, i) > pGetExp(bound,i))   return FALSE;
680  if (i == pVariables){
681    //PrintS("trivial");
682    return TRUE;
683  }
684  i++;
685      }
686  }
687  else
688  {
689    loop
690      {
691  if (pGetExp(p1, i)-pGetExp(m,i) + pGetExp(p2, i) > pGetExp(bound,i))   return FALSE;
692  if (i == pVariables){
693    pDelete(&m);
694    //PrintS("trivial");
695    return TRUE;
696  }
697  i++;
698      }
699  }
700
701
702
703
704}
705
706//! returns position sets w as weight
707int find_best(red_object* r,int l, int u, wlen_type &w, slimgb_alg* c){
708  int best=l;
709  int i;
710  w=r[l].guess_quality(c);
711  for(i=l+1;i<=u;i++){
712    wlen_type w2=r[i].guess_quality(c);
713    if(w2<w){
714      w=w2;
715      best=i;
716    }
717
718  }
719 return best;
720}
721
722
723void red_object::canonicalize(){
724  kBucketCanonicalize(bucket);
725
726
727}
728BOOLEAN good_has_t_rep(int i, int j,slimgb_alg* c){
729  assume(i>=0);
730    assume(j>=0);
731  if (has_t_rep(i,j,c)) return TRUE;
732  //poly lm=pOne();
733  assume (c->tmp_lm!=NULL);
734  poly lm=c->tmp_lm;
735
736  pLcm(c->S->m[i], c->S->m[j], lm);
737  pSetm(lm);
738  assume(lm!=NULL);
739  //int deciding_deg= pTotaldegree(lm);
740  int* i_con =make_connections(i,j,lm,c);
741  //p_Delete(&lm,c->r);
742
743
744  for (int n=0;((n<c->n) && (i_con[n]>=0));n++){
745    if (i_con[n]==j){
746      now_t_rep(i,j,c);
747      omfree(i_con);
748
749      return TRUE;
750    }
751  }
752  omfree(i_con);
753
754  return FALSE;
755}
756BOOLEAN lenS_correct(kStrategy strat){
757  int i;
758  for(i=0;i<=strat->sl;i++){
759    if (strat->lenS[i]!=pLength(strat->S[i]))
760      return FALSE;
761  }
762  return TRUE;
763}
764
765
766static void cleanS(kStrategy strat, slimgb_alg* c){
767  int i=0;
768  LObject P;
769  while(i<=strat->sl)
770  {
771    P.p=strat->S[i];
772    P.sev=strat->sevS[i];
773    int dummy=strat->sl;
774    //if(kFindDivisibleByInS(strat,&dummy,&P)!=i)
775    if (kFindDivisibleByInS_easy(strat,P.p,P.sev)!=i)
776    {
777      deleteInS(i,strat);
778      //remember destroying poly
779      BOOLEAN found=FALSE;
780      int j;
781      for(j=0;j<c->n;j++)
782      {
783        if(c->S->m[j]==P.p)
784        {
785          found=TRUE;
786          break;
787        }
788      }
789      if (!found)
790  pDelete(&P.p);
791      //remember additional reductors
792    }
793    else i++;
794  }
795}
796static int bucket_guess(kBucket* bucket){
797  int sum=0;
798  int i;
799  for (i=bucket->buckets_used;i>=0;i--){
800    if(bucket->buckets[i])
801       sum+=bucket->buckets_length[i];
802  }
803  return sum;
804}
805
806
807
808
809
810
811static int add_to_reductors(slimgb_alg* c, poly h, int len, BOOLEAN simplified){
812  //inDebug(h);
813  assume(lenS_correct(c->strat));
814  assume(len==pLength(h));
815  int i;
816//   if (c->isDifficultField)
817//        i=simple_posInS(c->strat,h,pSLength(h,len),c->isDifficultField);
818//   else
819//     i=simple_posInS(c->strat,h,len,c->isDifficultField);
820
821  LObject P; memset(&P,0,sizeof(P));
822  P.tailRing=c->r;
823  P.p=h; /*p_Copy(h,c->r);*/
824  P.FDeg=pFDeg(P.p,c->r);
825  if (!(simplified)){
826      if (!rField_is_Zp(c->r)){
827        pCleardenom(P.p);
828        pContent(P.p); //is a duplicate call, but belongs here
829
830      }
831      else
832        pNorm(P.p);
833    pNormalize(P.p);
834  }
835  wlen_type pq=pQuality(h,c,len);
836  i=simple_posInS(c->strat,h,len,pq);
837  c->strat->enterS(P,i,c->strat,-1);
838
839
840
841  c->strat->lenS[i]=len;
842  assume(pLength(c->strat->S[i])==c->strat->lenS[i]);
843  if(c->strat->lenSw!=NULL)
844    c->strat->lenSw[i]=pq;
845
846  return i;
847
848}
849static void length_one_crit(slimgb_alg* c, int pos, int len)
850{
851  if (c->nc)
852    return;
853  if (len==1)
854  {
855    int i;
856    for ( i=0;i<pos;i++)
857    {
858      if (c->lengths[i]==1)
859        c->states[pos][i]=HASTREP;
860    }
861    for ( i=pos+1;i<c->n;i++){
862      if (c->lengths[i]==1)
863        c->states[i][pos]=HASTREP;
864    }
865    if (!c->nc)
866      shorten_tails(c,c->S->m[pos]);
867  }
868}
869
870
871static void move_forward_in_S(int old_pos, int new_pos,kStrategy strat)
872{
873  assume(old_pos>=new_pos);
874  poly p=strat->S[old_pos];
875  int ecart=strat->ecartS[old_pos];
876  long sev=strat->sevS[old_pos];
877  int s_2_r=strat->S_2_R[old_pos];
878  int length=strat->lenS[old_pos];
879  assume(length==pLength(strat->S[old_pos]));
880  wlen_type length_w;
881  if(strat->lenSw!=NULL)
882    length_w=strat->lenSw[old_pos];
883  int i;
884  for (i=old_pos; i>new_pos; i--)
885  {
886    strat->S[i] = strat->S[i-1];
887    strat->ecartS[i] = strat->ecartS[i-1];
888    strat->sevS[i] = strat->sevS[i-1];
889    strat->S_2_R[i] = strat->S_2_R[i-1];
890  }
891  if (strat->lenS!=NULL)
892    for (i=old_pos; i>new_pos; i--)
893      strat->lenS[i] = strat->lenS[i-1];
894  if (strat->lenSw!=NULL)
895    for (i=old_pos; i>new_pos; i--)
896      strat->lenSw[i] = strat->lenSw[i-1];
897
898  strat->S[new_pos]=p;
899  strat->ecartS[new_pos]=ecart;
900  strat->sevS[new_pos]=sev;
901  strat->S_2_R[new_pos]=s_2_r;
902  strat->lenS[new_pos]=length;
903  if(strat->lenSw!=NULL)
904    strat->lenSw[new_pos]=length_w;
905  //assume(lenS_correct(strat));
906}
907
908static int* make_connections(int from, int to, poly bound, slimgb_alg* c)
909{
910  ideal I=c->S;
911  int* cans=(int*) omalloc(c->n*sizeof(int));
912  int* connected=(int*) omalloc(c->n*sizeof(int));
913  cans[0]=to;
914  int cans_length=1;
915  connected[0]=from;
916  int last_cans_pos=-1;
917  int connected_length=1;
918  long neg_bounds_short= ~p_GetShortExpVector(bound,c->r);
919
920  int not_yet_found=cans_length;
921  int con_checked=0;
922  int pos;
923
924  while(TRUE){
925    if ((con_checked<connected_length)&& (not_yet_found>0)){
926      pos=connected[con_checked];
927      for(int i=0;i<cans_length;i++){
928        if (cans[i]<0) continue;
929        //FIXME: triv. syz. does not hold on noncommutative, check it for modules
930        if ((has_t_rep(pos,cans[i],c)) ||((!rIsPluralRing(c->r))&&(trivial_syzygie(pos,cans[i],bound,c))))
931{
932
933          connected[connected_length]=cans[i];
934          connected_length++;
935          cans[i]=-1;
936          --not_yet_found;
937
938          if (connected[connected_length-1]==to){
939            if (connected_length<c->n){
940              connected[connected_length]=-1;
941            }
942            omfree(cans);
943            return connected;
944          }
945        }
946      }
947      con_checked++;
948    }
949    else
950    {
951      for(last_cans_pos++;last_cans_pos<=c->n;last_cans_pos++){
952        if (last_cans_pos==c->n){
953          if (connected_length<c->n){
954            connected[connected_length]=-1;
955          }
956          omfree(cans);
957          return connected;
958        }
959        if ((last_cans_pos==from)||(last_cans_pos==to))
960          continue;
961        if(p_LmShortDivisibleBy(I->m[last_cans_pos],c->short_Exps[last_cans_pos],bound,neg_bounds_short,c->r)){
962          cans[cans_length]=last_cans_pos;
963          cans_length++;
964          break;
965        }
966      }
967      not_yet_found++;
968      for (int i=0;i<con_checked;i++){
969        if (has_t_rep(connected[i],last_cans_pos,c)){
970
971          connected[connected_length]=last_cans_pos;
972          connected_length++;
973          cans[cans_length-1]=-1;
974
975          --not_yet_found;
976          if (connected[connected_length-1]==to){
977            if (connected_length<c->n){
978              connected[connected_length]=-1;
979            }
980
981            omfree(cans);
982            return connected;
983          }
984          break;
985        }
986      }
987    }
988  }
989  if (connected_length<c->n){
990    connected[connected_length]=-1;
991  }
992
993  omfree(cans);
994  return connected;
995}
996#ifdef HEAD_BIN
997static inline poly p_MoveHead(poly p, omBin b)
998{
999  poly np;
1000  omTypeAllocBin(poly, np, b);
1001  memmove(np, p, b->sizeW*sizeof(long));
1002  omFreeBinAddr(p);
1003  return np;
1004}
1005#endif
1006
1007static void replace_pair(int & i, int & j,slimgb_alg* c)
1008{
1009  if (i<0) return;
1010  c->soon_free=NULL;
1011  int syz_deg;
1012  poly lm=pOne();
1013
1014  pLcm(c->S->m[i], c->S->m[j], lm);
1015  pSetm(lm);
1016
1017  int* i_con =make_connections(i,j,lm,c);
1018
1019  for (int n=0;((n<c->n) && (i_con[n]>=0));n++){
1020    if (i_con[n]==j){
1021      now_t_rep(i,j,c);
1022      omfree(i_con);
1023      p_Delete(&lm,c->r);
1024      return;
1025    }
1026  }
1027
1028  int* j_con =make_connections(j,i,lm,c);
1029
1030//   if(c->n>1){
1031//     if (i_con[1]>=0)
1032//       i=i_con[1];
1033//     else {
1034//       if (j_con[1]>=0)
1035//         j=j_con[1];
1036//     }
1037 // }
1038
1039  int sugar=pTotaldegree(lm);
1040  p_Delete(&lm, c->r);
1041    if(c->T_deg_full)//Sugar
1042    {
1043      int t_i=c->T_deg_full[i]-c->T_deg[i];
1044      int t_j=c->T_deg_full[j]-c->T_deg[j];
1045      sugar+=si_max(t_i,t_j);
1046      //Print("\n max: %d\n",max(t_i,t_j));
1047    }
1048
1049
1050
1051
1052
1053  for (int m=0;((m<c->n) && (i_con[m]>=0));m++){
1054    if(c->T_deg_full!=NULL){
1055        int s1=c->T_deg_full[i_con[m]]+syz_deg-c->T_deg[i_con[m]];
1056        if (s1>sugar) continue;
1057    }
1058    if (c->weighted_lengths[i_con[m]]<c->weighted_lengths[i])
1059        i=i_con[m];
1060    }
1061    for (int m=0;((m<c->n) && (j_con[m]>=0));m++){
1062        if (c->T_deg_full!=NULL){
1063        int s1=c->T_deg_full[j_con[m]]+syz_deg-c->T_deg[j_con[m]];
1064        if (s1>sugar) continue;}
1065        if (c->weighted_lengths[j_con[m]]<c->weighted_lengths[j])
1066            j=j_con[m];
1067    }
1068
1069     //can also try dependend search
1070  omfree(i_con);
1071  omfree(j_con);
1072  return;
1073}
1074
1075
1076static void add_later(poly p, char* prot, slimgb_alg* c){
1077    int i=0;
1078    //check, if it is already in the queue
1079
1080
1081    while(c->add_later->m[i]!=NULL){
1082        if (p_LmEqual(c->add_later->m[i],p,c->r))
1083            return;
1084        i++;
1085    }
1086    if (TEST_OPT_PROT)
1087        PrintS(prot);
1088    c->add_later->m[i]=p;
1089}
1090static int simple_posInS (kStrategy strat, poly p,int len, wlen_type wlen)
1091{
1092
1093
1094  if(strat->sl==-1) return 0;
1095  if (strat->lenSw) return pos_helper(strat,p,(wlen_type) wlen,(wlen_set) strat->lenSw,strat->S);
1096  return pos_helper(strat,p,len,strat->lenS,strat->S);
1097
1098}
1099
1100/*2
1101 *if the leading term of p
1102 *divides the leading term of some S[i] it will be canceled
1103 */
1104static inline void clearS (poly p, unsigned long p_sev,int l, int* at, int* k,
1105                           kStrategy strat)
1106{
1107  assume(p_sev == pGetShortExpVector(p));
1108  if (!pLmShortDivisibleBy(p,p_sev, strat->S[*at], ~ strat->sevS[*at])) return;
1109  if (l>=strat->lenS[*at]) return;
1110  if (TEST_OPT_PROT)
1111    PrintS("!");
1112  mflush();
1113  //pDelete(&strat->S[*at]);
1114  deleteInS((*at),strat);
1115  (*at)--;
1116  (*k)--;
1117//  assume(lenS_correct(strat));
1118}
1119
1120
1121
1122static int iq_crit(const void* ap,const void* bp){
1123
1124  sorted_pair_node* a=*((sorted_pair_node**)ap);
1125  sorted_pair_node* b=*((sorted_pair_node**)bp);
1126  assume(a->i>a->j);
1127  assume(b->i>b->j);
1128
1129
1130  if (a->deg<b->deg) return -1;
1131  if (a->deg>b->deg) return 1;
1132  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
1133  if(comp!=0)
1134    return comp;
1135  if (a->expected_length<b->expected_length) return -1;
1136  if (a->expected_length>b->expected_length) return 1;
1137  if (a->j>b->j) return 1;
1138  if (a->j<b->j) return -1;
1139  return 0;
1140}
1141static wlen_type coeff_mult_size_estimate(int s1, int s2, ring r){
1142    if (rField_is_Q(r)) return s1+s2;
1143    else return s1*s2;
1144}
1145static wlen_type pair_weighted_length(int i, int j, slimgb_alg* c){
1146    if ((c->isDifficultField) && (c->eliminationProblem))  {
1147        int c1=slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r);
1148        int c2=slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r);
1149        wlen_type el1=c->weighted_lengths[i]/c1;
1150        assume(el1!=0);
1151        assume(c->weighted_lengths[i] %c1==0);
1152        wlen_type el2=c->weighted_lengths[j]/c2;
1153        assume(el2!=0);
1154        assume(c->weighted_lengths[j] %c2==0);
1155        //should be * for function fields
1156        //return (c1+c2) * (el1+el2-2);
1157        wlen_type res=coeff_mult_size_estimate(c1,c2,c->r);
1158        res*=el1+el2-2;
1159        return res;
1160
1161    }
1162    if (c->isDifficultField) {
1163        //int cs=slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r)+
1164        //    slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r);
1165        if(!(TEST_V_COEFSTRAT)){
1166        wlen_type cs=
1167            coeff_mult_size_estimate(
1168                slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r),
1169                slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r),c->r);
1170        return (wlen_type)(c->lengths[i]+c->lengths[j]-2)*
1171            (wlen_type)cs;}
1172            else {
1173
1174            wlen_type cs=
1175            coeff_mult_size_estimate(
1176                slim_nsize(p_GetCoeff(c->S->m[i],c->r),c->r),
1177                slim_nsize(p_GetCoeff(c->S->m[j],c->r),c->r),c->r);
1178            cs*=cs;
1179        return (wlen_type)(c->lengths[i]+c->lengths[j]-2)*
1180            (wlen_type)cs;
1181            }
1182    }
1183    if (c->eliminationProblem) {
1184
1185        return (c->weighted_lengths[i]+c->weighted_lengths[j]-2);
1186    }
1187    return c->lengths[i]+c->lengths[j]-2;
1188
1189}
1190sorted_pair_node** add_to_basis_ideal_quotient(poly h, slimgb_alg* c, int* ip)
1191{
1192
1193  assume(h!=NULL);
1194  poly got=gcd_of_terms(h,c->r);
1195  if((got!=NULL) &&(TEST_V_UPTORADICAL)) {
1196    poly copy=p_Copy(got,c->r);
1197    //p_wrp(got,c->r);
1198    BOOLEAN changed=monomial_root(got,c->r);
1199    if (changed)
1200    {
1201         poly div_by=pDivide(copy, got);
1202         poly iter=h;
1203         while(iter){
1204            pExpVectorSub(iter,div_by);
1205            pIter(iter);
1206         }
1207         p_Delete(&div_by, c->r);
1208         PrintS("U");
1209    }
1210    p_Delete(&copy,c->r);
1211  }
1212
1213#define ENLARGE(pointer, type) pointer=(type*) omrealloc(pointer, c->array_lengths*sizeof(type))
1214//  BOOLEAN corr=lenS_correct(c->strat);
1215  BOOLEAN R_found=FALSE;
1216  void* hp;
1217
1218  ++(c->n);
1219  ++(c->S->ncols);
1220  int i,j;
1221  i=c->n-1;
1222  sorted_pair_node** nodes=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1223  int spc=0;
1224  if(c->n>c->array_lengths){
1225    c->array_lengths=c->array_lengths*2;
1226    assume(c->array_lengths>=c->n);
1227    ENLARGE(c->T_deg, int);
1228    ENLARGE(c->tmp_pair_lm,poly);
1229    ENLARGE(c->tmp_spn,sorted_pair_node*);
1230
1231    ENLARGE(c->short_Exps,long);
1232    ENLARGE(c->lengths,int);
1233    #ifndef HAVE_BOOST
1234    #ifndef USE_STDVECBOOL
1235   
1236    ENLARGE(c->states, char*);
1237    #endif
1238    #endif
1239    ENLARGE(c->gcd_of_terms,poly);
1240    //if (c->weighted_lengths!=NULL) {
1241    ENLARGE(c->weighted_lengths,wlen_type);
1242    //}
1243    //ENLARGE(c->S->m,poly);
1244
1245  }
1246  pEnlargeSet(&c->S->m,c->n-1,1);
1247  if (c->T_deg_full)
1248    ENLARGE(c->T_deg_full,int);
1249  c->T_deg[i]=pTotaldegree(h);
1250  if(c->T_deg_full){
1251    c->T_deg_full[i]=pTotaldegree_full(h);
1252  }
1253
1254
1255  c->tmp_pair_lm[i]=pOne_Special(c->r);
1256
1257
1258  c->tmp_spn[i]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1259
1260
1261  c->lengths[i]=pLength(h);
1262
1263  //necessary for correct weighted length
1264
1265   if (!rField_is_Zp(c->r)){
1266    pCleardenom(h);
1267    pContent(h); //is a duplicate call, but belongs here
1268
1269  }
1270  else
1271    pNorm(h);
1272  pNormalize(h);
1273
1274  c->weighted_lengths[i]=pQuality(h, c, c->lengths[i]);
1275  c->gcd_of_terms[i]=got;
1276  #ifdef HAVE_BOOST
1277    c->states.push_back(dynamic_bitset<>(i));
1278
1279  #else
1280  #ifdef USE_STDVECBOOL
1281   
1282    c->states.push_back(vector<bool>(i));
1283   
1284 
1285  #else
1286  if (i>0)
1287    c->states[i]=(char*)  omalloc(i*sizeof(char));
1288  else
1289    c->states[i]=NULL;
1290  #endif
1291  #endif
1292
1293  c->S->m[i]=h;
1294  c->short_Exps[i]=p_GetShortExpVector(h,c->r);
1295
1296#undef ENLARGE
1297  if (p_GetComp(h,currRing)<=c->syz_comp){
1298  for (j=0;j<i;j++){
1299
1300
1301    #ifndef HAVE_BOOST
1302    c->states[i][j]=UNCALCULATED;
1303    #endif
1304    assume(p_LmDivisibleBy(c->S->m[i],c->S->m[j],c->r)==
1305     p_LmShortDivisibleBy(c->S->m[i],c->short_Exps[i],c->S->m[j],~(c->short_Exps[j]),c->r));
1306
1307
1308    if (_p_GetComp(c->S->m[i],c->r)!=_p_GetComp(c->S->m[j],c->r)){
1309      //c->states[i][j]=UNCALCULATED;
1310      //WARNUNG: be careful
1311      continue;
1312    } else
1313    if ((!c->nc) && (c->lengths[i]==1) && (c->lengths[j]==1)){
1314      c->states[i][j]=HASTREP;
1315
1316      }
1317    else if ((!(c->nc)) &&  (pHasNotCF(c->S->m[i],c->S->m[j])))
1318    {
1319      c->easy_product_crit++;
1320      c->states[i][j]=HASTREP;
1321      continue;
1322    }
1323    else if(extended_product_criterion(c->S->m[i],c->gcd_of_terms[i],c->S->m[j],c->gcd_of_terms[j],c))
1324    {
1325      c->states[i][j]=HASTREP;
1326      c->extended_product_crit++;
1327
1328      //PrintS("E");
1329    }
1330      //  if (c->states[i][j]==UNCALCULATED){
1331
1332    if ((TEST_V_FINDMONOM) &&(!c->nc)) {
1333        //PrintS("COMMU");
1334       //  if (c->lengths[i]==c->lengths[j]){
1335//             poly short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1336//             if (short_s==NULL){
1337//                 c->states[i][j]=HASTREP;
1338//             } else
1339//             {
1340//                 p_Delete(&short_s, currRing);
1341//             }
1342//         }
1343        if (c->lengths[i]+c->lengths[j]==3){
1344
1345
1346             poly short_s=ksCreateShortSpoly(c->S->m[i],c->S->m[j],c->r);
1347            if (short_s==NULL){
1348                c->states[i][j]=HASTREP;
1349            } else
1350            {
1351                assume(pLength(short_s)==1);
1352                if (TEST_V_UPTORADICAL)
1353                   monomial_root(short_s,c->r);
1354                int iS=
1355                   kFindDivisibleByInS_easy(c->strat,short_s, p_GetShortExpVector(short_s,c->r));
1356                if (iS<0){
1357                    //PrintS("N");
1358                    if (TRUE) {
1359                    c->states[i][j]=HASTREP;
1360                    add_later(short_s,"N",c);
1361                    } else p_Delete(&short_s,currRing);
1362                }
1363                else {
1364                    if (c->strat->lenS[iS]>1){
1365                        //PrintS("O");
1366                        if (TRUE) {
1367                        c->states[i][j]=HASTREP;
1368                        add_later(short_s,"O",c);
1369                        } else p_Delete(&short_s,currRing);
1370                    }
1371                    else
1372                     p_Delete(&short_s, currRing);
1373                     c->states[i][j]=HASTREP;
1374                }
1375
1376
1377            }
1378        }
1379    }
1380      //    if (short_s)
1381      //    {
1382    assume(spc<=j);
1383    sorted_pair_node* s=c->tmp_spn[spc];//(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1384    s->i=si_max(i,j);
1385    s->j=si_min(i,j);
1386    assume(s->j==j);
1387    s->expected_length=pair_weighted_length(i,j,c);//c->lengths[i]+c->lengths[j]-2;
1388
1389    poly lm=c->tmp_pair_lm[spc];//=pOne_Special();
1390
1391    pLcm(c->S->m[i], c->S->m[j], lm);
1392    pSetm(lm);
1393    s->deg=pTotaldegree(lm);
1394
1395    if(c->T_deg_full)//Sugar
1396    {
1397      int t_i=c->T_deg_full[s->i]-c->T_deg[s->i];
1398      int t_j=c->T_deg_full[s->j]-c->T_deg[s->j];
1399      s->deg+=si_max(t_i,t_j);
1400      //Print("\n max: %d\n",max(t_i,t_j));
1401    }
1402    s->lcm_of_lm=lm;
1403    //          pDelete(&short_s);
1404    //assume(lm!=NULL);
1405    nodes[spc]=s;
1406    spc++;
1407
1408  // }
1409  //else
1410  //{
1411        //c->states[i][j]=HASTREP;
1412  //}
1413  }
1414  }//if syz_comp end
1415 
1416 
1417 
1418 
1419  assume(spc<=i);
1420  //now ideal quotient crit
1421  qsort(nodes,spc,sizeof(sorted_pair_node*),iq_crit);
1422
1423    sorted_pair_node** nodes_final=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*i);
1424  int spc_final=0;
1425  j=0;
1426  while(j<spc)
1427  {
1428    int lower=j;
1429    int upper;
1430    BOOLEAN has=FALSE;
1431    for(upper=lower+1;upper<spc;upper++)
1432    {
1433
1434      if(!pLmEqual(nodes[lower]->lcm_of_lm,nodes[upper]->lcm_of_lm))
1435      {
1436  break;
1437      }
1438      if (has_t_rep(nodes[upper]->i,nodes[upper]->j,c))
1439  has=TRUE;
1440
1441    }
1442    upper=upper-1;
1443    int z;
1444    assume(spc_final<=j);
1445    for(z=0;z<spc_final;z++)
1446    {
1447      if(p_LmDivisibleBy(nodes_final[z]->lcm_of_lm,nodes[lower]->lcm_of_lm,c->r))
1448      {
1449  has=TRUE;
1450  break;
1451      }
1452    }
1453
1454    if(has)
1455    {
1456      for(;lower<=upper;lower++)
1457      {
1458  //free_sorted_pair_node(nodes[lower],c->r);
1459  //omfree(nodes[lower]);
1460  nodes[lower]=NULL;
1461      }
1462      j=upper+1;
1463      continue;
1464    }
1465    else
1466    {
1467      nodes[lower]->lcm_of_lm=pCopy(nodes[lower]->lcm_of_lm);
1468      assume(_p_GetComp(c->S->m[nodes[lower]->i],c->r)==_p_GetComp(c->S->m[nodes[lower]->j],c->r));
1469      nodes_final[spc_final]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1470
1471      *(nodes_final[spc_final++])=*(nodes[lower]);
1472      //c->tmp_spn[nodes[lower]->j]=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
1473      nodes[lower]=NULL;
1474      for(lower=lower+1;lower<=upper;lower++)
1475      {
1476  //  free_sorted_pair_node(nodes[lower],c->r);
1477  //omfree(nodes[lower]);
1478  nodes[lower]=NULL;
1479      }
1480      j=upper+1;
1481      continue;
1482    }
1483  }
1484
1485  //  Print("i:%d,spc_final:%d",i,spc_final);
1486
1487
1488
1489
1490  assume(spc_final<=spc);
1491  omfree(nodes);
1492  nodes=NULL;
1493
1494  add_to_reductors(c, h, c->lengths[c->n-1], TRUE);
1495  //i=posInS(c->strat,c->strat->sl,h,0 ecart);
1496  if (!(c->nc)){
1497    if (c->lengths[c->n-1]==1)
1498      shorten_tails(c,c->S->m[c->n-1]);
1499  }
1500  //you should really update c->lengths, c->strat->lenS, and the oder of polys in strat if you sort after lengths
1501
1502  //for(i=c->strat->sl; i>0;i--)
1503  //  if(c->strat->lenS[i]<c->strat->lenS[i-1]) printf("fehler bei %d\n",i);
1504  if (c->Rcounter>50) {
1505    c->Rcounter=0;
1506    cleanS(c->strat,c);
1507  }
1508  if(!ip){
1509    qsort(nodes_final,spc_final,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1510
1511
1512    c->apairs=spn_merge(c->apairs,c->pair_top+1,nodes_final,spc_final,c);
1513    c->pair_top+=spc_final;
1514    clean_top_of_pair_list(c);
1515    omfree(nodes_final);
1516    return NULL;
1517  }
1518  {
1519    *ip=spc_final;
1520    return nodes_final;
1521  }
1522
1523
1524
1525}
1526
1527
1528static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
1529{
1530  m=nInit(1);
1531  if (h==NULL) return NULL;
1532
1533  assume(len==pLength(h));
1534  kStrategy strat=c->strat;
1535  if (0 > strat->sl)
1536  {
1537    return h;
1538  }
1539  int j;
1540
1541  LObject P(h);
1542  P.SetShortExpVector();
1543  P.bucket = kBucketCreate(currRing);
1544  // BOOLEAN corr=lenS_correct(strat);
1545  kBucketInit(P.bucket,P.p,len /*pLength(P.p)*/);
1546  //wlen_set lenSw=(wlen_set) c->strat->lenS;
1547  //FIXME: plainly wrong
1548  //strat->lenS;
1549  //if (strat->lenSw!=NULL)
1550  //  lenSw=strat->lenSw;
1551  //int max_pos=simple_posInS(strat,P.p);
1552  loop
1553  {
1554      int dummy=strat->sl;
1555      j=kFindDivisibleByInS_easy(strat,P.p,P.sev);
1556      //j=kFindDivisibleByInS(strat,&dummy,&P);
1557      if ((j>=0) && ((!n)||
1558        ((strat->lenS[j]<=n) &&
1559         ((strat->lenSw==NULL)||
1560         (strat->lenSw[j]<=n)))))
1561      {
1562        nNormalize(pGetCoeff(P.p));
1563#ifdef KDEBUG
1564        if (TEST_OPT_DEBUG)
1565        {
1566          PrintS("red:");
1567          wrp(h);
1568          PrintS(" with ");
1569          wrp(strat->S[j]);
1570        }
1571#endif
1572
1573        number coef=kBucketPolyRed(P.bucket,strat->S[j],
1574                                   strat->lenS[j]/*pLength(strat->S[j])*/,
1575                                   strat->kNoether);
1576  number m2=nMult(m,coef);
1577  nDelete(&m);
1578  m=m2;
1579        nDelete(&coef);
1580        h = kBucketGetLm(P.bucket);
1581
1582  if (h==NULL) {
1583    len=0;
1584    kBucketDestroy(&P.bucket);
1585    return
1586    NULL;}
1587        P.p=h;
1588        P.t_p=NULL;
1589        P.SetShortExpVector();
1590#ifdef KDEBUG
1591        if (TEST_OPT_DEBUG)
1592        {
1593          PrintS("\nto:");
1594          wrp(h);
1595          PrintLn();
1596        }
1597#endif
1598      }
1599      else
1600      {
1601        kBucketClear(P.bucket,&(P.p),&len);
1602        kBucketDestroy(&P.bucket);
1603        pNormalize(P.p);
1604  assume(len==(pLength(P.p)));
1605        return P.p;
1606      }
1607    }
1608}
1609
1610
1611
1612static poly redTailShort(poly h, kStrategy strat){
1613  if (h==NULL) return NULL;//n_Init(1,currRing);
1614  if (TEST_V_MODPSOLVSB){
1615    bit_reduce(pNext(h), strat->tailRing);
1616  }
1617  int sl=strat->sl;
1618  int i;
1619  int len=pLength(h);
1620  for(i=0;i<=strat->sl;i++){
1621    if((strat->lenS[i]>2) || ((strat->lenSw!=NULL) && (strat->lenSw[i]>2)))
1622      break;
1623  }
1624  return(redNFTail(h,i-1,strat, len));
1625}
1626
1627static void line_of_extended_prod(int fixpos,slimgb_alg* c){
1628    if (c->gcd_of_terms[fixpos]==NULL)
1629  {
1630    c->gcd_of_terms[fixpos]=gcd_of_terms(c->S->m[fixpos],c->r);
1631    if (c->gcd_of_terms[fixpos])
1632    {
1633      int i;
1634      for(i=0;i<fixpos;i++)
1635        if((c->states[fixpos][i]!=HASTREP)&& (extended_product_criterion(c->S->m[fixpos],c->gcd_of_terms[fixpos], c->S->m[i],c->gcd_of_terms[i],c)))
1636{
1637          c->states[fixpos][i]=HASTREP;
1638    c->extended_product_crit++;
1639}
1640      for(i=fixpos+1;i<c->n;i++)
1641        if((c->states[i][fixpos]!=HASTREP)&& (extended_product_criterion(c->S->m[fixpos],c->gcd_of_terms[fixpos], c->S->m[i],c->gcd_of_terms[i],c)))
1642  {        c->states[i][fixpos]=HASTREP;
1643  c->extended_product_crit++;
1644  }
1645    }
1646  }
1647}
1648static void c_S_element_changed_hook(int pos, slimgb_alg* c){
1649  length_one_crit(c,pos, c->lengths[pos]);
1650  if (!c->nc)
1651    line_of_extended_prod(pos,c);
1652}
1653class poly_tree_node {
1654public:
1655  poly p;
1656  poly_tree_node* l;
1657  poly_tree_node* r;
1658  int n;
1659  poly_tree_node(int sn):l(NULL),r(NULL),n(sn){}
1660};
1661class exp_number_builder{
1662public:
1663  poly_tree_node* top_level;
1664  int n;
1665  int get_n(poly p);
1666  exp_number_builder():top_level(0),n(0){}
1667};
1668int exp_number_builder::get_n(poly p){
1669  poly_tree_node** node=&top_level;
1670  while(*node!=NULL){
1671    int c=pLmCmp(p,(*node)->p);
1672    if (c==0) return (*node)->n;
1673    if (c==-1) node=&((*node)->r);
1674    else
1675      node=&((*node)->l);
1676  }
1677  (*node)= new poly_tree_node(n);
1678  n++;
1679  (*node)->p=pLmInit(p);
1680  return (*node)->n;
1681}
1682
1683//mac_polys exp are smaller iff they are greater by monomial ordering
1684//corresponding to solving linear equations notation
1685
1686//! obsolete
1687struct int_poly_pair{
1688  poly p;
1689  int n;
1690};
1691
1692
1693//! obsolete
1694void t2ippa_rec(poly* ip,int* ia, poly_tree_node* k, int &offset){
1695    if(!k) return;
1696    t2ippa_rec(ip,ia,k->l,offset);
1697    ip[offset]=k->p;
1698    ia[k->n]=offset;
1699    ++offset;
1700
1701    t2ippa_rec(ip,ia,k->r,offset);
1702    delete k;
1703  }
1704
1705//! obsolete
1706void t2ippa(poly* ip,int* ia,exp_number_builder & e){
1707
1708  int o=0;
1709  t2ippa_rec(ip,ia,e.top_level,o);
1710}
1711int anti_poly_order(const void* a, const void* b){
1712  return -pLmCmp(((int_poly_pair*) a)->p,((int_poly_pair*) b)->p );
1713}
1714
1715BOOLEAN is_valid_ro(red_object & ro){
1716  red_object r2=ro;
1717  ro.validate();
1718  if ((r2.p!=ro.p)||(r2.sev!=ro.sev)) return FALSE;
1719  return TRUE;
1720}
1721
1722
1723
1724static void go_on (slimgb_alg* c){
1725  //set limit of 1000 for multireductions, at the moment for
1726  //programming reasons
1727  int i=0;
1728  c->average_length=0;
1729  for(i=0;i<c->n;i++){
1730    c->average_length+=c->lengths[i];
1731  }
1732  c->average_length=c->average_length/c->n;
1733  i=0;
1734  poly* p=(poly*) omalloc((PAR_N+1)*sizeof(poly));//nullterminated
1735
1736  int curr_deg=-1;
1737  while(i<PAR_N){
1738    sorted_pair_node* s=top_pair(c);//here is actually chain criterium done
1739
1740
1741    if (!s) break;
1742
1743    if(curr_deg>=0){
1744      if (s->deg >curr_deg) break;
1745    }
1746
1747    else curr_deg=s->deg;
1748    quick_pop_pair(c);
1749    if(s->i>=0){
1750      //be careful replace_pair use createShortSpoly which is not noncommutative
1751      now_t_rep(s->i,s->j,c);
1752    replace_pair(s->i,s->j,c);
1753
1754    if(s->i==s->j) {
1755      free_sorted_pair_node(s,c->r);
1756      continue;
1757    }
1758    now_t_rep(s->i,s->j,c);
1759    }
1760    poly h;
1761    if(s->i>=0){
1762      if (!c->nc)
1763  h=ksOldCreateSpoly(c->S->m[s->i], c->S->m[s->j], NULL, c->r);
1764      else
1765  h= nc_CreateSpoly(c->S->m[s->i], c->S->m[s->j], NULL, c->r);
1766    }
1767    else
1768      h=s->lcm_of_lm;
1769    // if(s->i>=0)
1770//       now_t_rep(s->j,s->i,c);
1771    number coef;
1772    int mlen=pLength(h);
1773    if (!c->nc){
1774      h=redNF2(h,c,mlen,coef,2);
1775      redTailShort(h,c->strat);
1776      nDelete(&coef);
1777    }
1778    free_sorted_pair_node(s,c->r);
1779    if(!h) continue;
1780    int len=pLength(h);
1781    p[i]=h;
1782
1783    i++;
1784  }
1785  p[i]=NULL;
1786//  pre_comp(p,i,c);
1787  if(i==0){
1788    omfree(p);
1789    return;
1790  }
1791  #ifdef TGB_RESORT_PAIRS
1792  c->replaced=new bool[c->n];
1793  c->used_b=FALSE;
1794  #endif
1795  red_object* buf=(red_object*) omalloc(i*sizeof(red_object));
1796  c->normal_forms+=i;
1797  int j;
1798  for(j=0;j<i;j++){
1799    buf[j].p=p[j];
1800    buf[j].sev=pGetShortExpVector(p[j]);
1801    buf[j].bucket = kBucketCreate(currRing);
1802
1803    int len=pLength(p[j]);
1804    kBucketInit(buf[j].bucket,buf[j].p,len);
1805    buf[j].initial_quality=buf[j].guess_quality(c);
1806    assume(buf[j].initial_quality>=0);
1807  }
1808  omfree(p);
1809  qsort(buf,i,sizeof(red_object),red_object_better_gen);
1810//    Print("\ncurr_deg:%i\n",curr_deg);
1811  if (TEST_OPT_PROT)
1812  {
1813    Print("%dM[%d,",curr_deg,i);
1814  }
1815#ifdef FIND_DETERMINISTIC
1816  c->modifiedS=(BOOLEAN*) omalloc((c->strat->sl+1)*sizeof(BOOLEAN));
1817  c->expandS=(poly*) omalloc((1)*sizeof(poly));
1818  c->expandS[0]=NULL;
1819  int z2;
1820  for(z2=0;z2<=c->strat->sl;z2++)
1821    c->modifiedS[z2]=FALSE;
1822#endif
1823  multi_reduction(buf, i, c);
1824  #ifdef TGB_RESORT_PAIRS
1825  if (c->used_b) {
1826    if (TEST_OPT_PROT)
1827        PrintS("B");
1828    int e;
1829    for(e=0;e<=c->pair_top;e++){
1830        if(c->apairs[e]->i<0) continue;
1831        assume(c->apairs[e]->j>=0);
1832        if ((c->replaced[c->apairs[e]->i])||(c->replaced[c->apairs[e]->j])) {
1833            sorted_pair_node* s=c->apairs[e];
1834            s->expected_length=pair_weighted_length(s->i,s->j,c);
1835        }
1836    }
1837    qsort(c->apairs,c->pair_top+1,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1838  }
1839  #endif
1840#ifdef TGB_DEBUG
1841 {
1842   int k;
1843   for(k=0;k<i;k++)
1844   {
1845     assume(kFindDivisibleByInS_easy(c->strat,buf[k])<0);
1846     int k2;
1847     for(k2=0;k2<i;k2++)
1848     {
1849       if(k==k2) continue;
1850       assume((!(p_LmDivisibleBy(buf[k].p,buf[k2].p,c->r)))||(wrp(buf[k].p),Print(" k %d k2 %d ",k,k2),wrp(buf[k2].p),FALSE));
1851     }
1852   }
1853 }
1854#endif
1855  //resort S
1856#ifdef FIND_DETERMINISTIC
1857  for(z2=0;z2<=c->strat->sl;z2++)
1858  {
1859    if (c->modifiedS[z2])
1860    {
1861      wlen_type qual;
1862      int new_pos;
1863      if (c->strat->lenSw!=NULL)
1864          new_pos=simple_posInS(c->strat,c->strat->S[z2],strat->lenS[z2],strat->Sw[z2]);
1865      else
1866          new_pos=simple_posInS(c->strat,c->strat->S[z2],strat->lenS[z2],lenS[z2]);
1867
1868      if (new_pos<z2)
1869      {
1870         move_forward_in_S(z2,new_pos,c->strat);
1871      }
1872
1873      assume(new_pos<=z2);
1874    }
1875  }
1876  for(z2=0;c->expandS[z2]!=NULL;z2++)
1877  {
1878    add_to_reductors(c,c->expandS[z2],pLength(c->expandS[z2]));
1879    // PrintS("E");
1880  }
1881  omfree(c->modifiedS);
1882  c->modifiedS=NULL;
1883  omfree(c->expandS);
1884  c->expandS=NULL;
1885#endif
1886  if (TEST_OPT_PROT)
1887      Print("%i]",i);
1888
1889  int* ibuf=(int*) omalloc(i*sizeof(int));
1890  sorted_pair_node*** sbuf=(sorted_pair_node***) omalloc(i*sizeof(sorted_pair_node**));
1891  for(j=0;j<i;j++)
1892  {
1893    int len;
1894    poly p;
1895    buf[j].flatten();
1896    kBucketClear(buf[j].bucket,&p, &len);
1897    kBucketDestroy(&buf[j].bucket);
1898
1899    if (!c->nc) {
1900      if ((TEST_OPT_REDTAIL)&&(c->S->rank<=1)){
1901      p=redNFTail(p,c->strat->sl,c->strat, 0);
1902      } else {
1903      p=redTailShort(p, c->strat);
1904      }
1905      }
1906    sbuf[j]=add_to_basis_ideal_quotient(p,c,ibuf+j);
1907    //sbuf[j]=add_to_basis(p,-1,-1,c,ibuf+j);
1908  }
1909  int sum=0;
1910  for(j=0;j<i;j++){
1911    sum+=ibuf[j];
1912  }
1913  sorted_pair_node** big_sbuf=(sorted_pair_node**) omalloc(sum*sizeof(sorted_pair_node*));
1914  int partsum=0;
1915  for(j=0;j<i;j++)
1916  {
1917    memmove(big_sbuf+partsum, sbuf[j],ibuf[j]*sizeof(sorted_pair_node*));
1918    omfree(sbuf[j]);
1919    partsum+=ibuf[j];
1920  }
1921
1922  qsort(big_sbuf,sum,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1923  c->apairs=spn_merge(c->apairs,c->pair_top+1,big_sbuf,sum,c);
1924  c->pair_top+=sum;
1925  clean_top_of_pair_list(c);
1926  omfree(big_sbuf);
1927  omfree(sbuf);
1928  omfree(ibuf);
1929  omfree(buf);
1930#ifdef TGB_DEBUG
1931  int z;
1932  for(z=1;z<=c->pair_top;z++)
1933  {
1934    assume(pair_better(c->apairs[z],c->apairs[z-1],c));
1935  }
1936#endif
1937  if (TEST_OPT_PROT)
1938      Print("(%d)",c->pair_top+1);
1939  while(!(idIs0(c->add_later))){
1940    ideal add=c->add_later;
1941    idSkipZeroes(add);
1942    for(j=0;j<add->idelems();j++){
1943        assume(pLength(add->m[j])==1);
1944        p_SetCoeff(add->m[j],n_Init(1,currRing),currRing);
1945
1946    }
1947    ideal add2=kInterRed(add,NULL);
1948    id_Delete(&add,currRing);
1949    idSkipZeroes(add2);
1950    c->add_later=idInit(ADD_LATER_SIZE,c->S->rank);
1951    memset(c->add_later->m,0,ADD_LATER_SIZE*sizeof(poly));
1952//     for(i=0;i<add2->idelems();i++){
1953//       if (add2->m[i]!=NULL)
1954//           add_to_basis_ideal_quotient(add2->m[i],-1,-1,c,NULL);
1955//       add2->m[i]=NULL;
1956//     }
1957    int i=add2->idelems();
1958    int* ibuf=(int*) omalloc(i*sizeof(int));
1959  sorted_pair_node*** sbuf=(sorted_pair_node***) omalloc(i*sizeof(sorted_pair_node**));
1960
1961  for(j=0;j<i;j++)
1962  {
1963    int len;
1964    poly p;
1965    //buf[j].flatten();
1966    //kBucketClear(buf[j].bucket,&p, &len);
1967    //kBucketDestroy(&buf[j].bucket);
1968    p=add2->m[j];
1969    add2->m[j]=NULL;
1970
1971    sbuf[j]=add_to_basis_ideal_quotient(p,c,ibuf+j);
1972    //sbuf[j]=add_to_basis(p,-1,-1,c,ibuf+j);
1973  }
1974  int sum=0;
1975  for(j=0;j<i;j++){
1976    sum+=ibuf[j];
1977  }
1978  sorted_pair_node** big_sbuf=(sorted_pair_node**) omalloc(sum*sizeof(sorted_pair_node*));
1979  int partsum=0;
1980  for(j=0;j<i;j++)
1981  {
1982    memmove(big_sbuf+partsum, sbuf[j],ibuf[j]*sizeof(sorted_pair_node*));
1983    omfree(sbuf[j]);
1984    partsum+=ibuf[j];
1985  }
1986
1987  qsort(big_sbuf,sum,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
1988  c->apairs=spn_merge(c->apairs,c->pair_top+1,big_sbuf,sum,c);
1989  c->pair_top+=sum;
1990  clean_top_of_pair_list(c);
1991  omfree(big_sbuf);
1992  omfree(sbuf);
1993  omfree(ibuf);
1994  //omfree(buf);
1995    id_Delete(&add2, c->r);
1996  }
1997  #ifdef TGB_RESORT_PAIRS
1998  delete c->replaced;
1999  c->replaced=NULL;
2000  c->used_b=FALSE;
2001  #endif
2002  return;
2003}
2004
2005
2006
2007#ifdef REDTAIL_S
2008
2009static poly redNFTail (poly h,const int sl,kStrategy strat, int len)
2010{
2011  if (h==NULL) return NULL;
2012  pTest(h);
2013  if (0 > sl)
2014    return h;
2015  if (pNext(h)==NULL) return h;
2016
2017  int j;
2018  poly res=h;
2019  poly act=res;
2020  LObject P(pNext(h));
2021  pNext(res)=NULL;
2022  P.bucket = kBucketCreate(currRing);
2023  len--;
2024  h=P.p;
2025  if (len <=0) len=pLength(h);
2026  kBucketInit(P.bucket,h /*P.p*/,len /*pLength(P.p)*/);
2027  pTest(h);
2028  loop
2029  {
2030      P.p=h;
2031      P.t_p=NULL;
2032      P.SetShortExpVector();
2033      loop
2034      {
2035          int dummy=strat->sl;
2036          j=kFindDivisibleByInS_easy(strat,P.p,P.sev);//kFindDivisibleByInS(strat,&dummy,&P);
2037          if (j>=0)
2038          {
2039#ifdef REDTAIL_PROT
2040            PrintS("r");
2041#endif
2042            nNormalize(pGetCoeff(P.p));
2043#ifdef KDEBUG
2044            if (TEST_OPT_DEBUG)
2045            {
2046              PrintS("red tail:");
2047              wrp(h);
2048              PrintS(" with ");
2049              wrp(strat->S[j]);
2050            }
2051#endif
2052            number coef;
2053            pTest(strat->S[j]);
2054            coef=kBucketPolyRed(P.bucket,strat->S[j],
2055                                strat->lenS[j]/*pLength(strat->S[j])*/,strat->kNoether);
2056            pMult_nn(res,coef);
2057            nDelete(&coef);
2058            h = kBucketGetLm(P.bucket);
2059            pTest(h);
2060            if (h==NULL)
2061            {
2062#ifdef REDTAIL_PROT
2063              PrintS(" ");
2064#endif
2065        kBucketDestroy(&P.bucket);
2066              return res;
2067            }
2068            pTest(h);
2069            P.p=h;
2070            P.t_p=NULL;
2071            P.SetShortExpVector();
2072#ifdef KDEBUG
2073            if (TEST_OPT_DEBUG)
2074            {
2075              PrintS("\nto tail:");
2076              wrp(h);
2077              PrintLn();
2078            }
2079#endif
2080          }
2081          else
2082          {
2083#ifdef REDTAIL_PROT
2084            PrintS("n");
2085#endif
2086            break;
2087          }
2088      } /* end loop current mon */
2089      //   poly tmp=pHead(h /*kBucketGetLm(P.bucket)*/);
2090      //act->next=tmp;pIter(act);
2091      act->next=kBucketExtractLm(P.bucket);pIter(act);
2092      h = kBucketGetLm(P.bucket);
2093      if (h==NULL)
2094      {
2095#ifdef REDTAIL_PROT
2096        PrintS(" ");
2097#endif
2098  kBucketDestroy(&P.bucket);
2099        return res;
2100      }
2101      pTest(h);
2102  }
2103}
2104#endif
2105
2106
2107//try to fill, return FALSE iff queue is empty
2108
2109//transfers ownership of m to mat
2110void init_with_mac_poly(tgb_sparse_matrix* mat, int row, mac_poly m){
2111  assume(mat->mp[row]==NULL);
2112  mat->mp[row]=m;
2113#ifdef TGB_DEBUG
2114  mac_poly r=m;
2115  while(r){
2116    assume(r->exp<mat->columns);
2117    r=r->next;
2118  }
2119#endif
2120}
2121poly free_row_to_poly(tgb_sparse_matrix* mat, int row, poly* monoms, int monom_index){
2122  poly p=NULL;
2123  poly* set_this=&p;
2124  mac_poly r=mat->mp[row];
2125  mat->mp[row]=NULL;
2126  while(r)
2127  {
2128    (*set_this)=pLmInit(monoms[monom_index-1-r->exp]);
2129    pSetCoeff((*set_this),r->coef);
2130    set_this=&((*set_this)->next);
2131    mac_poly old=r;
2132    r=r->next;
2133    delete old;
2134
2135  }
2136  return p;
2137
2138}
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150static int poly_crit(const void* ap1, const void* ap2){
2151  poly p1,p2;
2152  p1=*((poly*) ap1);
2153  p2=*((poly*)ap2);
2154
2155  int c=pLmCmp(p1,p2);
2156  if (c !=0) return c;
2157  int l1=pLength(p1);
2158  int l2=pLength(p2);
2159  if (l1<l2) return -1;
2160  if (l1>l2) return 1;
2161  return 0;
2162}
2163void slimgb_alg::introduceDelayedPairs(poly* pa,int s){
2164        if (s==0) return;
2165        sorted_pair_node** si_array=(sorted_pair_node**) omalloc(s* sizeof(sorted_pair_node*));
2166       
2167        for( unsigned int i = 0; i < s; i++ )
2168        {
2169                sorted_pair_node* si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
2170                si->i=-1;
2171                si->j=-2;
2172                poly p=pa[i];
2173                simplify_poly(p,r);
2174                si->expected_length=pQuality(p,this,pLength(p));
2175                si->deg=pTotaldegree_full(p);
2176                if (!rField_is_Zp(r)){
2177                  pCleardenom(p);
2178                }
2179                si->lcm_of_lm=p;
2180
2181                //      c->apairs[n-1-i]=si;
2182                si_array[i]=si;
2183               
2184  }
2185       
2186  qsort(si_array,s,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
2187        apairs=spn_merge(apairs,pair_top+1,si_array,s,this);
2188  pair_top+=s;
2189}
2190slimgb_alg::slimgb_alg(ideal I, int syz_comp,BOOLEAN F4){
2191  completed=FALSE;
2192  this->syz_comp=syz_comp;
2193  r=currRing;
2194  nc=rIsPluralRing(r);
2195  this->lastDpBlockStart=get_last_dp_block_start(r);
2196  //Print("last dp Block start: %i\n", this->lastDpBlockStart);
2197  is_homog=TRUE;
2198  {
2199    int hz;
2200    for(hz=0;hz<IDELEMS(I);hz++){
2201      assume(I->m[hz]!=NULL);
2202      int d=pTotaldegree(I->m[hz]);
2203      poly t=I->m[hz]->next;
2204      while(t)
2205      {
2206        if (d!=pTotaldegree(t,r))
2207        {
2208          is_homog=FALSE;
2209          break;
2210        }
2211        t=t->next;
2212      }
2213      if(!(is_homog)) break;
2214    }
2215  }
2216  eliminationProblem=((!(is_homog))&&((pLexOrder)));
2217  //  Print("is homog:%d",c->is_homog);
2218  void* h;
2219  poly hp;
2220  int i,j;
2221  to_destroy=NULL;
2222  easy_product_crit=0;
2223  extended_product_crit=0;
2224  if (rField_is_Zp(r))
2225    isDifficultField=FALSE;
2226  else
2227    isDifficultField=TRUE;
2228  //not fully correct
2229  //(rChar()==0);
2230  F4_mode=F4;
2231
2232  reduction_steps=0;
2233  last_index=-1;
2234
2235  F=NULL;
2236  F_minus=NULL;
2237
2238  Rcounter=0;
2239
2240  soon_free=NULL;
2241
2242  tmp_lm=pOne();
2243
2244  normal_forms=0;
2245  current_degree=1;
2246
2247  max_pairs=5*I->idelems();
2248
2249  apairs=(sorted_pair_node**) omalloc(sizeof(sorted_pair_node*)*max_pairs);
2250  pair_top=-1;
2251
2252  int n=I->idelems();
2253  array_lengths=n;
2254
2255
2256  i=0;
2257  this->n=0;
2258  T_deg=(int*) omalloc(n*sizeof(int));
2259  if(eliminationProblem)
2260    T_deg_full=(int*) omalloc(n*sizeof(int));
2261  else
2262    T_deg_full=NULL;
2263  tmp_pair_lm=(poly*) omalloc(n*sizeof(poly));
2264  tmp_spn=(sorted_pair_node**) omalloc(n*sizeof(sorted_pair_node*));
2265  lm_bin=omGetSpecBin(POLYSIZE + (r->ExpL_Size)*sizeof(long));
2266#ifdef HEAD_BIN
2267  HeadBin=omGetSpecBin(POLYSIZE + (currRing->ExpL_Size)*sizeof(long));
2268#endif
2269  /* omUnGetSpecBin(&(c->HeadBin)); */
2270  #ifndef HAVE_BOOST
2271  #ifdef USE_STDVECBOOL
2272  #else
2273  h=omalloc(n*sizeof(char*));
2274
2275  states=(char**) h;
2276  #endif
2277  #endif
2278  h=omalloc(n*sizeof(int));
2279  lengths=(int*) h;
2280  weighted_lengths=(wlen_type*)omalloc(n*sizeof(wlen_type));
2281  gcd_of_terms=(poly*) omalloc(n*sizeof(poly));
2282
2283  short_Exps=(long*) omalloc(n*sizeof(long));
2284  if (F4_mode)
2285    S=idInit(n,I->rank);
2286  else
2287    S=idInit(1,I->rank);
2288  strat=new skStrategy;
2289  strat->syzComp = 0;
2290  initBuchMoraCrit(strat);
2291  initBuchMoraPos(strat);
2292  strat->initEcart = initEcartBBA;
2293  strat->tailRing=r;
2294  strat->enterS = enterSBba;
2295  strat->sl = -1;
2296  i=n;
2297  i=1;//some strange bug else
2298  /* initS(c->S,NULL,c->strat); */
2299  /* intS start: */
2300  // i=((i+IDELEMS(c->S)+15)/16)*16;
2301  strat->ecartS=(intset)omAlloc(i*sizeof(int)); /*initec(i);*/
2302  strat->sevS=(unsigned long*)omAlloc0(i*sizeof(unsigned long));
2303  /*initsevS(i);*/
2304  strat->S_2_R=(int*)omAlloc0(i*sizeof(int));/*initS_2_R(i);*/
2305  strat->fromQ=NULL;
2306  strat->Shdl=idInit(1,1);
2307  strat->S=strat->Shdl->m;
2308  strat->lenS=(int*)omAlloc0(i*sizeof(int));
2309  if((isDifficultField)||(eliminationProblem))
2310    strat->lenSw=(wlen_type*)omAlloc0(i*sizeof(wlen_type));
2311  else
2312    strat->lenSw=NULL;
2313  sorted_pair_node* si;
2314  assume(n>0);
2315  add_to_basis_ideal_quotient(I->m[0],this,NULL);
2316
2317  assume(strat->sl==strat->Shdl->idelems()-1);
2318  if(!(F4_mode))
2319  {
2320                poly* array_arg=I->m;
2321                array_arg++;
2322                introduceDelayedPairs(array_arg,n-1);
2323                /*
2324    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
2325    {
2326      //     add_to_basis(I->m[i],-1,-1,c);
2327      si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
2328      si->i=-1;
2329      si->j=-2;
2330      si->expected_length=pQuality(I->m[i],this,pLength(I->m[i]));
2331      si->deg=pTotaldegree(I->m[i]);
2332      if (!rField_is_Zp(r)){
2333        pCleardenom(I->m[i]);
2334      }
2335      si->lcm_of_lm=I->m[i];
2336
2337      //      c->apairs[n-1-i]=si;
2338      apairs[n-i-1]=si;
2339      ++(pair_top);
2340    }*/
2341  }
2342  else
2343  {
2344    for (i=1;i<n;i++)//the 1 is wanted, because first element is added to basis
2345      add_to_basis_ideal_quotient(I->m[i],this,NULL);
2346  }
2347  for(i=0;i<I->idelems();i++)
2348  {
2349    I->m[i]=NULL;
2350  }
2351  idDelete(&I);
2352  add_later=idInit(ADD_LATER_SIZE,S->rank);
2353  memset(add_later->m,0,ADD_LATER_SIZE*sizeof(poly));
2354}
2355slimgb_alg::~slimgb_alg(){
2356 
2357  if (!(completed)){
2358      poly* add=(poly*) omalloc((pair_top+2)*sizeof(poly));
2359      int piter;
2360      int pos=0;
2361      for(piter=0;piter<=pair_top;piter++){
2362        sorted_pair_node* s=apairs[piter];
2363        if (s->i<0){
2364            //delayed element
2365            if (s->lcm_of_lm!=NULL){
2366                add[pos]=s->lcm_of_lm;
2367                pos++;
2368
2369            }
2370        }
2371   
2372        free_sorted_pair_node(s,r);
2373        apairs[piter]=NULL;
2374      }
2375      pair_top=-1;
2376      add[pos]=NULL;
2377      pos=0;
2378      while(add[pos]!=NULL){
2379        add_to_basis_ideal_quotient(add[pos],this,NULL);
2380        pos++;
2381      }
2382      for(piter=0;piter<=pair_top;piter++){
2383        sorted_pair_node* s=apairs[piter];
2384        assume(s->i>=0);
2385        free_sorted_pair_node(s,r);
2386        apairs[piter]=NULL;
2387      }
2388      pair_top=-1;
2389  }
2390  id_Delete(&add_later,r);
2391  int i,j;
2392  slimgb_alg* c=this;
2393  while(c->to_destroy)
2394  {
2395    pDelete(&(c->to_destroy->p));
2396    poly_list_node* old=c->to_destroy;
2397    c->to_destroy=c->to_destroy->next;
2398    omfree(old);
2399  }
2400  while(c->F)
2401  {
2402    for(i=0;i<c->F->size;i++){
2403      pDelete(&(c->F->mp[i].m));
2404    }
2405    omfree(c->F->mp);
2406    c->F->mp=NULL;
2407    mp_array_list* old=c->F;
2408    c->F=c->F->next;
2409    omfree(old);
2410  }
2411  while(c->F_minus)
2412  {
2413    for(i=0;i<c->F_minus->size;i++){
2414      pDelete(&(c->F_minus->p[i]));
2415    }
2416    omfree(c->F_minus->p);
2417    c->F_minus->p=NULL;
2418    poly_array_list* old=c->F_minus;
2419    c->F_minus=c->F_minus->next;
2420    omfree(old);
2421  }
2422  #ifndef HAVE_BOOST
2423  #ifndef USE_STDVECBOOL
2424  for(int z=1 /* zero length at 0 */;z<c->n;z++){
2425    omfree(c->states[z]);
2426  }
2427  omfree(c->states);
2428  #endif
2429  #endif
2430
2431  omfree(c->lengths);
2432  omfree(c->weighted_lengths);
2433  for(int z=0;z<c->n;z++)
2434  {
2435    pDelete(&c->tmp_pair_lm[z]);
2436    omfree(c->tmp_spn[z]);
2437  }
2438  omfree(c->tmp_pair_lm);
2439  omfree(c->tmp_spn);
2440
2441  omfree(c->T_deg);
2442  if(c->T_deg_full)
2443    omfree(c->T_deg_full);
2444
2445  omFree(c->strat->ecartS);
2446  omFree(c->strat->sevS);
2447//   initsevS(i);
2448  omFree(c->strat->S_2_R);
2449
2450
2451  omFree(c->strat->lenS);
2452
2453  if(c->strat->lenSw)  omFree(c->strat->lenSw);
2454
2455
2456
2457
2458  for(i=0;i<c->n;i++){
2459    if(c->gcd_of_terms[i])
2460      pDelete(&(c->gcd_of_terms[i]));
2461  }
2462  omfree(c->gcd_of_terms);
2463
2464  omfree(c->apairs);
2465  if (TEST_OPT_PROT)
2466  {
2467    Print("calculated %d NFs\n",c->normal_forms);
2468    Print("applied %i product crit, %i extended_product crit \n", c->easy_product_crit, c->extended_product_crit);
2469  }
2470  int deleted_form_c_s=0;
2471
2472  for(i=0;i<=c->strat->sl;i++){
2473    if (!c->strat->S[i]) continue;
2474    BOOLEAN found=FALSE;
2475    for(j=0;j<c->n;j++){
2476      if (c->S->m[j]==c->strat->S[i]){
2477        found=TRUE;
2478        break;
2479      }
2480    }
2481    if(!found) pDelete(&c->strat->S[i]);
2482  }
2483//   for(i=0;i<c->n;i++){
2484//     if (c->rep[i]!=i){
2485// //       for(j=0;j<=c->strat->sl;j++){
2486// //   if(c->strat->S[j]==c->S->m[i]){
2487// //     c->strat->S[j]=NULL;
2488// //     break;
2489// //   }
2490// //       }
2491// //      PrintS("R_delete");
2492//       pDelete(&c->S->m[i]);
2493//     }
2494//   }
2495
2496  if (completed){
2497  for(i=0;i<c->n;i++)
2498  {
2499    assume(c->S->m[i]!=NULL);
2500    if (p_GetComp(c->S->m[i],currRing)>this->syz_comp) continue;
2501    for(j=0;j<c->n;j++)
2502    {
2503      if((c->S->m[j]==NULL)||(i==j))
2504        continue;
2505      assume(p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
2506             c->S->m[i],~c->short_Exps[i],
2507             c->r)==p_LmDivisibleBy(c->S->m[j],
2508             c->S->m[i],
2509             c->r));
2510      if (p_LmShortDivisibleBy(c->S->m[j],c->short_Exps[j],
2511          c->S->m[i],~c->short_Exps[i],
2512          c->r))
2513      {
2514        pDelete(&c->S->m[i]);
2515        break;
2516      }
2517    }
2518  }
2519  }
2520  omfree(c->short_Exps);
2521
2522
2523  ideal I=c->S;
2524
2525  IDELEMS(I)=c->n;
2526
2527  idSkipZeroes(I);
2528  for(i=0;i<=c->strat->sl;i++)
2529    c->strat->S[i]=NULL;
2530  id_Delete(&c->strat->Shdl,c->r);
2531  pDelete(&c->tmp_lm);
2532  omUnGetSpecBin(&lm_bin);
2533  delete c->strat;
2534}
2535ideal t_rep_gb(ring r,ideal arg_I, int syz_comp, BOOLEAN F4_mode){
2536
2537  //  Print("QlogSize(0) %d, QlogSize(1) %d,QlogSize(-2) %d, QlogSize(5) %d\n", QlogSize(nlInit(0)),QlogSize(nlInit(1)),QlogSize(nlInit(-2)),QlogSize(nlInit(5)));
2538 
2539  if (TEST_OPT_PROT)
2540    if (F4_mode)
2541      PrintS("F4 Modus \n");
2542
2543  //debug_Ideal=arg_debug_Ideal;
2544  //if (debug_Ideal) PrintS("DebugIdeal received\n");
2545  // Print("Idelems %i \n----------\n",IDELEMS(arg_I));
2546  ideal I=idCompactify(arg_I);
2547   int i;
2548  if (idIs0(I)) return I;
2549  for(i=0;i<IDELEMS(I);i++)
2550  {
2551    assume(I->m[i]!=NULL);
2552    simplify_poly(I->m[i],currRing);
2553  }
2554 
2555
2556  qsort(I->m,IDELEMS(I),sizeof(poly),poly_crit);
2557  //Print("Idelems %i \n----------\n",IDELEMS(I));
2558  //slimgb_alg* c=(slimgb_alg*) omalloc(sizeof(slimgb_alg));
2559  //int syz_comp=arg_I->rank;
2560  slimgb_alg* c=new slimgb_alg(I, syz_comp,F4_mode);
2561
2562
2563  while ((c->pair_top>=0) && ((!(TEST_OPT_DEGBOUND)) || (c->apairs[c->pair_top]->deg<=Kstd1_deg)))
2564  {
2565    if(F4_mode)
2566      go_on_F4(c);
2567    else
2568      go_on(c);
2569  }
2570  if (c->pair_top<0)
2571    c->completed=TRUE;
2572  I=c->S;
2573  delete c;
2574  if (TEST_OPT_REDSB){
2575    ideal erg=kInterRed(I,NULL);
2576    assume(I!=erg);
2577    id_Delete(&I, currRing);
2578    return erg;
2579  }
2580  //qsort(I->m, IDELEMS(I),sizeof(poly),pLmCmp_func);
2581  assume(I->rank>=idRankFreeModule(I));
2582  return(I);
2583}
2584void now_t_rep(const int & arg_i, const int & arg_j, slimgb_alg* c){
2585  int i,j;
2586  if (arg_i==arg_j){
2587    return;
2588  }
2589  if (arg_i>arg_j){
2590    i=arg_j;
2591    j=arg_i;
2592  } else {
2593    i=arg_i;
2594    j=arg_j;
2595  }
2596  c->states[j][i]=HASTREP;
2597}
2598
2599static BOOLEAN has_t_rep(const int & arg_i, const  int & arg_j, slimgb_alg* state){
2600  assume(0<=arg_i);
2601  assume(0<=arg_j);
2602  assume(arg_i<state->n);
2603  assume(arg_j<state->n);
2604  if (arg_i==arg_j)
2605  {
2606    return (TRUE);
2607  }
2608  if (arg_i>arg_j)
2609  {
2610    return (state->states[arg_i][arg_j]==HASTREP);
2611  } else
2612  {
2613    return (state->states[arg_j][arg_i]==HASTREP);
2614  }
2615}
2616static int pLcmDeg(poly a, poly b)
2617{
2618  int i;
2619  int n=0;
2620  for (i=pVariables; i; i--)
2621  {
2622    n+=si_max( pGetExp(a,i), pGetExp(b,i));
2623  }
2624  return n;
2625
2626}
2627
2628
2629
2630static void shorten_tails(slimgb_alg* c, poly monom)
2631{
2632  return;
2633// BOOLEAN corr=lenS_correct(c->strat);
2634  for(int i=0;i<c->n;i++)
2635  {
2636    //enter tail
2637
2638    if (c->S->m[i]==NULL) continue;
2639    poly tail=c->S->m[i]->next;
2640    poly prev=c->S->m[i];
2641    BOOLEAN did_something=FALSE;
2642    while((tail!=NULL)&& (pLmCmp(tail, monom)>=0))
2643    {
2644      if (p_LmDivisibleBy(monom,tail,c->r))
2645      {
2646        did_something=TRUE;
2647        prev->next=tail->next;
2648        tail->next=NULL;
2649        p_Delete(& tail,c->r);
2650        tail=prev;
2651        //PrintS("Shortened");
2652        c->lengths[i]--;
2653      }
2654      prev=tail;
2655      tail=tail->next;
2656    }
2657    if (did_something)
2658    {
2659      int new_pos;
2660      wlen_type q;
2661      q=pQuality(c->S->m[i],c,c->lengths[i]);
2662      new_pos=simple_posInS(c->strat,c->S->m[i],c->lengths[i],q);
2663
2664      int old_pos=-1;
2665      //assume new_pos<old_pos
2666      for (int z=0;z<=c->strat->sl;z++)
2667      {
2668        if (c->strat->S[z]==c->S->m[i])
2669        {
2670          old_pos=z;
2671          break;
2672        }
2673      }
2674      if (old_pos== -1)
2675        for (int z=new_pos-1;z>=0;z--)
2676        {
2677          if (c->strat->S[z]==c->S->m[i])
2678          {
2679            old_pos=z;
2680            break;
2681          }
2682        }
2683      assume(old_pos>=0);
2684      assume(new_pos<=old_pos);
2685      assume(pLength(c->strat->S[old_pos])==c->lengths[i]);
2686      c->strat->lenS[old_pos]=c->lengths[i];
2687      if (c->strat->lenSw)
2688        c->strat->lenSw[old_pos]=q;
2689
2690      if (new_pos<old_pos)
2691        move_forward_in_S(old_pos,new_pos,c->strat);
2692
2693      length_one_crit(c,i,c->lengths[i]);
2694    }
2695  }
2696}
2697static sorted_pair_node* pop_pair(slimgb_alg* c){
2698  clean_top_of_pair_list(c);
2699
2700  if(c->pair_top<0) return NULL;
2701  else return (c->apairs[c->pair_top--]);
2702}
2703sorted_pair_node* top_pair(slimgb_alg* c){
2704  super_clean_top_of_pair_list(c);//yeah, I know, it's odd that I use a different proc here
2705
2706  if(c->pair_top<0) return NULL;
2707  else return (c->apairs[c->pair_top]);
2708}
2709sorted_pair_node* quick_pop_pair(slimgb_alg* c){
2710  if(c->pair_top<0) return NULL;
2711  else return (c->apairs[c->pair_top--]);
2712}
2713
2714
2715
2716static void super_clean_top_of_pair_list(slimgb_alg* c){
2717  while((c->pair_top>=0)
2718  && (c->apairs[c->pair_top]->i>=0)
2719  && (good_has_t_rep(c->apairs[c->pair_top]->j, c->apairs[c->pair_top]->i,c)))
2720  {
2721
2722    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
2723    c->pair_top--;
2724
2725  }
2726}
2727void clean_top_of_pair_list(slimgb_alg* c){
2728  while((c->pair_top>=0) && (c->apairs[c->pair_top]->i>=0) && (!state_is(UNCALCULATED,c->apairs[c->pair_top]->j, c->apairs[c->pair_top]->i,c))){
2729
2730    free_sorted_pair_node(c->apairs[c->pair_top],c->r);
2731    c->pair_top--;
2732
2733  }
2734}
2735static BOOLEAN state_is(calc_state state, const int & arg_i, const  int & arg_j, slimgb_alg* c){
2736  assume(0<=arg_i);
2737  assume(0<=arg_j);
2738  assume(arg_i<c->n);
2739  assume(arg_j<c->n);
2740  if (arg_i==arg_j)
2741  {
2742    return (TRUE);
2743  }
2744  if (arg_i>arg_j)
2745  {
2746    return (c->states[arg_i][arg_j]==state);
2747  }
2748  else return(c->states[arg_j][arg_i]==state);
2749}
2750
2751
2752void free_sorted_pair_node(sorted_pair_node* s, ring r){
2753  if (s->i>=0)
2754    p_Delete(&s->lcm_of_lm,r);
2755  omfree(s);
2756}
2757static BOOLEAN pair_better(sorted_pair_node* a,sorted_pair_node* b, slimgb_alg* c){
2758  if (a->deg<b->deg) return TRUE;
2759  if (a->deg>b->deg) return FALSE;
2760
2761
2762  int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2763  if (comp==1) return FALSE;
2764  if (-1==comp) return TRUE;
2765  if (a->expected_length<b->expected_length) return TRUE;
2766  if (a->expected_length>b->expected_length) return FALSE;
2767  if (a->i+a->j<b->i+b->j) return TRUE;
2768   if (a->i+a->j>b->i+b->j) return FALSE;
2769  if (a->i<b->i) return TRUE;
2770  if (a->i>b->i) return FALSE;
2771  return TRUE;
2772}
2773
2774static int tgb_pair_better_gen(const void* ap,const void* bp){
2775
2776  sorted_pair_node* a=*((sorted_pair_node**)ap);
2777  sorted_pair_node* b=*((sorted_pair_node**)bp);
2778  assume((a->i>a->j) || (a->i < 0));
2779  assume((b->i>b->j) || (b->i < 0));
2780  if (a->deg<b->deg) return -1;
2781  if (a->deg>b->deg) return 1;
2782
2783
2784 
2785 int comp=pLmCmp(a->lcm_of_lm, b->lcm_of_lm);
2786
2787  if (comp==1) return 1;
2788  if (-1==comp) return -1;
2789   if (a->expected_length<b->expected_length) return -1;
2790  if (a->expected_length>b->expected_length) return 1;
2791  if (a->i+a->j<b->i+b->j) return -1;
2792   if (a->i+a->j>b->i+b->j) return 1;
2793  if (a->i<b->i) return -1;
2794   if (a->i>b->i) return 1;
2795  return 0;
2796}
2797
2798
2799static poly gcd_of_terms(poly p, ring r){
2800  int max_g_0=0;
2801  assume(p!=NULL);
2802  int i;
2803  poly m=pOne();
2804  poly t;
2805  for (i=pVariables; i; i--)
2806  {
2807      pSetExp(m,i, pGetExp(p,i));
2808      if (max_g_0==0)
2809  if (pGetExp(m,i)>0)
2810    max_g_0=i;
2811  }
2812
2813  t=p->next;
2814  while (t!=NULL){
2815
2816    if (max_g_0==0) break;
2817    for (i=max_g_0; i; i--)
2818    {
2819      pSetExp(m,i, si_min(pGetExp(t,i),pGetExp(m,i)));
2820      if (max_g_0==i)
2821  if (pGetExp(m,i)==0)
2822    max_g_0=0;
2823      if ((max_g_0==0) && (pGetExp(m,i)>0)){
2824  max_g_0=i;
2825      }
2826    }
2827    t=t->next;
2828  }
2829
2830  if (max_g_0>0)
2831    return m;
2832  pDelete(&m);
2833  return NULL;
2834}
2835static inline BOOLEAN pHasNotCFExtended(poly p1, poly p2, poly m)
2836{
2837
2838  if (pGetComp(p1) > 0 || pGetComp(p2) > 0)
2839    return FALSE;
2840  int i = 1;
2841  loop
2842  {
2843    if ((pGetExp(p1, i)-pGetExp(m,i) >0) && (pGetExp(p2, i) -pGetExp(m,i)> 0))   return FALSE;
2844    if (i == pVariables)                                return TRUE;
2845    i++;
2846  }
2847}
2848
2849
2850//for impl reasons may return false if the the normal product criterion matches
2851static inline BOOLEAN extended_product_criterion(poly p1, poly gcd1, poly p2, poly gcd2, slimgb_alg* c){
2852  if (c->nc)
2853    return FALSE;
2854  if(gcd1==NULL) return FALSE;
2855        if(gcd2==NULL) return FALSE;
2856        gcd1->next=gcd2; //may ordered incorrect
2857        poly m=gcd_of_terms(gcd1,c->r);
2858        gcd1->next=NULL;
2859        if (m==NULL) return FALSE;
2860
2861        BOOLEAN erg=pHasNotCFExtended(p1,p2,m);
2862        pDelete(&m);
2863        return erg;
2864}
2865static poly kBucketGcd(kBucket* b, ring r)
2866{
2867  int s=0;
2868  int i;
2869  poly m, n;
2870  BOOLEAN initialized=FALSE;
2871  for (i=MAX_BUCKET-1;i>=0;i--)
2872  {
2873    if (b->buckets[i]!=NULL){
2874      if (!initialized){
2875  m=gcd_of_terms(b->buckets[i],r);
2876  initialized=TRUE;
2877  if (m==NULL) return NULL;
2878      }
2879      else
2880  {
2881    n=gcd_of_terms(b->buckets[i],r);
2882    if (n==NULL) {
2883      pDelete(&m);
2884      return NULL;
2885    }
2886    n->next=m;
2887    poly t=gcd_of_terms(n,r);
2888    n->next=NULL;
2889    pDelete(&m);
2890    pDelete(&n);
2891    m=t;
2892    if (m==NULL) return NULL;
2893
2894  }
2895    }
2896  }
2897  return m;
2898}
2899
2900
2901
2902
2903static inline wlen_type quality_of_pos_in_strat_S(int pos, slimgb_alg* c){
2904  if (c->strat->lenSw!=NULL) return c->strat->lenSw[pos];
2905  return c->strat->lenS[pos];
2906}
2907static inline wlen_type quality_of_pos_in_strat_S_mult_high(int pos, poly high, slimgb_alg* c)
2908  //meant only for nc
2909{
2910  poly m=pOne();
2911  pExpVectorDiff(m,high ,c->strat->S[pos]);
2912  poly product=nc_mm_Mult_p(m, pCopy(c->strat->S[pos]), c->r);
2913  wlen_type erg=pQuality(product,c);
2914  pDelete(&m);
2915  pDelete(&product);
2916  return erg;
2917}
2918
2919static void multi_reduction_lls_trick(red_object* los, int losl,slimgb_alg* c,find_erg & erg){
2920  erg.expand=NULL;
2921  BOOLEAN swap_roles; //from reduce_by, to_reduce_u if fromS
2922  if(erg.fromS){
2923    if(pLmEqual(c->strat->S[erg.reduce_by],los[erg.to_reduce_u].p))
2924    {
2925      int i;
2926      wlen_type quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
2927      int best=erg.to_reduce_u+1;
2928/*
2929      for (i=erg.to_reduce_u;i>=erg.to_reduce_l;i--){
2930  int qc=los[i].guess_quality(c);
2931  if (qc<quality_a){
2932    best=i;
2933    quality_a=qc;
2934  }
2935      }
2936      if(best!=erg.to_reduce_u+1){*/
2937      wlen_type qc;
2938      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
2939      if(qc<quality_a){
2940  los[best].flatten();
2941  int b_pos=kBucketCanonicalize(los[best].bucket);
2942  los[best].p=los[best].bucket->buckets[b_pos];
2943  qc=pQuality(los[best].bucket->buckets[b_pos],c);
2944  if(qc<quality_a){
2945    red_object h=los[erg.to_reduce_u];
2946    los[erg.to_reduce_u]=los[best];
2947    los[best]=h;
2948    swap_roles=TRUE;
2949  }
2950  else
2951    swap_roles=FALSE;
2952      }
2953      else{
2954
2955  swap_roles=FALSE;
2956      }
2957
2958    }
2959      else
2960    {
2961      if (erg.to_reduce_u>erg.to_reduce_l){
2962
2963  int i;
2964  wlen_type quality_a=quality_of_pos_in_strat_S(erg.reduce_by,c);
2965  if (c->nc)
2966    quality_a=quality_of_pos_in_strat_S_mult_high(erg.reduce_by, los[erg.to_reduce_u].p, c);
2967  int best=erg.to_reduce_u+1;
2968  wlen_type qc;
2969  best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
2970  assume(qc==los[best].guess_quality(c));
2971  if(qc<quality_a){
2972    los[best].flatten();
2973    int b_pos=kBucketCanonicalize(los[best].bucket);
2974    los[best].p=los[best].bucket->buckets[b_pos];
2975    qc==pQuality(los[best].bucket->buckets[b_pos],c);
2976    //(best!=erg.to_reduce_u+1)
2977    if(qc<quality_a){
2978    red_object h=los[erg.to_reduce_u];
2979    los[erg.to_reduce_u]=los[best];
2980    los[best]=h;
2981    erg.reduce_by=erg.to_reduce_u;
2982    erg.fromS=FALSE;
2983    erg.to_reduce_u--;
2984    }
2985  }
2986      }
2987      else
2988      {
2989  assume(erg.to_reduce_u==erg.to_reduce_l);
2990  wlen_type quality_a=
2991        quality_of_pos_in_strat_S(erg.reduce_by,c);
2992  wlen_type qc=los[erg.to_reduce_u].guess_quality(c);
2993  if (qc<0) PrintS("Wrong wlen_type");
2994  if(qc<quality_a){
2995    int best=erg.to_reduce_u;
2996    los[best].flatten();
2997    int b_pos=kBucketCanonicalize(los[best].bucket);
2998    los[best].p=los[best].bucket->buckets[b_pos];
2999    qc=pQuality(los[best].bucket->buckets[b_pos],c);
3000    assume(qc>=0);
3001    if(qc<quality_a){
3002      BOOLEAN exp=FALSE;
3003      if(qc<=2){
3004         //Print("\n qc is %lld \n",qc);
3005         exp=TRUE;
3006      }
3007
3008      else {
3009         if (qc<quality_a/2)
3010          exp=TRUE;
3011         else
3012       if(erg.reduce_by<c->n/4)
3013          exp=TRUE;
3014      }
3015      if (exp){
3016        poly clear_into;
3017        los[erg.to_reduce_u].flatten();
3018        kBucketClear(los[erg.to_reduce_u].bucket,&clear_into,&erg.expand_length);
3019        erg.expand=pCopy(clear_into);
3020        kBucketInit(los[erg.to_reduce_u].bucket,clear_into,erg.expand_length);
3021        if (TEST_OPT_PROT)
3022    PrintS("e");
3023
3024      }
3025    }
3026  }
3027
3028
3029      }
3030
3031      swap_roles=FALSE;
3032      return;
3033      }
3034
3035  }
3036  else{
3037    if(erg.reduce_by>erg.to_reduce_u){
3038      //then lm(rb)>= lm(tru) so =
3039      assume(erg.reduce_by==erg.to_reduce_u+1);
3040      int best=erg.reduce_by;
3041      wlen_type quality_a=los[erg.reduce_by].guess_quality(c);
3042      wlen_type qc;
3043      best=find_best(los,erg.to_reduce_l,erg.to_reduce_u,qc,c);
3044
3045      int i;
3046      if(qc<quality_a){
3047    red_object h=los[erg.reduce_by];
3048    los[erg.reduce_by]=los[best];
3049    los[best]=h;
3050  }
3051  swap_roles=FALSE;
3052  return;
3053
3054
3055    }
3056    else
3057    {
3058      assume(!pLmEqual(los[erg.reduce_by].p,los[erg.to_reduce_l].p));
3059      assume(erg.to_reduce_u==erg.to_reduce_l);
3060      //further assume, that reduce_by is the above all other polys
3061      //with same leading term
3062      int il=erg.reduce_by;
3063      wlen_type quality_a =los[erg.reduce_by].guess_quality(c);
3064      wlen_type qc;
3065      while((il>0) && pLmEqual(los[il-1].p,los[il].p)){
3066  il--;
3067  qc=los[il].guess_quality(c);
3068  if (qc<quality_a){
3069    quality_a=qc;
3070    erg.reduce_by=il;
3071  }
3072      }
3073      swap_roles=FALSE;
3074    }
3075
3076  }
3077  if(swap_roles)
3078  {
3079    if (TEST_OPT_PROT)
3080      PrintS("b");
3081    poly clear_into;
3082    int dummy_len;
3083    int new_length;
3084    int bp=erg.to_reduce_u;//bucket_positon
3085    //kBucketClear(los[bp].bucket,&clear_into,&new_length);
3086    new_length=los[bp].clear_to_poly();
3087    clear_into=los[bp].p;
3088    poly p=c->strat->S[erg.reduce_by];
3089    int j=erg.reduce_by;
3090    int old_length=c->strat->lenS[j];// in view of S
3091    los[bp].p=p;
3092    kBucketInit(los[bp].bucket,p,old_length);
3093    wlen_type qal=pQuality(clear_into,c,new_length);
3094    int pos_in_c=-1;
3095    int z;
3096    int new_pos;
3097    new_pos=simple_posInS(c->strat,clear_into,new_length, qal);
3098    assume(new_pos<=j);
3099    for (z=c->n;z;z--)
3100    {
3101      if(p==c->S->m[z-1])
3102      {
3103  pos_in_c=z-1;
3104  break;
3105      }
3106    }
3107    if(pos_in_c>=0)
3108    {
3109      #ifdef TGB_RESORT_PAIRS
3110      c->used_b=TRUE;
3111      c->replaced[pos_in_c]=TRUE;
3112      #endif
3113      c->S->m[pos_in_c]=clear_into;
3114      c->lengths[pos_in_c]=new_length;
3115      c->weighted_lengths[pos_in_c]=qal;
3116      if (c->gcd_of_terms[pos_in_c]==NULL)
3117        c->gcd_of_terms[pos_in_c]=gcd_of_terms(clear_into,c->r);
3118      if (c->T_deg_full)
3119  c->T_deg_full[pos_in_c]=pTotaldegree_full(clear_into);
3120      c_S_element_changed_hook(pos_in_c,c);
3121    }
3122    c->strat->S[j]=clear_into;
3123    c->strat->lenS[j]=new_length;
3124
3125    assume(pLength(clear_into)==new_length);
3126    if(c->strat->lenSw!=NULL)
3127      c->strat->lenSw[j]=qal;
3128    if (!rField_is_Zp(c->r))
3129    {
3130      pContent(clear_into);
3131      pCleardenom(clear_into);//should be unnecessary
3132    }
3133    else
3134      pNorm(clear_into);
3135#ifdef FIND_DETERMINISTIC
3136    erg.reduce_by=j;
3137    //resort later see diploma thesis, find_in_S must be deterministic
3138    //during multireduction if spolys are only in the span of the
3139    //input polys
3140#else
3141
3142    if (new_pos<j)
3143    {
3144      move_forward_in_S(j,new_pos,c->strat);
3145      erg.reduce_by=new_pos;
3146    }
3147#endif
3148  }
3149}
3150static int fwbw(red_object* los, int i){
3151   int i2=i;
3152   int step=1;
3153
3154   BOOLEAN bw=FALSE;
3155   BOOLEAN incr=TRUE;
3156
3157   while(1)
3158   {
3159     if(!bw)
3160     {
3161       step=si_min(i2,step);
3162       if (step==0) break;
3163       i2-=step;
3164
3165       if(!pLmEqual(los[i].p,los[i2].p))
3166       {
3167   bw=TRUE;
3168   incr=FALSE;
3169       }
3170       else
3171       {
3172   if ((!incr) &&(step==1)) break;
3173       }
3174
3175
3176     }
3177     else
3178     {
3179
3180       step=si_min(i-i2,step);
3181       if (step==0) break;
3182       i2+=step;
3183       if(pLmEqual(los[i].p,los[i2].p)){
3184   if(step==1) break;
3185   else
3186   {
3187     bw=FALSE;
3188   }
3189       }
3190
3191     }
3192     if (incr)
3193       step*=2;
3194     else
3195     {
3196       if (step%2==1)
3197   step=(step+1)/2;
3198       else
3199   step/=2;
3200
3201     }
3202   }
3203   return i2;
3204}
3205static void canonicalize_region(red_object* los, int l, int u,slimgb_alg* c){
3206    assume(l<=u+1);
3207    int i;
3208    for(i=l;i<=u;i++){
3209        kBucketCanonicalize(los[i].bucket);
3210    }
3211
3212}
3213static void multi_reduction_find(red_object* los, int losl,slimgb_alg* c,int startf,find_erg & erg){
3214  kStrategy strat=c->strat;
3215
3216  assume(startf<=losl);
3217  assume((startf==losl-1)||(pLmCmp(los[startf].p,los[startf+1].p)==-1));
3218  int i=startf;
3219
3220  int j;
3221  while(i>=0){
3222    assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)<=0));
3223    assume(is_valid_ro(los[i]));
3224    j=kFindDivisibleByInS_easy(strat,los[i]);
3225    if(j>=0){
3226
3227      erg.to_reduce_u=i;
3228      erg.reduce_by=j;
3229      erg.fromS=TRUE;
3230      int i2=fwbw(los,i);
3231      assume(pLmEqual(los[i].p,los[i2].p));
3232      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
3233      assume(i>=i2);
3234
3235
3236      erg.to_reduce_l=i2;
3237      assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
3238      canonicalize_region(los,erg.to_reduce_u+1,startf,c);
3239      return;
3240    }
3241    if (j<0){
3242
3243      //not reduceable, try to use this for reducing higher terms
3244      int i2=fwbw(los,i);
3245      assume(pLmEqual(los[i].p,los[i2].p));
3246      assume((i2==0)||(!pLmEqual(los[i2].p,los[i2-1].p)));
3247      assume(i>=i2);
3248      if(i2!=i){
3249
3250
3251  erg.to_reduce_u=i-1;
3252  erg.to_reduce_l=i2;
3253  erg.reduce_by=i;
3254  erg.fromS=FALSE;
3255  assume((i==losl-1)||(pLmCmp(los[i].p,los[i+1].p)==-1));
3256  canonicalize_region(los,erg.to_reduce_u+1,startf,c);
3257  return;
3258      }
3259
3260      i--;
3261    }
3262  }
3263  erg.reduce_by=-1;//error code
3264  return;
3265}
3266
3267 //  nicht reduzierbare eintraege in ergebnisliste schreiben
3268//   nullen loeschen
3269//   while(finde_groessten leitterm reduzierbar(c,erg)){
3270
3271static int multi_reduction_clear_zeroes(red_object* los, int  losl, int l, int u)
3272{
3273
3274
3275  int deleted=0;
3276  int  i=l;
3277  int last=-1;
3278  while(i<=u)
3279  {
3280
3281    if(los[i].p==NULL){
3282      kBucketDestroy(&los[i].bucket);
3283//      delete los[i];//here we assume los are constructed with new
3284      //destroy resources, must be added here
3285     if (last>=0)
3286     {
3287       memmove(los+(int)(last+1-deleted),los+(last+1),sizeof(red_object)*(i-1-last));
3288     }
3289     last=i;
3290     deleted++;
3291    }
3292    i++;
3293  }
3294  if((last>=0)&&(last!=losl-1))
3295      memmove(los+(int)(last+1-deleted),los+last+1,sizeof(red_object)*(losl-1-last));
3296  return deleted;
3297
3298}
3299
3300static void sort_region_down(red_object* los, int l, int u, slimgb_alg* c)
3301{
3302  qsort(los+l,u-l+1,sizeof(red_object),red_object_better_gen);
3303  int i;
3304
3305  for(i=l;i<=u;i++)
3306  {
3307    BOOLEAN moved=FALSE;
3308    int j;
3309    for(j=i;j;j--)
3310    {
3311      if(pLmCmp(los[j].p,los[j-1].p)==-1){
3312  red_object h=los[j];
3313  los[j]=los[j-1];
3314  los[j-1]=h;
3315  moved=TRUE;
3316      }
3317      else break;
3318    }
3319    if(!moved) return;
3320  }
3321}
3322
3323//assume that los is ordered ascending by leading term, all non zero
3324static void multi_reduction(red_object* los, int & losl, slimgb_alg* c)
3325{
3326  poly* delay=(poly*) omalloc(losl*sizeof(poly));
3327  int delay_s=0;
3328  //initialize;
3329  assume(c->strat->sl>=0);
3330  assume(losl>0);
3331  int i;
3332  wlen_type max_initial_quality=0;
3333
3334  for(i=0;i<losl;i++){
3335    los[i].sev=pGetShortExpVector(los[i].p);
3336//SetShortExpVector();
3337    los[i].p=kBucketGetLm(los[i].bucket);
3338    if (los[i].initial_quality>max_initial_quality)
3339        max_initial_quality=los[i].initial_quality;
3340    // else
3341//         Print("init2_qal=%lld;", los[i].initial_quality);
3342//     Print("initial_quality=%lld;",max_initial_quality);
3343  }
3344
3345  kStrategy strat=c->strat;
3346  int curr_pos=losl-1;
3347
3348
3349//  nicht reduzierbare einträge in ergebnisliste schreiben
3350  // nullen loeschen
3351  while(curr_pos>=0){
3352
3353    find_erg erg;
3354    multi_reduction_find(los, losl,c,curr_pos,erg);//last argument should be curr_pos
3355    if(erg.reduce_by<0) break;
3356
3357
3358
3359    erg.expand=NULL;
3360    int d=erg.to_reduce_u-erg.to_reduce_l+1;
3361
3362
3363    multi_reduction_lls_trick(los,losl,c,erg);
3364
3365
3366    int i;
3367    int len;
3368    //    wrp(los[erg.to_reduce_u].p);
3369    //Print("\n");
3370    multi_reduce_step(erg,los,c);
3371
3372
3373    if(!K_TEST_OPT_REDTHROUGH){
3374  for(i=erg.to_reduce_l;i<=erg.to_reduce_u;i++){
3375     if  (los[i].p!=NULL)  //the check (los[i].p!=NULL) might be invalid
3376     {
3377         //
3378         assume(los[i].initial_quality>0);
3379
3380               if(los[i].guess_quality(c)
3381                  >1.5*delay_factor*max_initial_quality){
3382                       if (TEST_OPT_PROT)
3383                           PrintS("v");
3384                       los[i].canonicalize();
3385                       if(los[i].guess_quality(c)
3386                           >delay_factor*max_initial_quality){
3387                               if (TEST_OPT_PROT)
3388                                   PrintS(".");
3389                               los[i].clear_to_poly();
3390                               //delay.push_back(los[i].p);
3391                               delay[delay_s]=los[i].p;
3392                               delay_s++;
3393
3394                               los[i].p=NULL;
3395
3396                      }
3397                  }
3398
3399            }
3400     }
3401  }
3402    int deleted=multi_reduction_clear_zeroes(los, losl, erg.to_reduce_l, erg.to_reduce_u);
3403    if(erg.fromS==FALSE)
3404      curr_pos=si_max(erg.to_reduce_u,erg.reduce_by);
3405    else
3406      curr_pos=erg.to_reduce_u;
3407    losl -= deleted;
3408    curr_pos -= deleted;
3409
3410    //Print("deleted %i \n",deleted);
3411    if ((TEST_V_UPTORADICAL) &&(!(erg.fromS)))
3412        sort_region_down(los,si_min(erg.to_reduce_l,erg.reduce_by),(si_max(erg.to_reduce_u,erg.reduce_by))-deleted,c);
3413    else
3414    sort_region_down(los, erg.to_reduce_l, erg.to_reduce_u-deleted, c);
3415
3416
3417    if(erg.expand)
3418    {
3419#ifdef FIND_DETERMINISTIC
3420      int i;
3421      for(i=0;c->expandS[i];i++);
3422      c->expandS=(poly*) omrealloc(c->expandS,(i+2)*sizeof(poly));
3423      c->expandS[i]=erg.expand;
3424      c->expandS[i+1]=NULL;
3425#else
3426      add_to_reductors(c,erg.expand,erg.expand_length);
3427#endif
3428    }
3429
3430  }
3431
3432
3433  //sorted_pair_node** pairs=(sorted_pair_node**)
3434  //  omalloc(delay_s*sizeof(sorted_pair_node*));
3435  c->introduceDelayedPairs(delay,delay_s);
3436  /*
3437  for(i=0;i<delay_s;i++){
3438
3439      poly p=delay[i];
3440      //if (rPar(c->r)==0)
3441      simplify_poly(p,c->r);
3442      sorted_pair_node* si=(sorted_pair_node*) omalloc(sizeof(sorted_pair_node));
3443      si->i=-1;
3444      si->j=-1;
3445       if (!rField_is_Zp(c->r)){
3446        if (!c->nc)
3447            p=redTailShort(p, c->strat);
3448        pCleardenom(p);
3449        pContent(p);
3450      }
3451      si->expected_length=pQuality(p,c,pLength(p));
3452      si->deg=pTotaldegree(p);
3453
3454      si->lcm_of_lm=p;
3455      pairs[i]=si;
3456  }
3457  qsort(pairs,delay_s,sizeof(sorted_pair_node*),tgb_pair_better_gen2);
3458  c->apairs=spn_merge(c->apairs,c->pair_top+1,pairs,delay_s,c);
3459  c->pair_top+=delay_s;*/
3460  omfree(delay);
3461  //omfree(pairs);
3462  return;
3463}
3464void red_object::flatten(){
3465  assume(p==kBucketGetLm(bucket));
3466}
3467void red_object::validate(){
3468  p=kBucketGetLm(bucket);
3469  if(p)
3470    sev=pGetShortExpVector(p);
3471}
3472int red_object::clear_to_poly(){
3473  flatten();
3474  int l;
3475  kBucketClear(bucket,&p,&l);
3476  return l;
3477}
3478
3479
3480
3481
3482
3483void reduction_step::reduce(red_object* r, int l, int u){}
3484void simple_reducer::do_reduce(red_object & ro){
3485  number coef;
3486  if (!c->nc)
3487    coef=kBucketPolyRed(ro.bucket,p,
3488       p_len,
3489       c->strat->kNoether);
3490  else
3491    nc_kBucketPolyRed_Z(ro.bucket, p, &coef);
3492  nDelete(&coef);
3493}
3494
3495
3496void simple_reducer::reduce(red_object* r, int l, int u){
3497  this->pre_reduce(r,l,u);
3498  int i;
3499//debug start
3500  int im;
3501
3502
3503  for(i=l;i<=u;i++){
3504
3505
3506
3507    this->do_reduce(r[i]);
3508
3509  }
3510  for(i=l;i<=u;i++){
3511
3512    kBucketSimpleContent(r[i].bucket);
3513    r[i].validate();
3514    #ifdef TGB_DEBUG
3515    #endif
3516  }
3517}
3518reduction_step::~reduction_step(){}
3519simple_reducer::~simple_reducer(){
3520  if(fill_back!=NULL)
3521  {
3522    kBucketInit(fill_back,p,p_len);
3523  }
3524  fill_back=NULL;
3525
3526}
3527
3528void multi_reduce_step(find_erg & erg, red_object* r, slimgb_alg* c){
3529  static int id=0;
3530  id++;
3531  unsigned long sev;
3532    BOOLEAN lt_changed=FALSE;
3533  int rn=erg.reduce_by;
3534  poly red;
3535  int red_len;
3536  simple_reducer* pointer;
3537  BOOLEAN work_on_copy=FALSE;
3538  if(erg.fromS){
3539    red=c->strat->S[rn];
3540    red_len=c->strat->lenS[rn];
3541    assume(red_len==pLength(red));
3542  }
3543  else
3544  {
3545    r[rn].flatten();
3546    kBucketClear(r[rn].bucket,&red,&red_len);
3547
3548    if (!rField_is_Zp(c->r))
3549    {
3550      pContent(red);
3551      pCleardenom(red);//should be unnecessary
3552
3553    }
3554    pNormalize(red);
3555
3556
3557    if ((!(erg.fromS))&&(TEST_V_UPTORADICAL)){
3558
3559         if (polynomial_root(red,c->r))
3560            lt_changed=TRUE;
3561            sev=p_GetShortExpVector(red,c->r);}
3562    red_len=pLength(red);
3563  }
3564  if (((TEST_V_MODPSOLVSB)&&(red_len>1))||((c->nc)||(erg.to_reduce_u-erg.to_reduce_l>5))){
3565    work_on_copy=TRUE;
3566    // poly m=pOne();
3567    poly m=c->tmp_lm;
3568    pSetCoeff(m,nInit(1));
3569    for(int i=1;i<=pVariables;i++)
3570      pSetExp(m,i,(pGetExp(r[erg.to_reduce_l].p, i)-pGetExp(red,i)));
3571    pSetm(m);
3572    poly red_cp;
3573    if (!c->nc)
3574      red_cp=ppMult_mm(red,m);
3575    else
3576      red_cp=nc_mm_Mult_p(m, pCopy(red), c->r);
3577    if(!erg.fromS){
3578      kBucketInit(r[rn].bucket,red,red_len);
3579    }
3580    //now reduce the copy
3581    //static poly redNF2 (poly h,slimgb_alg* c , int &len, number&  m,int n)
3582
3583    if (!c->nc)
3584      redTailShort(red_cp,c->strat);
3585    //number mul;
3586    // red_len--;
3587//     red_cp->next=redNF2(red_cp->next,c,red_len,mul,c->average_length);
3588//     pSetCoeff(red_cp,nMult(red_cp->coef,mul));
3589//     nDelete(&mul);
3590//     red_len++;
3591    red=red_cp;
3592    red_len=pLength(red);
3593    // pDelete(&m);
3594
3595  }
3596  int i;
3597
3598
3599
3600  assume(red_len==pLength(red));
3601
3602  pointer=new simple_reducer(red,red_len,c);
3603
3604  if ((!work_on_copy) && (!erg.fromS))
3605    pointer->fill_back=r[rn].bucket;
3606  else
3607    pointer->fill_back=NULL;
3608  pointer->reduction_id=id;
3609  pointer->c=c;
3610
3611  pointer->reduce(r,erg.to_reduce_l, erg.to_reduce_u);
3612  if(work_on_copy) pDelete(&pointer->p);
3613  delete pointer;
3614  if (lt_changed){
3615    assume(!erg.fromS);
3616    r[erg.reduce_by].sev=sev;
3617  }
3618
3619};
3620
3621
3622
3623
3624void simple_reducer:: pre_reduce(red_object* r, int l, int u){}
3625
Note: See TracBrowser for help on using the repository browser.