Frobby  0.9.5
fplllIO.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 "fplllIO.h"
19 
20 #include "BigIdeal.h"
21 #include "Scanner.h"
22 #include "error.h"
23 #include "FrobbyStringStream.h"
24 
25 namespace fplll {
26  void readLatticeBasis(Scanner& scanner, BigIdeal& basis) {
27  vector<vector<mpz_class> > tmp;
28 
29  scanner.expect('[');
30  while (!scanner.match(']')) {
31  scanner.expect('[');
32  tmp.resize(tmp.size() + 1);
33  while (!scanner.match(']')) {
34  mpz_class integer;
35  scanner.readInteger(integer);
36  tmp.back().push_back(integer);
37  }
38 
39  if (tmp.front().size() != tmp.back().size()) {
40  FrobbyStringStream errorMsg;
41  errorMsg << "Row 1 has "
42  << tmp.front().size()
43  << " entries, while row "
44  << tmp.size()
45  << " has "
46  << tmp.back().size()
47  << " entries.";
48  reportSyntaxError(scanner, errorMsg);
49  }
50  }
51 
52  VarNames names(tmp.empty() ? 0 : tmp.front().size());
53  basis.clearAndSetNames(names);
54 
55  for (unsigned int i = 0; i < tmp.size(); ++i) {
56  basis.newLastTerm();
57  for (unsigned int j = 0; j < tmp[i].size(); ++j)
58  basis.getLastTermExponentRef(j) = tmp[i][j];
59  }
60  }
61 
62  void writeLatticeBasis(FILE* out, const BigIdeal& basis) {
63  fputs("[\n", out);
64  for (unsigned int i = 0; i < basis.getGeneratorCount(); ++i) {
65  fputs(" [", out);
66  const char* prefix = "";
67  for (unsigned int j = 0; j < basis[i].size(); ++j) {
68  gmp_fprintf(out, "%s%Zd", prefix, basis[i][j].get_mpz_t());
69  prefix = " ";
70  }
71  fputs("]\n", out);
72  }
73  fputs("]\n", out);
74  }
75 }
void clearAndSetNames(const VarNames &names)
Definition: BigIdeal.cpp:222
void newLastTerm()
Definition: BigIdeal.cpp:104
mpz_class & getLastTermExponentRef(size_t var)
Definition: BigIdeal.h:126
size_t getGeneratorCount() const
Definition: BigIdeal.h:144
A replacement for stringstream.
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition: Scanner.h:50
void expect(char expected)
Require the next character to be equal to expected.
Definition: Scanner.h:231
void readInteger(mpz_class &integer)
Read an arbitrary-precision integer.
Definition: Scanner.h:238
bool match(char c)
Return true if the next character is c, and in that case skip past it.
Definition: Scanner.h:215
Defines the variables of a polynomial ring and facilities IO involving them.
Definition: VarNames.h:40
void reportSyntaxError(const Scanner &scanner, const string &errorMsg)
Definition: error.cpp:44
void readLatticeBasis(Scanner &scanner, BigIdeal &basis)
Definition: fplllIO.cpp:26
void writeLatticeBasis(FILE *out, const BigIdeal &basis)
Definition: fplllIO.cpp:62