source: git/kernel/f5lists.cc @ 416ea2

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