source: git/kernel/f5lists.cc @ 598870

spielwiese
Last change on this file since 598870 was 598870, checked in by Christian Eder, 15 years ago
still debugging git-svn-id: file:///usr/local/Singular/svn/trunk@11454 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 22.1 KB
Line 
1#include "mod2.h"
2
3#ifdef HAVE_F5
4#include "kutil.h"
5#include "structs.h"
6#include "omalloc.h"
7#include "polys.h"
8#include "p_polys.h"
9#include "ideals.h"
10#include "febase.h"
11#include "kstd1.h"
12#include "khstd.h"
13#include "kbuckets.h"
14#include "weight.h"
15#include "intvec.h"
16#include "pInline1.h"
17#include "f5gb.h"
18#include "f5data.h"
19#include "f5lists.h"
20
21/*
22====================================
23functions working on the class LNode
24====================================
25*/
26
27// generating new list elements (labeled / classical polynomial / LNode view)
28LNode::LNode() {
29    data                =   NULL;
30    next                =   NULL;
31    gPrevRedCheck       =   NULL;
32}
33 LNode::LNode(LPoly* lp) {
34    data                =   lp;
35    next                =   NULL;
36    gPrevRedCheck       =   NULL;
37}
38       
39LNode::LNode(LPoly* lp, LNode* l) {
40//Print("HIER LNODE\n");
41    data                =   lp;
42    next                =   l;
43    gPrevRedCheck       =   NULL;
44}
45
46LNode::LNode(poly t, int i, poly p, Rule* r, LNode* gPCheck) {
47LPoly* lp           =   new LPoly(t,i,p,r);
48data                =   lp;
49next                =   NULL;
50gPrevRedCheck       =   gPCheck;
51}
52       
53LNode::LNode(poly t, int i, poly p, Rule* r, LNode* gPCheck, LNode* l) {
54    LPoly* lp           =   new LPoly(t,i,p,r);
55    data                =   lp;
56    next                =   l;
57    gPrevRedCheck       =   gPCheck;
58}
59
60 LNode::LNode(LNode* ln) {
61    data                =   ln->getLPoly();
62    next                =   ln->getNext();
63    gPrevRedCheck       =   NULL;
64}
65       
66LNode::~LNode() {
67    //delete next;
68    delete gPrevRedCheck;
69    delete data;   
70}
71       
72// insert new elements to the list always at the end (labeled / classical polynomial view)
73// needed for list gPrev
74LNode* LNode::insert(LPoly* lp) {
75    //Print("INSERTION: \n");
76    //Print("LAST GPREV: ");
77    //pWrite(this->getPoly());
78LNode* newElement   =   new LNode(lp, NULL);
79    this->next          =   newElement;
80    return newElement;
81}
82       
83LNode* LNode::insert(poly t, int i, poly p, Rule* r) {
84    LNode* newElement   =   new LNode(t, i, p, r, NULL, NULL);
85    this->next          =   newElement;
86    return newElement;
87}
88
89// insert new elements to the list always in front (labeled / classical polynomial view)
90// needed for sPolyList
91LNode* LNode::insertSP(LPoly* lp) {
92    LNode* newElement   =   new LNode(lp, this);
93    return newElement;
94}
95       
96LNode* LNode::insertSP(poly t, int i, poly p, Rule* r) {
97    LNode* newElement   =   new LNode(t, i, p, r, NULL, this);
98    return newElement;
99}
100// insert new elemets to the list w.r.t. increasing labels
101// only used for the S-polys to be reduced (TopReduction building new S-polys with higher label)
102LNode* LNode::insertByLabel(poly t, int i, poly p, Rule* r) {
103    if(NULL == this || NULL == data) {
104        LNode* newElement   =   new LNode(t, i, p, r, this);
105        return newElement;
106    }
107    else {
108        if(0 == pLmCmp(t,this->getTerm()) || -1 == pLmCmp(t,this->getTerm())) {
109            LNode* newElement   =   new LNode(t, i, p, r, this);
110            return newElement;
111        }
112        else {
113            LNode* temp = this;
114            while(NULL != temp->next && NULL != temp->next->data) {
115                if( 0 == pLmCmp(t,temp->next->getTerm()) || -1 == pLmCmp(t,temp->next->getTerm())) {
116                    LNode* newElement   =   new LNode(t, i, p, r, temp->next);
117                    temp->next          =   newElement;
118                    return this;
119                }
120                else {
121                    temp = temp->next;
122                    //Print("%p\n",temp);
123                    //Print("%p\n",temp->data);
124                   
125                    //Print("%p\n",temp->next);
126                }
127            }
128        //Print("HIER\n");
129            LNode* newElement   =   new LNode(t, i, p, r, temp->next);
130            temp->next          =   newElement;
131            return this;
132        }
133    }
134}
135
136// deletes the first elements of the list with the same degree
137// only used for the S-polys, which are already sorted by increasing degree by CList
138LNode*  LNode::deleteByDeg() {
139    return this;
140}
141
142// get next from current LNode
143LNode* LNode::getNext() {
144    return next;
145}
146
147// get the LPoly* out of LNode*
148LPoly* LNode::getLPoly() {
149    return data;
150}
151
152// get the data from the LPoly saved in LNode
153poly LNode::getPoly() {
154    return data->getPoly();
155}
156
157poly LNode::getTerm() {
158    return data->getTerm();
159}
160
161int LNode::getIndex() {
162    return data->getIndex();
163}
164
165Rule* LNode::getRule() {
166    return data->getRule();
167}
168
169LNode* LNode::getGPrevRedCheck() {
170    return gPrevRedCheck;
171}
172
173// set the data from the LPoly saved in LNode
174void LNode::setPoly(poly p) {
175    data->setPoly(p);
176}
177
178void LNode::setTerm(poly t) {
179    data->setTerm(t);
180}
181
182void LNode::setIndex(int i) {
183    data->setIndex(i);
184}
185
186void LNode::setGPrevRedCheck(LNode* l) {
187    gPrevRedCheck   =   l;
188}
189
190// test if for any list element the polynomial part of the data is equal to *p
191bool LNode::polyTest(poly* p) {
192    LNode* temp = new LNode(this);
193    while(NULL != temp) {
194        if(pComparePolys(temp->getPoly(),*p)) {
195            return 1;
196        }
197        temp = temp->next;
198    }
199    return 0;
200}
201
202LNode* LNode::getNext(LNode* l) {
203    return l->next;
204}
205
206// for debugging
207void LNode::print() {
208    LNode* temp = this;
209    Print("___________________List of S-polynomials______________________:\n");
210    Print("%p\n",this);
211    while(NULL != temp && NULL != temp->data) {
212        Print("HIER\n");
213        Print("Index: %d\n",temp->getIndex());
214        Print("Term: ");
215        //pWrite(temp->getTerm());
216        Print("Poly: ");
217        //pWrite(temp->getPoly());
218        Print("\n");
219        temp = temp->next;
220    }
221}
222
223
224/*
225====================================
226functions working on the class LList
227====================================
228*/
229
230LList::LList() {
231    first   =   new LNode();
232    last    =   first;
233    length  =   0;
234}
235
236LList::LList(LPoly* lp) {
237    first   =   new LNode(lp);
238    last    =   first;
239    length  =   1;
240}
241
242LList::LList(poly t,int i,poly p,Rule* r) {
243    first   =   new LNode(t,i,p,r);
244    last    =   first;
245    length  =   1;
246} 
247
248LList::~LList() {
249    delete first;
250}
251
252// insertion at the end of the list, needed for gPrev
253void LList::insert(LPoly* lp) {
254    last = last->insert(lp);
255    //Print("NEW LAST GPREV: ");
256    //pWrite(last->getPoly());
257    length++;
258    //Print("LENGTH %d\n",length);
259}
260
261void LList::insert(poly t,int i, poly p, Rule* r) {
262    last = last->insert(t,i,p,r);
263    length++;
264    //Print("LENGTH %d\n",length);
265}
266
267// insertion in front of the list, needed for sPolyList
268void LList::insertSP(LPoly* lp) {
269    first = first->insertSP(lp);
270    length++;
271    //Print("LENGTH %d\n",length);
272}
273
274void LList::insertSP(poly t,int i, poly p, Rule* r) {
275    first = first->insertSP(t,i,p,r);
276    length++;
277    //Print("LENGTH %d\n",length);
278}
279
280
281void LList::insertByLabel(poly t, int i, poly p, Rule* r) {
282    first = first->insertByLabel(t,i,p,r);
283    length++;
284    //Print("LENGTH %d\n",length);
285}
286
287void LList::insertByLabel(LNode* l) {
288    first = first->insertByLabel(l->getTerm(),l->getIndex(),l->getPoly(),l->getRule());
289    length++;
290    //Print("LENGTH %d\n",length);
291}
292
293void LList::deleteByDeg() {
294    first = first->deleteByDeg();
295}
296
297bool LList::polyTest(poly* p) {
298    return first->polyTest(p);
299}
300
301LNode* LList::getFirst() {
302    return first;
303}
304
305LNode* LList::getLast() {
306    return last;
307}
308
309int LList::getLength() {
310    return length;
311}
312
313void LList::setFirst(LNode* l) {
314    first       =   l;
315    length--;
316}
317
318void LList::print() {
319    first->print();
320}
321
322/*
323=======================================
324functions working on the class LTagNode
325=======================================
326*/
327LTagNode::LTagNode() {
328    data    =   NULL;
329    next    =   NULL;
330}
331
332LTagNode::LTagNode(LNode* l) {
333    data = l;
334    next = NULL;
335}
336       
337LTagNode::LTagNode(LNode* l, LTagNode* n) {
338    data = l;
339    next = n;
340}
341
342 LTagNode::~LTagNode() {
343    delete next;
344    delete data;   
345}
346       
347// declaration with first as parameter due to sorting of LTagList
348LTagNode* LTagNode::insert(LNode* l) {
349    LTagNode* newElement  = new LTagNode(l, this);
350    return newElement;
351}
352
353LNode* LTagNode::getLNode() {
354    return this->data;
355}
356
357LTagNode* LTagNode::getNext() {
358    return next;
359}
360
361// NOTE: We insert at the beginning of the list and length = i-1, where i is the actual index.
362//       Thus given actual index i and idx being the index of the LPoly under investigation
363//       the element on position length-idx is the right one
364LNode* LTagNode::get(int idx, int length) {
365    if(idx == 1) {
366        return NULL;
367    }
368    else {
369        int j;
370        LTagNode* temp = this; // last
371        for(j=1;j<=length-idx+1;j++) {
372            temp = temp->next;
373        }
374        return temp->data;
375    }
376}
377
378
379/*
380=======================================
381functions working on the class LTagList
382=======================================
383*/
384LTagList::LTagList() {
385    LTagNode* first =   new LTagNode();
386   
387    length          =   0;
388}
389
390LTagList::LTagList(LNode* l) {
391    LTagNode* first =   new LTagNode(l);
392    length          =   1;
393}
394
395// declaration with first as parameter in LTagNode due to sorting of LTagList
396void LTagList::insert(LNode* l) {
397    first   =   first->insert(l);
398    length++;
399}
400
401void LTagList::setFirstCurrentIdx(LNode* l) {
402    firstCurrentIdx =   l;
403}
404
405LNode* LTagList::get(int idx) {
406    return first->get(idx, length);
407}
408
409LNode* LTagList::getFirst() {
410    return first->getLNode();
411}
412
413LNode* LTagList::getFirstCurrentIdx() {
414    return firstCurrentIdx;
415}
416
417/*
418=====================================
419functions working on the class TopRed
420=====================================
421*/
422
423TopRed::TopRed() {
424    _completed  =   NULL;
425    _toDo       =   NULL;
426}
427
428TopRed::TopRed(LList* c, LList* t) {
429    _completed  =   c;
430    _toDo       =   t;
431}
432
433LList* TopRed::getCompleted() {
434    return _completed;
435}
436
437LList* TopRed::getToDo() {
438    return _toDo;
439}
440
441/*
442====================================
443functions working on the class CNode
444====================================
445*/
446
447CNode::CNode() {
448    data    =   NULL;   
449    next    =   NULL;   
450}
451
452CNode::CNode(CPair* c) {
453    data    =   c;   
454    next    =   NULL;   
455}
456
457CNode::CNode(CPair* c, CNode* n) {
458    data    =   c;   
459    next    =   n;   
460}
461
462CNode::~CNode() {
463    delete next;
464    delete data;
465}
466
467// insert sorts the critical pairs firstly by increasing total degree, secondly by increasing label
468// note: as all critical pairs have the same index here, the second sort is done on the terms of the labels
469// working only with linked, but not doubly linked lists due to memory usage we have to check the
470// insertion around the first element separately from the insertion around all other elements in the list
471CNode* CNode::insert(CPair* c, CNode* last) {
472    if(NULL == this->data) {
473        CNode* newElement   =   new CNode(c, this);
474        return newElement;
475    }
476    else {
477        poly u1 = ppMult_qq(c->getT1(),c->getLp1Term());
478        if( c->getDeg() < this->data->getDeg() ) { // lower degree than the first list element
479            CNode* newElement   =   new CNode(c, this);
480            return newElement;
481        }
482        if( c->getDeg() == this->data->getDeg() ) { // same degree than the first list element
483            if(1 != pLmCmp(u1,ppMult_qq(this->data->getT1(), this->data->getLp1Term()))) {
484                //pWrite(u1);
485                //Print("Multi-Term in CritPairs Sortierung altes Element: ");
486                //pWrite(ppMult_qq(this->data->getT1(),this->data->getLp1Term()));
487                CNode* newElement   =   new CNode(c, this);
488                return newElement;
489            }
490            else {
491                //Print("Insert Deg\n");
492                CNode* temp = this;
493                while(  NULL != temp->next->data ) {
494                    if(temp->next->data->getDeg() == c->getDeg() ) { 
495                        if(1 == pLmCmp(u1,ppMult_qq(temp->next->data->getT1(),temp->next->data->getLp1Term()))) {
496                            temp = temp->next;
497                        }
498                        else {
499                            CNode* newElement   =   new CNode(c, temp->next);
500                            temp->next          =   newElement;
501                            return this;
502                        } 
503                    }
504                    else {
505                        CNode* newElement   =   new CNode(c, temp->next);
506                        temp->next          =   newElement;
507                        return this;
508                    }
509                }
510                CNode* newElement   =   new CNode(c, last);
511                temp->next          =   newElement;
512                return this;
513            }
514        } // outer if-clause
515        if( c->getDeg() > this->data->getDeg() ) { // greater degree than the first list element
516            CNode* temp =   this;
517            while( NULL != temp->next->data ) {   
518                if( c->getDeg() < temp->next->data->getDeg() ) {
519                    CNode* newElement   =   new CNode(c, temp->next);
520                    temp->next          =   newElement;
521                    return this;
522                }
523                if( c->getDeg() == temp->next->data->getDeg() ) {
524                    if(1 != pLmCmp(u1,ppMult_qq(temp->next->data->getT1(),temp->next->data->getLp1Term()))) { 
525                        CNode* newElement   =   new CNode(c, temp->next);
526                        temp->next          =   newElement;
527                        return this;
528                    }
529                    else {
530                        temp = temp->next;
531                        while(  NULL != temp->next->data ) {
532                            if( temp->next->data->getDeg() == c->getDeg() ) { 
533                                if(1 == pLmCmp(u1,ppMult_qq(temp->next->data->getT1(),
534                                               temp->next->data->getLp1Term()))) {
535                                    temp = temp->next;
536                                }
537                                else {
538                                    CNode* newElement   =   new CNode(c, temp->next);
539                                    temp->next          =   newElement;
540                                    return this;
541                                } 
542                            }
543                            else {
544                                CNode* newElement   =   new CNode(c, temp->next);
545                                temp->next          =   newElement;
546                                return this;
547                            }
548                        }
549                        CNode* newElement   =   new CNode(c, last);
550                        temp->next          =   newElement;
551                        return this;
552                    }
553                }
554                if( c->getDeg() > temp->next->data->getDeg() ) {
555                    temp    =   temp->next;
556                }
557            }
558            CNode* newElement   =   new CNode(c, last);
559            temp->next          =   newElement;
560            return this;
561        }
562    }
563}
564
565// get the first elements from CList which by the above sorting have minimal degree
566CNode* CNode::getMinDeg() {
567    CNode* temp = this;
568    while( NULL != temp->data ) {
569        while(NULL != temp->next->data && temp->next->data->getDeg() == this->data->getDeg()) {
570            temp = temp->next;
571        }
572        CNode* returnCNode  =   temp->next;   
573        // every CList should end with a (NULL,NULL) element for a similar behaviour
574        // using termination conditions throughout the algorithm
575        temp->next          =   new CNode();
576        return returnCNode;
577    }
578    return NULL;
579}
580
581CPair* CNode::getData() {
582    return data;
583}
584
585CNode* CNode::getNext() {
586    return next;
587}
588
589LPoly* CNode::getAdLp1() {
590    return this->data->getAdLp1();
591}
592
593LPoly* CNode::getAdLp2() {
594    return this->data->getAdLp2();
595}
596
597poly CNode::getLp1Poly() {
598    return this->data->getLp1Poly();
599}
600
601poly CNode::getLp2Poly() {
602    return this->data->getLp2Poly();
603}
604
605poly CNode::getLp1Term() {
606    return this->data->getLp1Term();
607}
608
609poly CNode::getLp2Term() {
610    return this->data->getLp2Term();
611}
612
613int CNode::getLp1Index() {
614    return this->data->getLp1Index();
615}
616
617int CNode::getLp2Index() {
618    return this->data->getLp2Index();
619}
620
621poly CNode::getT1() {
622    return this->data->getT1();
623}
624
625poly* CNode::getAdT1() {
626    return this->data->getAdT1();
627}
628
629poly CNode::getT2() {
630    return this->data->getT2();
631}
632
633poly* CNode::getAdT2() {
634    return this->data->getAdT2();
635}
636
637Rule* CNode::getTestedRule() {
638    return this->data->getTestedRule();
639}
640
641// for debugging
642void CNode::print() {
643    CNode* temp = this;
644    Print("___________________List of critical pairs______________________:\n");
645    while(NULL != temp->data) {
646        Print("LP1 Index: %d\n",temp->getLp1Index());
647        Print("T1: ");
648        pWrite(temp->getT1());
649        Print("LP1 Term: ");
650        pWrite(temp->getLp1Term());
651        Print("LP1 Poly: ");
652        pWrite(temp->getLp1Poly());
653        Print("LP2 Index: %d\n",temp->getLp2Index());
654        Print("T2: ");
655        pWrite(temp->getT2());
656        Print("LP2 Term: ");
657        pWrite(temp->getLp2Term());
658        Print("LP2 Poly: ");
659        pWrite(temp->getLp2Poly());
660        Print("\n");
661        temp = temp->next;
662    }
663}
664
665/*
666====================================
667functions working on the class CList
668====================================
669*/
670// for initialization of CLists, last element alwas has data=NULL and next=NULL
671CList::CList() {
672    first   =   new CNode();
673    last    =   first;
674}
675
676CList::CList(CPair* c) {
677    first   =   new CNode(c);
678    last    =   first;
679}
680
681CList::~CList() {
682    delete first;
683}
684
685// insert sorts the critical pairs firstly by increasing total degree, secondly by increasing label
686// note: as all critical pairs have the same index here, the second sort is done on the terms of the labels
687void CList::insert(CPair* c) {
688    first = first->insert(c, last);
689}
690
691CNode* CList::getFirst() {
692    return first;
693}
694
695// get the first elements from CList which by the above sorting have minimal degree
696// returns the pointer on the first element of those
697CNode* CList::getMinDeg() {
698    CNode* temp     =   first;
699    first           =   first->getMinDeg();
700    return temp;
701}
702
703void CList::print() {
704    first->print();
705}
706
707/*
708====================================
709functions working on the class RNode
710====================================
711*/
712RNode::RNode() {
713    data    =   NULL;
714    next    =   NULL;
715}
716
717RNode::RNode(Rule* r) {
718    data    =   r;
719    next    =   NULL;
720}
721
722RNode::~RNode() {
723    delete  next;
724    delete  data;
725}
726
727RNode* RNode::insert(Rule* r) {
728    RNode* newElement   =   new RNode(r);
729    newElement->next    =   this;
730    return newElement;
731}
732
733RNode* RNode::insert(int i, poly t) {
734    Rule*   r           =   new Rule(i,t);
735    RNode* newElement   =   new RNode(r);
736    newElement->next    =   this;
737    return newElement;
738}
739
740RNode* RNode::getNext() {
741    return next;
742}   
743
744Rule* RNode::getRule() {
745    return data;
746}
747
748int RNode::getRuleIndex() {
749    return data->getIndex();
750}
751
752poly RNode::getRuleTerm() {
753    return data->getTerm();
754}
755
756/*
757====================================
758functions working on the class RList
759====================================
760*/
761RList::RList() {
762    first = new RNode();
763}
764
765RList::RList(Rule* r) {
766    first = new RNode(r);
767}
768
769void RList::insert(int i, poly t) {
770    first = first->insert(i,t);
771}
772
773void RList::insert(Rule* r) {
774    first = first->insert(r);
775}
776
777RNode* RList::getFirst() {
778    return first;
779}
780
781Rule* RList::getRule() {
782    return this->getRule();
783}
784
785/*
786=======================================
787functions working on the class RTagNode
788=======================================
789*/
790
791RTagNode::RTagNode() {
792    data = NULL;
793    next = NULL;
794}
795 
796RTagNode::RTagNode(RNode* r) {
797    data = r;
798    next = NULL;
799}
800       
801RTagNode::RTagNode(RNode* r, RTagNode* n) {
802    data = r;
803    next = n;
804}
805
806 RTagNode::~RTagNode() {
807    delete next;
808    delete data;   
809}
810       
811// declaration with first as parameter due to sorting of RTagList
812RTagNode* RTagNode::insert(RNode* r) {
813    //Print("Hier1\n");
814    RTagNode* newElement  = new RTagNode(r, this);
815    //Print("Hier2\n");
816    return newElement;
817}
818
819RNode* RTagNode::getRNode() {
820    return this->data;
821}
822
823// NOTE: We insert at the beginning of the list and length = i-1, where i is the actual index.
824//       Thus given actual index i and idx being the index of the LPoly under investigation
825//       the element on position length-idx+1 is the right one
826RNode* RTagNode::get(int idx, int length) {
827    if(idx==1 || idx==0) {
828        // NOTE: We set this NULL as putting it the last element in the list, i.e. the element having
829        //       RNode* = NULL would cost lots of iterations at each step of F5inc, with increasing
830        //       length of the list this should be prevented
831        return NULL;
832    }
833    else {
834        int j;
835        RTagNode* temp = this; 
836    //Print("\n\nHIER IN GET IDX\n");
837    //Print("FOR LOOP: %d\n",length-idx+1);   
838    for(j=1; j<=length-idx+1; j++) {
839            temp = temp->next;
840        }
841        return temp->data;
842    }
843}
844
845void RTagNode::set(RNode* r) {
846    this->data  =   r;
847}
848
849void RTagNode::print() {
850    RTagNode* temp  =   this;
851    Print("1. element: %d",getRNode()->getRule()->getIndex());
852    //pWrite(getRNode()->getRule()->getTerm());
853    temp    =   temp->next;
854    int i   =   2;
855    while(NULL != temp->getRNode()) {
856        Print("%d. element: %d",i,getRNode()->getRule()->getIndex());
857        //pWrite(getRNode()->getRule()->getTerm());
858        temp    =   temp->next;
859        i++;
860    }
861}
862/*
863=======================================
864functions working on the class LTagList
865=======================================
866*/
867
868RTagList::RTagList() {
869    RTagNode* first =   new RTagNode();
870    length          =   0;
871}
872
873RTagList::RTagList(RNode* r) {
874    RTagNode* first =   new RTagNode(r);
875    length          =   1;
876}
877
878// declaration with first as parameter in LTagNode due to sorting of LTagList
879void RTagList::insert(RNode* r) {
880    first = first->insert(r);
881    //Print("LENGTH:%d\n",length);
882    length = length +1;
883    //Print("LENGTH:%d\n",length);
884}
885
886RNode* RTagList::getFirst() {
887    return first->getRNode();
888}
889
890RNode* RTagList::get(int idx) {
891    return first->get(idx, length);
892}
893
894void RTagList::setFirst(RNode* r) {
895    first->set(r);
896}
897
898void RTagList::print() {
899    first->print();
900}
901
902int RTagList::getLength() {
903    return length;
904}
905#endif
Note: See TracBrowser for help on using the repository browser.