Macaulay2 Engine
Loading...
Searching...
No Matches
MonomialHashTable.cpp
Go to the documentation of this file.
2#include <iostream>
3
4namespace newf4 {
5
7 {
8 std::cout << " number of calls = " << n_calls_find << std::endl;
9 std::cout << " number of clashes = " << n_clashes << std::endl;
10 std::cout << " max run length = " << max_run_length << std::endl;
11 std::cout << " monequal calls = " << monequal_count << std::endl;
12 std::cout << " monequal false = " << monequal_fails << std::endl;
13 }
14
15 // A private function used in grow().
16 // Note that the monomial already exist in mMonomialPointers (and has storage still)
17 // but we need to re-insert its value into mBuckets.
19 {
20 auto hashval = mHashValues[i];
21 auto hash = hashval & mHashMask;
22 while (mBuckets[hash] != 0)
23 {
24 hash++;
25 mStats.n_clashes++;
26 if (hash == mBuckets.size()) hash = 0;
27 }
28 mBuckets[hash] = i;
29 }
30
32 {
33 // Increase logsize, reset fields, and repopulate new hash table.
34 // if (M2_gbTrace >= 2) dump();
35
36 std::cout << "Growing hash table..." << std::flush;
37 dump();
38 mLog2Size++;
39 mThreshold *= 2;
40 mHashMask = (1 << mLog2Size) - 1;
41 std::vector<MonomialIndex> newBuckets(1<<mLog2Size, 0);
42 std::swap(mBuckets, newBuckets);
43
44 // Repopulates the mBuckets vector
45 for (size_t i = 1; i < mMonomialPointers.size(); ++i)
46 reInsert(i);
47
48 std::cout << "...Done" << std::endl;
49 }
50
52 // Interface functions ///////
54
56 : mLog2Size(log2size),
57 mHashMask((1<<log2size)-1),
58 mThreshold((1<<log2size) >> 2), // ouch
59 mBuckets(1<<log2size, 0) // set to a vector of 2^log2size 0's.
60 {
61 mMonomialPointers.push_back(MonomialView(nullptr));
62 mHashValues.push_back(0);
63 }
64
71
72 // Clear out the hash table, resetting all values to 0, and
73 // all stats values back to 0.
74 // BUT: the size is kept the same.
76 {
77 std::fill(mBuckets.begin(), mBuckets.end(), 0);
78 mMonomialSpace.deallocateAll();
80 }
81
84 {
85 mStats.n_calls_find++;
86 if (mMonomialPointers.size() >= mThreshold) grow();
87 auto hash = mhash & mHashMask;
88 long run = 0;
89 while (mBuckets[hash] != 0)
90 {
91 MonomialIndex current = mBuckets[hash];
92 if (mhash == mHashValues[current])
93 {
94 // likely a match. But need to check equality first
95 mStats.monequal_count++;
96 if (m == mMonomialPointers[current]) // this == is a required MonomialView method!
97 {
98 // Already in the table
99 if (run > mStats.max_run_length) mStats.max_run_length = run;
100 return current;
101 }
102 mStats.monequal_fails++;
103 }
104 ++hash;
105 ++run;
106 mStats.n_clashes++;
107 if (hash == mBuckets.size()) hash = 0;
108 }
109 if (run > mStats.max_run_length) mStats.max_run_length = run;
110 mBuckets[hash] = mMonomialPointers.size(); // index of the new element.
111 mMonomialPointers.emplace_back(m, mMonomialSpace);
112 mHashValues.push_back(mhash);
113 return mBuckets[hash];
114 }
115
116 auto MonomialHashTable::dump() const -> void
117 {
118 std::cout << "--hash table info--" << std::endl;
119 std::cout << " size of hashtable = " << mBuckets.size() << std::endl;
120 std::cout << " threshold = " << mThreshold << std::endl;
121 std::cout << " number of monoms = " << size() << std::endl;
122 mStats.dump();
123 }
124
126 {
127 for (long i=0; i < mBuckets.size(); ++i)
128 {
129 if (i % 100 == 0) std::cout << std::endl;
130 std::cout << mBuckets[i] << " ";
131 }
132 std::cout << std::endl;
133 }
134
135};
136
137// Local Variables:
138// indent-tabs-mode: nil
139// End:
newf4::MonomialHashFunction and the new-F4 monomial-to-index hash table.
auto find(const MonomialView &m, HashInt mhash) -> MonomialIndex
Essentially the previous case when monomial(n) = monomial 1.
void dump() const
stats and debugging information.
std::vector< MonomialView > mMonomialPointers
std::vector< HashInt > mHashValues
auto size() const -> size_t
The actual number of monomials in the table.
MonomialHashTable(int log2size=19)
void reInsert(MonomialIndex i)
std::vector< MonomialIndex > mBuckets
uint64_t HashInt
int32_t MonomialIndex
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
Definition mpreal.h:3244