Frobby  0.9.0
MonosIOHandler.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 "MonosIOHandler.h"
19 
20 #include "Scanner.h"
21 #include "BigTermConsumer.h"
22 #include "DataType.h"
23 #include "IdealWriter.h"
24 #include "error.h"
25 #include "InputConsumer.h"
26 
27 #include <cstdio>
28 
29 namespace IO {
30  namespace Monos {
31  void writeRing(const VarNames& names, FILE* out);
32  }
33  namespace M = Monos;
34 
35  class MonosIdealWriter : public IdealWriter {
36  public:
37  MonosIdealWriter(FILE* out): IdealWriter(out) {
38  }
39 
40  private:
41  virtual void doWriteHeader(bool first) {
42  M::writeRing(getNames(), getFile());
43  fputc('[', getFile());
44  }
45 
46  virtual void doWriteTerm(const Term& term,
47  const TermTranslator& translator,
48  bool first) {
49  fputs(first ? "\n " : ",\n ", getFile());
50  writeTermProduct(term, translator, getFile());
51  }
52 
53  virtual void doWriteTerm(const vector<mpz_class>& term,
54  bool first) {
55  fputs(first ? "\n " : ",\n ", getFile());
56  writeTermProduct(term, getNames(), getFile());
57  }
58 
59  virtual void doWriteFooter(bool wasZeroIdeal) {
60  fputs("\n];\n", getFile());
61  }
62 
63  virtual void doWriteEmptyList() {
64  M::writeRing(getNames(), getFile());
65  }
66  };
67 
69  IOHandlerCommon(staticGetName(),
70  "Older format used by the program Monos.") {
75  }
76 
78  return "monos";
79  }
80 
82  return new MonosIdealWriter(out);
83  }
84 
85  void MonosIOHandler::doWriteTerm(const vector<mpz_class>& term,
86  const VarNames& names,
87  FILE* out) {
88  writeTermProduct(term, names, out);
89  }
90 
92  consumer.consumeTermProductNotation(in);
93  }
94 
96  names.clear();
97  in.expect("vars");
98  if (!in.match(';')) {
99  do {
100  names.addVarSyntaxCheckUnique(in, in.readIdentifier());
101  } while (in.match(','));
102  in.expect(';');
103  }
104  }
105 
107  return in.peek('v');
108  }
109 
111  consumer.beginIdeal();
112 
113  in.expect('[');
114  if (!in.match(']')) {
115  do {
116  consumer.consumeTermProductNotation(in);
117  } while (in.match(','));
118  if (!in.match(']')) {
119  if (in.peekIdentifier())
120  in.expect('*');
121  else
122  in.expect(']');
123  }
124  }
125  in.expect(';');
126 
127  consumer.endIdeal();
128  }
129 
130  void M::writeRing(const VarNames& names, FILE* out) {
131  fputs("vars ", out);
132  const char* pre = "";
133  for (unsigned int i = 0; i < names.getVarCount(); ++i) {
134  fputs(pre, out);
135  fputs(names.getName(i).c_str(), out);
136  pre = ", ";
137  }
138  fputs(";\n", out);
139  }
140 }
This class contains further functionality that makes it more convenient to derive from than IOHandler...
virtual void doReadTerm(Scanner &in, InputConsumer &consumer)
static const DataType & getMonomialIdealListType()
Returns the one and only instance for monomial ideal lists.
Definition: DataType.cpp:54
void writeRing(const VarNames &names, FILE *out)
virtual void doWriteHeader(bool first)
void clear()
Resets the number of variables to zero.
Definition: VarNames.cpp:106
virtual void doWriteEmptyList()
void registerOutput(const DataType &type)
Specify that output of the argument type is supported.
virtual void doWriteTerm(const vector< mpz_class > &term, bool first)
void endIdeal()
Done reading an ideal.
Defines the variables of a polynomial ring and facilities IO involving them.
Definition: VarNames.h:40
static const char * staticGetName()
void consumeTermProductNotation(Scanner &in)
Reads a term in a format like "a^4*b*c^2".
virtual void doWriteTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)
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
virtual void doWriteTerm(const Term &term, const TermTranslator &translator, bool first)
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition: Scanner.h:50
virtual void doReadRing(Scanner &in, VarNames &names)
virtual void doWriteFooter(bool wasZeroIdeal)
bool peekIdentifier()
Skips whitespace and returns true if the next token is an identifier.
Definition: Scanner.h:257
void registerInput(const DataType &type)
Specify that input of the argument type is supported.
virtual BigTermConsumer * doCreateIdealWriter(FILE *out)
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
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
virtual void doReadBareIdeal(Scanner &in, InputConsumer &consumer)
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
virtual bool doPeekRing(Scanner &in)