1 /* Frobby: Software for monomial ideal computations.
2    Copyright (C) 2009 University of Aarhus
3    Contact Bjarke Hammersholt Roune for license information (www.broune.com)
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see http://www.gnu.org/licenses/.
17 */
18 #include "stdinc.h"
19 #include "IdealWriter.h"
20 
21 #include "BigIdeal.h"
22 #include "Term.h"
23 
24 namespace IO {
IdealWriter(FILE * out)25   IdealWriter::IdealWriter(FILE* out):
26     _out(out),
27     _firstIdeal(true),
28     _firstGenerator(true) {
29   }
30 
consumeRing(const VarNames & names)31   void IdealWriter::consumeRing(const VarNames& names) {
32     if (_names != names) {
33       _names = names;
34       _firstIdeal = true;
35     }
36   }
37 
beginConsumingList()38   void IdealWriter::beginConsumingList() {
39     _firstIdeal = true;
40   }
41 
beginConsuming()42   void IdealWriter::beginConsuming() {
43     _firstGenerator = true;
44     doWriteHeader(_firstIdeal);
45   }
46 
consume(const Term & term,const TermTranslator & translator)47   void IdealWriter::consume(const Term& term, const TermTranslator& translator) {
48     ASSERT(term.getVarCount() == _names.getVarCount());
49     bool firstGenerator = _firstGenerator; // To get tail recursion.
50     _firstGenerator = false;
51     doWriteTerm(term, translator, firstGenerator);
52   }
53 
consume(const vector<mpz_class> & term)54   void IdealWriter::consume(const vector<mpz_class>& term) {
55     ASSERT(term.size() == _names.getVarCount());
56     bool firstGenerator = _firstGenerator; // To get tail recursion.
57     _firstGenerator = false;
58     doWriteTerm(term, firstGenerator);
59   }
60 
doneConsuming()61   void IdealWriter::doneConsuming() {
62     _firstIdeal = false;
63     doWriteFooter(_firstGenerator);
64   }
65 
doneConsumingList()66   void IdealWriter::doneConsumingList() {
67     if (_firstIdeal)
68       doWriteEmptyList();
69   }
70 
consume(const BigIdeal & ideal)71   void IdealWriter::consume(const BigIdeal& ideal) {
72     consumeRing(ideal.getNames());
73     _firstGenerator = true;
74     doWriteHeader(_firstIdeal, ideal.getGeneratorCount());
75 
76     for (size_t term = 0; term < ideal.getGeneratorCount(); ++term)
77       consume(ideal.getTerm(term));
78     doneConsuming();
79   }
80 
doWriteHeader(bool firstIdeal,size_t generatorCount)81   void IdealWriter::doWriteHeader(bool firstIdeal, size_t generatorCount) {
82     doWriteHeader(firstIdeal);
83   }
84 }
85