[mathicgb] 357/393: Moved some of the code of PolyHashTable into its implementation file.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:59:34 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 3666e57059660d4a8446e46174a91366e9e2089b
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Tue Sep 3 16:11:05 2013 +0200

    Moved some of the code of PolyHashTable into its implementation file.
---
 src/mathicgb/PolyHashTable.cpp |  53 +++++++++++++-
 src/mathicgb/PolyHashTable.hpp | 153 +++--------------------------------------
 2 files changed, 60 insertions(+), 146 deletions(-)

diff --git a/src/mathicgb/PolyHashTable.cpp b/src/mathicgb/PolyHashTable.cpp
index 563cd0e..b3574d6 100755
--- a/src/mathicgb/PolyHashTable.cpp
+++ b/src/mathicgb/PolyHashTable.cpp
@@ -9,6 +9,57 @@
 
 MATHICGB_NAMESPACE_BEGIN
 
+PolyHashTable::PolyHashTable(const PolyRing& ring):
+  mHashToIndexMask(computeHashMask(1000)),
+  mBuckets
+    (make_unique_array<Node*>(hashMaskToBucketCount(mHashToIndexMask))),
+  mRing(ring),
+  mNodes(sizeofNode(ring)
+  ),
+  mSize()
+{
+  mMaxSize = static_cast<size_t>(bucketCount() * maxLoadFactor());
+  std::fill_n(mBuckets.get(), bucketCount(), nullptr);
+}
+
+void PolyHashTable::rehash(const size_t requestedBucketCount) {
+  const auto newHashToIndexMask = computeHashMask(requestedBucketCount);
+  const auto newBucketCount = hashMaskToBucketCount(newHashToIndexMask);
+  auto newBuckets = make_unique_array<Node*>(newBucketCount);
+  std::fill_n(newBuckets.get(), newBucketCount, nullptr);
+
+  const auto bucketsEnd = mBuckets.get() + bucketCount();
+  for (auto bucket = mBuckets.get(); bucket != bucketsEnd; ++bucket) {
+    for (auto node = *bucket; node != 0;) {
+      const auto hash = monoid().hash(node->mono());
+      const auto newIndex = hashToIndex(hash, newHashToIndexMask);
+      const auto next = node->next();
+      node->next() = newBuckets[newIndex];
+      newBuckets[newIndex] = node;
+      node = next;
+    }
+  }
+
+  mHashToIndexMask = newHashToIndexMask;
+  mBuckets = std::move(newBuckets);
+  mMaxSize = static_cast<size_t>(bucketCount() * maxLoadFactor());
+}
+
+HashValue PolyHashTable::computeHashMask(const size_t requestedBucketCount) {
+  // round request up to nearest power of 2.
+  size_t pow2 = 1;
+  while (pow2 < requestedBucketCount && 2 * pow2 != 0)
+    pow2 *= 2;
+  MATHICGB_ASSERT(pow2 > 0 && (pow2 & (pow2 - 1)) == 0); // power of two
+
+  // If casting to a hash value overflows, then we get the maximum
+  // possible number of buckets based on the range of the hash
+  // value type. Only unsigned overflow is defined, so we need
+  // to assert that the hash type is unsigned.
+  static_assert(!std::numeric_limits<HashValue>::is_signed, "");
+  const auto hashToIndexMask = static_cast<HashValue>(pow2 - 1);
+  MATHICGB_ASSERT(pow2 == hashMaskToBucketCount(hashToIndexMask));
+  return hashToIndexMask;
+}
 
-  
 MATHICGB_NAMESPACE_END
diff --git a/src/mathicgb/PolyHashTable.hpp b/src/mathicgb/PolyHashTable.hpp
index 5040ed0..88b45d6 100755
--- a/src/mathicgb/PolyHashTable.hpp
+++ b/src/mathicgb/PolyHashTable.hpp
@@ -40,18 +40,7 @@ public:
   // Construct a hash table with at least requestedBucketCount buckets. There
   // may be more buckets. Currently the number is rounded up to the next power
   // of two.
-  PolyHashTable(const PolyRing& ring):
-    mHashToIndexMask(computeHashMask(1000)),
-    mBuckets
-      (make_unique_array<Node*>(hashMaskToBucketCount(mHashToIndexMask))),
-    mRing(ring),
-    mNodes(sizeofNode(ring)
-    ),
-    mSize()
-  {
-    mMaxSize = static_cast<size_t>(bucketCount() * maxLoadFactor());
-    std::fill_n(mBuckets.get(), bucketCount(), nullptr);
-  }
+  PolyHashTable(const PolyRing& ring);
 
   const PolyRing::Monoid& monoid() const {return mRing.monoid();}
 
@@ -140,45 +129,12 @@ public:
   }
 
 private:
-  void rehash(const size_t requestedBucketCount) {
-    const auto newHashToIndexMask = computeHashMask(requestedBucketCount);
-    const auto newBucketCount = hashMaskToBucketCount(newHashToIndexMask);
-    auto newBuckets = make_unique_array<Node*>(newBucketCount);
-    std::fill_n(newBuckets.get(), newBucketCount, nullptr);
-
-    const auto bucketsEnd = mBuckets.get() + bucketCount();
-    for (auto bucket = mBuckets.get(); bucket != bucketsEnd; ++bucket) {
-      for (auto node = *bucket; node != 0;) {
-        const auto hash = monoid().hash(node->mono());
-        const auto newIndex = hashToIndex(hash, newHashToIndexMask);
-        const auto next = node->next();
-        node->next() = newBuckets[newIndex];
-        newBuckets[newIndex] = node;
-        node = next;
-      }
-    }
-
-    mHashToIndexMask = newHashToIndexMask;
-    mBuckets = std::move(newBuckets);
-    mMaxSize = static_cast<size_t>(bucketCount() * maxLoadFactor());
-  }
+  /// Change the number of buckets and put all the nodes into their new
+  /// places. The bucket array gets put into new memory, but all the nodes
+  /// stay where they are.
+  void rehash(const size_t requestedBucketCount);
 
-  static HashValue computeHashMask(const size_t requestedBucketCount) {
-    // round request up to nearest power of 2.
-    size_t pow2 = 1;
-    while (pow2 < requestedBucketCount && 2 * pow2 != 0)
-      pow2 *= 2;
-    MATHICGB_ASSERT(pow2 > 0 && (pow2 & (pow2 - 1)) == 0); // power of two
-
-    // If casting to a hash value overflows, then we get the maximum
-    // possible number of buckets based on the range of the hash
-    // value type. Only unsigned overflow is defined, so we need
-    // to assert that the hash type is unsigned.
-    static_assert(!std::numeric_limits<HashValue>::is_signed, "");
-    const auto hashToIndexMask = static_cast<HashValue>(pow2 - 1);
-    MATHICGB_ASSERT(pow2 == hashMaskToBucketCount(hashToIndexMask));
-    return hashToIndexMask;
-  }
+  static HashValue computeHashMask(const size_t requestedBucketCount);
 
   static size_t hashMaskToBucketCount(const HashValue mask) {
     const auto count = static_cast<size_t>(mask) + 1u; // should be power of 2
@@ -194,7 +150,8 @@ private:
       ring.maxMonomialByteSize();
   }
 
-  /// The maximum allowed value of elementCount() / bucketCount().
+  /// The maximum allowed value of size() / bucketCount() before a rehash
+  /// is done.
   static double maxLoadFactor() {return 0.10;}
 
   size_t hashToIndex(const HashValue hash) const {
@@ -215,99 +172,5 @@ private:
   size_t mMaxSize;
 };
 
-/*
-// The hash table is a map:  monomial => coeff
-// Operations required on monomials:
-//  hash (this will currently pick out one entry of a monomial)
-//
-// Operations required on coefficients:
-//  add, maybe multiply too?
-//  isZero?
-//
-// Does not take ownership of any of the monomials.
-class PolyHashTable {
-public:
-  typedef PolyRing::Monoid Monoid;
-  typedef Monoid::MonoRef MonoRef;
-  typedef Monoid::ConstMonoRef ConstMonoRef;
-  typedef Monoid::MonoPtr MonoPtr;
-  typedef Monoid::ConstMonoPtr ConstMonoPtr;
-  typedef coefficient Value;
-
-  class Node {
-  public:
-    const const_monomial& mono() {return mMonom;}
-    const const_monomial& mono() const {return mMonom;}
-
-    Value& value() {return coeff;}
-    const Value& value() const {return coeff;}
-
-  private:
-    friend class PolyHashTable;
-
-    Node*& next() {return mNext;}
-    Node* next() const {return mNext;}
-
-    Node* mNext;
-    coefficient coeff;
-    const_monomial mMonom;
-  };
-
-  PolyHashTable(const PolyRing& ring);
-
-
-  const Monoid& monoid() const {return mRing.monoid();}
-
-  std::pair<Node*, bool> insertProduct
-    (ConstMonoRef a, ConstMonoRef b);
-
-  std::pair<Node*, bool> insertProduct
-    (ConstMonoRef a, ConstMonoRef b, Value add)
-  {
-    auto p = insertProduct(a, b);
-    mRing.coefficientAddTo(p.first->value(), add);
-    return p;
-  }
-
-  std::pair<Node*, bool> insertProduct(const_term a, const_term b)
-  {
-    Value prod;
-    mRing.coefficientMult(a.coeff, b.coeff, prod);
-    return insertProduct(a.monom, b.monom, prod);
-  }
-
-  // Removes the node from the hash table.
-  void remove(Node* n);
-
-  void clear();  // Clear the table, and memory areas.
-
-  bool empty() const {return mNodeCount == 0;}
-
-  size_t getMemoryUse() const;
-
-private:
-  typedef std::vector<Node*> MonomialArray;
-  void resize(size_t new_nbits);  // Don't change the nodes, table, but do recreate hashtable_
-
-  Node* makeNode(const_monomial monom);
-  bool lookup_and_insert(const_monomial m, Node *&result);
-
-  const PolyRing& mRing;
-  std::vector<Node*> mHashTable;
-  HashValue mHashMask; // this is the number, in binary:  00001111...1, where
-                    // the number of 1's is mLogTableSize
-
-  memt::Arena mArena; // space for monomials represented in this class.  Also nodes??
-
-  size_t mTableSize;
-  size_t mLogTableSize; // number of bits in the table: mTableSize should be 2^mLogTableSize
-
-  size_t mNodeCount;  // current number of nodes in the hash table
-  size_t mBinCount;
-  size_t mMaxCountBeforeRebuild;
-
-  size_t mMonomialSize;
-};
-*/
 MATHICGB_NAMESPACE_END
 #endif

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