Frobby  0.9.0
NewMonosIOHandler.cpp
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see http://www.gnu.org/licenses/.
16 */
17 #include "stdinc.h"
18 #include "NewMonosIOHandler.h"
19 
20 #include "Scanner.h"
21 #include "VarNames.h"
22 #include "BigTermConsumer.h"
23 #include "DataType.h"
24 #include "IdealWriter.h"
25 #include "error.h"
26 #include "InputConsumer.h"
27 
28 #include <cstdio>
29 
30 namespace IO {
31  namespace NewMonos {
32  void writeRing(const VarNames& names, FILE* out);
33  void readRingNoLeftParen(Scanner& in, VarNames& names);
34  void readIdealNoLeftParen(Scanner& in, InputConsumer& consumer);
35  }
36  namespace N = NewMonos;
37 
39  public:
40  NewMonosIdealWriter(FILE* out): IdealWriter(out) {
41  }
42 
43  private:
44  virtual void doWriteHeader(bool first) {
45  fputs("(monomial-ideal-with-order\n ", getFile());
46  N::writeRing(getNames(), getFile());
47  }
48 
49  virtual void doWriteTerm(const Term& term,
50  const TermTranslator& translator,
51  bool first) {
52  fputs("\n ", getFile());
53  writeTermProduct(term, translator, getFile());
54  }
55 
56  virtual void doWriteTerm(const vector<mpz_class>& term,
57  bool first) {
58  fputs("\n ", getFile());
59  writeTermProduct(term, getNames(), getFile());
60  }
61 
62  virtual void doWriteFooter(bool wasZeroIdeal) {
63  fputs("\n)\n", getFile());
64  }
65 
66  virtual void doWriteEmptyList() {
67  N::writeRing(getNames(), getFile());
68  }
69  };
70 
72  IOHandlerImpl(staticGetName(), "Newer format used by the program Monos.") {
76  }
77 
79  return "newmonos";
80  }
81 
83  return new NewMonosIdealWriter(out);
84  }
85 
86  void NewMonosIOHandler::doWriteTerm(const vector<mpz_class>& term,
87  const VarNames& names,
88  FILE* out) {
89  writeTermProduct(term, names, out);
90  }
91 
93  consumer.consumeTermProductNotation(in);
94  }
95 
97  in.expect('(');
98  N::readIdealNoLeftParen(in, consumer);
99  }
100 
102  in.expect('(');
103  if (in.peek('l') || in.peek('L')) {
104  VarNames names;
105  N::readRingNoLeftParen(in, names);
106  consumer.consumeRing(names);
107  return;
108  }
109 
110  do {
111  N::readIdealNoLeftParen(in, consumer);
112  } while (in.match('('));
113  }
114 
115  void N::writeRing(const VarNames& names, FILE* out) {
116  fputs("(lex-order", out);
117  for (unsigned int i = 0; i < names.getVarCount(); ++i) {
118  putc(' ', out);
119  fputs(names.getName(i).c_str(), out);
120  }
121  fputc(')', out);
122  }
123 
124  void N::readRingNoLeftParen(Scanner& in, VarNames& names) {
125  in.expect("lex-order");
126  while (!in.match(')'))
127  names.addVarSyntaxCheckUnique(in, in.readIdentifier());
128  }
129 
130  void N::readIdealNoLeftParen(Scanner& in, InputConsumer& consumer) {
131  in.expect("monomial-ideal-with-order");
132 
133  VarNames names;
134  in.expect('(');
135  N::readRingNoLeftParen(in, names);
136  consumer.consumeRing(names);
137 
138  consumer.beginIdeal();
139  while (!in.match(')'))
140  consumer.consumeTermProductNotation(in);
141  consumer.endIdeal();
142  }
143 }
static const DataType & getMonomialIdealListType()
Returns the one and only instance for monomial ideal lists.
Definition: DataType.cpp:54
virtual BigTermConsumer * doCreateIdealWriter(FILE *out)
void registerOutput(const DataType &type)
Specify that output of the argument type is supported.
void endIdeal()
Done reading an ideal.
void writeRing(const VarNames &names, FILE *out)
Defines the variables of a polynomial ring and facilities IO involving them.
Definition: VarNames.h:40
virtual void doWriteHeader(bool first)
void consumeRing(const VarNames &names)
void consumeTermProductNotation(Scanner &in)
Reads a term in a format like "a^4*b*c^2".
void doReadIdeals(Scanner &in, InputConsumer &consumer)
void readIdealNoLeftParen(Scanner &in, InputConsumer &consumer)
void addVarSyntaxCheckUnique(const Scanner &in, const string &name)
As addvar, except it reports a syntax error if name is already a variable.
Definition: VarNames.cpp:68
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition: Scanner.h:50
virtual void doWriteTerm(const vector< mpz_class > &term, bool first)
virtual void doWriteTerm(const Term &term, const TermTranslator &translator, bool first)
virtual void doWriteTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)
static const char * staticGetName()
void doReadIdeal(Scanner &in, InputConsumer &consumer)
void registerInput(const DataType &type)
Specify that input of the argument type is supported.
virtual void doWriteFooter(bool wasZeroIdeal)
void readRingNoLeftParen(Scanner &in, VarNames &names)
This class contains a minimum level of functionality that makes it more convenient to derive from tha...
Definition: IOHandlerImpl.h:37
bool peek(char character)
Skips whitespace and returns true if the next character is equal to the parameter(s).
Definition: Scanner.h:262
const char * readIdentifier()
The returned string is only valid until the next method on this object gets called.
Definition: Scanner.cpp:255
void beginIdeal()
Start consuming an ideal.
TermTranslator handles translation between terms whose exponents are infinite precision integers and ...
void expect(char expected)
Require the next character to be equal to expected.
Definition: Scanner.h:231
virtual void doReadTerm(Scanner &in, InputConsumer &consumer)
static const DataType & getMonomialIdealType()
Returns the one and only instance for monomial ideals.
Definition: DataType.cpp:45
void writeTermProduct(const Term &term, const TermTranslator &translator, FILE *out)
size_t getVarCount() const
Returns the current number of variables.
Definition: VarNames.h:112
const string & getName(size_t index) const
The returned reference can become invalid next time addVar is called.
Definition: VarNames.cpp:100
Term represents a product of variables which does not include a coefficient.
Definition: Term.h:49
bool match(char c)
Return true if the next character is c, and in that case skip past it.
Definition: Scanner.h:215