[mathicgb] 340/393: Tidying DivisorLookup.cpp

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:59:32 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 74107dd4688bc174d9e39d27740dc302dbb59dd9
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Tue Aug 27 18:32:54 2013 +0200

    Tidying DivisorLookup.cpp
---
 src/mathicgb/DivLookup.hpp     |  84 ++++++++++++++++-----------
 src/mathicgb/DivisorLookup.cpp | 127 ++++++++++++++++++++---------------------
 2 files changed, 111 insertions(+), 100 deletions(-)

diff --git a/src/mathicgb/DivLookup.hpp b/src/mathicgb/DivLookup.hpp
index 9ea749e..8a01846 100755
--- a/src/mathicgb/DivLookup.hpp
+++ b/src/mathicgb/DivLookup.hpp
@@ -154,6 +154,15 @@ public:
 
   DivLookup(const Configuration& c): mLookup(c) {}
 
+  DivLookup(
+    const Monoid& monoid,
+    int type,
+    bool preferSparseReducers
+  ):
+    mLookup(Configuration(monoid, type, preferSparseReducers))
+  {}
+
+
   virtual void setBasis(const PolyBasis& basis) {
     mLookup.getConfiguration().setBasis(basis);
   }
@@ -172,11 +181,48 @@ public:
   private:
     Lambda& mLambda;
   };
+
   template<class Lambda>
   static LambdaWrap<Lambda> lambdaWrap(Lambda& lambda) {
     return LambdaWrap<Lambda>(lambda);
   }
 
+  // *** Signature specific functionality
+
+  virtual size_t regularReducer(ConstMonoRef sig, ConstMonoRef mono) const {
+    const auto& conf = mLookup.getConfiguration();
+    SigPolyBasis::StoredRatioCmp ratioCmp
+      (Monoid::toOld(sig), Monoid::toOld(mono), conf.sigBasis());
+    const auto& basis = conf.basis();
+    const bool preferSparse = conf.preferSparseReducers();
+
+    auto reducer = size_t(-1);
+    auto proceed = [&](const Entry& e) {
+      if (ratioCmp.compare(e.index) != GT)
+        return true;
+
+      if (reducer != size_t(-1)) {
+        if (preferSparse) {
+          const auto newTermCount = basis.poly(e.index).nTerms();
+          const auto oldTermCount = basis.poly(reducer).nTerms();
+          if (newTermCount > oldTermCount)
+            return true; // what we already have is sparser
+          // resolve ties by picking oldest
+          if (newTermCount == oldTermCount && e.index > reducer)
+            return true;
+        } else { // pick oldest
+          if (e.index > reducer)
+            return true; // what we already have is older
+        }
+      }
+      reducer = e.index;
+      return true;
+    };
+    auto wrap = lambdaWrap(proceed);
+    mLookup.findAllDivisors(mono, wrap);
+    return reducer;
+  }
+
   virtual void lowBaseDivisors(
     std::vector<size_t>& divisors,
     size_t maxDivisors,
@@ -284,6 +330,8 @@ public:
     return minLeadGen;
   }
 
+  // *** Classic GB specific functionality
+
   virtual size_t classicReducer(ConstMonoRef mono) const {
     const auto& basis = mLookup.getConfiguration().basis();
     const auto preferSparse =
@@ -315,6 +363,8 @@ public:
     return reducer;
   }
 
+  // *** General functionality
+
   virtual size_t divisor(ConstMonoRef mono) const {
     const Entry* entry = mLookup.findDivisor(mono);
     return entry == 0 ? static_cast<size_t>(-1) : entry->index;
@@ -351,40 +401,6 @@ public:
     mLookup.insert(Entry(mono, value));
   }
 
-  virtual size_t regularReducer(ConstMonoRef sig, ConstMonoRef mono) const {
-    const auto& conf = mLookup.getConfiguration();
-    SigPolyBasis::StoredRatioCmp ratioCmp
-      (Monoid::toOld(sig), Monoid::toOld(mono), conf.sigBasis());
-    const auto& basis = conf.basis();
-    const bool preferSparse = conf.preferSparseReducers();
-
-    auto reducer = size_t(-1);
-    auto proceed = [&](const Entry& e) {
-      if (ratioCmp.compare(e.index) != GT)
-        return true;
-
-      if (reducer != size_t(-1)) {
-        if (preferSparse) {
-          const auto newTermCount = basis.poly(e.index).nTerms();
-          const auto oldTermCount = basis.poly(reducer).nTerms();
-          if (newTermCount > oldTermCount)
-            return true; // what we already have is sparser
-          // resolve ties by picking oldest
-          if (newTermCount == oldTermCount && e.index > reducer)
-            return true;
-        } else { // pick oldest
-          if (e.index > reducer)
-            return true; // what we already have is older
-        }
-      }
-      reducer = e.index;
-      return true;
-    };
-    auto wrap = lambdaWrap(proceed);
-    mLookup.findAllDivisors(mono, wrap);
-    return reducer;
-  }
-
 private:
   BaseLookup mLookup;
 };
diff --git a/src/mathicgb/DivisorLookup.cpp b/src/mathicgb/DivisorLookup.cpp
index 370f7ef..1dc4887 100755
--- a/src/mathicgb/DivisorLookup.cpp
+++ b/src/mathicgb/DivisorLookup.cpp
@@ -10,72 +10,78 @@
 MATHICGB_NAMESPACE_BEGIN
 
 namespace {
-  class DivListFactory : public DivisorLookup::Factory {
-  public:
-    DivListFactory(const PolyRing& ring, bool useDivMask): mRing(ring), mUseDivMask(useDivMask) {}
-    virtual std::unique_ptr<DivisorLookup> create(
-      bool preferSparseReducers,
-      bool allowRemovals
-    ) const {
-      if (mUseDivMask)
-        return createIt<true>(preferSparseReducers);
+  template<
+    template<class> class BaseLookup,
+    bool AllowRemovals,
+    bool UseDivMask
+  >
+  std::unique_ptr<DivisorLookup> create(
+    const DivisorLookup::Monoid& monoid,
+    int type,
+    bool preferSparseReducers
+  ) {
+    typedef DivLookupConfiguration<AllowRemovals, UseDivMask> Configuration;
+    auto p = new DivLookup<BaseLookup<Configuration>>
+      (monoid, type, preferSparseReducers);
+    return std::unique_ptr<DivisorLookup>(p);
+  }
+
+  std::unique_ptr<DivisorLookup> createGeneral(
+    const DivisorLookup::Monoid& monoid,
+    int type,
+    bool preferSparseReducers,
+    bool allowRemovals
+  ) {
+    const bool sparse = preferSparseReducers;
+    switch (type) {
+    case 1:
+      if (allowRemovals)
+        return create<mathic::DivList, true, true>(monoid, type, sparse);
       else
-        return createIt<false>(preferSparseReducers);
-    }
+        return create<mathic::DivList, false, true>(monoid, type, sparse);
 
-  private:
-    template<bool UseDivMask>
-    std::unique_ptr<DivisorLookup> createIt(bool preferSparseReducers) const {
-      typedef DivLookupConfiguration<true, UseDivMask> Configuration;
-      bool useDM = UseDivMask;
-      Configuration configuration(
-        mRing.monoid(),
-        (useDM ? 1 : 3),
-        preferSparseReducers);
-      return std::unique_ptr<DivisorLookup>
-        (new DivLookup<mathic::DivList<Configuration> >(configuration));
-    }
+    case 2:
+      if (allowRemovals)
+        return create<mathic::KDTree, true, true>(monoid, type, sparse);
+      else
+        return create<mathic::KDTree, false, true>(monoid, type, sparse);
 
-    const PolyRing& mRing;
-    bool mUseDivMask;
-  };
+    case 3:
+      if (allowRemovals)
+        return create<mathic::DivList, true, false>(monoid, type, sparse);
+      else
+        return create<mathic::DivList, false, false>(monoid, type, sparse);
 
-  class KDTreeFactory : public DivisorLookup::Factory {
+    case 4:
+      if (allowRemovals)
+        return create<mathic::KDTree, true, false>(monoid, type, sparse);
+      else
+        return create<mathic::KDTree, false, false>(monoid, type, sparse);
+
+    default:
+      MATHICGB_ASSERT_NO_ASSUME(false);
+      throw std::runtime_error("Unknown code for monomial data structure");
+    }
+  }
+
+  class ConcreteFactory : public DivisorLookup::Factory {
   public:
-    KDTreeFactory(const PolyRing& ring, bool useDivMask): mRing(ring), mUseDivMask(useDivMask) {}
+    ConcreteFactory(const Monoid& monoid, int type): 
+      mMonoid(monoid),
+      mType(type)
+    {}
+
     virtual std::unique_ptr<DivisorLookup> create(
       bool preferSparseReducers,
       bool allowRemovals
     ) const {
-      if (allowRemovals) {
-        if (mUseDivMask)
-          return createAllowRemovals<true,true>(preferSparseReducers);
-        else
-          return createAllowRemovals<true,false>(preferSparseReducers);
-      } else {
-        if (mUseDivMask)
-          return createAllowRemovals<false,true>(preferSparseReducers);
-        else
-          return createAllowRemovals<false,false>(preferSparseReducers);
-      }
+      return createGeneral
+        (mMonoid, mType, preferSparseReducers, allowRemovals);
     }
 
   private:
-    template<bool AllowRemovals, bool UseDivMask>
-    std::unique_ptr<DivisorLookup> createAllowRemovals(
-      bool preferSparseReducers
-    ) const {
-      typedef DivLookupConfiguration<AllowRemovals, UseDivMask> Configuration;
-      bool useDM = UseDivMask;
-      Configuration configuration(
-        mRing.monoid(),
-        (useDM ? 2 : 4),
-        preferSparseReducers);
-      return std::unique_ptr<DivisorLookup>
-        (new DivLookup<mathic::KDTree<Configuration> >(configuration));
-    }
-    const PolyRing& mRing;
-    bool mUseDivMask;
+    const Monoid& mMonoid;
+    const int mType;
   };
 }
 
@@ -83,18 +89,7 @@ std::unique_ptr<DivisorLookup::Factory> DivisorLookup::makeFactory(
   const PolyRing& ring,
   int type
 ) {
-  if (type == 1)
-    return std::unique_ptr<Factory>(new DivListFactory(ring, true));
-  else if (type == 2)
-    return std::unique_ptr<Factory>(new KDTreeFactory(ring, true));
-  if (type == 3)
-    return std::unique_ptr<Factory>(new DivListFactory(ring, false));
-  else if (type == 4)
-    return std::unique_ptr<Factory>(new KDTreeFactory(ring, false));
-  else if (type == 0)
-    throw std::runtime_error("Divisor lookup 0 (DivisorLookupGB) disabled.");
-  else
-    throw std::runtime_error("Unknown divisor lookup code.");
+  return std::unique_ptr<Factory>(new ConcreteFactory(ring.monoid(), type));
 }
 
 void DivisorLookup::displayDivisorLookupTypes(std::ostream &o)

-- 
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