[mathicgb] 332/393: Moved all IO code out of MonoMonoid. Use MathicIO instead.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:59:31 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch upstream
in repository mathicgb.

commit 118c1f52b6e8af63a5b03d36c738b3fdbf4b2712
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Fri Aug 23 16:18:55 2013 +0200

    Moved all IO code out of MonoMonoid. Use MathicIO instead.
---
 Makefile.am                                        |   2 +-
 build/vs12/mathicgb-lib/mathicgb-lib.vcxproj       |   1 -
 .../vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters |   7 -
 src/mathicgb/Basis.cpp                             |  10 -
 src/mathicgb/Basis.hpp                             |   7 -
 src/mathicgb/MathicIO.cpp                          | 403 ---------------------
 src/mathicgb/MathicIO.hpp                          |  57 ++-
 src/mathicgb/MonoMonoid.hpp                        | 310 +---------------
 src/mathicgb/PolyRing.cpp                          |  16 -
 src/mathicgb/PolyRing.hpp                          |  18 +-
 src/mathicgb/SPairs.cpp                            |   4 +-
 src/mathicgb/io-util.cpp                           |   7 +-
 src/test/MonoMonoid.cpp                            |  57 +--
 src/test/poly-test.cpp                             |   9 -
 14 files changed, 94 insertions(+), 814 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 303ec57..823429c 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -67,7 +67,7 @@ libmathicgb_la_SOURCES = src/mathicgb/BjarkeGeobucket2.cpp		\
   src/mathicgb/MonoProcessor.hpp src/mathicgb/MonoOrder.hpp		\
   src/mathicgb/Scanner.hpp src/mathicgb/Scanner.cpp			\
   src/mathicgb/Unchar.hpp src/mathicgb/MathicIO.hpp			\
-  src/mathicgb/MathicIO.cpp src/mathicgb/NonCopyable.hpp
+  src/mathicgb/NonCopyable.hpp
 
 
 # The headers that libmathicgb installs.
diff --git a/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj b/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj
index b3c2206..a568045 100755
--- a/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj
+++ b/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj
@@ -457,7 +457,6 @@
     <ClCompile Include="..\..\..\src\mathicgb\io-util.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\LogDomain.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\LogDomainSet.cpp" />
-    <ClCompile Include="..\..\..\src\mathicgb\MathicIO.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\MonTableNaive.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\MTArray.cpp" />
     <ClCompile Include="..\..\..\src\mathicgb\PairTriangle.cpp" />
diff --git a/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters b/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters
index 8b95d32..324575c 100755
--- a/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters
+++ b/build/vs12/mathicgb-lib/mathicgb-lib.vcxproj.filters
@@ -9,10 +9,6 @@
       <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
       <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
     </Filter>
-    <Filter Include="Resource Files">
-      <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
-      <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
-    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\..\src\mathicgb\BjarkeGeobucket.cpp">
@@ -132,9 +128,6 @@
     <ClCompile Include="..\..\..\src\mathicgb\ClassicGBAlg.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\..\src\mathicgb\MathicIO.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="..\..\..\src\mathicgb\Scanner.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
diff --git a/src/mathicgb/Basis.cpp b/src/mathicgb/Basis.cpp
index e416afb..fce129b 100755
--- a/src/mathicgb/Basis.cpp
+++ b/src/mathicgb/Basis.cpp
@@ -30,14 +30,4 @@ void Basis::sort() {
   std::sort(mGenerators.begin(), mGenerators.end(), cmp);
 }
 
-void Basis::display(std::ostream& out, bool printComponent, bool componentIncreasingDesired) const
-{
-  mRing.write(out, componentIncreasingDesired);
-  out << '\n' << mGenerators.size() << '\n';
-  for (size_t i = 0; i < mGenerators.size(); ++i) {
-    mGenerators[i]->display(out, printComponent);
-    out << '\n';
-  }
-}
-
 MATHICGB_NAMESPACE_END
diff --git a/src/mathicgb/Basis.hpp b/src/mathicgb/Basis.hpp
index acea0a3..4d8b4c6 100755
--- a/src/mathicgb/Basis.hpp
+++ b/src/mathicgb/Basis.hpp
@@ -24,13 +24,6 @@ public:
 
   void insert(std::unique_ptr<Poly>&& p);
 
-  /// inverse operation to parse().
-  void display(
-    std::ostream &o,
-    bool print_comp,
-    bool componentIncreasingDesired
-  ) const;
-
   const PolyRing& ring() const { return mRing; }
 
   const PolyRing *getPolyRing() const { return &mRing; }
diff --git a/src/mathicgb/MathicIO.cpp b/src/mathicgb/MathicIO.cpp
deleted file mode 100755
index 6e0f23e..0000000
--- a/src/mathicgb/MathicIO.cpp
+++ /dev/null
@@ -1,403 +0,0 @@
-// MathicGB copyright 2012 all rights reserved. MathicGB comes with ABSOLUTELY
-// NO WARRANTY and is licensed as GPL v2.0 or later - see LICENSE.txt.
-#include "stdinc.h"
-#include "MathicIO.hpp"
-
-MATHICGB_NAMESPACE_BEGIN
-  /*
-template<class M, class BF>
-auto MathicIO<M, BF>::readBaseField(Scanner& in) -> BaseField {
-  return BaseField(in.readInteger<RawCoefficient>());
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::writeBaseField(
-  const BaseField& field,
-  std::ostream& out
-) {
-  out << field.charac();
-}
-
-template<class M, class BF>
-auto MathicIO<M, BF>::readRing(
-  const bool withComponent,
-  Scanner& in
-) -> std::pair<std::unique_ptr<PolyRing>, Processor> {
-  auto baseField = readBaseField(in);
-  const auto varCount = in.readInteger<VarIndex>();
-  auto order = readOrder(varCount, withComponent, in);
-  const bool componentsAscendingDesired = order.componentsAscendingDesired();
-  const bool schreyering = order.schreyering();
-  auto ring = make_unique<PolyRing>
-    (std::move(baseField), Monoid(std::move(order)));
-
-  Processor processor(
-    ring->monoid(),
-    componentsAscendingDesired,
-    schreyering
-  );
-
-  return std::make_pair(std::move(ring), std::move(processor));
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::writeRing(
-  const PolyRing& ring,
-  const Processor& processor,
-  const bool withComponent,
-  std::ostream& out
-){
-  writeBaseField(ring.field(), out);
-  out << ' ' << ring.varCount() << '\n';
-
-  auto&& order = ring.monoid().makeOrder(
-    processor.componentsAscendingDesired(),
-    processor.schreyering()
-  );
-  writeOrder(order, withComponent, out);
-}
-
-template<class M, class BF>
-auto MathicIO<M, BF>::readOrder(
-  const VarIndex varCount,
-  const bool withComponent,
-  Scanner& in
-) -> Order {
-  const bool schreyering = in.match("schreyer");
-  bool lexBaseOrder = !in.match("revlex") && in.match("lex");
-
-  auto gradingCount = in.readInteger<VarIndex>();
-  bool componentsAscendingDesired = true;
-  auto componentCompareIndex = Order::ComponentAfterBaseOrder;
-  Gradings gradings(static_cast<size_t>(varCount) * gradingCount);
-  size_t index = 0;
-  for (VarIndex grading = 0; grading <  gradingCount; ++grading) {
-    const bool com = in.match("component");
-    if (com || in.match("revcomponent")) {
-      if (!withComponent)
-        in.reportError("Cannot specify component comparison for non-modules.");
-      MATHICGB_ASSERT(Monoid::HasComponent);
-      if (componentCompareIndex != Order::ComponentAfterBaseOrder)
-        in.reportError("Component comparison must be specified at most once.");
-      componentsAscendingDesired = com;
-      componentCompareIndex = grading;
-      index += varCount;
-    } else {
-      for (VarIndex i = 0; i < varCount; ++i, ++index)
-        gradings[index] = in.readInteger<Exponent>();
-    }
-  }
-  MATHICGB_ASSERT(index == gradings.size());
-
-  const bool moreLex = in.match("_lex");
-  if (moreLex || in.match("_revlex")) {
-    lexBaseOrder = moreLex;
-    const bool moreCom = in.match("component");
-    if (moreCom || in.match("revcomponent")) {
-      if (!withComponent)
-        in.reportError("Cannot specify component comparison for non-modules.");
-      MATHICGB_ASSERT(Monoid::HasComponent);
-      componentsAscendingDesired = moreCom;
-    }
-  }
-
-  Order order(
-    varCount,
-    std::move(gradings),
-    lexBaseOrder ?
-      Order::LexBaseOrderFromRight : Order::RevLexBaseOrderFromRight,
-    componentCompareIndex,
-    componentsAscendingDesired,
-    schreyering
-  );
-  return std::move(order);
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::writeOrder(
-  const Order& order,
-  const bool withComponent,
-  std::ostream& out
-) {
-  MATHICGB_ASSERT(Monoid::HasComponent || !withComponent);
-
-  const auto baseOrder =
-    order.baseOrder() == Order::LexBaseOrderFromRight ? "lex" : "revlex";
-  const auto componentOrder =
-    order.componentsAscendingDesired() ? "component" : "revcomponent";
-
-  if (order.schreyering())
-    out << "schreyer ";
-  out << baseOrder << ' ' << order.gradingCount() << '\n';
-  for (VarIndex grading = 0; grading < order.gradingCount(); ++grading) {
-    if (withComponent && grading == order.componentGradingIndex())
-      out << ' ' << componentOrder;
-    else {
-      for (VarIndex var = 0; var < order.varCount(); ++var) {
-        const auto index = var + grading * order.varCount();
-        out << ' ' << unchar(order.gradings()[index]);
-      }
-    }
-    out << '\n';
-  }
-  if (
-    withComponent &&
-    !order.componentsAscendingDesired() &&
-    order.componentGradingIndex() == Order::ComponentAfterBaseOrder
-  ) {
-    out << " _" << baseOrder << "\n " << componentOrder << '\n';
-  }
-}
-
-template<class M, class BF>
-Basis MathicIO<M, BF>::readBasis(
-  const PolyRing& ring,
-  const bool readComponent,
-  Scanner& in
-) {
-  const auto polyCount = in.readInteger<size_t>();
-  Basis basis(ring);
-  for (size_t i = 0; i < polyCount; ++i) {
-    auto p = make_unique<Poly>(readPoly(ring, readComponent, in));
-    p->sortTermsDescending();
-    basis.insert(std::move(p));
-  }
-  return std::move(basis);
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::writeBasis(
-  const Basis& basis,
-  const bool writeComponent,
-  std::ostream& out
-) {
-  out << basis.size() << '\n';
-  for (size_t i = 0; i < basis.size(); ++i) {
-    out << ' ';
-    writePoly(*basis.getPoly(i), writeComponent, out);
-    out << '\n';
-  }
-}
-
-template<class M, class BF>
-Poly MathicIO<M, BF>::readPoly(
-  const PolyRing& ring,
-  const bool readComponent,
-  Scanner& in
-) {
-  Poly p(ring);
-
-  // also skips whitespace
-  if (in.match('0') || in.match("+0") || in.match("-0"))
-    return std::move(p);
-  MATHICGB_ASSERT(!in.peekWhite());
-
-  auto mono = ring.monoid().alloc();
-  auto coef = ring.field().zero();
-  do {
-    if (!p.isZero() && !in.peekSign())
-      in.expect('+', '-');
-    readTerm(ring, readComponent, coef, mono, in);
-    p.appendTerm(coef.value(), mono);
-  } while (!in.peekWhite() && !in.matchEOF());
-  return std::move(p);
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::writePoly(
-  const Poly& poly,
-  const bool writeComponent,
-  std::ostream& out
-) {
-  if (poly.isZero()) {
-    out << '0';
-    return;
-  }
-
-  const auto end = poly.end();
-  for (auto it = poly.begin(); it != end; ++it) {
-    if (it != poly.begin())
-      out << '+';
-    writeTerm(
-      poly.ring(),
-      writeComponent,
-      poly.ring().field().toElement(it.getCoefficient()),
-      it.getMonomial(),
-      out
-    );
-  }
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::readTerm(
-  const PolyRing& ring,
-  const bool readComponent,
-  Coefficient& coef,
-  MonoRef mono,
-  Scanner& in
-) {
-  // ** Read coefficient, if any.
-  const auto& field = ring.field();
-  const auto& monoid = ring.monoid();
-  const bool negate = !in.match('+') && in.match('-');
-  if (in.peekDigit()) {
-    coef = in.readModular(field, negate);
-
-    if (!in.peekAlpha()) {
-      // Identify a number c on its own as the monomial 1 times c.
-      monoid.setIdentity(mono);
-      if (readComponent)
-        this->readComponent(monoid, mono, in);
-      return;
-    }
-  } else if (negate)
-    coef = field.minusOne();
-  else
-    coef = field.one();
-
-  readMonomial(monoid, readComponent, mono, in);
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::writeTerm(
-  const PolyRing& ring,
-  const bool writeComponent,
-  const Coefficient coef,
-  ConstMonoRef mono,
-  std::ostream& out
-) {
-  if (!ring.field().isOne(coef)) {
-    out << unchar(coef.value());
-    if (ring.monoid().isIdentity(mono)) {
-      if (writeComponent)
-        this->writeComponent(ring.monoid(), mono, out);
-      return;
-    }
-  } 
-  writeMonomial(ring.monoid(), writeComponent, mono, out);
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::readMonomial(
-  const Monoid& monoid,
-  const bool readComponent,
-  MonoRef mono,
-  Scanner& in
-) {
-  MATHICGB_ASSERT(!readComponent || Monoid::HasComponent);
-
-  monoid.setIdentity(mono);
-  if (in.peek() == '1') {
-    const auto e = in.readInteger<Exponent>();
-    if (e != 1) {
-      std::ostringstream err;
-      err << "Expected monomial, but got " << e << " (did you mean 1?).";
-      in.reportError(err.str());
-    }
-  } else {
-    bool sawSome = false;
-    while (true) {
-      const auto letterCount = 'z' - 'a' + 1;
-      const auto letter = in.peek();
-      
-      VarIndex var;
-      if ('a' <= letter && letter <= 'z')
-        var = letter - 'a';
-      else if ('A' <= letter && letter <= 'Z')
-        var = (letter - 'A') + letterCount;
-      else if (sawSome)
-        break;
-      else {
-        std::ostringstream err;
-        err << "Expected letter while reading monomial, but got '"
-          << static_cast<char>(letter) << "'.";
-        in.reportError(err.str());
-        return;
-      }
-      in.get(); // skip past letter
-      
-      MATHICGB_ASSERT(var < 2 * letterCount);
-      if (var >= monoid.varCount()) {
-        std::ostringstream err;
-        err << "Saw the variable " << static_cast<char>(letter)
-          << ", but the monoid only has "
-          << monoid.varCount() << " variables.";
-        in.reportError(err.str());
-        return;
-      }
-      if (monoid.exponent(mono, var) > static_cast<Exponent>(0)) {
-        std::ostringstream err;
-        err << "Variable " << static_cast<char>(letter) <<
-          " must not be written twice in one monomial.";
-        in.reportError(err.str());
-      }
-      
-      if (in.peekDigit())
-        monoid.setExponent(var, in.readInteger<Exponent>(), mono);
-      else
-        monoid.setExponent(var, static_cast<Exponent>(1), mono);
-      sawSome = true;
-    }
-  }
-
-  if (readComponent)
-    this->readComponent(monoid, mono, in);
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::readComponent(
-  const Monoid& monoid,
-  MonoRef mono,
-  Scanner& in
-) {
-  MATHICGB_ASSERT(Monoid::HasComponent);
-  in.expect('<');
-  monoid.setComponent(in.readInteger<Exponent>(), mono);
-  in.expect('>');
-}
-
-template<class M, class BF>
-void MathicIO<M, BF>::writeComponent(
-  const Monoid& monoid,
-  ConstMonoRef mono,
-  std::ostream& out
-) {
-  MATHICGB_ASSERT(Monoid::HasComponent);
-  out << '<' << unchar(monoid.component(mono)) << '>';
-}
-
-/// Print a monomial with no coefficient.
-template<class M, class BF>
-void MathicIO<M, BF>::writeMonomial(
-  const Monoid& monoid,
-  const bool writeComponent,
-  ConstMonoRef mono,
-  std::ostream& out
-) {
-  const auto letterCount = 'z' - 'a' + 1;
-
-  bool printedSome = false;
-  for (VarIndex var = 0; var < monoid.varCount(); ++var) {
-    if (monoid.exponent(mono, var) == 0)
-      continue;
-    char letter;
-    if (var < letterCount)
-      letter = 'a' + static_cast<char>(var);
-    else if (var < 2 * letterCount)
-      letter = 'A' + (static_cast<char>(var) - letterCount);
-    else {
-      mathic::reportError("Too few letters in alphabet to print variable.");
-      return;
-    }
-    printedSome = true;
-    out << letter;
-    if (monoid.exponent(mono, var) != 1)
-      out << unchar(monoid.exponent(mono, var));
-  }
-  if (!printedSome)
-    out << '1';
-  if (writeComponent)
-    this->writeComponent(monoid, mono, out);
-}
-*/
-MATHICGB_NAMESPACE_END
diff --git a/src/mathicgb/MathicIO.hpp b/src/mathicgb/MathicIO.hpp
index 68f04f4..8dd2f39 100755
--- a/src/mathicgb/MathicIO.hpp
+++ b/src/mathicgb/MathicIO.hpp
@@ -33,22 +33,11 @@ public:
   typedef typename Monoid::VarIndex VarIndex;
   typedef typename Monoid::ConstMonoRef ConstMonoRef;
   typedef typename Monoid::Order Order;
+  typedef typename Monoid::MonoVector MonoVector;
   typedef MonoProcessor<Monoid> Processor;
 
   typedef typename  Order::Gradings Gradings;
 
-/*
-  /// reads ring, #gens, each generator in turn
-  typedef std::tuple<
-    std::unique_ptr<PolyRing>,
-    std::unique_ptr<Basis>,
-    std::unique_ptr<MonoProcessor<PolyRing::Monoid>>
-  > BasisData;
-  BasisData readBasis();
-
-
-
-*/
   BaseField readBaseField(Scanner& in);
   void writeBaseField(const BaseField& field, std::ostream& out);
 
@@ -139,6 +128,37 @@ public:
     std::ostream& out
   );
 
+  /// Reads a non-empty space-separated list of monomials. The monomials
+  /// are appended to the end of the vector.
+  void readMonomialVector(
+    const bool readComponents,
+    Scanner& in,
+    MonoVector& v
+  ) {
+    MATHICGB_ASSERT(Monoid::HasComponent || !readComponents);
+    while (true) {
+      v.push_back();
+      readMonomial(v.monoid(), readComponents, v.back(), in);
+      if (in.peek() != ' ')
+        break;
+      in.get();
+    }
+  }
+
+  void writeMonomialVector(
+    const MonoVector& v,
+    const bool writeComponent,
+    std::ostream& out
+  ) {
+    MATHICGB_ASSERT(Monoid::HasComponent || !writeComponent);
+    for (auto it = v.begin(); it != v.end(); ++it) {
+      if (it != v.begin())
+        out << ' ';
+      writeMonomial(v.monoid(), writeComponent, *it, out);
+    }
+    out << '\n';
+  }
+
   /// Read the trailing indicator of the component of a module monomial.
   void readComponent(
     const Monoid& monoid,
@@ -492,7 +512,7 @@ void MathicIO<M, BF>::readMonomial(
         in.reportError(err.str());
         return;
       }
-      if (monoid.exponent(mono, var) > static_cast<Exponent>(0)) {
+      if (monoid.externalExponent(mono, var) > static_cast<Exponent>(0)) {
         std::ostringstream err;
         err << "Variable " << static_cast<char>(letter) <<
           " must not be written twice in one monomial.";
@@ -500,9 +520,9 @@ void MathicIO<M, BF>::readMonomial(
       }
       
       if (in.peekDigit())
-        monoid.setExponent(var, in.readInteger<Exponent>(), mono);
+        monoid.setExternalExponent(var, in.readInteger<Exponent>(), mono);
       else
-        monoid.setExponent(var, static_cast<Exponent>(1), mono);
+        monoid.setExternalExponent(var, static_cast<Exponent>(1), mono);
       sawSome = true;
     }
   }
@@ -545,7 +565,8 @@ void MathicIO<M, BF>::writeMonomial(
 
   bool printedSome = false;
   for (VarIndex var = 0; var < monoid.varCount(); ++var) {
-    if (monoid.exponent(mono, var) == 0)
+    const auto e = monoid.exponent(mono, var);
+    if (e == 0)
       continue;
     char letter;
     if (var < letterCount)
@@ -558,8 +579,8 @@ void MathicIO<M, BF>::writeMonomial(
     }
     printedSome = true;
     out << letter;
-    if (monoid.exponent(mono, var) != 1)
-      out << unchar(monoid.exponent(mono, var));
+    if (e != 1)
+      out << unchar(e);
   }
   if (!printedSome)
     out << '1';
diff --git a/src/mathicgb/MonoMonoid.hpp b/src/mathicgb/MonoMonoid.hpp
index 2471272..366fdfc 100755
--- a/src/mathicgb/MonoMonoid.hpp
+++ b/src/mathicgb/MonoMonoid.hpp
@@ -394,15 +394,6 @@ public:
     return MonoMonoid(monoid.makeOrder(false, false));
   }
 
-  /// The second.first value of the return pair indicates whether it
-  /// is desired that i>j => e_i > e_j. the second.second value
-  /// indicates whether to do a Schreyer order. TODO: clearly this is
-  /// a mess that needs to be cleaned up. Step 1 is to move IO out of
-  /// MonoMonoid entirely.
-  static std::pair<MonoMonoid, std::pair<bool, bool>> readMonoid(std::istream& in);
-  void printMonoid
-    (const bool componentsAscendingDesired, std::ostream& out) const;
-
   /// Returns an Order object that is equivalent to the order that
   /// this monoid was constructed with. The settings not handled by
   /// the monoid, and therefore not known by the monoid, are passed in
@@ -947,6 +938,16 @@ public:
     MATHICGB_ASSERT(debugValid(mono));
   }
 
+  /// Sets an exponent based on external/unpermtuted var.
+  /// After this, exponent(mono, internvalVar(exVar)) is newExponent.
+  void setExternalExponent(
+    const VarIndex exVar,
+    const Exponent newExponent,
+    MonoRef mono
+  ) const {
+    setExponent(internalVar(exVar), newExponent, mono);
+  }
+
   /// Sets all the exponents of mono from an external/unpermuted array.
   /// exponents must point to an array of size varCount().
   /// After this, exponent(mono, var) is exponents[externalVar(var)].
@@ -1164,27 +1165,6 @@ public:
   void freeRaw(MonoRef mono) const {mPool.freeRaw(mono);}
   bool fromPool(ConstMonoRef mono) const {mPool.fromPool(mono);}
 
-  /// Parses a monomial out of a string. Valid examples: 1 abc a2bc
-  /// aA. Variable names are case sensitive. Whitespace terminates the
-  /// parse as does any other character that is not a letter or a
-  /// digit.  The monomial must not include a coefficient, not even 1,
-  /// except for the special case of a 1 on its own. An input like 1a
-  /// will be parsed as two separate monomials. A suffix like <2> puts
-  /// the monomial in component 2, so a5<2> is a^5e_2. The default
-  /// component is 0.
-  void parseM2(std::istream& in, MonoRef mono) const;
-
-  // Inverse of parseM2().
-  void printM2(ConstMonoRef mono, std::ostream& out) const;
-
-  // As printM2, but returns a string.
-  std::string toString(ConstMonoRef mono) const {
-    std::ostringstream out;
-    printM2(mono, out);
-    return out.str();
-  }
-
-
   // *** Classes for holding and referring to monomials
 
   class ConstMonoPtr {
@@ -1390,7 +1370,7 @@ public:
     /// relevant methods to call. Implement it if you need it.
     ///
     /// There are no postfix increment operator as prefix is
-    /// better. Add it if you y need it (you probably do not).
+    /// better. Add it if you need it (you probably do not).
     ///
     /// We could make this a random access iterator, but that would
     /// make it tricky to support variable-sized exponent vectors
@@ -1537,29 +1517,6 @@ public:
       return mMonos.capacity() * sizeof(mMonos[0]);
     }
 
-    /// As parseM2 on monoid, but accepts a non-empty space-separated
-    /// list of monomials. The monomials are appended to the end of
-    /// the vector.
-    void parseM2(std::istream& in) {
-      while(true) {
-        push_back();
-        monoid().parseM2(in, back());
-        if (in.peek() != ' ')
-          break;
-        in.get();
-      }
-    }
-
-    /// The inverse of parseM2.
-    void printM2(std::ostream& out) const {
-      for (auto it = begin(); it != end(); ++it) {
-      if (it != begin())
-        out << ' ';
-       monoid().printM2(*it, out);
-      }
-      out << '\n';
-    }
-
     const MonoMonoid& monoid() const {return mMonoid;}
 
   private:
@@ -1877,250 +1834,5 @@ namespace MonoMonoidHelper {
   struct unchar<unsigned char> {typedef unsigned short type;};
 }
 
-template<class E, bool HC, bool SH, bool SO>
-auto MonoMonoid<E, HC, SH, SO>::readMonoid(std::istream& in) ->
-  std::pair<MonoMonoid, std::pair<bool, bool>>
-{
-  using MonoMonoidHelper::unchar;
-  VarIndex varCount;
-  in >> varCount;
-
-  bool doSchreyer = false;
-  bool lexBaseOrder = false;
-  std::string str;
-  char c;
-  in >> c;
-  in.unget();
-  if (!std::isdigit(c)) {
-    std::string str;
-    in >> str;
-    if (str == "schreyer") {
-      doSchreyer = true;
-      in >> str;
-    }
-
-    if (str == "revlex")
-      lexBaseOrder = false;
-    else if (str == "lex")
-      lexBaseOrder = true;
-    else
-      mathic::reportError("Expected lex or revlex but read \"" + str + "\".");
-  }
-
-  VarIndex gradingCount;
-  in >> gradingCount;
-
-  Gradings gradings(static_cast<size_t>(varCount) * gradingCount);
-  bool componentsAscendingDesired = true;
-  auto componentCompareIndex = Order::ComponentAfterBaseOrder;
-  size_t w = 0;
-  for (VarIndex grading = 0; grading <  gradingCount; ++grading) {
-    char c;
-    in >> c;
-    in.unget();
-    if (!std::isdigit(c)) {
-      std::string str;
-      in >> str;
-    
-      if (str == "component")
-        componentsAscendingDesired = true;
-      else if (str == "revcomponent")
-        componentsAscendingDesired = false;
-      else
-        mathic::reportError
-          ("Expected component or revcomponent but read \"" + str + "\".");
-      if (!HasComponent)
-        mathic::reportError
-          ("Cannot specify component comparison for non-modules.");
-      if (componentCompareIndex != Order::ComponentAfterBaseOrder)
-        mathic::reportError("Component must be considered at most once.");
-      componentCompareIndex = grading;
-      w += varCount;
-    } else {
-      for (VarIndex i = 0; i < varCount; ++i, ++w) {
-        typename unchar<Exponent>::type e;
-        in >> e;
-        gradings[w] = static_cast<Exponent>(e);
-      } 
-    }
-  }
-  MATHICGB_ASSERT(w == gradings.size());
-
-  in >> c;
-  in.unget();
-  if (c == '_') {
-    in >> str;
-
-    if (str == "_revlex")
-      lexBaseOrder = false;
-    else if (str == "_lex")
-      lexBaseOrder = true;
-    else
-      mathic::reportError("Expected _lex or _revlex but read \"" + str + "\".");
-
-    in >> c;
-    in.unget();
-    if (!std::isdigit(c)) {
-      in >> str;
-      if (str == "component")
-        componentsAscendingDesired = true;
-      else if (str == "revcomponent")
-        componentsAscendingDesired = false;
-      else
-        mathic::reportError
-          ("Expected component or revcomponent but read \"" + str + "\".");
-    }
-  }
-
-  Order order(
-    varCount,
-    std::move(gradings),
-    lexBaseOrder ?
-      Order::LexBaseOrderFromRight : Order::RevLexBaseOrderFromRight,
-    componentCompareIndex
-  );
-  return std::make_pair(
-    MonoMonoid(order),
-    std::make_pair(componentsAscendingDesired, doSchreyer)
-  );
-}
-
-template<class E, bool HC, bool SH, bool SO>
-void MonoMonoid<E, HC, SH, SO>::printMonoid(
-  const bool componentsAscendingDesired,
-  std::ostream& out
-) const {
-  using MonoMonoidHelper::unchar;
-  typedef typename unchar<Exponent>::type UncharredExponent;
-
-  out << varCount() << '\n'
-      << (isLexBaseOrder() ? "lex" : "revlex")
-      << ' ' << gradingCount() << '\n';
-  MATHICGB_ASSERT(gradings().size() == gradingCount() * varCount());
-  for (VarIndex grading = 0; grading < gradingCount(); ++grading) {
-    if (
-      HasComponent &&
-      grading == gradingCount() - 1 - componentGradingIndex()
-    ) {
-      out << (componentsAscendingDesired ? " component\n" : " revcomponent\n");
-      continue;
-    }
-
-    for (VarIndex var = 0; var < varCount(); ++var) {
-      auto w = gradings()[gradingsOppositeRowIndex(grading, var)];
-      if (!isLexBaseOrder())
-        w = -w;
-      out << ' ' << static_cast<UncharredExponent>(w);
-    }
-    out << '\n';
-  }
-}
-
-template<class E, bool HC, bool SH, bool SO>
-void MonoMonoid<E, HC, SH, SO>::parseM2(std::istream& in, MonoRef mono) const {
-  using MonoMonoidHelper::unchar;
-  // todo: signal error on exponent overflow
-
-  setIdentity(mono);
-
-  bool sawSome = false;
-  while (true) {
-    const char next = in.peek();
-    if (!sawSome && next == '1') {
-      in.get();
-      break;
-    }
-
-    VarIndex var;
-    const auto letterCount = 'z' - 'a' + 1;
-    if ('a' <= next && next <= 'z')
-      var = next - 'a';
-    else if ('A' <= next && next <= 'Z')
-      var = (next - 'A') + letterCount;
-    else if (sawSome)
-      break;
-    else {
-      mathic::reportError("Could not parse monomial.");
-      return;
-    }
-    MATHICGB_ASSERT(var < 2 * letterCount);
-    if (var >= varCount()) {
-      mathic::reportError("Unknown variable.");
-      return;
-    }
-
-    in.get();
-    auto& exponent = access(mono, exponentsIndexBegin() + externalVar(var));
-    if (isdigit(in.peek())) {
-      typename unchar<Exponent>::type e;
-      in >> e;
-      exponent = static_cast<Exponent>(e);
-    } else
-      exponent = 1;
-    sawSome = true;
-  }
-
-  if (in.peek() == '<') {
-    if (!HasComponent) {
-      mathic::reportError("Read unexpected < for start of module component\n");
-      return;
-    }
-
-    in.get();
-    if (!isdigit(in.peek())) {
-      mathic::reportError("Component was not integer.");
-      return;
-    }
-    typename unchar<Exponent>::type e;
-    in >> e;
-    access(mono, componentIndex()) = static_cast<Exponent>(e);
-    if (in.peek() != '>') {
-      mathic::reportError("Component < was not matched by >.");
-      return;
-    }
-    in.get();
-  }
-
-  setOrderData(mono);
-  setHash(mono);
-  MATHICGB_ASSERT(debugValid(mono));
-}
-
-template<class E, bool HC, bool SH, bool SO>
-void MonoMonoid<E, HC, SH, SO>::printM2(
-  ConstMonoRef mono,
-  std::ostream& out
-) const {
-  using MonoMonoidHelper::unchar;
-  const auto letterCount = 'z' - 'a' + 1;
-
-  bool printedSome = false;
-  for (VarIndex var = 0; var < varCount(); ++var) {
-    const Exponent& e = externalExponent(mono, var);
-    if (e == 0)
-      continue;
-    char letter;
-    if (var < letterCount)
-      letter = 'a' + static_cast<char>(var);
-    else if (var < 2 * letterCount)
-      letter = 'A' + (static_cast<char>(var) - letterCount);
-    else {
-      mathic::reportError("Too few letters in alphabet to print variable.");
-      return;
-    }
-    printedSome = true;
-    out << letter;
-    if (e != 1)
-      out << static_cast<typename unchar<Exponent>::type>(e);
-  }
-  if (!printedSome)
-    out << '1';
-  if (HasComponent && component(mono) != 0) {
-    out << '<'
-        << static_cast<typename unchar<Exponent>::type>(component(mono))
-        << '>';
-  }
-}
-
 MATHICGB_NAMESPACE_END
 #endif
diff --git a/src/mathicgb/PolyRing.cpp b/src/mathicgb/PolyRing.cpp
index 66c063e..2f89cb5 100755
--- a/src/mathicgb/PolyRing.cpp
+++ b/src/mathicgb/PolyRing.cpp
@@ -231,20 +231,4 @@ void PolyRing::printMonomialFrobbyM2Format(std::ostream& out, ConstMonomial m) c
     out << '1';
 }
 
-std::pair<PolyRing*, std::pair<bool, bool>> PolyRing::read(std::istream &i)
-{
-  int64 characInt;
-  coefficient charac;
-  i >> characInt;
-  charac = static_cast<exponent>(characInt);
-  auto p = Monoid::readMonoid(i);
-  return std::make_pair(new PolyRing(charac, std::move(p.first)), p.second);
-}
-
-void PolyRing::write(std::ostream &o, bool componentIncreasingDesired) const
-{
-  o << charac() << ' ';
-  monoid().printMonoid(componentIncreasingDesired, o);
-}
-
 MATHICGB_NAMESPACE_END
diff --git a/src/mathicgb/PolyRing.hpp b/src/mathicgb/PolyRing.hpp
index 3e20175..d8d7358 100755
--- a/src/mathicgb/PolyRing.hpp
+++ b/src/mathicgb/PolyRing.hpp
@@ -217,16 +217,9 @@ public:
   coefficient charac() const { return mField.charac(); }
   size_t getNumVars() const { return varCount();}
   size_t varCount() const {return monoid().varCount();}
-  //       const std::vector<int> &degs,
-  //       const std::string &monorder);
 
-  static std::pair<PolyRing*, std::pair<bool, bool>> read(std::istream &i);
-  void write(std::ostream &o, bool componentIncreasingDesired) const;
-  // Format for ring
-  //   <char> <mNumVars> <deg1> ... <deg_n> <monorder>
-
-  //  Allocate a monomial from an arena A
-  //  This monomial may only be freed if no other elements that were allocated
+  // Allocate a monomial from an arena A.
+  // This monomial may only be freed if no other elements that were allocated
   // later are live on A.  In this case, use freeMonomial(A,m) to free 'm'.
   Monomial allocMonomial(memt::Arena &A) const {
     exponent* ptr = static_cast<exponent*>(A.alloc(maxMonomialByteSize()));
@@ -305,13 +298,6 @@ public:
   bool coefficientIsOne(coefficient a) const { return a == 1; }
   bool coefficientEQ(coefficient a, coefficient b) const { return a == b; }
 
-  // Format for monomials might be changeable, but for now, let us just do
-  // array of ints:
-  //  -comp exp0 exp1 ... exp(mNumVars-1) -wtr ... -wt1
-  // However when written and read from a file the format will be:
-  //  nterms comp v1 e1 ... v_nterms e_nterms
-  // with each e_i nonzero, and v_1 > v_2 > ... > v_nterms
-
   size_t maxMonomialByteSize() const { return maxMonomialSize() * sizeof(exponent); }
 
   size_t maxMonomialSize() const { return monoid().entryCount(); }
diff --git a/src/mathicgb/SPairs.cpp b/src/mathicgb/SPairs.cpp
index 6551e71..90cc66a 100755
--- a/src/mathicgb/SPairs.cpp
+++ b/src/mathicgb/SPairs.cpp
@@ -5,6 +5,7 @@
 
 #include "SigPolyBasis.hpp"
 #include "LogDomain.hpp"
+#include "MathicIO.hpp"
 #include <iostream>
 
 MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS(
@@ -94,7 +95,8 @@ std::pair<size_t, size_t> SPairs::pop(exponent& w) {
     mEliminated.setBit(p.first, p.second, true);
     MATHICGB_IF_STREAM_LOG(SPairLcm) {
       stream << "Scheduling S-pair with lcm ";
-      bareMonoid().printM2(lcm, stream);
+      MathicIO<BareMonoid>().writeMonomial
+        (bareMonoid(), BareMonoid::HasComponent, lcm, stream);
       stream << '.' << std::endl;
     };
     return p;
diff --git a/src/mathicgb/io-util.cpp b/src/mathicgb/io-util.cpp
index 0475618..a5dbdcd 100755
--- a/src/mathicgb/io-util.cpp
+++ b/src/mathicgb/io-util.cpp
@@ -46,10 +46,11 @@ std::unique_ptr<Basis> basisParseFromString(std::string str)
   return make_unique<Basis>(MathicIO<>().readBasis(ring, false, in));
 }
 
-std::unique_ptr<PolyRing> ringFromString(std::string ringinfo)
+std::unique_ptr<PolyRing> ringFromString(std::string ringStr)
 {
-  std::stringstream ifil(ringinfo);
-  return std::unique_ptr<PolyRing>(PolyRing::read(ifil).first);
+  std::stringstream inStream(ringStr);
+  Scanner in(inStream);
+  return MathicIO<>().readRing(true, in).first;
 }
 
 Monomial stringToMonomial(const PolyRing *R, std::string mon)
diff --git a/src/test/MonoMonoid.cpp b/src/test/MonoMonoid.cpp
index 6576a4f..c9a4676 100755
--- a/src/test/MonoMonoid.cpp
+++ b/src/test/MonoMonoid.cpp
@@ -315,25 +315,29 @@ TYPED_TEST(Monoids, MonoPool) {
 
 namespace {
   template<class M>
-  typename M::MonoVector parseVector(M& monoid, const char* str) {
+  typename M::MonoVector parseVector(
+    M& monoid,
+    const char* str,
+    const bool readComponent
+  ) {
     typename M::MonoVector v(monoid);
-    std::istringstream in(str);
-    v.parseM2(in);
+    Scanner in(str);
+    MathicIO<M>().readMonomialVector(readComponent, in, v);
     return v;
   }
 }
 
-TYPED_TEST(Monoids, ParsePrintM2) {
+
+TYPED_TEST(Monoids, setExponentAndComponent) {
   typedef TypeParam Monoid;
   Monoid m(100);
-  std::string str = "1 a z A Z ab a2 a2b ab2 a20b30";
-  if (Monoid::HasComponent)
-    str += " 1<1> a<2> a2<3> ab<11>\n";
-  else
-    str += '\n';
-  auto v2 = parseVector(m, str.c_str());
+  const std::string str = Monoid::HasComponent ?
+    "1<0> a<0> z<0> A<0> Z<0> ab<0> a2<0> a2b<0> ab2<0> "
+      "a20b30<0> 1<1> a<2> a2<3> ab<11>\n" :
+    "1 a z A Z ab a2 a2b ab2 a20b30\n";
+  auto v2 = parseVector(m, str.c_str(), Monoid::HasComponent);
   std::ostringstream v2Out;
-  v2.printM2(v2Out);
+  MathicIO<Monoid>().writeMonomialVector(v2, Monoid::HasComponent, v2Out);
   ASSERT_EQ(str, v2Out.str());
 
   decltype(v2) v(m);
@@ -389,13 +393,12 @@ TYPED_TEST(Monoids, ParsePrintM2) {
   }
 
   std::ostringstream vOut;
-  v.printM2(vOut);
+  MathicIO<Monoid>().writeMonomialVector(v, Monoid::HasComponent, vOut);
   ASSERT_EQ(str, vOut.str());
-  
+
   ASSERT_EQ(v, v2);
 }
 
-
 TYPED_TEST(Monoids, MultiplyDivide) {
   typedef TypeParam Monoid;
   Monoid m(49);
@@ -404,7 +407,7 @@ TYPED_TEST(Monoids, MultiplyDivide) {
   auto check = [&](const char* const str, const bool component) -> void {
     if (component && !Monoid::HasComponent)
       return;
-    auto v = parseVector(m, str);
+    auto v = parseVector(m, str, component);
     MATHICGB_ASSERT(v.size() == 3);
     const auto& a = v.front();
     const auto& b = *++v.begin();
@@ -510,13 +513,14 @@ TYPED_TEST(Monoids, MultiplyDivide) {
     ASSERT_TRUE(m.equal(mono, a));
   };
   check("1 1 1", false);
-  check("a<5> 1 a<5>", true);
+  check("a<5> 1<0> a<5>", true);
   check("1 Vx Vx", false);
   check("aV bx abxV", false);
   check("a a2 a3", false);
-  check("V<2> V2 V3<2>", true);
+  check("V<2> V2<0> V3<2>", true);
   check("arlgh svug arlg2hsvu", false);
-  check("abcdefghiV<7> ab2c3d4e5f6g7h8i9V11 a2b3c4d5e6f7g8h9i10V12<7>", true);
+  check
+    ("abcdefghiV<7> ab2c3d4e5f6g7h8i9V11<0> a2b3c4d5e6f7g8h9i10V12<7>", true);
 }
 
 TYPED_TEST(Monoids, LcmColon) {
@@ -529,7 +533,7 @@ TYPED_TEST(Monoids, LcmColon) {
   auto check = [&](const char* const str, const bool component) -> void {
     if (component && !Monoid::HasComponent)
       return;
-    auto v = parseVector(m, str);
+    auto v = parseVector(m, str, component);
     MATHICGB_ASSERT(v.size() == 3);
     const auto& a = v.front();
     const auto& b = *++v.begin();
@@ -594,12 +598,18 @@ TYPED_TEST(Monoids, Order) {
   typedef TypeParam Monoid;
   typedef typename Monoid::Order Order;
   typedef typename Monoid::Exponent Exponent;
+  typedef typename Monoid::ConstMonoRef ConstMonoRef;
 
   auto check = [](
     const Monoid& m,
     const char* const sorted
   ) -> void {
-    auto v = parseVector(m, sorted);
+    auto toStr = [&](ConstMonoRef mono) {
+      std::ostringstream out;
+      MathicIO<Monoid>().writeMonomial(m, false, mono, out);
+      return out.str();
+    };
+    auto v = parseVector(m, sorted, false);
     for (auto greater = v.begin(); greater != v.end(); ++greater) {
       ASSERT_EQ(m.compare(*greater, *greater), Monoid::EqualTo);
       ASSERT_TRUE(m.equal(*greater, *greater));
@@ -608,8 +618,9 @@ TYPED_TEST(Monoids, Order) {
       for (auto lesser = v.begin(); lesser != greater; ++lesser) {
         ASSERT_FALSE(m.equal(*lesser, *greater));
         ASSERT_TRUE(m.lessThan(*lesser, *greater))
-          << "*lesser  is " << m.toString(*lesser) << '\n'
-          << "*greater is " << m.toString(*greater) << '\n';
+          << "String   = " << sorted << '\n'
+          << "*lesser  = " << toStr(*lesser) << '\n'
+          << "*greater = " << toStr(*greater) << '\n';
         ASSERT_FALSE(m.lessThan(*greater, *lesser));
         ASSERT_EQ(m.compare(*lesser, *greater), Monoid::LessThan);
         ASSERT_EQ(m.compare(*greater, *lesser), Monoid::GreaterThan);
@@ -721,7 +732,7 @@ TYPED_TEST(Monoids, RelativelyPrime) {
   auto mono = pool.alloc();
   auto mono2 = pool.alloc();
   auto check = [&](const char* str, bool relativelyPrime) -> void {
-    auto v = parseVector(m, str);
+    auto v = parseVector(m, str, false);
     MATHICGB_ASSERT(v.size() == 2);
     ASSERT_EQ(relativelyPrime, m.relativelyPrime(v.front(), v.back()));
     ASSERT_EQ(relativelyPrime, m.relativelyPrime(v.back(), v.front()));
diff --git a/src/test/poly-test.cpp b/src/test/poly-test.cpp
index d00fc09..db8c188 100755
--- a/src/test/poly-test.cpp
+++ b/src/test/poly-test.cpp
@@ -43,15 +43,6 @@ a2c2d-b2d3 \
 c3d3-b2d4 \
 ";
 
-TEST(PolyRing, read) {
-  std::stringstream o;
-  std::string ringinfo = "32003 6\n1 1 1 1 1 1";
-  std::unique_ptr<PolyRing> R(ringFromString(ringinfo));
-  R->write(o, true);
-
-  EXPECT_EQ("32003 6\nrevlex 1\n 1 1 1 1 1 1\n", o.str());
-}
-
 TEST(Poly,readwrite) {
   std::string f1 = "14ce2<72>+13adf<16>";
   std::unique_ptr<PolyRing> R(ringFromString("32003 6 1\n1 1 1 1 1 1"));

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathicgb.git



More information about the debian-science-commits mailing list